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