at32_msp.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-04-13 shelton first version
  9. */
  10. #include <at32f4xx.h>
  11. #include <rtthread.h>
  12. #include "at32_msp.h"
  13. #ifdef BSP_USING_UART
  14. void at32_msp_usart_init(void *Instance)
  15. {
  16. GPIO_InitType GPIO_InitStruct;
  17. USART_Type *USARTx = (USART_Type *)Instance;
  18. GPIO_StructInit(&GPIO_InitStruct);
  19. GPIO_InitStruct.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  20. #ifdef BSP_USING_UART1
  21. if(USART1 == USARTx)
  22. {
  23. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_USART1, ENABLE);
  24. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
  25. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  26. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_9;
  27. GPIO_Init(GPIOA, &GPIO_InitStruct);
  28. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  29. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_10;
  30. GPIO_Init(GPIOA, &GPIO_InitStruct);
  31. }
  32. #endif
  33. #ifdef BSP_USING_UART2
  34. if(USART2 == USARTx)
  35. {
  36. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_USART2, ENABLE);
  37. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
  38. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  39. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_2;
  40. GPIO_Init(GPIOA, &GPIO_InitStruct);
  41. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  42. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_3;
  43. GPIO_Init(GPIOA, &GPIO_InitStruct);
  44. }
  45. #endif
  46. #ifdef BSP_USING_UART3
  47. if(USART3 == USARTx)
  48. {
  49. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_USART3, ENABLE);
  50. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOB, ENABLE);
  51. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  52. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_10;
  53. GPIO_Init(GPIOB, &GPIO_InitStruct);
  54. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  55. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_11;
  56. GPIO_Init(GPIOB, &GPIO_InitStruct);
  57. }
  58. #endif
  59. /* Add others */
  60. }
  61. #endif /* BSP_USING_SERIAL */
  62. #ifdef BSP_USING_SPI
  63. void at32_msp_spi_init(void *Instance)
  64. {
  65. GPIO_InitType GPIO_InitStruct;
  66. SPI_Type *SPIx = (SPI_Type *)Instance;
  67. GPIO_StructInit(&GPIO_InitStruct);
  68. GPIO_InitStruct.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  69. #ifdef BSP_USING_SPI1
  70. if(SPI1 == SPIx)
  71. {
  72. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_SPI1, ENABLE);
  73. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
  74. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT_PP;
  75. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_4;
  76. GPIO_Init(GPIOA, &GPIO_InitStruct);
  77. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  78. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_5 | GPIO_Pins_7;
  79. GPIO_Init(GPIOA, &GPIO_InitStruct);
  80. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  81. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_6;
  82. GPIO_Init(GPIOA, &GPIO_InitStruct);
  83. }
  84. #endif
  85. #ifdef BSP_USING_SPI2
  86. if(SPI2 == SPIx)
  87. {
  88. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_SPI2, ENABLE);
  89. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOB, ENABLE);
  90. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT_PP;
  91. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_12;
  92. GPIO_Init(GPIOB, &GPIO_InitStruct);
  93. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  94. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_13 | GPIO_Pins_15;
  95. GPIO_Init(GPIOB, &GPIO_InitStruct);
  96. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  97. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_14;
  98. GPIO_Init(GPIOB, &GPIO_InitStruct);
  99. }
  100. #endif
  101. /* Add others */
  102. }
  103. #endif /* BSP_USING_SPI */
  104. #ifdef BSP_USING_SDIO
  105. void at32_msp_sdio_init(void *Instance)
  106. {
  107. GPIO_InitType GPIO_InitStructure;
  108. SDIO_Type *SDIOx = (SDIO_Type *)Instance;
  109. GPIO_StructInit(&GPIO_InitStructure);
  110. GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  111. if(SDIO1 == SDIOx)
  112. {
  113. /* if used dma ... */
  114. RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_DMA2, ENABLE);
  115. RCC_AHBPeriphClockCmd(RCC_AHBPERIPH_SDIO1, ENABLE);
  116. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOC | RCC_APB2PERIPH_GPIOD, ENABLE);
  117. GPIO_InitStructure.GPIO_Pins = GPIO_Pins_8 | GPIO_Pins_9 | GPIO_Pins_10 | GPIO_Pins_11 | GPIO_Pins_12;
  118. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  119. GPIO_Init(GPIOC, &GPIO_InitStructure);
  120. GPIO_InitStructure.GPIO_Pins = GPIO_Pins_2;
  121. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  122. GPIO_Init(GPIOD, &GPIO_InitStructure);
  123. }
  124. }
  125. #endif /* BSP_USING_SDIO */
  126. #ifdef BSP_USING_PWM
  127. void at32_msp_tmr_init(void *Instance)
  128. {
  129. GPIO_InitType GPIO_InitStructure;
  130. TMR_Type *TMRx = (TMR_Type *)Instance;
  131. if(TMRx == TMR1)
  132. {
  133. /* TMR1 clock enable */
  134. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_TMR1, ENABLE);
  135. /* GPIOA clock enable */
  136. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
  137. /* GPIOA Configuration:TMR1 Channel1 and Channel4 as alternate function push-pull */
  138. GPIO_InitStructure.GPIO_Pins = GPIO_Pins_8 | GPIO_Pins_11;
  139. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  140. GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  141. GPIO_Init(GPIOA, &GPIO_InitStructure);
  142. }
  143. if(TMRx == TMR2)
  144. {
  145. /* TMR2 clock enable */
  146. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR2, ENABLE);
  147. /* GPIOA clock enable */
  148. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
  149. /* GPIOA Configuration:TMR2 Channel1 and Channel2 as alternate function push-pull */
  150. GPIO_InitStructure.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1;
  151. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  152. GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  153. GPIO_Init(GPIOA, &GPIO_InitStructure);
  154. }
  155. /* Add others */
  156. }
  157. #endif /* BSP_USING_PWM */
  158. #ifdef BSP_USING_ADC
  159. void at32_msp_adc_init(void *Instance)
  160. {
  161. GPIO_InitType GPIO_InitStruct;
  162. ADC_Type *ADCx = (ADC_Type *)Instance;
  163. #ifdef BSP_USING_ADC1
  164. if(ADCx == ADC1)
  165. {
  166. /* ADC1 & GPIO clock enable */
  167. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_ADC1 | RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_GPIOB | RCC_APB2PERIPH_GPIOC,ENABLE);
  168. /* Configure ADC Channel as analog input */
  169. GPIO_StructInit(&GPIO_InitStruct);
  170. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1 | GPIO_Pins_2 | GPIO_Pins_3 | GPIO_Pins_4 | GPIO_Pins_5;
  171. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_ANALOG;
  172. GPIO_Init(GPIOC, &GPIO_InitStruct);
  173. }
  174. #endif
  175. #ifdef BSP_USING_ADC2
  176. if(ADCx == ADC2)
  177. {
  178. /* ADC2 & GPIO clock enable */
  179. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_ADC2 | RCC_APB2PERIPH_GPIOA | RCC_APB2PERIPH_GPIOB | RCC_APB2PERIPH_GPIOC,ENABLE);
  180. /* Configure ADC Channel as analog input */
  181. GPIO_StructInit(&GPIO_InitStruct);
  182. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_0 | GPIO_Pins_1 | GPIO_Pins_2 | GPIO_Pins_3 | GPIO_Pins_4 | GPIO_Pins_5;
  183. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_ANALOG;
  184. GPIO_Init(GPIOC, &GPIO_InitStruct);
  185. }
  186. #endif
  187. }
  188. #endif /* BSP_USING_ADC */
  189. #ifdef BSP_USING_HWTIMER
  190. void at32_msp_hwtmr_init(void *Instance)
  191. {
  192. TMR_Type *TMRx = (TMR_Type *)Instance;
  193. #ifdef BSP_USING_HWTMR3
  194. if(TMRx == TMR3)
  195. {
  196. /* TMR3 clock enable */
  197. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR3, ENABLE);
  198. }
  199. #endif
  200. #ifdef BSP_USING_HWTMR4
  201. if(TMRx == TMR4)
  202. {
  203. /* TMR4 clock enable */
  204. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR4, ENABLE);
  205. }
  206. #endif
  207. #ifdef BSP_USING_HWTMR5
  208. if(TMRx == TMR5)
  209. {
  210. /* TMR5 clock enable */
  211. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_TMR5, ENABLE);
  212. }
  213. #endif
  214. }
  215. #endif
  216. #ifdef BSP_USING_CAN
  217. void at32_msp_can_init(void *Instance)
  218. {
  219. GPIO_InitType GPIO_InitStruct;
  220. CAN_Type *CANx = (CAN_Type *)Instance;
  221. GPIO_StructInit(&GPIO_InitStruct);
  222. GPIO_InitStruct.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;
  223. #ifdef BSP_USING_CAN1
  224. if(CAN1 == CANx)
  225. {
  226. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_CAN1, ENABLE);
  227. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOA, ENABLE);
  228. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  229. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_12;
  230. GPIO_Init(GPIOA, &GPIO_InitStruct);
  231. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  232. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_11;
  233. GPIO_Init(GPIOA, &GPIO_InitStruct);
  234. }
  235. #endif
  236. #ifdef BSP_USING_CAN2
  237. if(CAN2 == CANx)
  238. {
  239. RCC_APB1PeriphClockCmd(RCC_APB1PERIPH_CAN2, ENABLE);
  240. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_AFIO, ENABLE);
  241. RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_GPIOB, ENABLE);
  242. GPIO_PinsRemapConfig(AFIO_MAP6_CAN2_0001, ENABLE);
  243. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
  244. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_6;
  245. GPIO_Init(GPIOB, &GPIO_InitStruct);
  246. GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  247. GPIO_InitStruct.GPIO_Pins = GPIO_Pins_5;
  248. GPIO_Init(GPIOB, &GPIO_InitStruct);
  249. }
  250. #endif
  251. }
  252. #endif /* BSP_USING_CAN */