apm32_msp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. else if (spi_x == SPI1) {
  99. /* Enable related Clock */
  100. RCM_EnableAPB1PeriphClock(RCM_APB2_PERIPH_SPI1);;
  101. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_AFIO);
  102. GPIO_ConfigPinRemap(GPIO_REMAP_SWJ_JTAGDISABLE);
  103. /* Configure FLASH_SPI pins: SCK */
  104. gpioConfig.pin = GPIO_PIN_5;
  105. gpioConfig.mode = GPIO_MODE_AF_PP;
  106. gpioConfig.speed = GPIO_SPEED_50MHz;
  107. GPIO_Config(GPIOA, &gpioConfig);
  108. /* Configure FLASH_SPI pins: MOSI */
  109. gpioConfig.pin = GPIO_PIN_7;
  110. GPIO_Config(GPIOA, &gpioConfig);
  111. /* Configure FLASH_SPI pins: MISO */
  112. gpioConfig.pin = GPIO_PIN_6;
  113. gpioConfig.mode = GPIO_MODE_IN_FLOATING;
  114. GPIO_Config(GPIOA, &gpioConfig);
  115. }
  116. #endif
  117. }
  118. /**
  119. * @brief apm32 timer gpio init
  120. *
  121. * @note This function is only used as an example, please initialize
  122. * the peripherals according to the specific hardware board.
  123. */
  124. void apm32_msp_timer_init(void *Instance)
  125. {
  126. #ifdef BSP_USING_PWM3
  127. GPIO_Config_T gpio_config;
  128. TMR_T *tmr_x = (TMR_T *)Instance;
  129. if (tmr_x == TMR3)
  130. {
  131. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR3);
  132. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC | RCM_APB2_PERIPH_AFIO);
  133. GPIO_ConfigPinRemap(GPIO_FULL_REMAP_TMR3);
  134. /* TMR3 channel 1 gpio config */
  135. gpio_config.pin = GPIO_PIN_6;
  136. gpio_config.mode = GPIO_MODE_AF_PP;
  137. gpio_config.speed = GPIO_SPEED_50MHz;
  138. GPIO_Config(GPIOC, &gpio_config);
  139. /* TMR3 channel 2 gpio config */
  140. gpio_config.pin = GPIO_PIN_7;
  141. GPIO_Config(GPIOC, &gpio_config);
  142. /* TMR3 channel 3 gpio config */
  143. gpio_config.pin = GPIO_PIN_8;
  144. GPIO_Config(GPIOC, &gpio_config);
  145. /* TMR3 channel 4 gpio config */
  146. gpio_config.pin = GPIO_PIN_9;
  147. GPIO_Config(GPIOC, &gpio_config);
  148. }
  149. #endif
  150. }
  151. /**
  152. * @brief apm32 sdio gpio init
  153. *
  154. * @note This function is only used as an example, please initialize
  155. * the peripherals according to the specific hardware board.
  156. */
  157. void apm32_msp_sdio_init(void *Instance)
  158. {
  159. #ifdef BSP_USING_SDIO
  160. GPIO_Config_T GPIO_InitStructure;
  161. /* Enable the GPIO and DMA2 Clock */
  162. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC | RCM_APB2_PERIPH_GPIOD);
  163. /* Enable the SDIO Clock */
  164. RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_SDIO);
  165. /* Configure the GPIO pin */
  166. GPIO_InitStructure.pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
  167. GPIO_InitStructure.mode = GPIO_MODE_AF_PP;
  168. GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
  169. GPIO_Config(GPIOC, &GPIO_InitStructure);
  170. GPIO_InitStructure.pin = GPIO_PIN_2;
  171. GPIO_Config(GPIOD, &GPIO_InitStructure);
  172. #endif
  173. }
  174. /**
  175. * @brief apm32 can gpio init
  176. *
  177. * @note This function is only used as an example, please initialize
  178. * the peripherals according to the specific hardware board.
  179. */
  180. void apm32_msp_can_init(void *Instance)
  181. {
  182. #if defined(BSP_USING_CAN1) || defined(BSP_USING_CAN2)
  183. GPIO_Config_T GPIO_InitStructure;
  184. CAN_T *CANx = (CAN_T *)Instance;
  185. if (CAN1 == CANx)
  186. {
  187. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_CAN1);
  188. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  189. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOD);
  190. GPIO_ConfigPinRemap(GPIO_REMAP2_CAN1);
  191. /* CAN1 Tx */
  192. GPIO_InitStructure.pin = GPIO_PIN_1;
  193. GPIO_InitStructure.mode = GPIO_MODE_AF_PP;
  194. GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
  195. GPIO_Config(GPIOD, &GPIO_InitStructure);
  196. /* CAN1 Rx */
  197. GPIO_InitStructure.pin = GPIO_PIN_0;
  198. GPIO_InitStructure.mode = GPIO_MODE_IN_FLOATING;
  199. GPIO_Config(GPIOD, &GPIO_InitStructure);
  200. }
  201. else if (CAN2 == CANx)
  202. {
  203. RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_CAN2);
  204. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  205. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
  206. GPIO_ConfigPinRemap(GPIO_REMAP_CAN2);
  207. /* CAN2 Tx */
  208. GPIO_InitStructure.pin = GPIO_PIN_6;
  209. GPIO_InitStructure.mode = GPIO_MODE_AF_PP;
  210. GPIO_InitStructure.speed = GPIO_SPEED_50MHz;
  211. GPIO_Config(GPIOB, &GPIO_InitStructure);
  212. /* CAN2 Rx */
  213. GPIO_InitStructure.pin = GPIO_PIN_5;
  214. GPIO_InitStructure.mode = GPIO_MODE_IN_FLOATING;
  215. GPIO_Config(GPIOB, &GPIO_InitStructure);
  216. }
  217. #endif
  218. }
  219. /**
  220. * @brief apm32 eth gpio init
  221. *
  222. * @note This function is only used as an example, please initialize
  223. * the peripherals according to the specific hardware board.
  224. */
  225. void apm32_msp_eth_init(void *Instance)
  226. {
  227. #ifdef BSP_USING_ETH
  228. GPIO_Config_T GPIO_ConfigStruct;
  229. /* Enable SYSCFG clock */
  230. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  231. /* Enable GPIOs clocks */
  232. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_GPIOB
  233. | RCM_APB2_PERIPH_GPIOC | RCM_APB2_PERIPH_GPIOD);
  234. /* ETHERNET pins remapp in APM32107-EVAL board: RX_DV and RxD[3:0] */
  235. GPIO_ConfigPinRemap(GPIO_REMAP_ETH_MAC);
  236. /* MII/RMII Media interface selection */
  237. GPIO_ConfigPinRemap(GPIO_REMAP_MACEISEL_RMII);
  238. /*********************** Ethernet pins configuration ***************************/
  239. /*
  240. ETH_MDIO -------------------------> PA2
  241. ETH_MDC --------------------------> PC1
  242. ETH_MII_RX_CLK/ETH_RMII_REF_CLK---> PA1
  243. ETH_MII_RX_DV/ETH_RMII_CRS_DV ----> PD8
  244. ETH_MII_RXD0/ETH_RMII_RXD0 -------> PD9
  245. ETH_MII_RXD1/ETH_RMII_RXD1 -------> PD10
  246. ETH_MII_TX_EN/ETH_RMII_TX_EN -----> PB11
  247. ETH_MII_TXD0/ETH_RMII_TXD0 -------> PB12
  248. ETH_MII_TXD1/ETH_RMII_TXD1 -------> PB13
  249. */
  250. /* Configure PA1 as input floating */
  251. GPIO_ConfigStruct.pin = GPIO_PIN_1;
  252. GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
  253. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  254. /* Configure PD8, PD9, PD10 as input floating */
  255. GPIO_ConfigStruct.pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
  256. GPIO_ConfigStruct.mode = GPIO_MODE_IN_FLOATING;
  257. GPIO_Config(GPIOD, &GPIO_ConfigStruct);
  258. /* Configure PA2 as alternate function output Push-pull*/
  259. GPIO_ConfigStruct.pin = GPIO_PIN_2;
  260. GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
  261. GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
  262. GPIO_Config(GPIOA, &GPIO_ConfigStruct);
  263. /* Configure PC1 as alternate function output Push-pull */
  264. GPIO_ConfigStruct.pin = GPIO_PIN_1;
  265. GPIO_Config(GPIOC, &GPIO_ConfigStruct);
  266. /* Configure PB11, PB12, PB13 as alternate function push-pull */
  267. GPIO_ConfigStruct.pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  268. GPIO_Config(GPIOB, &GPIO_ConfigStruct);
  269. #endif
  270. }