| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- #include "soft_uart.h"
- #include "stm32f4xx_hal_uart.h"
- #include "string.h"
- #include "soft_flash.h"
- #include "soft_crc.h"
- extern UART_HandleTypeDef huart1;
- void uart1_send_msg(uint8_t *data, uint8_t size)
- {
- static uint32_t send_time = 0;
- send_time = HAL_GetTick();
- while (HAL_GetTick() - send_time < 1)
- ;
- HAL_UART_Transmit_DMA(&huart1, data, size);
- }
- uint8_t FMU_uart_buf[MAX_UART_BUF] = {0};
- Update update;
- void USER_UART_IRQHandler(UART_HandleTypeDef *huart)
- {
- uint32_t recv_count = 0;
- if (huart == &huart1)
- {
- if (RESET != __HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE))
- {
- __HAL_UART_CLEAR_IDLEFLAG(huart);
- HAL_UART_AbortReceive(huart);
- recv_count = MAX_UART_BUF - __HAL_DMA_GET_COUNTER(huart->hdmarx);
- // 检测是否是新的一包数据
- if (FMU_uart_buf[0] == 0xFE && FMU_uart_buf[4] == 204 && update.usebuf_flag == false)
- {
- memcpy(update.data, FMU_uart_buf, recv_count);
- update.usebuf_flag = true;
- }
- memset(FMU_uart_buf, 0, MAX_UART_BUF);
- HAL_UART_Receive_DMA(huart, FMU_uart_buf, MAX_UART_BUF);
- }
- }
- }
- uint8_t msg_buf[64] = {0};
- void Update_ack_fmu(uint8_t msg_id, uint8_t *ackbuf)
- {
- int index = 0;
- msg_buf[index++] = 0xFE;
- msg_buf[index++] = 0;
- msg_buf[index++] = 0;
- msg_buf[index++] = 0x00;
- msg_buf[index++] = 204;
- msg_buf[index++] = 21;
- msg_buf[index++] = msg_id;
- msg_buf[index++] = *ackbuf;
- msg_buf[index++] = *(ackbuf + 1);
- msg_buf[index++] = 1;
- msg_buf[1] = index - 6;
- uint16_t uart_crc = Get_Crc16(msg_buf, index);
- memcpy(&msg_buf[index], &uart_crc, 2);
- index += 2;
- uart1_send_msg(msg_buf, index);
- }
- bool check_usart_is_ok(void)
- {
- 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))
- {
- }
- else
- {
- return false;
- }
- return true;
- }
- uint32_t addr_offset = 0;
- uint32_t cur_pack_num = 0;
- bool update_complete = false;
- void update_function(void)
- {
- uint16_t crc = Get_Crc16(update.data, (uint16_t)update.data[LEN] + 2 + 6);
- int temp_32t = 0;
- if (crc != 0 || check_usart_is_ok() != true)
- {
- update.usebuf_flag = false;
- return;
- }
- switch (update.data[MSGID_ID])
- {
- case UPDATE_START:
- memcpy(&temp_32t, &update.data[PAYLOAD], 4);
- if (temp_32t < 512000) // 500kb 避免把FMU固件刷到pmu
- {
- temp_32t = 0;
- Update_ack_fmu(UPDATE_START, (uint8_t *)&temp_32t);
- addr_offset = 0;
- cur_pack_num = 1;
- }
- break;
- case UPDATE_ING:
- if (cur_pack_num == update.data[PAYLOAD] + update.data[PAYLOAD + 1] * 256)
- {
- temp_32t = Flash_WriteData(FLASH_APP_ADDR + addr_offset, (uint16_t *)&update.data[PAYLOAD + 2], (update.data[LEN] - 2) / 2);
- if (temp_32t == WRITE_SUCCESS)
- {
- cur_pack_num++;
- addr_offset += update.data[LEN] - 2;
- Update_ack_fmu(UPDATE_ING, &update.data[PAYLOAD]);
- }
- }
- else if(cur_pack_num > update.data[PAYLOAD] + update.data[PAYLOAD + 1] * 256)
- {
- Update_ack_fmu(UPDATE_ING, &update.data[PAYLOAD]);
- }
- break;
- case UPDATE_END:
- temp_32t = 0;
- if(update_complete != true)
- {
- update_complete = wirte_update_flag();
- if(update_complete == true)
- Update_ack_fmu(UPDATE_END, (uint8_t *)&temp_32t);
- HAL_Delay(50);
- }
- break;
- default:
- break;
- }
- update.usebuf_flag = false;
- memset(update.data, 0, MAX_UART_BUF);
- }
|