peripheral_main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : main.c
  3. * Author : WCH
  4. * Version : V1.1
  5. * Date : 2019/11/05
  6. * Description : Upgrade slave application main function and task system initialization
  7. *********************************************************************************
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. /******************************************************************************/
  13. /* Header file contains */
  14. #include "define.h"
  15. #include "HAL.h"
  16. #include "Peripheral.h"
  17. #include "OTA.h"
  18. #include "OTAprofile.h"
  19. #include "led.h"
  20. #include "user_ws2812b.h"
  21. #include "user_upgrade.h"
  22. /* 记录当前的Image */
  23. unsigned char CurrImageFlag = 0xff;
  24. static bool iwdg_rst_stat = false;
  25. /*********************************************************************
  26. * GLOBAL TYPEDEFS
  27. */
  28. __attribute__((aligned(4))) uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
  29. // uint8_t MacAddr[6] = {0};
  30. /* Note: The operation of Flash after the program is upgraded must be performed first
  31. * without turning on any interruption to prevent operation interruption and failure
  32. */
  33. /*********************************************************************
  34. * @fn ReadImageFlag
  35. *
  36. * @brief Read the iMage logo of the current program.
  37. * If the DataFlash is empty, it will be Imagea by default.
  38. *
  39. * @return none
  40. */
  41. void ReadImageFlag(void)
  42. {
  43. OTADataFlashInfo_t p_image_flash;
  44. FLASH_read(OTA_DATAFLASH_ADD, (uint8_t *)&p_image_flash, 4);
  45. CurrImageFlag = p_image_flash.ImageFlag;
  46. /* The program is executed for the first time, or it has not been updated,
  47. * and the DataFLASH is erased after being updated in the future
  48. */
  49. if((p_image_flash.flag[0] != IMAGE_FLAG_1)
  50. || (p_image_flash.flag[1] != IMAGE_FLAG_2)
  51. || (p_image_flash.flag[2] != IMAGE_FLAG_3))
  52. {
  53. CurrImageFlag = IMAGE_A_FLAG;
  54. }
  55. PRINT("Image Flag %02x\n", CurrImageFlag);
  56. // if(CurrImageFlag == IMAGE_A_FLAG)
  57. // {
  58. // PRINT("jump App \n");
  59. // Delay_Ms(5);
  60. // app_start();
  61. // }
  62. }
  63. /*********************************************************************
  64. * @fn Main_Circulation
  65. *
  66. * @brief Main loop
  67. *
  68. * @return none
  69. */
  70. __attribute__((section(".highcode")))
  71. __attribute__((noinline))
  72. void Main_Circulation(void)
  73. {
  74. while(1)
  75. {
  76. TMOS_SystemProcess();
  77. }
  78. }
  79. /*******************************************************************************
  80. * @函数名称 User_GPIO_Init
  81. * @函数说明 GPIO初始化
  82. * @输入参数 无
  83. * @输出参数 无
  84. * @返回参数 无
  85. *******************************************************************************/
  86. static void User_GPIO_Init(void)
  87. {
  88. GPIO_InitTypeDef GPIO_InitStructure={0};
  89. KEY_GPIO_RCC_ENABLE;
  90. GPIO_InitStructure.GPIO_Pin = CHARGE_LEFT_PIN;
  91. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  92. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  93. GPIO_Init(CHARGE_LEFT_GPIO, &GPIO_InitStructure);
  94. CHARGE_LEFT_OFF;
  95. GPIO_InitStructure.GPIO_Pin = CHARGE_RIGHT_PIN;
  96. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  97. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  98. GPIO_Init(CHARGE_RIGHT_GPIO, &GPIO_InitStructure);
  99. CHARGE_RIGHT_OFF;
  100. GPIO_InitStructure.GPIO_Pin = LEFT_FAN_PIN;
  101. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  102. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  103. GPIO_Init(LEFT_FAN_GPIO, &GPIO_InitStructure);
  104. LEFT_FAN_OFF;
  105. GPIO_InitStructure.GPIO_Pin = RIGHT_FAN_PIN;
  106. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  107. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  108. GPIO_Init(RIGHT_FAN_GPIO, &GPIO_InitStructure);
  109. RIGHT_FAN_OFF;
  110. GPIO_InitStructure.GPIO_Pin = ELECT_LEFT_OPEN_PIN;
  111. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  112. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  113. GPIO_Init(ELECT_LEFT_OPEN_GPIO, &GPIO_InitStructure);
  114. ELECT_LEFT_OPEN_OFF;
  115. GPIO_InitStructure.GPIO_Pin = ELECT_LEFT_CLOSE_PIN;
  116. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  117. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  118. GPIO_Init(ELECT_LEFT_CLOSE_GPIO, &GPIO_InitStructure);
  119. ELECT_LEFT_CLOSE_OFF;
  120. GPIO_InitStructure.GPIO_Pin = ELECT_RIGHT_OPEN_PIN;
  121. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  122. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  123. GPIO_Init(ELECT_RIGHT_OPEN_GPIO, &GPIO_InitStructure);
  124. ELECT_RIGHT_OPEN_OFF;
  125. GPIO_InitStructure.GPIO_Pin = ELECT_RIGHT_CLOSE_PIN;
  126. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  127. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  128. GPIO_Init(ELECT_RIGHT_CLOSE_GPIO, &GPIO_InitStructure);
  129. ELECT_RIGHT_CLOSE_OFF;
  130. }
  131. /*********************************************************************
  132. * @fn main
  133. *
  134. * @brief Main function
  135. *
  136. * @return none
  137. */
  138. int main(void)
  139. {
  140. SystemCoreClockUpdate();
  141. Delay_Init();
  142. #ifdef DEBUG
  143. USART_Printf_Init(115200);
  144. #endif
  145. PRINT("SystemClk:%d\r\n",SystemCoreClock);
  146. PRINT( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
  147. PRINT("%s\n", VER_LIB);
  148. ReadImageFlag();
  149. if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET)
  150. {
  151. PRINT("iwdg reset\n");
  152. }
  153. // else if(RCC_GetFlagStatus(RCC_FLAG_SFTRST) == SET)
  154. // {
  155. // // Soft reset does not jump app app
  156. // PRINT("Soft reset does not jump app \n");
  157. // }
  158. else
  159. {
  160. if(CurrImageFlag == IMAGE_A_FLAG)
  161. {
  162. PRINT("jump User App \n");
  163. Delay_Ms(5);
  164. app_start();
  165. }
  166. else if(CurrImageFlag == IMAGE_OTA_FLAG)
  167. {
  168. PRINT("jump ota \n");
  169. SwitchImageFlag(IMAGE_A_FLAG);
  170. }
  171. }
  172. PRINT("ENTER OTA!\n");
  173. User_GPIO_Init();
  174. WCHBLE_Init();
  175. HAL_Init();
  176. GAPRole_PeripheralInit();
  177. Peripheral_Init();
  178. vUser_ws2812b_init(USER_WS2812B_COUNT_DEFAULT);
  179. led_task_init();
  180. user_upgrade_start();
  181. vUser_ws2812b_task_start();
  182. led_task_start();
  183. vUser_led_set_view_stat(LED_VIEW_ENTER_OTA);
  184. PRINT("start task\n");
  185. ota_timeout_task_start(true);
  186. Main_Circulation();
  187. }
  188. /******************************** endfile @ main ******************************/