bsp.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. ******************************** STM32F10x *********************************
  3. * @文件名 : bsp.c
  4. * @作者 : sun
  5. * @库版本 : V3.5.0
  6. * @文件版本 : V1.0.0
  7. * @日期 : 2016年05月09日
  8. * @摘要 : BSP板级支持包源文件
  9. ******************************************************************************/
  10. /*----------------------------------------------------------------------------
  11. 更新日志:
  12. 2016-05-09 V1.0.0:初始版本
  13. ----------------------------------------------------------------------------*/
  14. /* 包含的头文件 --------------------------------------------------------------*/
  15. #include "bsp.h"
  16. #include "systick.h"
  17. #include "usart.h"
  18. #include "led.h"
  19. #include <string.h>
  20. #include "AT24C128.h"
  21. #include "bsp.h"
  22. Device_version_info_t device_version_info;
  23. eeprom_first_info_t eeprom_first_info;
  24. /************************************************
  25. 函数名称 : RCC_Configuration
  26. 功 能 : 时钟配置
  27. 参 数 : 无
  28. 返 回 值 : 无
  29. 作 者 : sun
  30. *************************************************/
  31. void RCC_Configuration(void)
  32. {
  33. /* 使能APB2时钟 */
  34. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
  35. RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD |
  36. RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF |
  37. RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1 |
  38. RCC_APB2Periph_SPI1, ENABLE);
  39. /* 使能APB1时钟 */
  40. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 | RCC_APB1Periph_UART4 |
  41. RCC_APB1Periph_TIM4,
  42. ENABLE);
  43. /* 使能APB时钟 */
  44. /* CRC时钟 */
  45. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);
  46. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟
  47. PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问
  48. }
  49. void GPIO_COMM_Init(u32 clk, GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIOMode_TypeDef mode, GPIOSpeed_TypeDef speed)
  50. {
  51. GPIO_InitTypeDef GPIO_InitStructure;
  52. RCC_APB2PeriphClockCmd(clk, ENABLE);
  53. GPIO_InitStructure.GPIO_Pin = GPIO_Pin;
  54. GPIO_InitStructure.GPIO_Mode = mode;
  55. GPIO_InitStructure.GPIO_Speed = speed;
  56. GPIO_Init(GPIOx, &GPIO_InitStructure);
  57. GPIO_SetBits(GPIOx, GPIO_Pin);
  58. }
  59. /************************************************
  60. 函数名称 : GPIO_Basic_Configuration
  61. 功 能 : 基本输入输出引脚配置
  62. 参 数 : 无
  63. 返 回 值 : 无
  64. 作 者 : sun
  65. *************************************************/
  66. void GPIO_Basic_Configuration(void)
  67. {
  68. GPIO_InitTypeDef GPIO_InitStructure;
  69. GPIO_InitStructure.GPIO_Pin = PIN_LED; //引脚
  70. //频率(50M)
  71. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  72. //输出类型(推挽式输出)
  73. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  74. GPIO_Init(PORT_LED, &GPIO_InitStructure);
  75. }
  76. /************************************************
  77. 函数名称 : NVIC_Configuration
  78. 功 能 : NVIC配置
  79. 参 数 : 无
  80. 返 回 值 : 无
  81. 作 者 : sun
  82. *************************************************/
  83. void NVIC_Configuration(void)
  84. {
  85. NVIC_InitTypeDef NVIC_InitStructure;
  86. /* 优先级分组 */
  87. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  88. /* 外设中断 */
  89. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  90. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  91. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  92. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  93. NVIC_Init(&NVIC_InitStructure);
  94. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  95. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  96. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  97. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  98. NVIC_Init(&NVIC_InitStructure);
  99. /**/
  100. NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;
  101. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
  102. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  103. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  104. NVIC_Init(&NVIC_InitStructure);
  105. NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
  106. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  107. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  108. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  109. NVIC_Init(&NVIC_InitStructure);
  110. }
  111. /************************************************
  112. 函数名称 : System_Initializes
  113. 功 能 : 系统初始化
  114. 参 数 : 无
  115. 返 回 值 : 无
  116. 作 者 : sun
  117. *************************************************/
  118. void System_Initializes(void)
  119. {
  120. LED_Init(); //初始化与LED连接的硬件接口
  121. RCC_Configuration();
  122. USART_Initializes();
  123. I2C_init();
  124. }
  125. /**
  126. *@brief STM32系统软复位函数
  127. *@param 无
  128. *@return 无
  129. */
  130. void SysReboot(void)
  131. {
  132. NVIC_SystemReset();// 复位
  133. }
  134. ///////////////////////////////////////////////////////////////////////////////
  135. ///////////////////////////////////////////////////////////////////////////////
  136. extern uint8_t updata_flag;
  137. uint8_t eeprom_buf[EECFG_NUM]={0};
  138. uint8_t eeprom_erase_buf[64] = {0};
  139. uint32_t eeprom_addr=0;
  140. #define EEPROM_PAGE_NUM 256
  141. #define EEPROM_BUF_LEN 64
  142. //eeprom上电初始化处理
  143. void eeprom_init_handle(void)
  144. {
  145. // int i;
  146. //将eeprom存储的共有数据全部读出
  147. AT24CXX_Read(EECFG_FIRST_INIT0,eeprom_buf,EECFG_NUM);
  148. //
  149. memcpy(&eeprom_first_info,eeprom_buf,sizeof(eeprom_first_info_t));
  150. //读设备版本信息
  151. memcpy(&device_version_info,&eeprom_buf[EECFG_BT_VER0],sizeof(Device_version_info_t));
  152. // //擦除eeprom
  153. // if(eeprom_first_info.eeprom_first_init_flag != EEPROM_FIRST_INIT_FLAG)
  154. // {
  155. // memset(eeprom_erase_buf,0,sizeof(eeprom_erase_buf));
  156. // for(i = 0; i < EEPROM_PAGE_NUM; i++)
  157. // {
  158. // AT24CXX_Write(eeprom_addr, eeprom_erase_buf, EEPROM_BUF_LEN);
  159. // eeprom_addr += EEPROM_BUF_LEN;
  160. // IWDG_ReloadCounter();
  161. // }
  162. // }
  163. }
  164. //更新eeprom信息
  165. void eeprom_info_updata(void)
  166. {
  167. // if(eeprom_first_info.eeprom_first_init_flag != EEPROM_FIRST_INIT_FLAG)//初次上电
  168. // {
  169. // //写0x55AA,eeprom版本,复位次数
  170. // eeprom_first_info.eeprom_first_init_flag = EEPROM_FIRST_INIT_FLAG;
  171. // eeprom_first_info.eeprom_ver = EEPROM_VER;
  172. // eeprom_first_info.reset_total_num = 1;
  173. // AT24CXX_Write(EECFG_FIRST_INIT0, (uint8_t *)&eeprom_first_info, 7);
  174. //
  175. // //写bt版本
  176. // device_version_info.boot_ver = SOFTWARE_VERSION_BOOT;
  177. // AT24CXX_Write(EECFG_BT_VER0, (uint8_t *)&device_version_info.boot_ver, 4);
  178. // }
  179. // else//不是初次上电
  180. {
  181. //写复位次数
  182. eeprom_first_info.reset_total_num++;
  183. AT24CXX_Write(EECFG_RST_TIME0, (uint8_t *)&eeprom_first_info.reset_total_num, 4);
  184. //如果bt版本不对,写bt版本
  185. if(device_version_info.boot_ver != SOFTWARE_VERSION_BOOT)
  186. {
  187. device_version_info.boot_ver = SOFTWARE_VERSION_BOOT;
  188. AT24CXX_Write(EECFG_BT_VER0, (uint8_t *)&device_version_info.boot_ver, 4);
  189. }
  190. //如果执行过升级或回退,写app1、2、3版本
  191. if(updata_flag)
  192. {
  193. updata_flag = 0;
  194. AT24CXX_Write(EECFG_APP1_VER0, (uint8_t *)&device_version_info.APP1_ver, 12);
  195. }
  196. }
  197. }
  198. /**** Copyright (C)2016 sun. All Rights Reserved **** END OF FILE ****/