RefreshMenu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. ******************************************************************************
  3. * @file RefreshMenu.c
  4. * @author Vincent
  5. * @version V1.0.0
  6. * @date 23-12-2015
  7. * @brief
  8. ******************************************************************************
  9. ******************************************************************************
  10. */
  11. /* Includes ------------------------------------------------------------------*/
  12. #include "RefreshMenu.h"
  13. #include "protocol.h"
  14. #include "uart_app.h"
  15. #include "can_app.h"
  16. #include "flash_if.h"
  17. #include "hwsetup.h"
  18. #include "ti_msp_dl_config.h"
  19. /* Private variables ---------------------------------------------------------*/
  20. download_t downLoad;
  21. updatecount_t update;
  22. uint8_t flag_FlashWR[APPPACKGECOUNT_MAX_ADDRESS];
  23. /* Private variables ---------------------------------------------------------*/
  24. /* Private function prototypes -----------------------------------------------*/
  25. //void Protocol_Ack(void);
  26. //typedef void (*iapfun)(void); //瀹氫箟涓�涓嚱鏁扮被鍨嬬殑鍙傛暟
  27. //iapfun jumpFun;
  28. otafun jump_to_app;
  29. void JumpToApp(__IO uint32_t ApplicationAddress)
  30. {
  31. /* check app starting address whether 0x08xxxxxx */
  32. if(((*(uint32_t*)(ApplicationAddress + 0)) & 0xFFFF0000) == 0x20200000)
  33. // return;
  34. /* check the address of stack */
  35. // if(((*(uint32_t*)ApplicationAddress) - 0x20000000) < (SRAM_SIZE * 1024))
  36. {
  37. //crm_reset();
  38. __disable_irq();
  39. /* disable periph clock */
  40. // crm_periph_clock_enable(CRM_TMR3_PERIPH_CLOCK, FALSE);
  41. // crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, FALSE);
  42. // crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, FALSE);
  43. DL_GPIO_reset(GPIOA);
  44. DL_GPIO_reset(GPIOB);
  45. DL_GPIO_disablePower(GPIOA);
  46. DL_GPIO_disablePower(GPIOB);
  47. //DL_UART_Main_disablePower(UART_BAT_INST);
  48. DL_UART_Main_disablePower(UART_HMI_INST);
  49. DL_MCAN_disablePower(MCAN0_INST);
  50. #if 1 //UART2_ENABLE
  51. // crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, FALSE);
  52. // crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, FALSE);
  53. // nvic_irq_disable(USART2_IRQn);
  54. // __NVIC_ClearPendingIRQ(USART2_IRQn);
  55. //__NVIC_DisableIRQ(UART_BAT_INST_INT_IRQN);
  56. //__NVIC_ClearPendingIRQ(UART_BAT_INST_INT_IRQN);
  57. #endif
  58. /* disable nvic irq and clear pending */
  59. // nvic_irq_disable(USART1_IRQn);
  60. // nvic_irq_disable(UART1_INT_IRQn);
  61. __NVIC_DisableIRQ(UART_HMI_INST_INT_IRQN);
  62. // __NVIC_ClearPendingIRQ(USART1_IRQn);
  63. __NVIC_ClearPendingIRQ(UART_HMI_INST_INT_IRQN);
  64. __NVIC_DisableIRQ(MCAN0_INST_INT_IRQN);
  65. __NVIC_ClearPendingIRQ(MCAN0_INST_INT_IRQN);
  66. // nvic_irq_disable(SysTick_IRQn);
  67. __NVIC_DisableIRQ(SysTick_IRQn);
  68. __NVIC_ClearPendingIRQ(SysTick_IRQn);
  69. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  70. SysTick->VAL = 0x00; /* Load the SysTick Counter Value */
  71. // nvic_irq_disable(TMR3_GLOBAL_IRQn);
  72. // __NVIC_ClearPendingIRQ(TMR3_GLOBAL_IRQn);
  73. // SCB->VTOR =0x00006000;// nvic_vector_table_set(NVIC_VECTTAB_FLASH, 0x00006000);
  74. // delay_cycles(100000);
  75. // jump_to_app =(otafun)*(uint32_t*)(0x00006131);
  76. // (*(void(*)(void))(0x00006131))();
  77. jump_to_app = (otafun)*(uint32_t*)(ApplicationAddress + 4); /* code second word is reset address */
  78. __set_MSP(*(uint32_t*)ApplicationAddress); /* init app stack pointer(code first word is stack address) */
  79. jump_to_app(); /* jump to user app */
  80. }
  81. }
  82. /*******************************************************************************
  83. * Function: void User_Manu_Init(void)
  84. * Discrible: 鍒濆鍖�
  85. * Input:
  86. * Output:
  87. * Return:
  88. * Others:
  89. * date version author record
  90. * -----------------------------------------------
  91. * 20180419 V1.0 VINCENT Create
  92. *********************************************************************************/
  93. void User_Manu_Init(void)
  94. {
  95. //_.ackcmd = ACK_REQ;
  96. downLoad.step = 0;
  97. downLoad.packagecount = 0;
  98. downLoad.wrenable = DISABLE;
  99. downLoad.finished = DISABLE;
  100. downLoad.startUpSendCount = 0;
  101. update.TimeCounter_Startup = 1000;
  102. update.TimeCounter_Update = 0;
  103. }
  104. /*******************************************************************************
  105. * Function: void User_Main_Manu(void)
  106. * Discrible: 涓昏彍鍗�
  107. * Input:
  108. * Output:
  109. * Return:
  110. * Others:
  111. * date version author record
  112. * -----------------------------------------------
  113. * 20180419 V1.0 VINCENT Create
  114. *********************************************************************************/
  115. void User_Main_Manu(void)
  116. {
  117. while(1)
  118. {
  119. //UART_RxData_Process(&UART_RxBuff_Struct1,&UART_TxBuff_Struct1);
  120. //UART_RxData_Process(&UART_RxBuff_Struct2,&UART_TxBuff_Struct2);
  121. CAN_RxData_Process(&CAN_RxBuf_Struct1);
  122. startUpAck();
  123. if(CAN_RxBuf_Struct1.ucCANEnable == 1)
  124. {
  125. updateAck(&UART_TxBuff_Struct2);
  126. timeOutAck(&UART_TxBuff_Struct2);
  127. }
  128. /*else if(UART_RxBuff_Struct1.ucCOMEnable == 1)
  129. {
  130. updateAck(&UART_TxBuff_Struct1);
  131. timeOutAck(&UART_TxBuff_Struct1);
  132. }*/
  133. /*else if(UART_RxBuff_Struct2.ucCOMEnable == 1)
  134. {
  135. updateAck(&UART_TxBuff_Struct2);
  136. timeOutAck(&UART_TxBuff_Struct2);
  137. }*/
  138. if(downLoad.finished == ENABLE)
  139. {
  140. delay_1ms(50);
  141. //DL_UART_reset(UART_HMI_INST);
  142. DL_MCAN_reset(MCAN0_INST);
  143. DL_GPIO_clearPins(OUTPUT_PORT, OUTPUT_POWER_EN_PIN);
  144. #if UART2_ENABLE
  145. // usart_reset(USART2);
  146. // DL_UART_reset(UART_BAT_INST);
  147. #endif
  148. JumpToApp(APPLICATION_ADDRESS);
  149. }
  150. }
  151. }
  152. void timeOutAck(USART_Buf_TypeDef* ptUartTx)
  153. {
  154. if((downLoad.finished == DISABLE) && (downLoad.step != 0))
  155. {
  156. if(update.TimeCounter_Update > 3000 ) //3000ms
  157. {
  158. update.TimeCounter_Update = 0;
  159. sendUpdateAck(CMD_ACK_ERR , downLoad.packagecount);
  160. downLoad.finished = ENABLE;
  161. }
  162. }
  163. }
  164. /*上电启动,或复位后*/
  165. void startUpAck(void)
  166. {
  167. if(downLoad.step == 0 )
  168. {
  169. if(downLoad.startUpSendCount < 4)
  170. {
  171. if(update.TimeCounter_Startup >= 50) //200ms
  172. {
  173. update.TimeCounter_Startup = 0;
  174. downLoad.startUpSendCount++;
  175. // DL_UART_Main_transmitData(UART_0_INST, 0x5a);
  176. // DL_UART_Main_transmitData(UART_HMI_INST, 0x5a);
  177. //uart_sendBootInfo(&UART_TxBuff_Struct1);
  178. //uart_sendBootInfo(&UART_TxBuff_Struct2);
  179. CAN_sendBootInfo();
  180. //sendBootInfo();
  181. }
  182. }
  183. else
  184. {
  185. downLoad.finished = ENABLE;
  186. }
  187. }
  188. }
  189. uint32_t fisrt_data;
  190. void updateAck(USART_Buf_TypeDef* ptUartTx)
  191. {
  192. if( update.AckEnable == ENABLE )
  193. {
  194. update.AckEnable = DISABLE;
  195. switch(downLoad.step)
  196. {
  197. case 1:
  198. sendUpdateAck(downLoad.userCmd , 0x0000);
  199. break;
  200. case 2:
  201. sendUpdateAck(downLoad.userCmd , downLoad.packagecount);
  202. break;
  203. case 3:
  204. sendUpdateAck(downLoad.userCmd , 0x0000);
  205. downLoad.finished = ENABLE;
  206. break;
  207. case 5:
  208. uart_sendAck(ptUartTx);
  209. downLoad.step = 0;
  210. downLoad.finished = DISABLE;
  211. downLoad.startUpSendCount = 0;
  212. break;
  213. default :
  214. break;
  215. }
  216. }
  217. if(downLoad.wrenable == ENABLE)
  218. {
  219. downLoad.wrenable = DISABLE;
  220. // write flash
  221. if((downLoad.packagecount <= APPPACKGECOUNT_MAX_ADDRESS) && ( downLoad.packagecount > 0 ) ) //
  222. {
  223. if(flag_FlashWR[downLoad.packagecount - 1] == 0 )
  224. {
  225. fisrt_data=*(uint32_t *)downLoad.dataBuff_128B;
  226. if(FLASH_If_Write( (downLoad.packagecount - 1) * PACKET_128B_SIZE + APPLICATION_ADDRESS, (uint32_t *)downLoad.dataBuff_128B, 16) != FLASHIF_OK )
  227. {
  228. while(1)
  229. {
  230. sendUpdateAck(CMD_ACK_ERR_FLASH , 0xee);
  231. // Iwdg_Refresh();
  232. delay_1ms(1000);
  233. }
  234. }
  235. flag_FlashWR[downLoad.packagecount - 1] = 1;
  236. /* 写完FLASH后立即应答,清超时计数 */
  237. update.AckEnable = ENABLE;
  238. /*清除超时标志*/
  239. update.TimeCounter_Update = 0;
  240. }
  241. else
  242. {
  243. /*重复写入,可能是上位机没有接收到正确应答信号,重发了数据包,
  244. 应答一个正确信号,让上位机发送下一包数据*/
  245. sendUpdateAck(CMD_ACK_OK, downLoad.packagecount);
  246. sendUpdateAck(CMD_ACK_ERR_FLASH , 0xe1);
  247. }
  248. }
  249. else
  250. {
  251. sendUpdateAck(CMD_ACK_ERR_FLASH , 0xe2);
  252. }
  253. }
  254. }
  255. void sendUpdateAck(uint16_t cmd, uint16_t packageNum)
  256. {
  257. if(CAN_RxBuf_Struct1.ucCANEnable == 1)
  258. {
  259. CAN_sendUpdateAck(cmd, packageNum);
  260. }
  261. /*else if(UART_RxBuff_Struct1.ucCOMEnable == 1)
  262. {
  263. uart_sendUpdateAck(&UART_TxBuff_Struct1, cmd, packageNum);
  264. }*/
  265. else if(UART_RxBuff_Struct2.ucCOMEnable == 1)
  266. {
  267. uart_sendUpdateAck(&UART_TxBuff_Struct2, cmd, packageNum);
  268. }
  269. }
  270. void sendAck(void)
  271. {
  272. if(CAN_RxBuf_Struct1.ucCANEnable == 1)
  273. {
  274. CAN_sendAck();
  275. }
  276. /*else if(UART_RxBuff_Struct1.ucCOMEnable == 1)
  277. {
  278. uart_sendAck(&UART_TxBuff_Struct1);
  279. }*/
  280. else if(UART_RxBuff_Struct2.ucCOMEnable == 1)
  281. {
  282. uart_sendAck(&UART_TxBuff_Struct2);
  283. }
  284. }
  285. /*******************************************************************************
  286. * Function: void Period_Stick(void)
  287. * Discrible: 周期定时器
  288. * Input:
  289. * Output:
  290. * Return:
  291. * Others:
  292. * date version author record
  293. * -----------------------------------------------
  294. * 20160307 V1.0 VINCENT Create
  295. *********************************************************************************/
  296. void Period_Stick(void)
  297. {
  298. update.TimeCounter_Startup ++;
  299. update.TimeCounter_Update++;
  300. }
  301. /************************ (C) END OF FILE *********************************/