board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-22 airm2m first version
  9. */
  10. #include "board.h"
  11. void SystemClock_Config(void)
  12. {
  13. RCC_DeInit(); //复位RCC寄存器
  14. RCC_HSEConfig(RCC_HSE_ON); //使能HSE
  15. while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
  16. ; //等待HSE就绪
  17. RCC_PLLCmd(DISABLE); //关闭PLL
  18. AIR_RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_27, FLASH_Div_2); //配置PLL,8*27=216MHz
  19. RCC_PLLCmd(ENABLE); //使能PLL
  20. while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  21. ; //等待PLL就绪
  22. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //选择PLL作为系统时钟
  23. RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB时钟
  24. RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1时钟
  25. RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2时钟
  26. RCC_LSICmd(ENABLE); //使能内部低速时钟
  27. while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  28. ; //等待LSI就绪
  29. RCC_HSICmd(ENABLE); //使能内部高速时钟
  30. while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
  31. ; //等待HSI就绪
  32. }
  33. #ifdef BSP_USING_UART
  34. void air32_usart_clock_and_io_init(USART_TypeDef *usartx)
  35. {
  36. GPIO_InitTypeDef GPIO_InitStructure;
  37. /* USART1 TX-->PA9 RX-->PA10 */
  38. if (usartx == USART1)
  39. {
  40. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  41. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  42. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  43. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  44. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  45. GPIO_Init(GPIOA, &GPIO_InitStructure);
  46. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  47. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  48. GPIO_Init(GPIOA, &GPIO_InitStructure);
  49. }
  50. /* USART2 TX-->PA2 RX-->PA3 */
  51. if (usartx == USART2)
  52. {
  53. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  54. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  55. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  56. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  57. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  58. GPIO_Init(GPIOA, &GPIO_InitStructure);
  59. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  60. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  61. GPIO_Init(GPIOA, &GPIO_InitStructure);
  62. }
  63. /* USART3 TX-->PC10 RX-->PC11 */
  64. if (usartx == USART3)
  65. {
  66. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  67. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  68. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  69. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  70. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  71. GPIO_Init(GPIOC, &GPIO_InitStructure);
  72. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  73. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  74. GPIO_Init(GPIOC, &GPIO_InitStructure);
  75. GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
  76. }
  77. }
  78. #endif
  79. #ifdef BSP_USING_SPI
  80. void air32_spi_clock_and_io_init(SPI_TypeDef *spix)
  81. {
  82. GPIO_InitTypeDef GPIO_InitStructure;
  83. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  84. if (spix == SPI1)
  85. {
  86. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  87. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
  88. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  89. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  90. GPIO_Init(GPIOA, &GPIO_InitStructure);
  91. GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
  92. }
  93. if (spix == SPI2)
  94. {
  95. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  96. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
  97. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  98. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  99. GPIO_Init(GPIOB, &GPIO_InitStructure);
  100. GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  101. }
  102. }
  103. rt_uint32_t air32_spi_clock_get(SPI_TypeDef *spix)
  104. {
  105. RCC_ClocksTypeDef RCC_Clocks;
  106. RCC_GetClocksFreq(&RCC_Clocks);
  107. if (spix == SPI1)
  108. {
  109. return RCC_Clocks.PCLK2_Frequency;
  110. }
  111. if (spix == SPI2)
  112. {
  113. return RCC_Clocks.PCLK1_Frequency;
  114. }
  115. return RCC_Clocks.PCLK2_Frequency;
  116. }
  117. #endif
  118. #ifdef BSP_USING_TIM
  119. void air32_tim_clock_init(TIM_TypeDef *timx)
  120. {
  121. if (timx == TIM1)
  122. {
  123. RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  124. }
  125. if (timx == TIM2)
  126. {
  127. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  128. }
  129. if (timx == TIM3)
  130. {
  131. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  132. }
  133. if (timx == TIM4)
  134. {
  135. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  136. }
  137. }
  138. rt_uint32_t air32_tim_clock_get(TIM_TypeDef *timx)
  139. {
  140. RCC_ClocksTypeDef RCC_Clocks;
  141. RCC_GetClocksFreq(&RCC_Clocks);
  142. /*tim1~4 all in HCLK*/
  143. return RCC_Clocks.HCLK_Frequency;
  144. }
  145. struct rt_hwtimer_info hwtimer_info1 =
  146. {
  147. .maxfreq = 1000000,
  148. .minfreq = 2000,
  149. .maxcnt = 0xFFFF,
  150. .cntmode = HWTIMER_CNTMODE_UP,
  151. };
  152. struct rt_hwtimer_info hwtimer_info2 =
  153. {
  154. .maxfreq = 1000000,
  155. .minfreq = 2000,
  156. .maxcnt = 0xFFFF,
  157. .cntmode = HWTIMER_CNTMODE_UP,
  158. };
  159. struct rt_hwtimer_info hwtimer_info3 =
  160. {
  161. .maxfreq = 1000000,
  162. .minfreq = 2000,
  163. .maxcnt = 0xFFFF,
  164. .cntmode = HWTIMER_CNTMODE_UP,
  165. };
  166. struct rt_hwtimer_info hwtimer_info4 =
  167. {
  168. .maxfreq = 1000000,
  169. .minfreq = 2000,
  170. .maxcnt = 0xFFFF,
  171. .cntmode = HWTIMER_CNTMODE_UP,
  172. };
  173. struct rt_hwtimer_info *air32_hwtimer_info_config_get(TIM_TypeDef *timx)
  174. {
  175. struct rt_hwtimer_info *info = RT_NULL;
  176. if (timx == TIM1)
  177. {
  178. info = &hwtimer_info1;
  179. }
  180. else if (timx == TIM2)
  181. {
  182. info = &hwtimer_info2;
  183. }
  184. else if (timx == TIM3)
  185. {
  186. info = &hwtimer_info3;
  187. }
  188. else if (timx == TIM4)
  189. {
  190. info = &hwtimer_info4;
  191. }
  192. return info;
  193. }
  194. #endif
  195. #ifdef BSP_USING_PWM
  196. void air32_pwm_io_init(TIM_TypeDef *timx, rt_uint8_t channel)
  197. {
  198. GPIO_InitTypeDef GPIO_InitStructure;
  199. if (timx == TIM1)
  200. {
  201. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  202. if (channel == TIM_Channel_1)
  203. {
  204. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  205. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  206. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  207. GPIO_Init(GPIOA, &GPIO_InitStructure);
  208. }
  209. if (channel == TIM_Channel_2)
  210. {
  211. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  212. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  213. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  214. GPIO_Init(GPIOA, &GPIO_InitStructure);
  215. }
  216. if (channel == TIM_Channel_3)
  217. {
  218. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  219. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  220. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  221. GPIO_Init(GPIOA, &GPIO_InitStructure);
  222. }
  223. if (channel == TIM_Channel_4)
  224. {
  225. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  226. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  227. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  228. GPIO_Init(GPIOA, &GPIO_InitStructure);
  229. }
  230. }
  231. if (timx == TIM2)
  232. {
  233. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  234. if (channel == TIM_Channel_1)
  235. {
  236. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  237. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  238. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  239. GPIO_Init(GPIOA, &GPIO_InitStructure);
  240. }
  241. if (channel == TIM_Channel_2)
  242. {
  243. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  244. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  245. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  246. GPIO_Init(GPIOA, &GPIO_InitStructure);
  247. }
  248. if (channel == TIM_Channel_3)
  249. {
  250. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  251. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  252. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  253. GPIO_Init(GPIOA, &GPIO_InitStructure);
  254. }
  255. if (channel == TIM_Channel_4)
  256. {
  257. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  258. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  259. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  260. GPIO_Init(GPIOA, &GPIO_InitStructure);
  261. }
  262. }
  263. if (timx == TIM3)
  264. {
  265. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  266. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  267. if (channel == TIM_Channel_1)
  268. {
  269. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  270. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  271. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  272. GPIO_Init(GPIOA, &GPIO_InitStructure);
  273. }
  274. if (channel == TIM_Channel_2)
  275. {
  276. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  277. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  278. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  279. GPIO_Init(GPIOA, &GPIO_InitStructure);
  280. }
  281. if (channel == TIM_Channel_3)
  282. {
  283. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  284. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  285. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  286. GPIO_Init(GPIOB, &GPIO_InitStructure);
  287. }
  288. if (channel == TIM_Channel_4)
  289. {
  290. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  291. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  292. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  293. GPIO_Init(GPIOB, &GPIO_InitStructure);
  294. }
  295. }
  296. if (timx == TIM4)
  297. {
  298. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  299. if (channel == TIM_Channel_1)
  300. {
  301. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  302. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  303. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  304. GPIO_Init(GPIOB, &GPIO_InitStructure);
  305. }
  306. if (channel == TIM_Channel_2)
  307. {
  308. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
  309. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  310. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  311. GPIO_Init(GPIOB, &GPIO_InitStructure);
  312. }
  313. if (channel == TIM_Channel_3)
  314. {
  315. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
  316. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  317. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  318. GPIO_Init(GPIOB, &GPIO_InitStructure);
  319. }
  320. if (channel == TIM_Channel_4)
  321. {
  322. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  323. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  324. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  325. GPIO_Init(GPIOB, &GPIO_InitStructure);
  326. }
  327. }
  328. }
  329. #endif