1.介绍
(1)bsp是board support packet 板级支持包 很多源码 支持开发板开发,别人封装操作硬件的代码,核心是驱动
(2)bsp其实就是对硬件操作的封装(底层驱动或中间驱动层封装)时序:底层 中间层:寄存器
(3)完全移植的工作量主要就在bsp这一块 BSP工程师
2.BSP_init
BSP_init,定义在bsp/bsp.c中,在APP/app.c中的main函数被调用
BSP_init作用:进行BSP初始化(板载所有外设初始化)
这里只是对部分进行了初始化,可扩展
这里只对基本的模块进行了初始化
一个硬件一个.c 如com.c/com.h key.c/key.h
BSP.c BSP_Init 就是调用各种硬件的初始化
注意:下面我们是以一个任务来开始的,设置自己需要的~~~不需要的不需要加载进去
就类似我们之前的初始化
void BSP_Init(void)
{
/* System Clocks Configuration */
RCC_Configuration(); //RCC
GPIO_Configuration(); //GPIO
/* NVIC configuration */
NVIC_Configuration(); //NVIC
USART1_InitConfig(57600); //USart
}
3.RCC_Configuration
RCC_Configuration 事实上就是时钟的设置
注意在移植的时候,对BSP进行裁剪
void RCC_Configuration(void)
{ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
4. GPIO_Configuration
对系统的CPIO进行初始化
在动手编程前,就应该规划好硬件的分配,都要提前设置好,进行初始化
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//UnableJTAG();
//GPIOC输出// PC13=LED
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
//usart_init----------------------------------------------------
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
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);
}
5.NVIC_Configuration
void NVIC_Configuration(void)
{
// NVIC_InitTypeDef NVIC_InitStructure;
//#ifdef VECT_TAB_RAM
#if defined (VECT_TAB_RAM)
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#elif defined(VECT_TAB_FLASH_IAP)
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */ 设置向量表
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Configure the NVIC Preemption Priority Bits */
//异常向量表组 抢占优先级和次优先级
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
}
6.USART1_InitConfig
串口初始化
void USART1_InitConfig(uint32 BaudRate)
{USART_InitTypeDef USART_InitStructure;
//USART1->SR &= ~USART_FLAG_TXE; // clear interrupt
//USART1->SR &= ~USART_FLAG_TC; // clear interrupt
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 Receive and Transmit interrupts */
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);//发送时才打开
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);//仿真看到执行这里,TC标志居然被设置为1了,不知道实际在flash中运行是否是这样
// USART1->SR &= ~USART_FLAG_TXE; // clear interrupt
// USART1->SR &= ~USART_FLAG_TC; // clear interrupt
}