peripheral_main.c 6.6 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. /* ¼Ç¼µ±Ç°µÄImage */
  21. unsigned char CurrImageFlag = 0xff;
  22. static bool iwdg_rst_stat = false;
  23. /*********************************************************************
  24. * GLOBAL TYPEDEFS
  25. */
  26. __attribute__((aligned(4))) uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
  27. // uint8_t MacAddr[6] = {0};
  28. /* Note: The operation of Flash after the program is upgraded must be performed first
  29. * without turning on any interruption to prevent operation interruption and failure
  30. */
  31. /*********************************************************************
  32. * @fn ReadImageFlag
  33. *
  34. * @brief Read the iMage logo of the current program.
  35. * If the DataFlash is empty, it will be Imagea by default.
  36. *
  37. * @return none
  38. */
  39. void ReadImageFlag(void)
  40. {
  41. OTADataFlashInfo_t p_image_flash;
  42. FLASH_read(OTA_DATAFLASH_ADD, (uint8_t *)&p_image_flash, 4);
  43. CurrImageFlag = p_image_flash.ImageFlag;
  44. /* The program is executed for the first time, or it has not been updated,
  45. * and the DataFLASH is erased after being updated in the future
  46. */
  47. if((p_image_flash.flag[0] != IMAGE_FLAG_1)
  48. || (p_image_flash.flag[1] != IMAGE_FLAG_2)
  49. || (p_image_flash.flag[2] != IMAGE_FLAG_3))
  50. {
  51. CurrImageFlag = IMAGE_A_FLAG;
  52. }
  53. PRINT("Image Flag %02x\n", CurrImageFlag);
  54. // if(CurrImageFlag == IMAGE_A_FLAG)
  55. // {
  56. // PRINT("jump App \n");
  57. // Delay_Ms(5);
  58. // app_start();
  59. // }
  60. }
  61. /*********************************************************************
  62. * @fn Main_Circulation
  63. *
  64. * @brief Main loop
  65. *
  66. * @return none
  67. */
  68. __attribute__((section(".highcode")))
  69. __attribute__((noinline))
  70. void Main_Circulation(void)
  71. {
  72. while(1)
  73. {
  74. TMOS_SystemProcess();
  75. }
  76. }
  77. static void User_GPIO_Init(void)
  78. {
  79. GPIO_InitTypeDef GPIO_InitStructure={0};
  80. KEY_GPIO_RCC_ENABLE;
  81. GPIO_InitStructure.GPIO_Pin = ELECT_01_OPEN_PIN;
  82. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  83. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  84. GPIO_Init(ELECT_01_OPEN_GPIO, &GPIO_InitStructure);
  85. ELECT_01_OPEN_OFF;
  86. GPIO_InitStructure.GPIO_Pin = ELECT_01_CLOSE_PIN;
  87. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  88. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  89. GPIO_Init(ELECT_01_CLOSE_GPIO, &GPIO_InitStructure);
  90. ELECT_01_CLOSE_OFF;
  91. GPIO_InitStructure.GPIO_Pin = ELECT_02_OPEN_PIN;
  92. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  93. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  94. GPIO_Init(ELECT_02_OPEN_GPIO, &GPIO_InitStructure);
  95. ELECT_02_OPEN_OFF;
  96. GPIO_InitStructure.GPIO_Pin = ELECT_02_CLOSE_PIN;
  97. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  98. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  99. GPIO_Init(ELECT_02_CLOSE_GPIO, &GPIO_InitStructure);
  100. ELECT_02_CLOSE_OFF;
  101. GPIO_InitStructure.GPIO_Pin = ELECT_03_OPEN_PIN;
  102. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  103. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  104. GPIO_Init(ELECT_03_OPEN_GPIO, &GPIO_InitStructure);
  105. ELECT_03_OPEN_OFF;
  106. GPIO_InitStructure.GPIO_Pin = ELECT_03_CLOSE_PIN;
  107. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  108. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  109. GPIO_Init(ELECT_03_CLOSE_GPIO, &GPIO_InitStructure);
  110. ELECT_03_CLOSE_OFF;
  111. GPIO_InitStructure.GPIO_Pin = ELECT_04_OPEN_PIN;
  112. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  113. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  114. GPIO_Init(ELECT_04_OPEN_GPIO, &GPIO_InitStructure);
  115. ELECT_04_OPEN_OFF;
  116. GPIO_InitStructure.GPIO_Pin = ELECT_04_CLOSE_PIN;
  117. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  118. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  119. GPIO_Init(ELECT_03_CLOSE_GPIO, &GPIO_InitStructure);
  120. ELECT_04_CLOSE_OFF;
  121. GPIO_InitStructure.GPIO_Pin = ELECT_05_OPEN_PIN;
  122. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  123. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  124. GPIO_Init(ELECT_05_OPEN_GPIO, &GPIO_InitStructure);
  125. ELECT_05_OPEN_OFF;
  126. GPIO_InitStructure.GPIO_Pin = ELECT_03_CLOSE_PIN;
  127. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  128. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  129. GPIO_Init(ELECT_03_CLOSE_GPIO, &GPIO_InitStructure);
  130. ELECT_05_CLOSE_OFF;
  131. }
  132. /*********************************************************************
  133. * @fn main
  134. *
  135. * @brief Main function
  136. *
  137. * @return none
  138. */
  139. int main(void)
  140. {
  141. SystemCoreClockUpdate();
  142. Delay_Init();
  143. #ifdef DEBUG
  144. USART_Printf_Init(115200);
  145. #endif
  146. PRINT("SystemClk:%d\r\n",SystemCoreClock);
  147. PRINT( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
  148. PRINT("%s\n", VER_LIB);
  149. ReadImageFlag();
  150. if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET)
  151. {
  152. PRINT("iwdg reset\n");
  153. }
  154. // else if(RCC_GetFlagStatus(RCC_FLAG_SFTRST) == SET)
  155. // {
  156. // // Soft reset does not jump app app
  157. // PRINT("Soft reset does not jump app \n");
  158. // }
  159. else
  160. {
  161. if(CurrImageFlag == IMAGE_A_FLAG)
  162. {
  163. PRINT("jump User App \n");
  164. Delay_Ms(5);
  165. app_start();
  166. }
  167. else if(CurrImageFlag == IMAGE_OTA_FLAG)
  168. {
  169. PRINT("jump ota \n");
  170. SwitchImageFlag(IMAGE_A_FLAG);
  171. }
  172. }
  173. PRINT("ENTER OTA!\n");
  174. User_GPIO_Init();
  175. WCHBLE_Init();
  176. HAL_Init();
  177. GAPRole_PeripheralInit();
  178. Peripheral_Init();
  179. led_task_init();
  180. led_task_start();
  181. vUser_led_set_view_stat(LED_VIEW_ENTER_OTA);
  182. PRINT("start task\n");
  183. Main_Circulation();
  184. }
  185. /******************************** endfile @ main ******************************/