at32_msp.c 9.6 KB

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