firmware_upgrade.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "firmware_upgrade.h"
  2. #include "hal_can.h"
  3. #include "string.h"
  4. #include "hal_flash.h"
  5. #include "app_protocol.h"
  6. extern sys_paratemer_info_t sys_paratemer_info;
  7. extern QueueHandle_t can1_recieve1_data_queue;
  8. void firmware_upgrade_task(void *pvParameters)
  9. {
  10. can1_recieve1_data_queue = xQueueCreate(5,sizeof(stc_mcan_rx_msg_t));//can1存放升级帧的队列
  11. stc_mcan_rx_msg_t rx_msg;
  12. stc_mcan_tx_msg_t tx_msg;
  13. while(1)
  14. {
  15. if(pdTRUE==xQueueReceive(can1_recieve1_data_queue,&rx_msg,pdFALSE)) /*CAN升级 下线检测*/
  16. {
  17. printf("recive id =%x\r\n",rx_msg.ID);
  18. if(rx_msg.ID== MSG_RECEIVE_ID_UDS)//下线检测
  19. {
  20. //UDSAnsFunc(rx_msg.au8Data);
  21. }
  22. else//CAN升级
  23. {
  24. //判断接收的数据地址是否和本节点地址匹配
  25. if(rx_msg.IDE == 0x01)//扩展帧
  26. {
  27. if(rx_msg.ID!= MSG_RECEIVE_ID_EXTD)
  28. {
  29. printf("recive ex_id err\r\n");
  30. }
  31. tx_msg.ID = MSG_SEND_ID_EXTD;
  32. tx_msg.IDE = 1U;
  33. }
  34. else if(rx_msg.IDE == 0x00)//标准帧
  35. {
  36. if(rx_msg.ID!= MSG_RECEIVE_ID_STD)
  37. {
  38. printf("recive std_id err\r\n");
  39. }
  40. tx_msg.ID = MSG_SEND_ID_STD;
  41. tx_msg.IDE = 0U;
  42. }
  43. //满足id
  44. if(rx_msg.ID== MSG_RECEIVE_ID_EXTD||rx_msg.ID== MSG_RECEIVE_ID_STD)
  45. {
  46. switch(rx_msg.au8Data[0])
  47. {
  48. case 0xB6: //进入boot
  49. hc32_flash_read(BOOTLOADER_INFO_ADDR,(uint8_t*)&sys_paratemer_info,sizeof(sys_paratemer_info));
  50. sys_paratemer_info.destination_address = (uint32_t)APPLICATION_A_ADDR;
  51. sys_paratemer_info.is_can_write_flag = 1;
  52. /*写入升级标志*/
  53. hc32_flash_write(BOOTLOADER_INFO_ADDR,(uint8_t*)&sys_paratemer_info,sizeof(sys_paratemer_info));
  54. __disable_irq();//关闭所有中断
  55. NVIC_SystemReset();
  56. break;
  57. case 0xB1:
  58. tx_msg.au8Data[0] = 0x1B;
  59. tx_msg.au8Data[1] = VER_MAJOR_H;//主版本号,两字节
  60. tx_msg.au8Data[2] = VER_MAJOR_L;
  61. tx_msg.au8Data[3] = VER_SLAVE_H;//次版本号,两字节
  62. tx_msg.au8Data[4] = VER_SLAVE_L;
  63. tx_msg.au8Data[5] = 0xAA;//0x55固件是Bootloader,0xaa:固件是App
  64. tx_msg.au8Data[6] = 0xFF;
  65. tx_msg.au8Data[7] = 0xFF;
  66. tx_msg.DLC = 8;
  67. if(rx_msg.ID== MSG_RECEIVE_ID_STD) tx_msg.IDE = 0;
  68. else if(rx_msg.ID== MSG_RECEIVE_ID_EXTD) tx_msg.IDE = 1;
  69. tx_msg.u32TxBuffer = MCAN_TX_BUF0;
  70. MCAN_AddMsgToTxFifoQueue(CM_MCAN1,&tx_msg);
  71. printf("ack\r\n");
  72. break;
  73. }
  74. }
  75. }
  76. memset(&rx_msg,0,sizeof(rx_msg));
  77. memset(&tx_msg,0,sizeof(tx_msg));
  78. }
  79. vTaskDelay(10);
  80. }
  81. }