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 就是调用各种硬件的初始化

注意:下面我们是以一个任务来开始的,设置自己需要的~~~不需要的不需要加载进去
就类似我们之前的初始化

  1. void BSP_Init(void)
  2. {
  3. /* System Clocks Configuration */
  4. RCC_Configuration(); //RCC
  5. GPIO_Configuration(); //GPIO
  6. /* NVIC configuration */
  7. NVIC_Configuration(); //NVIC
  8. USART1_InitConfig(57600); //USart
  9. }

3.RCC_Configuration

RCC_Configuration 事实上就是时钟的设置
注意在移植的时候,对BSP进行裁剪

  1. void RCC_Configuration(void)
  2. {ErrorStatus HSEStartUpStatus;
  3. /* RCC system reset(for debug purpose) */
  4. RCC_DeInit();
  5. /* Enable HSE */
  6. RCC_HSEConfig(RCC_HSE_ON);
  7. /* Wait till HSE is ready */
  8. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  9. if(HSEStartUpStatus == SUCCESS)
  10. {
  11. /* Enable Prefetch Buffer */
  12. FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  13. /* Flash 2 wait state */
  14. FLASH_SetLatency(FLASH_Latency_2);
  15. /* HCLK = SYSCLK */
  16. RCC_HCLKConfig(RCC_SYSCLK_Div1);
  17. /* PCLK2 = HCLK */
  18. RCC_PCLK2Config(RCC_HCLK_Div1);
  19. /* PCLK1 = HCLK/2 */
  20. RCC_PCLK1Config(RCC_HCLK_Div2);
  21. /* PLLCLK = 8MHz * 9 = 72 MHz */
  22. RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  23. /* Enable PLL */
  24. RCC_PLLCmd(ENABLE);
  25. /* Wait till PLL is ready */
  26. while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  27. {
  28. }
  29. /* Select PLL as system clock source */
  30. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  31. /* Wait till PLL is used as system clock source */
  32. while(RCC_GetSYSCLKSource() != 0x08)
  33. {
  34. }

4. GPIO_Configuration

对系统的CPIO进行初始化
在动手编程前,就应该规划好硬件的分配,都要提前设置好,进行初始化

  1. void GPIO_Configuration(void)
  2. {
  3. GPIO_InitTypeDef GPIO_InitStructure;
  4. //UnableJTAG();
  5. //GPIOC输出// PC13=LED
  6. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
  7. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  9. GPIO_Init(GPIOC, &GPIO_InitStructure);
  10. //usart_init----------------------------------------------------
  11. /* Configure USART1 Rx (PA.10) as input floating */
  12. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  13. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  14. GPIO_Init(GPIOA, &GPIO_InitStructure);
  15. /* Configure USART1 Tx (PA.09) as alternate function push-pull */
  16. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  17. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  18. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  19. GPIO_Init(GPIOA, &GPIO_InitStructure);
  20. }

5.NVIC_Configuration

  1. void NVIC_Configuration(void)
  2. {
  3. // NVIC_InitTypeDef NVIC_InitStructure;
  4. //#ifdef VECT_TAB_RAM
  5. #if defined (VECT_TAB_RAM)
  6. /* Set the Vector Table base location at 0x20000000 */
  7. NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  8. #elif defined(VECT_TAB_FLASH_IAP)
  9. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x2000);
  10. #else /* VECT_TAB_FLASH */
  11. /* Set the Vector Table base location at 0x08000000 */ 设置向量表
  12. NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  13. #endif
  14. /* Configure the NVIC Preemption Priority Bits */
  15. //异常向量表组 抢占优先级和次优先级
  16. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  17. }

6.USART1_InitConfig

串口初始化

  1. void USART1_InitConfig(uint32 BaudRate)
  2. {USART_InitTypeDef USART_InitStructure;
  3. //USART1->SR &= ~USART_FLAG_TXE; // clear interrupt
  4. //USART1->SR &= ~USART_FLAG_TC; // clear interrupt
  5. USART_InitStructure.USART_BaudRate = BaudRate;
  6. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  7. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  8. USART_InitStructure.USART_Parity = USART_Parity_No;
  9. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  10. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  11. /* Configure USART1 */
  12. USART_Init(USART1, &USART_InitStructure);
  13. /* Enable USART1 Receive and Transmit interrupts */
  14. //USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  15. //USART_ITConfig(USART1, USART_IT_TXE, ENABLE);//发送时才打开
  16. /* Enable the USART1 */
  17. USART_Cmd(USART1, ENABLE);//仿真看到执行这里,TC标志居然被设置为1了,不知道实际在flash中运行是否是这样
  18. // USART1->SR &= ~USART_FLAG_TXE; // clear interrupt
  19. // USART1->SR &= ~USART_FLAG_TC; // clear interrupt
  20. }