382 lines
11 KiB
C
382 lines
11 KiB
C
/*
|
|
* Copyright (c) 2006-2022, RT-Thread Development Team
|
|
*
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*
|
|
* Change Logs:
|
|
* Date Author Notes
|
|
* 2022-02-22 airm2m first version
|
|
*/
|
|
|
|
#include "board.h"
|
|
|
|
void SystemClock_Config(void)
|
|
{
|
|
RCC_DeInit(); //复位RCC寄存器
|
|
|
|
RCC_HSEConfig(RCC_HSE_ON); //使能HSE
|
|
while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET)
|
|
; //等待HSE就绪
|
|
|
|
RCC_PLLCmd(DISABLE); //关闭PLL
|
|
AIR_RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_27, FLASH_Div_2); //配置PLL,8*27=216MHz
|
|
|
|
RCC_PLLCmd(ENABLE); //使能PLL
|
|
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
|
|
; //等待PLL就绪
|
|
|
|
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //选择PLL作为系统时钟
|
|
|
|
RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB时钟
|
|
RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1时钟
|
|
RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2时钟
|
|
|
|
RCC_LSICmd(ENABLE); //使能内部低速时钟
|
|
while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
|
|
; //等待LSI就绪
|
|
RCC_HSICmd(ENABLE); //使能内部高速时钟
|
|
while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET)
|
|
; //等待HSI就绪
|
|
}
|
|
|
|
#ifdef BSP_USING_UART
|
|
void air32_usart_clock_and_io_init(USART_TypeDef *usartx)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
/* USART1 TX-->PA9 RX-->PA10 */
|
|
if (usartx == USART1)
|
|
{
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
/* USART2 TX-->PA2 RX-->PA3 */
|
|
if (usartx == USART2)
|
|
{
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
|
|
/* USART3 TX-->PC10 RX-->PC11 */
|
|
if (usartx == USART3)
|
|
{
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
GPIO_Init(GPIOC, &GPIO_InitStructure);
|
|
GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#ifdef BSP_USING_SPI
|
|
void air32_spi_clock_and_io_init(SPI_TypeDef *spix)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
if (spix == SPI1)
|
|
{
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
|
|
}
|
|
|
|
if (spix == SPI2)
|
|
{
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
|
|
}
|
|
}
|
|
|
|
rt_uint32_t air32_spi_clock_get(SPI_TypeDef *spix)
|
|
{
|
|
RCC_ClocksTypeDef RCC_Clocks;
|
|
|
|
RCC_GetClocksFreq(&RCC_Clocks);
|
|
|
|
if (spix == SPI1)
|
|
{
|
|
return RCC_Clocks.PCLK2_Frequency;
|
|
}
|
|
|
|
if (spix == SPI2)
|
|
{
|
|
return RCC_Clocks.PCLK1_Frequency;
|
|
}
|
|
|
|
return RCC_Clocks.PCLK2_Frequency;
|
|
}
|
|
#endif
|
|
|
|
#ifdef BSP_USING_TIM
|
|
void air32_tim_clock_init(TIM_TypeDef *timx)
|
|
{
|
|
if (timx == TIM1)
|
|
{
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
|
|
}
|
|
|
|
if (timx == TIM2)
|
|
{
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
|
}
|
|
|
|
if (timx == TIM3)
|
|
{
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
|
|
}
|
|
|
|
if (timx == TIM4)
|
|
{
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
|
|
}
|
|
}
|
|
|
|
rt_uint32_t air32_tim_clock_get(TIM_TypeDef *timx)
|
|
{
|
|
RCC_ClocksTypeDef RCC_Clocks;
|
|
|
|
RCC_GetClocksFreq(&RCC_Clocks);
|
|
|
|
/*tim1~4 all in HCLK*/
|
|
return RCC_Clocks.HCLK_Frequency;
|
|
}
|
|
|
|
struct rt_hwtimer_info hwtimer_info1 =
|
|
{
|
|
.maxfreq = 1000000,
|
|
.minfreq = 2000,
|
|
.maxcnt = 0xFFFF,
|
|
.cntmode = HWTIMER_CNTMODE_UP,
|
|
|
|
};
|
|
|
|
struct rt_hwtimer_info hwtimer_info2 =
|
|
{
|
|
.maxfreq = 1000000,
|
|
.minfreq = 2000,
|
|
.maxcnt = 0xFFFF,
|
|
.cntmode = HWTIMER_CNTMODE_UP,
|
|
|
|
};
|
|
|
|
struct rt_hwtimer_info hwtimer_info3 =
|
|
{
|
|
.maxfreq = 1000000,
|
|
.minfreq = 2000,
|
|
.maxcnt = 0xFFFF,
|
|
.cntmode = HWTIMER_CNTMODE_UP,
|
|
|
|
};
|
|
|
|
struct rt_hwtimer_info hwtimer_info4 =
|
|
{
|
|
.maxfreq = 1000000,
|
|
.minfreq = 2000,
|
|
.maxcnt = 0xFFFF,
|
|
.cntmode = HWTIMER_CNTMODE_UP,
|
|
|
|
};
|
|
|
|
struct rt_hwtimer_info *air32_hwtimer_info_config_get(TIM_TypeDef *timx)
|
|
{
|
|
struct rt_hwtimer_info *info = RT_NULL;
|
|
|
|
if (timx == TIM1)
|
|
{
|
|
info = &hwtimer_info1;
|
|
}
|
|
else if (timx == TIM2)
|
|
{
|
|
info = &hwtimer_info2;
|
|
}
|
|
else if (timx == TIM3)
|
|
{
|
|
info = &hwtimer_info3;
|
|
}
|
|
else if (timx == TIM4)
|
|
{
|
|
info = &hwtimer_info4;
|
|
}
|
|
|
|
return info;
|
|
}
|
|
#endif
|
|
|
|
#ifdef BSP_USING_PWM
|
|
void air32_pwm_io_init(TIM_TypeDef *timx, rt_uint8_t channel)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
if (timx == TIM1)
|
|
{
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
if (channel == TIM_Channel_1)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_2)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_3)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_4)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
}
|
|
|
|
if (timx == TIM2)
|
|
{
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
if (channel == TIM_Channel_1)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_2)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_3)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_4)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
}
|
|
|
|
if (timx == TIM3)
|
|
{
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|
|
|
if (channel == TIM_Channel_1)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_2)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_3)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_4)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
}
|
|
}
|
|
|
|
if (timx == TIM4)
|
|
{
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|
|
|
if (channel == TIM_Channel_1)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_2)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_3)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
}
|
|
if (channel == TIM_Channel_4)
|
|
{
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
}
|
|
}
|
|
}
|
|
#endif
|