board.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. /*
  2. * Copyright (c) 2023-2025 HPMicro
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. *
  6. */
  7. #include "board.h"
  8. #include "hpm_uart_drv.h"
  9. #include "hpm_gptmr_drv.h"
  10. #include "hpm_i2c_drv.h"
  11. #include "hpm_gpio_drv.h"
  12. #include "pinmux.h"
  13. #include "hpm_pmp_drv.h"
  14. #include "hpm_clock_drv.h"
  15. /* #include "hpm_sysctl_drv.h" */
  16. #include "hpm_pllctlv2_drv.h"
  17. #include "hpm_enet_drv.h"
  18. #include "hpm_usb_drv.h"
  19. #include "hpm_pcfg_drv.h"
  20. /**
  21. * @brief FLASH configuration option definitions:
  22. * option[0]:
  23. * [31:16] 0xfcf9 - FLASH configuration option tag
  24. * [15:4] 0 - Reserved
  25. * [3:0] option words (exclude option[0])
  26. * option[1]:
  27. * [31:28] Flash probe type
  28. * 0 - SFDP SDR / 1 - SFDP DDR
  29. * 2 - 1-4-4 Read (0xEB, 24-bit address) / 3 - 1-2-2 Read(0xBB, 24-bit address)
  30. * 4 - HyperFLASH 1.8V / 5 - HyperFLASH 3V
  31. * 6 - OctaBus DDR (SPI -> OPI DDR)
  32. * 8 - Xccela DDR (SPI -> OPI DDR)
  33. * 10 - EcoXiP DDR (SPI -> OPI DDR)
  34. * [27:24] Command Pads after Power-on Reset
  35. * 0 - SPI / 1 - DPI / 2 - QPI / 3 - OPI
  36. * [23:20] Command Pads after Configuring FLASH
  37. * 0 - SPI / 1 - DPI / 2 - QPI / 3 - OPI
  38. * [19:16] Quad Enable Sequence (for the device support SFDP 1.0 only)
  39. * 0 - Not needed
  40. * 1 - QE bit is at bit 6 in Status Register 1
  41. * 2 - QE bit is at bit1 in Status Register 2
  42. * 3 - QE bit is at bit7 in Status Register 2
  43. * 4 - QE bit is at bit1 in Status Register 2 and should be programmed by 0x31
  44. * [15:8] Dummy cycles
  45. * 0 - Auto-probed / detected / default value
  46. * Others - User specified value, for DDR read, the dummy cycles should be 2 * cycles on FLASH datasheet
  47. * [7:4] Misc.
  48. * 0 - Not used
  49. * 1 - SPI mode
  50. * 2 - Internal loopback
  51. * 3 - External DQS
  52. * [3:0] Frequency option
  53. * 1 - 30MHz / 2 - 50MHz / 3 - 66MHz / 4 - 80MHz / 5 - 100MHz / 6 - 111MHz / 7 - 133MHz / 8 - 166MHz
  54. *
  55. * option[2] (Effective only if the bit[3:0] in option[0] > 1)
  56. * [31:20] Reserved
  57. * [19:16] IO voltage
  58. * 0 - 3V / 1 - 1.8V
  59. * [15:12] Pin group
  60. * 0 - 1st group / 1 - 2nd group
  61. * [11:8] Connection selection
  62. * 0 - CA_CS0 / 1 - CB_CS0 / 2 - CA_CS0 + CB_CS0 (Two FLASH connected to CA and CB respectively)
  63. * [7:0] Drive Strength
  64. * 0 - Default value
  65. * option[3] (Effective only if the bit[3:0] in option[0] > 2, required only for the QSPI NOR FLASH that not supports
  66. * JESD216)
  67. * [31:16] reserved
  68. * [15:12] Sector Erase Command Option, not required here
  69. * [11:8] Sector Size Option, not required here
  70. * [7:0] Flash Size Option
  71. * 0 - 4MB / 1 - 8MB / 2 - 16MB
  72. */
  73. #if defined(FLASH_XIP) && FLASH_XIP
  74. __attribute__((section(".nor_cfg_option"), used)) const uint32_t option[4] = { 0xfcf90002, 0x00000005, 0x1000, 0x0 };
  75. #endif
  76. #if defined(FLASH_UF2) && FLASH_UF2
  77. ATTR_PLACE_AT(".uf2_signature") __attribute__((used)) const uint32_t uf2_signature = BOARD_UF2_SIGNATURE;
  78. #endif
  79. void board_init_console(void)
  80. {
  81. #if !defined(CONFIG_NDEBUG_CONSOLE) || !CONFIG_NDEBUG_CONSOLE
  82. #if BOARD_CONSOLE_TYPE == CONSOLE_TYPE_UART
  83. console_config_t cfg;
  84. /* uart needs to configure pin function before enabling clock, otherwise the level change of
  85. * uart rx pin when configuring pin function will cause a wrong data to be received.
  86. * And a uart rx dma request will be generated by default uart fifo dma trigger level.
  87. */
  88. init_uart_pins((UART_Type *) BOARD_CONSOLE_UART_BASE);
  89. clock_add_to_group(BOARD_CONSOLE_UART_CLK_NAME, 0);
  90. cfg.type = BOARD_CONSOLE_TYPE;
  91. cfg.base = (uint32_t) BOARD_CONSOLE_UART_BASE;
  92. cfg.src_freq_in_hz = clock_get_frequency(BOARD_CONSOLE_UART_CLK_NAME);
  93. cfg.baudrate = BOARD_CONSOLE_UART_BAUDRATE;
  94. if (status_success != console_init(&cfg)) {
  95. /* failed to initialize debug console */
  96. while (1) {
  97. }
  98. }
  99. #else
  100. while (1)
  101. ;
  102. #endif
  103. #endif
  104. }
  105. void board_print_clock_freq(void)
  106. {
  107. printf("==============================\n");
  108. printf(" %s clock summary\n", BOARD_NAME);
  109. printf("==============================\n");
  110. printf("cpu0:\t\t %dHz\n", clock_get_frequency(clock_cpu0));
  111. printf("ahb:\t\t %luHz\n", clock_get_frequency(clock_ahb0));
  112. printf("axif:\t\t %dHz\n", clock_get_frequency(clock_axif));
  113. printf("axis:\t\t %dHz\n", clock_get_frequency(clock_axis));
  114. printf("axic:\t\t %dHz\n", clock_get_frequency(clock_axic));
  115. printf("xpi0:\t\t %dHz\n", clock_get_frequency(clock_xpi0));
  116. printf("mchtmr0:\t %dHz\n", clock_get_frequency(clock_mchtmr0));
  117. printf("==============================\n");
  118. }
  119. void board_init_uart(UART_Type *ptr)
  120. {
  121. /* configure uart's pin before opening uart's clock */
  122. init_uart_pins(ptr);
  123. board_init_uart_clock(ptr);
  124. }
  125. void board_print_banner(void)
  126. {
  127. const uint8_t banner[] = { "\n\
  128. ----------------------------------------------------------------------\n\
  129. $$\\ $$\\ $$$$$$$\\ $$\\ $$\\ $$\\\n\
  130. $$ | $$ |$$ __$$\\ $$$\\ $$$ |\\__|\n\
  131. $$ | $$ |$$ | $$ |$$$$\\ $$$$ |$$\\ $$$$$$$\\ $$$$$$\\ $$$$$$\\\n\
  132. $$$$$$$$ |$$$$$$$ |$$\\$$\\$$ $$ |$$ |$$ _____|$$ __$$\\ $$ __$$\\\n\
  133. $$ __$$ |$$ ____/ $$ \\$$$ $$ |$$ |$$ / $$ | \\__|$$ / $$ |\n\
  134. $$ | $$ |$$ | $$ |\\$ /$$ |$$ |$$ | $$ | $$ | $$ |\n\
  135. $$ | $$ |$$ | $$ | \\_/ $$ |$$ |\\$$$$$$$\\ $$ | \\$$$$$$ |\n\
  136. \\__| \\__|\\__| \\__| \\__|\\__| \\_______|\\__| \\______/\n\
  137. ----------------------------------------------------------------------\n" };
  138. #ifdef SDK_VERSION_STRING
  139. printf("hpm_sdk: %s\n", SDK_VERSION_STRING);
  140. #endif
  141. printf("%s", banner);
  142. }
  143. uint8_t board_get_led_gpio_off_level(void)
  144. {
  145. return BOARD_LED_OFF_LEVEL;
  146. }
  147. void board_ungate_mchtmr_at_lp_mode(void)
  148. {
  149. /* Keep cpu clock on wfi, so that mchtmr irq can still work after wfi */
  150. sysctl_set_cpu_lp_mode(HPM_SYSCTL, BOARD_RUNNING_CORE, cpu_lp_mode_ungate_cpu_clock);
  151. }
  152. void board_init(void)
  153. {
  154. board_init_clock();
  155. board_init_console();
  156. board_init_pmp();
  157. #if BOARD_SHOW_CLOCK
  158. board_print_clock_freq();
  159. #endif
  160. #if BOARD_SHOW_BANNER
  161. board_print_banner();
  162. #endif
  163. }
  164. void board_delay_us(uint32_t us)
  165. {
  166. clock_cpu_delay_us(us);
  167. }
  168. void board_delay_ms(uint32_t ms)
  169. {
  170. clock_cpu_delay_ms(ms);
  171. }
  172. #if !defined(NO_BOARD_TIMER_SUPPORT) || !NO_BOARD_TIMER_SUPPORT
  173. static board_timer_cb timer_cb;
  174. SDK_DECLARE_EXT_ISR_M(BOARD_CALLBACK_TIMER_IRQ, board_timer_isr);
  175. void board_timer_isr(void)
  176. {
  177. if (gptmr_check_status(BOARD_CALLBACK_TIMER, GPTMR_CH_RLD_STAT_MASK(BOARD_CALLBACK_TIMER_CH))) {
  178. gptmr_clear_status(BOARD_CALLBACK_TIMER, GPTMR_CH_RLD_STAT_MASK(BOARD_CALLBACK_TIMER_CH));
  179. timer_cb();
  180. }
  181. }
  182. void board_timer_create(uint32_t ms, board_timer_cb cb)
  183. {
  184. uint32_t gptmr_freq;
  185. gptmr_channel_config_t config;
  186. timer_cb = cb;
  187. gptmr_channel_get_default_config(BOARD_CALLBACK_TIMER, &config);
  188. clock_add_to_group(BOARD_CALLBACK_TIMER_CLK_NAME, 0);
  189. gptmr_freq = clock_get_frequency(BOARD_CALLBACK_TIMER_CLK_NAME);
  190. config.reload = gptmr_freq / 1000 * ms;
  191. gptmr_channel_config(BOARD_CALLBACK_TIMER, BOARD_CALLBACK_TIMER_CH, &config, false);
  192. gptmr_enable_irq(BOARD_CALLBACK_TIMER, GPTMR_CH_RLD_IRQ_MASK(BOARD_CALLBACK_TIMER_CH));
  193. intc_m_enable_irq_with_priority(BOARD_CALLBACK_TIMER_IRQ, 1);
  194. gptmr_start_counter(BOARD_CALLBACK_TIMER, BOARD_CALLBACK_TIMER_CH);
  195. }
  196. #endif
  197. void board_i2c_bus_clear(I2C_Type *ptr)
  198. {
  199. if (i2c_get_line_scl_status(ptr) == false) {
  200. printf("CLK is low, please power cycle the board\n");
  201. while (1) {
  202. }
  203. }
  204. if (i2c_get_line_sda_status(ptr) == false) {
  205. printf("SDA is low, try to issue I2C bus clear\n");
  206. } else {
  207. printf("I2C bus is ready\n");
  208. return;
  209. }
  210. i2c_gen_reset_signal(ptr, 9);
  211. board_delay_ms(100);
  212. printf("I2C bus is cleared\n");
  213. }
  214. uint32_t board_init_i2c_clock(I2C_Type *ptr)
  215. {
  216. uint32_t freq = 0;
  217. if (ptr == HPM_I2C0) {
  218. clock_add_to_group(clock_i2c0, 0);
  219. freq = clock_get_frequency(clock_i2c0);
  220. } else if (ptr == HPM_I2C1) {
  221. clock_add_to_group(clock_i2c1, 0);
  222. freq = clock_get_frequency(clock_i2c1);
  223. } else if (ptr == HPM_I2C2) {
  224. clock_add_to_group(clock_i2c2, 0);
  225. freq = clock_get_frequency(clock_i2c2);
  226. } else if (ptr == HPM_I2C3) {
  227. clock_add_to_group(clock_i2c3, 0);
  228. freq = clock_get_frequency(clock_i2c3);
  229. } else {
  230. ;
  231. }
  232. return freq;
  233. }
  234. void board_init_i2c(I2C_Type *ptr)
  235. {
  236. i2c_config_t config;
  237. hpm_stat_t stat;
  238. uint32_t freq;
  239. freq = board_init_i2c_clock(ptr);
  240. init_i2c_pins(ptr);
  241. board_i2c_bus_clear(ptr);
  242. config.i2c_mode = i2c_mode_normal;
  243. config.is_10bit_addressing = false;
  244. stat = i2c_init_master(ptr, freq, &config);
  245. if (stat != status_success) {
  246. printf("failed to initialize i2c 0x%lx\n", (uint32_t) ptr);
  247. while (1) {
  248. }
  249. }
  250. }
  251. uint32_t board_init_spi_clock(SPI_Type *ptr)
  252. {
  253. if (ptr == HPM_SPI1) {
  254. clock_add_to_group(clock_spi1, 0);
  255. return clock_get_frequency(clock_spi1);
  256. } else if (ptr == HPM_SPI3) {
  257. clock_add_to_group(clock_spi3, 0);
  258. return clock_get_frequency(clock_spi3);
  259. } else {
  260. ;
  261. }
  262. return 0;
  263. }
  264. void board_init_gpio_pins(void)
  265. {
  266. init_gpio_pins();
  267. /* Key A*/
  268. gpio_set_pin_input(BOARD_APP_GPIO_CTRL, BOARD_APP_GPIO_INDEX, BOARD_APP_GPIO_PIN);
  269. }
  270. void board_init_spi_pins(SPI_Type *ptr)
  271. {
  272. init_spi_pins(ptr);
  273. }
  274. void board_init_spi_pins_with_gpio_as_cs(SPI_Type *ptr)
  275. {
  276. init_spi_pins_with_gpio_as_cs(ptr);
  277. gpio_set_pin_output_with_initial(BOARD_SPI_CS_GPIO_CTRL, GPIO_GET_PORT_INDEX(BOARD_SPI_CS_PIN),
  278. GPIO_GET_PIN_INDEX(BOARD_SPI_CS_PIN), !BOARD_SPI_CS_ACTIVE_LEVEL);
  279. }
  280. void board_write_spi_cs(uint32_t pin, uint8_t state)
  281. {
  282. gpio_write_pin(BOARD_SPI_CS_GPIO_CTRL, GPIO_GET_PORT_INDEX(pin), GPIO_GET_PIN_INDEX(pin), state);
  283. }
  284. void board_init_led_pins(void)
  285. {
  286. init_led_pins();
  287. gpio_set_pin_output_with_initial(BOARD_LED_GPIO_CTRL, BOARD_LED_GPIO_INDEX, BOARD_LED_GPIO_PIN, board_get_led_gpio_off_level());
  288. }
  289. void board_led_toggle(void)
  290. {
  291. gpio_toggle_pin(BOARD_LED_GPIO_CTRL, BOARD_LED_GPIO_INDEX, BOARD_LED_GPIO_PIN);
  292. }
  293. void board_led_write(uint8_t state)
  294. {
  295. gpio_write_pin(BOARD_LED_GPIO_CTRL, BOARD_LED_GPIO_INDEX, BOARD_LED_GPIO_PIN, state);
  296. }
  297. void board_init_pmp(void)
  298. {
  299. pmp_entry_t pmp_entry[16];
  300. extern uint32_t __noncacheable_start__[];
  301. extern uint32_t __noncacheable_end__[];
  302. volatile uint32_t axi_sram_start = 0x01200000; /* AXI SRAM start */
  303. volatile uint32_t axi_sram_end = 0x01240000; /* AXI SRAM end */
  304. uint32_t start_addr, end_addr, length;
  305. uint8_t index = 0;
  306. start_addr = (uint32_t) __noncacheable_start__;
  307. end_addr = (uint32_t) __noncacheable_end__;
  308. if ((start_addr >= axi_sram_start) && (end_addr <= axi_sram_end)) {
  309. length = end_addr - start_addr;
  310. if (length > 0) {
  311. /* Ensure the address and the length are power of 2 aligned */
  312. assert((length & (length - 1U)) == 0U);
  313. assert((start_addr & (length - 1U)) == 0U);
  314. pmp_entry[index].pmp_addr = PMP_NAPOT_ADDR(start_addr, length);
  315. pmp_entry[index].pmp_cfg.val = PMP_CFG(READ_EN, WRITE_EN, EXECUTE_EN, ADDR_MATCH_NAPOT, REG_UNLOCK);
  316. pmp_entry[index].pma_addr = PMA_NAPOT_ADDR(start_addr, length);
  317. pmp_entry[index].pma_cfg.val = PMA_CFG(ADDR_MATCH_NAPOT, MEM_TYPE_MEM_NON_CACHE_BUF, AMO_EN);
  318. index++;
  319. }
  320. pmp_config(&pmp_entry[0], index);
  321. }
  322. }
  323. void board_init_clock(void)
  324. {
  325. uint32_t cpu0_freq = clock_get_frequency(clock_cpu0);
  326. if (cpu0_freq == PLLCTL_SOC_PLL_REFCLK_FREQ) {
  327. /* Configure the External OSC ramp-up time: ~9ms */
  328. pllctlv2_xtal_set_rampup_time(HPM_PLLCTLV2, 32ul * 1000ul * 9u);
  329. /* select clock setting preset1 */
  330. sysctl_clock_set_preset(HPM_SYSCTL, 2);
  331. }
  332. /* Add Clocks to group 0 */
  333. clock_add_to_group(clock_cpu0, 0);
  334. clock_add_to_group(clock_mchtmr0, 0);
  335. clock_add_to_group(clock_ahb0, 0);
  336. clock_add_to_group(clock_axif, 0);
  337. clock_add_to_group(clock_axis, 0);
  338. clock_add_to_group(clock_axic, 0);
  339. clock_add_to_group(clock_rom0, 0);
  340. clock_add_to_group(clock_xpi0, 0);
  341. clock_add_to_group(clock_lmm0, 0);
  342. clock_add_to_group(clock_ram0, 0);
  343. clock_add_to_group(clock_hdma, 0);
  344. clock_add_to_group(clock_xdma, 0);
  345. clock_add_to_group(clock_gpio, 0);
  346. clock_add_to_group(clock_ptpc, 0);
  347. /* Motor Related */
  348. clock_add_to_group(clock_qei0, 0);
  349. clock_add_to_group(clock_plb0, 0);
  350. clock_add_to_group(clock_qei1, 0);
  351. clock_add_to_group(clock_qeo0, 0);
  352. clock_add_to_group(clock_qeo1, 0);
  353. clock_add_to_group(clock_pwm0, 0);
  354. clock_add_to_group(clock_pwm1, 0);
  355. clock_add_to_group(clock_emds, 0);
  356. /* Connect Group0 to CPU0 */
  357. clock_connect_group_to_cpu(0, 0);
  358. /* Bump up DCDC voltage to 1275mv */
  359. pcfg_dcdc_set_voltage(HPM_PCFG, 1275);
  360. /* Configure PLL0 Post Divider */
  361. pllctlv2_set_postdiv(HPM_PLLCTLV2, pllctlv2_pll0, pllctlv2_clk0, pllctlv2_div_1p0); /* PLL0CLK0: 480MHz */
  362. pllctlv2_set_postdiv(HPM_PLLCTLV2, pllctlv2_pll0, pllctlv2_clk1, pllctlv2_div_1p2); /* PLL0CLK1: 400MHz */
  363. /* Configure PLL0 Frequency to 480MHz */
  364. pllctlv2_init_pll_with_freq(HPM_PLLCTLV2, pllctlv2_pll0, BOARD_CPU_FREQ);
  365. /* CPU clock use clk_src_pll0_clk0 */
  366. clock_set_source_divider(clock_cpu0, clk_src_pll0_clk0, 1);
  367. clock_update_core_clock();
  368. /* Configure mchtmr to 24MHz */
  369. clock_set_source_divider(clock_mchtmr0, clk_src_osc24m, 1);
  370. }
  371. uint32_t board_init_uart_clock(UART_Type *ptr)
  372. {
  373. uint32_t freq = 0U;
  374. if (ptr == HPM_UART0) {
  375. clock_add_to_group(clock_uart0, 0);
  376. freq = clock_get_frequency(clock_uart0);
  377. } else if (ptr == HPM_UART4) {
  378. clock_add_to_group(clock_uart4, 0);
  379. freq = clock_get_frequency(clock_uart4);
  380. } else {
  381. /* Not supported */
  382. }
  383. return freq;
  384. }
  385. void board_init_usb(USB_Type *ptr)
  386. {
  387. if (ptr == HPM_USB0) {
  388. init_usb_pins(ptr);
  389. clock_add_to_group(clock_usb0, 0);
  390. usb_hcd_set_power_ctrl_polarity(ptr, true);
  391. /* Wait USB_PWR pin control vbus power stable. Time depend on decoupling capacitor, you can decrease or increase this time */
  392. board_delay_ms(100);
  393. /* As LQFP100 has no vbus pin, so should be call usb_phy_using_internal_vbus() API to use internal vbus. */
  394. /* usb_phy_using_internal_vbus(ptr); */
  395. }
  396. }
  397. void board_init_adc16_pins(void)
  398. {
  399. init_adc16_pins();
  400. }
  401. uint32_t board_init_adc_clock(void *ptr, bool clk_src_bus) /* motor system should be use clk_adc_src_ahb0 */
  402. {
  403. uint32_t freq = 0;
  404. if (ptr == (void *)HPM_ADC0) {
  405. clock_add_to_group(clock_adc0, 0);
  406. if (clk_src_bus) {
  407. /* Configure the ADC clock from AHB (@200MHz by default)*/
  408. clock_set_adc_source(clock_adc0, clk_adc_src_ahb0);
  409. } else {
  410. /* Configure the ADC clock from ANA (@200MHz by default)*/
  411. clock_set_adc_source(clock_adc0, clk_adc_src_ana0);
  412. }
  413. freq = clock_get_frequency(clock_adc0);
  414. } else if (ptr == (void *)HPM_ADC1) {
  415. clock_add_to_group(clock_adc1, 0);
  416. if (clk_src_bus) {
  417. /* Configure the ADC clock from AHB (@200MHz by default)*/
  418. clock_set_adc_source(clock_adc1, clk_adc_src_ahb0);
  419. } else {
  420. /* Configure the ADC clock from ANA (@200MHz by default)*/
  421. clock_set_adc_source(clock_adc1, clk_adc_src_ana1);
  422. }
  423. freq = clock_get_frequency(clock_adc1);
  424. } else {
  425. ;
  426. }
  427. return freq;
  428. }
  429. void board_init_acmp_pins(void)
  430. {
  431. init_acmp_pins();
  432. }
  433. void board_init_acmp_clock(ACMP_Type *ptr)
  434. {
  435. (void)ptr;
  436. clock_add_to_group(BOARD_ACMP_CLK, BOARD_RUNNING_CORE & 0x1);
  437. }
  438. void board_init_can(MCAN_Type *ptr)
  439. {
  440. init_can_pins(ptr);
  441. }
  442. uint32_t board_init_can_clock(MCAN_Type *ptr)
  443. {
  444. uint32_t freq = 0;
  445. if (ptr == HPM_MCAN0) {
  446. /* Set the CAN0 peripheral clock to 80MHz */
  447. clock_add_to_group(clock_can0, 0);
  448. clock_set_source_divider(clock_can0, clk_src_pll1_clk0, 10);
  449. freq = clock_get_frequency(clock_can0);
  450. } else if (ptr == HPM_MCAN1) {
  451. /* Set the CAN1 peripheral clock to 80MHz */
  452. clock_add_to_group(clock_can1, 0);
  453. clock_set_source_divider(clock_can1, clk_src_pll1_clk0, 10);
  454. freq = clock_get_frequency(clock_can1);
  455. } else if (ptr == HPM_MCAN2) {
  456. /* Set the CAN2 peripheral clock to 80MHz */
  457. clock_add_to_group(clock_can2, 0);
  458. clock_set_source_divider(clock_can2, clk_src_pll1_clk0, 10);
  459. freq = clock_get_frequency(clock_can2);
  460. } else if (ptr == HPM_MCAN3) {
  461. /* Set the CAN3 peripheral clock to 80MHz */
  462. clock_add_to_group(clock_can3, 0);
  463. clock_set_source_divider(clock_can3, clk_src_pll1_clk0, 10);
  464. freq = clock_get_frequency(clock_can3);
  465. } else {
  466. /* Invalid CAN instance */
  467. }
  468. return freq;
  469. }
  470. hpm_stat_t board_init_enet_ptp_clock(ENET_Type *ptr)
  471. {
  472. /* set clock source */
  473. if (ptr == HPM_ENET0) {
  474. clock_add_to_group(clock_ptp0, 0);
  475. /* make sure pll0_clk0 output clock at 400MHz to get a clock at 100MHz for the enet0 ptp function */
  476. /* clock_set_source_divider(clock_ptp0, clk_src_pll1_clk1, 4); */ /* 100MHz */
  477. } else {
  478. return status_invalid_argument;
  479. }
  480. return status_success;
  481. }
  482. hpm_stat_t board_init_enet_pins(ENET_Type *ptr)
  483. {
  484. init_enet_pins(ptr);
  485. if (ptr == HPM_ENET0) {
  486. gpio_set_pin_output_with_initial(BOARD_ENET_RGMII_RST_GPIO, BOARD_ENET_RGMII_RST_GPIO_INDEX, BOARD_ENET_RGMII_RST_GPIO_PIN, 0);
  487. } else {
  488. return status_invalid_argument;
  489. }
  490. return status_success;
  491. }
  492. hpm_stat_t board_reset_enet_phy(ENET_Type *ptr)
  493. {
  494. if (ptr == HPM_ENET0) {
  495. gpio_write_pin(BOARD_ENET_RGMII_RST_GPIO, BOARD_ENET_RGMII_RST_GPIO_INDEX, BOARD_ENET_RGMII_RST_GPIO_PIN, 0);
  496. board_delay_ms(1);
  497. gpio_write_pin(BOARD_ENET_RGMII_RST_GPIO, BOARD_ENET_RGMII_RST_GPIO_INDEX, BOARD_ENET_RGMII_RST_GPIO_PIN, 1);
  498. } else {
  499. return status_invalid_argument;
  500. }
  501. return status_success;
  502. }
  503. uint8_t board_get_enet_dma_pbl(ENET_Type *ptr)
  504. {
  505. (void) ptr;
  506. return enet_pbl_32;
  507. }
  508. hpm_stat_t board_enable_enet_irq(ENET_Type *ptr)
  509. {
  510. if (ptr == HPM_ENET0) {
  511. intc_m_enable_irq(IRQn_ENET0);
  512. } else {
  513. return status_invalid_argument;
  514. }
  515. return status_success;
  516. }
  517. hpm_stat_t board_disable_enet_irq(ENET_Type *ptr)
  518. {
  519. if (ptr == HPM_ENET0) {
  520. intc_m_disable_irq(IRQn_ENET0);
  521. } else {
  522. return status_invalid_argument;
  523. }
  524. return status_success;
  525. }
  526. void board_init_enet_pps_pins(ENET_Type *ptr)
  527. {
  528. (void) ptr;
  529. init_enet_pps_pins();
  530. }
  531. hpm_stat_t board_init_enet_rmii_reference_clock(ENET_Type *ptr, bool internal)
  532. {
  533. /* Configure Enet clock to output reference clock */
  534. if (ptr == HPM_ENET0) {
  535. clock_add_to_group(clock_eth0, 0);
  536. if (internal) {
  537. /* set pll output frequency at 1GHz */
  538. if (pllctlv2_init_pll_with_freq(HPM_PLLCTLV2, pllctlv2_pll2, 1000000000UL) == status_success) {
  539. /* set pll2_clk1 output frequency at 250MHz from PLL2 divided by 4 (1 + 15 / 5) */
  540. pllctlv2_set_postdiv(HPM_PLLCTLV2, pllctlv2_pll2, pllctlv2_clk1, pllctlv2_div_4p0);
  541. /* set eth clock frequency at 50MHz for enet0 */
  542. /* clock_set_source_divider(clock_eth0, clk_src_pll2_clk1, 5); */
  543. } else {
  544. return status_fail;
  545. }
  546. }
  547. } else {
  548. return status_invalid_argument;
  549. }
  550. enet_rmii_enable_clock(ptr, internal); /* defined in hpm_enet_soc_drv.h, not sure */
  551. return status_success;
  552. }
  553. hpm_stat_t board_init_enet_rgmii_clock_delay(ENET_Type *ptr)
  554. {
  555. if (ptr == HPM_ENET0) {
  556. clock_add_to_group(clock_eth0, BOARD_RUNNING_CORE & 0x1);
  557. return enet_rgmii_set_clock_delay(ptr, BOARD_ENET_RGMII_TX_DLY, BOARD_ENET_RGMII_RX_DLY); /* defined in hpm_enet_soc_drv.h, not sure */
  558. }
  559. return status_invalid_argument;
  560. }
  561. void board_init_owr_pins(OWR_Type *ptr)
  562. {
  563. init_owr_pins(ptr);
  564. }
  565. void board_init_ethercat(ESC_Type *ptr)
  566. {
  567. (void)ptr;
  568. clock_add_to_group(clock_esc0, 0);
  569. init_esc_pins();
  570. /* PHY reset pin */
  571. gpio_set_pin_output_with_initial(HPM_GPIO0, BOARD_ECAT_PHY0_RESET_GPIO_PORT_INDEX, BOARD_ECAT_PHY0_RESET_PIN_INDEX, 0);
  572. #if BOARD_ECAT_SUPPORT_PORT1
  573. gpio_set_pin_output_with_initial(HPM_GPIO0, BOARD_ECAT_PHY1_RESET_GPIO_PORT_INDEX, BOARD_ECAT_PHY1_RESET_PIN_INDEX, 0);
  574. #endif
  575. #if BOARD_ECAT_SUPPORT_PORT2
  576. gpio_set_pin_output_with_initial(HPM_GPIO0, BOARD_ECAT_PHY2_RESET_GPIO_PORT_INDEX, BOARD_ECAT_PHY2_RESET_PIN_INDEX, 0);
  577. #endif
  578. }
  579. /* switch and led pin for ethercat io test */
  580. void board_init_switch_led(void)
  581. {
  582. init_esc_in_out_pin();
  583. gpio_set_pin_input(BOARD_ECAT_IN1_GPIO, BOARD_ECAT_IN1_GPIO_PORT_INDEX, BOARD_ECAT_IN1_GPIO_PIN_INDEX);
  584. gpio_set_pin_input(BOARD_ECAT_IN2_GPIO, BOARD_ECAT_IN2_GPIO_PORT_INDEX, BOARD_ECAT_IN2_GPIO_PIN_INDEX);
  585. gpio_set_pin_output_with_initial(BOARD_ECAT_OUT1_GPIO, BOARD_ECAT_OUT1_GPIO_PORT_INDEX, BOARD_ECAT_OUT1_GPIO_PIN_INDEX, 0);
  586. gpio_set_pin_output_with_initial(BOARD_ECAT_OUT2_GPIO, BOARD_ECAT_OUT2_GPIO_PORT_INDEX, BOARD_ECAT_OUT2_GPIO_PIN_INDEX, 0);
  587. }
  588. void board_init_adc_qeiv2_pins(void)
  589. {
  590. init_adc_qeiv2_pins();
  591. }
  592. void board_init_gptmr_channel_pin(GPTMR_Type *ptr, uint32_t channel, bool as_comp)
  593. {
  594. init_gptmr_channel_pin(ptr, channel, as_comp);
  595. }
  596. void board_init_owr_clock(OWR_Type *ptr)
  597. {
  598. (void) ptr;
  599. clock_add_to_group(BOARD_OWR_CLK_NAME, 0);
  600. }
  601. void board_init_clk_ref_pin(void)
  602. {
  603. init_clk_ref_pins();
  604. }
  605. uint32_t board_init_gptmr_clock(GPTMR_Type *ptr)
  606. {
  607. uint32_t freq = 0U;
  608. if (ptr == HPM_GPTMR0) {
  609. clock_add_to_group(clock_gptmr0, BOARD_RUNNING_CORE & 0x1);
  610. freq = clock_get_frequency(clock_gptmr0);
  611. } else if (ptr == HPM_GPTMR1) {
  612. clock_add_to_group(clock_gptmr1, BOARD_RUNNING_CORE & 0x1);
  613. freq = clock_get_frequency(clock_gptmr1);
  614. } else if (ptr == HPM_GPTMR2) {
  615. clock_add_to_group(clock_gptmr2, BOARD_RUNNING_CORE & 0x1);
  616. freq = clock_get_frequency(clock_gptmr2);
  617. } else if (ptr == HPM_GPTMR3) {
  618. clock_add_to_group(clock_gptmr3, BOARD_RUNNING_CORE & 0x1);
  619. freq = clock_get_frequency(clock_gptmr3);
  620. } else if (ptr == HPM_PTMR) {
  621. clock_add_to_group(clock_ptmr, BOARD_RUNNING_CORE & 0x1);
  622. freq = clock_get_frequency(clock_ptmr);
  623. } else {
  624. /* Not supported */
  625. }
  626. return freq;
  627. }