peripheral_main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /*******************************************************************************
  78. * @函数名称 User_GPIO_Init
  79. * @函数说明 GPIO初始化
  80. * @输入参数 无
  81. * @输出参数 无
  82. * @返回参数 无
  83. *******************************************************************************/
  84. static void User_GPIO_Init(void)
  85. {
  86. GPIO_InitTypeDef GPIO_InitStructure={0};
  87. KEY_GPIO_RCC_ENABLE;
  88. GPIO_InitStructure.GPIO_Pin = ELECT_LEFT_OPEN_PIN;
  89. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  90. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  91. GPIO_Init(ELECT_LEFT_OPEN_GPIO, &GPIO_InitStructure);
  92. ELECT_LEFT_OPEN_OFF;
  93. GPIO_InitStructure.GPIO_Pin = ELECT_RIGHT_OPEN_PIN;
  94. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  95. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  96. GPIO_Init(ELECT_RIGHT_OPEN_GPIO, &GPIO_InitStructure);
  97. ELECT_RIGHT_OPEN_OFF;
  98. GPIO_InitStructure.GPIO_Pin = LEFT_LIGHT_PIN;
  99. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  100. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  101. GPIO_Init(LEFT_LIGHT_GPIO, &GPIO_InitStructure);
  102. LEFT_LIGHT_OFF;
  103. GPIO_InitStructure.GPIO_Pin = RIGHT_LIGHT_PIN;
  104. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  105. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  106. GPIO_Init(RIGHT_LIGHT_GPIO, &GPIO_InitStructure);
  107. RIGHT_LIGHT_OFF;
  108. GPIO_InitStructure.GPIO_Pin = LEFT_PWM_PIN;
  109. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  110. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  111. GPIO_Init(LEFT_PWM_GPIO, &GPIO_InitStructure);
  112. LEFT_PWM_OFF;
  113. GPIO_InitStructure.GPIO_Pin = RIGHT_PWM_PIN;
  114. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  115. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  116. GPIO_Init(RIGHT_PWM_GPIO, &GPIO_InitStructure);
  117. RIGHT_PWM_OFF;
  118. }
  119. /*********************************************************************
  120. * @fn main
  121. *
  122. * @brief Main function
  123. *
  124. * @return none
  125. */
  126. int main(void)
  127. {
  128. SystemCoreClockUpdate();
  129. Delay_Init();
  130. #ifdef DEBUG
  131. USART_Printf_Init(115200);
  132. #endif
  133. PRINT("SystemClk:%d\r\n",SystemCoreClock);
  134. PRINT( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
  135. PRINT("%s\n", VER_LIB);
  136. ReadImageFlag();
  137. if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) == SET)
  138. {
  139. PRINT("iwdg reset\n");
  140. }
  141. // else if(RCC_GetFlagStatus(RCC_FLAG_SFTRST) == SET)
  142. // {
  143. // // Soft reset does not jump app app
  144. // PRINT("Soft reset does not jump app \n");
  145. // }
  146. else
  147. {
  148. if(CurrImageFlag == IMAGE_A_FLAG)
  149. {
  150. PRINT("jump User App \n");
  151. Delay_Ms(5);
  152. app_start();
  153. }
  154. else if(CurrImageFlag == IMAGE_OTA_FLAG)
  155. {
  156. PRINT("jump ota \n");
  157. SwitchImageFlag(IMAGE_A_FLAG);
  158. }
  159. }
  160. PRINT("ENTER OTA!\n");
  161. User_GPIO_Init();
  162. WCHBLE_Init();
  163. HAL_Init();
  164. GAPRole_PeripheralInit();
  165. Peripheral_Init();
  166. led_task_init();
  167. led_task_start();
  168. vUser_led_set_view_stat(LED_VIEW_ENTER_OTA);
  169. PRINT("start task\n");
  170. Main_Circulation();
  171. }
  172. /******************************** endfile @ main ******************************/