soft_uart.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "soft_uart.h"
  2. #include "stm32f4xx_hal_uart.h"
  3. #include "string.h"
  4. #include "soft_flash.h"
  5. #include "soft_crc.h"
  6. extern UART_HandleTypeDef huart1;
  7. void uart1_send_msg(uint8_t *data, uint8_t size)
  8. {
  9. static uint32_t send_time = 0;
  10. send_time = HAL_GetTick();
  11. while (HAL_GetTick() - send_time < 1)
  12. ;
  13. HAL_UART_Transmit_DMA(&huart1, data, size);
  14. }
  15. uint8_t FMU_uart_buf[MAX_UART_BUF] = {0};
  16. Update update;
  17. void USER_UART_IRQHandler(UART_HandleTypeDef *huart)
  18. {
  19. uint32_t recv_count = 0;
  20. if (huart == &huart1)
  21. {
  22. if (RESET != __HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
  23. {
  24. __HAL_UART_CLEAR_IDLEFLAG(huart);
  25. HAL_UART_AbortReceive(huart);
  26. recv_count = MAX_UART_BUF - __HAL_DMA_GET_COUNTER(huart->hdmarx);
  27. // 检测是否是新的一包数据
  28. if (FMU_uart_buf[0] == 0xFE && FMU_uart_buf[4] == 204 && update.usebuf_flag == false)
  29. {
  30. memcpy(update.data, FMU_uart_buf, recv_count);
  31. update.usebuf_flag = true;
  32. }
  33. memset(FMU_uart_buf, 0, MAX_UART_BUF);
  34. HAL_UART_Receive_DMA(huart, FMU_uart_buf, MAX_UART_BUF);
  35. }
  36. }
  37. }
  38. uint8_t msg_buf[64] = {0};
  39. void Update_ack_fmu(uint8_t msg_id, uint8_t *ackbuf)
  40. {
  41. int index = 0;
  42. msg_buf[index++] = 0xFE;
  43. msg_buf[index++] = 0;
  44. msg_buf[index++] = 0;
  45. msg_buf[index++] = 0x00;
  46. msg_buf[index++] = 204;
  47. msg_buf[index++] = 21;
  48. msg_buf[index++] = msg_id;
  49. msg_buf[index++] = *ackbuf;
  50. msg_buf[index++] = *(ackbuf + 1);
  51. msg_buf[index++] = 1;
  52. msg_buf[1] = index - 6;
  53. uint16_t uart_crc = Get_Crc16(msg_buf, index);
  54. memcpy(&msg_buf[index], &uart_crc, 2);
  55. index += 2;
  56. uart1_send_msg(msg_buf, index);
  57. }
  58. bool check_usart_is_ok(void)
  59. {
  60. if (huart1.gState == HAL_UART_STATE_READY && (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_TC) != RESET && __HAL_UART_GET_FLAG(&huart1, UART_FLAG_TXE) != RESET))
  61. {
  62. }
  63. else
  64. {
  65. return false;
  66. }
  67. return true;
  68. }
  69. uint32_t addr_offset = 0;
  70. uint32_t cur_pack_num = 0;
  71. bool update_complete = false;
  72. void update_function(void)
  73. {
  74. uint16_t crc = Get_Crc16(update.data, (uint16_t)update.data[LEN] + 2 + 6);
  75. int temp_32t = 0;
  76. if (crc != 0 || check_usart_is_ok() != true)
  77. {
  78. update.usebuf_flag = false;
  79. return;
  80. }
  81. switch (update.data[MSGID_ID])
  82. {
  83. case UPDATE_START:
  84. memcpy(&temp_32t, &update.data[PAYLOAD], 4);
  85. if (temp_32t < 512000) // 500kb 避免把FMU固件刷到pmu
  86. {
  87. temp_32t = 0;
  88. Update_ack_fmu(UPDATE_START, (uint8_t *)&temp_32t);
  89. addr_offset = 0;
  90. cur_pack_num = 1;
  91. }
  92. break;
  93. case UPDATE_ING:
  94. if (cur_pack_num == update.data[PAYLOAD] + update.data[PAYLOAD + 1] * 256)
  95. {
  96. temp_32t = Flash_WriteData(FLASH_APP_ADDR + addr_offset, (uint16_t *)&update.data[PAYLOAD + 2], (update.data[LEN] - 2) / 2);
  97. if (temp_32t == WRITE_SUCCESS)
  98. {
  99. cur_pack_num++;
  100. addr_offset += update.data[LEN] - 2;
  101. Update_ack_fmu(UPDATE_ING, &update.data[PAYLOAD]);
  102. }
  103. }
  104. else if(cur_pack_num > update.data[PAYLOAD] + update.data[PAYLOAD + 1] * 256)
  105. {
  106. Update_ack_fmu(UPDATE_ING, &update.data[PAYLOAD]);
  107. }
  108. break;
  109. case UPDATE_END:
  110. temp_32t = 0;
  111. if(update_complete != true)
  112. {
  113. update_complete = wirte_update_flag();
  114. if(update_complete == true)
  115. Update_ack_fmu(UPDATE_END, (uint8_t *)&temp_32t);
  116. HAL_Delay(50);
  117. }
  118. break;
  119. default:
  120. break;
  121. }
  122. update.usebuf_flag = false;
  123. memset(update.data, 0, MAX_UART_BUF);
  124. }