peripheral_main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. }
  80. /*********************************************************************
  81. * @fn main
  82. *
  83. * @brief Main function
  84. *
  85. * @return none
  86. */
  87. int main(void)
  88. {
  89. SystemCoreClockUpdate();
  90. Delay_Init();
  91. #ifdef DEBUG
  92. USART_Printf_Init(115200);
  93. #endif
  94. PRINT("SystemClk:%d\r\n",SystemCoreClock);
  95. PRINT( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
  96. PRINT("%s\n", VER_LIB);
  97. ReadImageFlag();
  98. if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET)
  99. {
  100. PRINT("iwdg reset\n");
  101. }
  102. // else if(RCC_GetFlagStatus(RCC_FLAG_SFTRST) == SET)
  103. // {
  104. // // Soft reset does not jump app app
  105. // PRINT("Soft reset does not jump app \n");
  106. // }
  107. else
  108. {
  109. if(CurrImageFlag == IMAGE_A_FLAG)
  110. {
  111. PRINT("jump User App \n");
  112. Delay_Ms(5);
  113. app_start();
  114. }
  115. else if(CurrImageFlag == IMAGE_OTA_FLAG)
  116. {
  117. PRINT("ota jump App \n");
  118. Delay_Ms(5);
  119. app_start();
  120. }
  121. }
  122. PRINT("ENTER OTA!\n");
  123. User_GPIO_Init();
  124. WCHBLE_Init();
  125. HAL_Init();
  126. GAPRole_PeripheralInit();
  127. Peripheral_Init();
  128. led_task_init();
  129. led_task_start();
  130. led_enter_ota();
  131. PRINT("start task\n");
  132. Main_Circulation();
  133. }
  134. /******************************** endfile @ main ******************************/