apm32_msp.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-03-08 luobeihai first version
  9. * 2023-04-07 luobeihai add uart/can/sdio peripherals Init
  10. */
  11. #include <rtthread.h>
  12. #include "board.h"
  13. #include "drv_common.h"
  14. #include "apm32_msp.h"
  15. /**
  16. * @brief apm32 usart gpio init
  17. *
  18. * @note This function is only used as an example, please initialize
  19. * the peripherals according to the specific hardware board.
  20. */
  21. void apm32_usart_init(void)
  22. {
  23. GPIO_Config_T GPIO_ConfigStruct;
  24. #ifdef BSP_USING_UART1
  25. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_USART1);
  26. GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
  27. GPIO_ConfigStruct.pin = GPIO_PIN_9;
  28. GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
  29. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  30. GPIO_ConfigStruct.mode = GPIO_MODE_IN_PU;
  31. GPIO_ConfigStruct.pin = GPIO_PIN_10;
  32. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  33. #endif
  34. #ifdef BSP_USING_UART2
  35. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
  36. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_USART2);
  37. GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
  38. GPIO_ConfigStruct.pin = GPIO_PIN_2;
  39. GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
  40. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  41. GPIO_ConfigStruct.mode = GPIO_MODE_IN_PU;
  42. GPIO_ConfigStruct.pin = GPIO_PIN_3;
  43. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  44. #endif
  45. #ifdef BSP_USING_UART3
  46. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
  47. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_USART3);
  48. GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
  49. GPIO_ConfigStruct.pin = GPIO_PIN_10;
  50. GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
  51. GPIO_Config(GPIOB, &GPIO_ConfigStruct);
  52. GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
  53. GPIO_ConfigStruct.pin = GPIO_PIN_11;
  54. GPIO_Config(GPIOB, &GPIO_ConfigStruct);
  55. #endif
  56. #ifdef BSP_USING_UART4
  57. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);
  58. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_UART4);
  59. GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
  60. GPIO_ConfigStruct.pin = GPIO_PIN_10;
  61. GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
  62. GPIO_Config(GPIOC, &GPIO_ConfigStruct);
  63. GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
  64. GPIO_ConfigStruct.pin = GPIO_PIN_11;
  65. GPIO_Config(GPIOC, &GPIO_ConfigStruct);
  66. #endif
  67. }
  68. /**
  69. * @brief apm32 spi gpio init
  70. *
  71. * @note This function is only used as an example, please initialize
  72. * the peripherals according to the specific hardware board.
  73. */
  74. void apm32_msp_spi_init(void *Instance)
  75. {
  76. #if defined (BSP_USING_SPI1) || defined (BSP_USING_SPI2) || defined (BSP_USING_SPI3)
  77. GPIO_Config_T gpioConfig;
  78. SPI_T *spi_x = (SPI_T *)Instance;
  79. if(spi_x == SPI3)
  80. {
  81. /* Enable related Clock */
  82. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_SPI3);
  83. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB | RCM_APB2_PERIPH_AFIO);
  84. GPIO_ConfigPinRemap(GPIO_REMAP_SWJ_JTAGDISABLE);
  85. /* Configure FLASH_SPI pins: SCK */
  86. gpioConfig.pin = GPIO_PIN_3;
  87. gpioConfig.mode = GPIO_MODE_AF_PP;
  88. gpioConfig.speed = GPIO_SPEED_50MHz;
  89. GPIO_Config(GPIOB, &gpioConfig);
  90. /* Configure FLASH_SPI pins: MOSI */
  91. gpioConfig.pin = GPIO_PIN_5;
  92. GPIO_Config(GPIOB, &gpioConfig);
  93. /* Configure FLASH_SPI pins: MISO */
  94. gpioConfig.pin = GPIO_PIN_4;
  95. gpioConfig.mode = GPIO_MODE_IN_FLOATING;
  96. GPIO_Config(GPIOB, &gpioConfig);
  97. }
  98. #endif
  99. }
  100. /**
  101. * @brief apm32 timer gpio init
  102. *
  103. * @note This function is only used as an example, please initialize
  104. * the peripherals according to the specific hardware board.
  105. */
  106. void apm32_msp_timer_init(void *Instance)
  107. {
  108. #ifdef BSP_USING_PWM3
  109. GPIO_Config_T gpio_config;
  110. TMR_T *tmr_x = (TMR_T *)Instance;
  111. if (tmr_x == TMR3)
  112. {
  113. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR3);
  114. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC | RCM_APB2_PERIPH_AFIO);
  115. GPIO_ConfigPinRemap(GPIO_FULL_REMAP_TMR3);
  116. /* TMR3 channel 1 gpio config */
  117. gpio_config.pin = GPIO_PIN_6;
  118. gpio_config.mode = GPIO_MODE_AF_PP;
  119. gpio_config.speed = GPIO_SPEED_50MHz;
  120. GPIO_Config(GPIOC, &gpio_config);
  121. /* TMR3 channel 2 gpio config */
  122. gpio_config.pin = GPIO_PIN_7;
  123. GPIO_Config(GPIOC, &gpio_config);
  124. /* TMR3 channel 3 gpio config */
  125. gpio_config.pin = GPIO_PIN_8;
  126. GPIO_Config(GPIOC, &gpio_config);
  127. /* TMR3 channel 4 gpio config */
  128. gpio_config.pin = GPIO_PIN_9;
  129. GPIO_Config(GPIOC, &gpio_config);
  130. }
  131. #endif
  132. }
  133. /**
  134. * @brief apm32 sdio gpio init
  135. *
  136. * @note This function is only used as an example, please initialize
  137. * the peripherals according to the specific hardware board.
  138. */
  139. void apm32_msp_sdio_init(void *Instance)
  140. {
  141. #ifdef BSP_USING_SDIO
  142. GPIO_Config_T GPIO_InitStructure;
  143. /* Enable the GPIO and DMA2 Clock */
  144. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC | RCM_APB2_PERIPH_GPIOD);
  145. /* Enable the SDIO Clock */
  146. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_SDIO);
  147. /* Configure the GPIO pin */
  148. GPIO_InitStructure.pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
  149. GPIO_InitStructure.mode = GPIO_MODE_AF_PP;
  150. GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
  151. GPIO_Config(GPIOC, &GPIO_InitStructure);
  152. GPIO_InitStructure.pin = GPIO_PIN_2;
  153. GPIO_Config(GPIOD, &GPIO_InitStructure);
  154. #endif
  155. }
  156. /**
  157. * @brief apm32 can gpio init
  158. *
  159. * @note This function is only used as an example, please initialize
  160. * the peripherals according to the specific hardware board.
  161. */
  162. void apm32_msp_can_init(void *Instance)
  163. {
  164. #if defined(BSP_USING_CAN1) || defined(BSP_USING_CAN2)
  165. GPIO_Config_T GPIO_InitStructure;
  166. CAN_T *CANx = (CAN_T *)Instance;
  167. if (CAN1 == CANx)
  168. {
  169. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_CAN1);
  170. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  171. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOD);
  172. GPIO_ConfigPinRemap(GPIO_REMAP2_CAN1);
  173. /* CAN1 Tx */
  174. GPIO_InitStructure.pin = GPIO_PIN_1;
  175. GPIO_InitStructure.mode = GPIO_MODE_AF_PP;
  176. GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
  177. GPIO_Config(GPIOD, &GPIO_InitStructure);
  178. /* CAN1 Rx */
  179. GPIO_InitStructure.pin = GPIO_PIN_0;
  180. GPIO_InitStructure.mode = GPIO_MODE_IN_FLOATING;
  181. GPIO_Config(GPIOD, &GPIO_InitStructure);
  182. }
  183. else if (CAN2 == CANx)
  184. {
  185. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_CAN2);
  186. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  187. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
  188. GPIO_ConfigPinRemap(GPIO_REMAP_CAN2);
  189. /* CAN2 Tx */
  190. GPIO_InitStructure.pin = GPIO_PIN_6;
  191. GPIO_InitStructure.mode = GPIO_MODE_AF_PP;
  192. GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
  193. GPIO_Config(GPIOB, &GPIO_InitStructure);
  194. /* CAN2 Rx */
  195. GPIO_InitStructure.pin = GPIO_PIN_5;
  196. GPIO_InitStructure.mode = GPIO_MODE_IN_FLOATING;
  197. GPIO_Config(GPIOB, &GPIO_InitStructure);
  198. }
  199. #endif
  200. }
  201. /**
  202. * @brief apm32 eth gpio init
  203. *
  204. * @note This function is only used as an example, please initialize
  205. * the peripherals according to the specific hardware board.
  206. */
  207. void apm32_msp_eth_init(void *Instance)
  208. {
  209. #ifdef BSP_USING_ETH
  210. GPIO_Config_T GPIO_ConfigStruct;
  211. /* Enable SYSCFG clock */
  212. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  213. /* Enable GPIOs clocks */
  214. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_GPIOB
  215. | RCM_APB2_PERIPH_GPIOC | RCM_APB2_PERIPH_GPIOD);
  216. /* ETHERNET pins remapp in APM32107-EVAL board: RX_DV and RxD[3:0] */
  217. GPIO_ConfigPinRemap(GPIO_REMAP_ETH_MAC);
  218. /* MII/RMII Media interface selection */
  219. GPIO_ConfigPinRemap(GPIO_REMAP_MACEISEL_RMII);
  220. /*********************** Ethernet pins configuration ***************************/
  221. /*
  222. ETH_MDIO -------------------------> PA2
  223. ETH_MDC --------------------------> PC1
  224. ETH_MII_RX_CLK/ETH_RMII_REF_CLK---> PA1
  225. ETH_MII_RX_DV/ETH_RMII_CRS_DV ----> PD8
  226. ETH_MII_RXD0/ETH_RMII_RXD0 -------> PD9
  227. ETH_MII_RXD1/ETH_RMII_RXD1 -------> PD10
  228. ETH_MII_TX_EN/ETH_RMII_TX_EN -----> PB11
  229. ETH_MII_TXD0/ETH_RMII_TXD0 -------> PB12
  230. ETH_MII_TXD1/ETH_RMII_TXD1 -------> PB13
  231. */
  232. /* Configure PA1 as input floating */
  233. GPIO_ConfigStruct.pin = GPIO_PIN_1;
  234. GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
  235. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  236. /* Configure PD8, PD9, PD10 as input floating */
  237. GPIO_ConfigStruct.pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
  238. GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
  239. GPIO_Config(GPIOD, &GPIO_ConfigStruct);
  240. /* Configure PA2 as alternate function output Push-pull*/
  241. GPIO_ConfigStruct.pin = GPIO_PIN_2;
  242. GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
  243. GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
  244. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  245. /* Configure PC1 as alternate function output Push-pull */
  246. GPIO_ConfigStruct.pin = GPIO_PIN_1;
  247. GPIO_Config(GPIOC, &GPIO_ConfigStruct);
  248. /* Configure PB11, PB12, PB13 as alternate function push-pull */
  249. GPIO_ConfigStruct.pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  250. GPIO_Config(GPIOB, &GPIO_ConfigStruct);
  251. #endif
  252. }