peripheral_main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. static void User_GPIO_Init(void)
  80. {
  81. GPIO_InitTypeDef GPIO_InitStructure={0};
  82. KEY_GPIO_RCC_ENABLE;
  83. GPIO_InitStructure.GPIO_Pin = ELECT_01_OPEN_PIN;
  84. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  85. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  86. GPIO_Init(ELECT_01_OPEN_GPIO, &GPIO_InitStructure);
  87. ELECT_01_OPEN_OFF;
  88. GPIO_InitStructure.GPIO_Pin = ELECT_01_CLOSE_PIN;
  89. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  90. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  91. GPIO_Init(ELECT_01_CLOSE_GPIO, &GPIO_InitStructure);
  92. ELECT_01_CLOSE_OFF;
  93. GPIO_InitStructure.GPIO_Pin = ELECT_02_OPEN_PIN;
  94. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  95. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  96. GPIO_Init(ELECT_02_OPEN_GPIO, &GPIO_InitStructure);
  97. ELECT_02_OPEN_OFF;
  98. GPIO_InitStructure.GPIO_Pin = ELECT_02_CLOSE_PIN;
  99. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  100. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  101. GPIO_Init(ELECT_02_CLOSE_GPIO, &GPIO_InitStructure);
  102. ELECT_02_CLOSE_OFF;
  103. GPIO_InitStructure.GPIO_Pin = ELECT_03_OPEN_PIN;
  104. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  105. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  106. GPIO_Init(ELECT_03_OPEN_GPIO, &GPIO_InitStructure);
  107. ELECT_03_OPEN_OFF;
  108. GPIO_InitStructure.GPIO_Pin = ELECT_03_CLOSE_PIN;
  109. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  110. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  111. GPIO_Init(ELECT_03_CLOSE_GPIO, &GPIO_InitStructure);
  112. ELECT_03_CLOSE_OFF;
  113. GPIO_InitStructure.GPIO_Pin = ELECT_04_OPEN_PIN;
  114. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  115. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  116. GPIO_Init(ELECT_04_OPEN_GPIO, &GPIO_InitStructure);
  117. ELECT_04_OPEN_OFF;
  118. GPIO_InitStructure.GPIO_Pin = ELECT_04_CLOSE_PIN;
  119. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  120. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  121. GPIO_Init(ELECT_03_CLOSE_GPIO, &GPIO_InitStructure);
  122. ELECT_04_CLOSE_OFF;
  123. GPIO_InitStructure.GPIO_Pin = ELECT_05_OPEN_PIN;
  124. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  125. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  126. GPIO_Init(ELECT_05_OPEN_GPIO, &GPIO_InitStructure);
  127. ELECT_05_OPEN_OFF;
  128. GPIO_InitStructure.GPIO_Pin = ELECT_03_CLOSE_PIN;
  129. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  130. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  131. GPIO_Init(ELECT_03_CLOSE_GPIO, &GPIO_InitStructure);
  132. ELECT_05_CLOSE_OFF;
  133. }
  134. /*********************************************************************
  135. * @fn main
  136. *
  137. * @brief Main function
  138. *
  139. * @return none
  140. */
  141. int main(void)
  142. {
  143. SystemCoreClockUpdate();
  144. Delay_Init();
  145. #ifdef DEBUG
  146. USART_Printf_Init(115200);
  147. #endif
  148. PRINT("SystemClk:%d\r\n",SystemCoreClock);
  149. PRINT( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
  150. PRINT("%s\n", VER_LIB);
  151. ReadImageFlag();
  152. if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET)
  153. {
  154. PRINT("iwdg reset\n");
  155. }
  156. // else if(RCC_GetFlagStatus(RCC_FLAG_SFTRST) == SET)
  157. // {
  158. // // Soft reset does not jump app app
  159. // PRINT("Soft reset does not jump app \n");
  160. // }
  161. else
  162. {
  163. if(CurrImageFlag == IMAGE_A_FLAG)
  164. {
  165. PRINT("jump User App \n");
  166. Delay_Ms(5);
  167. app_start();
  168. }
  169. else if(CurrImageFlag == IMAGE_OTA_FLAG)
  170. {
  171. PRINT("jump ota \n");
  172. SwitchImageFlag(IMAGE_A_FLAG);
  173. }
  174. }
  175. PRINT("ENTER OTA!\n");
  176. User_GPIO_Init();
  177. WCHBLE_Init();
  178. HAL_Init();
  179. GAPRole_PeripheralInit();
  180. Peripheral_Init();
  181. vUser_ws2812b_init(USER_WS2812B_COUNT_DEFAULT);
  182. led_task_init();
  183. user_upgrade_start();
  184. vUser_ws2812b_task_start();
  185. led_task_start();
  186. vUser_led_set_view_stat(LED_VIEW_ENTER_OTA);
  187. PRINT("start task\n");
  188. ota_timeout_task_start(true);
  189. Main_Circulation();
  190. }
  191. /******************************** endfile @ main ******************************/