From 3b560799af15552c97a71266d5a74a68b67c726a Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 3 Aug 2025 22:52:01 +0200 Subject: [PATCH] Working OTA Update over UART to the Master --- goTool/main.go | 48 +++++++++++++++++++++---- main/message_builder.c | 5 ++- main/message_handler.c | 4 +-- main/ota_update.c | 82 ++++++++++++++++++++++++++++++++---------- main/uart_handler.c | 6 ++-- 5 files changed, 112 insertions(+), 33 deletions(-) diff --git a/goTool/main.go b/goTool/main.go index 82958c1..1f5bfe8 100644 --- a/goTool/main.go +++ b/goTool/main.go @@ -69,7 +69,6 @@ type OTASyncManager struct { func (ot *OTASyncManager) WaitForNextMessageTimeout() (*MessageReceive, error) { select { case msg := <-ot.NewOTAMessage: - log.Printf("OTASyncManager MessageReceive %v", msg) return &msg, nil case <-time.After(ot.TimeoutMessage): return nil, fmt.Errorf("Message Timeout") @@ -97,7 +96,7 @@ func addByteToParsedBuffer(mr *MessageReceive, pbyte byte) { } func parse_uart_ota_payload_payload(payloadBuffer []byte, payload_len int) { - fmt.Printf("RAW BUFFER: % 02X", payloadBuffer[:payload_len]) + //fmt.Printf("RAW BUFFER: % 02X", payloadBuffer[:payload_len]) if payload_len != 4 { fmt.Printf("Payload should be 4 is %v", payload_len) return @@ -206,7 +205,6 @@ func message_receive_failed_callback(mr MessageReceive) { } func parseByte(mr *MessageReceive, pbyte byte) { - fmt.Printf("Parsing %v", pbyte) addByteToRawBuffer(mr, pbyte) switch mr.state { case WAITING_FOR_START_BYTE: @@ -279,6 +277,10 @@ func buildMessage(payloadBuffer []byte, payload_len int, sendBuffer []byte) int writeIndex++ checksum ^= b } + if checksum == START_BYTE || checksum == ESCAPE_BYTE || checksum == END_BYTE { + sendBuffer[writeIndex] = ESCAPE_BYTE + writeIndex++ + } sendBuffer[writeIndex] = checksum writeIndex++ sendBuffer[writeIndex] = END_BYTE @@ -310,11 +312,12 @@ func main() { OTA_MessageCounter: 0, OTA_PayloadMessageSequence: 0, NewOTAMessage: make(chan MessageReceive), - TimeoutMessage: time.Millisecond * 1000, + TimeoutMessage: time.Millisecond * 30000, } mode := &serial.Mode{ - BaudRate: 115200, + //BaudRate: 115200, + BaudRate: 921600, } port, err := serial.Open("/dev/ttyUSB0", mode) if err != nil { @@ -367,15 +370,45 @@ func main() { payload_buffer[0] = UART_OTA_START n := buildMessage(payload_buffer, 1, send_buffer) sendMessage(port, send_buffer[:n]) - _, err = OTA_UpdateHandler.WaitForNextMessageTimeout() + msg, err := OTA_UpdateHandler.WaitForNextMessageTimeout() if err != nil { log.Printf("Error Message not acked %v", err) } else { - log.Printf("Message Waiting hat funktionioert") + if msg.parsed_message[2] != 0x00 { + log.Printf("Update Start failed %v", msg.parsed_message[2]) + return + } else { + log.Printf("Update Start confirmed Updating Partition %v", msg.parsed_message[1]) + } } + update_write_index := 0 // write update parts + for update_write_index < len(update) { + payload_buffer = make([]byte, 1024) + send_buffer = make([]byte, 1024) + payload_buffer[0] = UART_OTA_PAYLOAD + write_len := min(200, len(update)-update_write_index) + + //end_payload_len := min(update_write_index+200, len(update)) + + copy(payload_buffer[1:write_len+1], update[update_write_index:update_write_index+write_len]) + n = buildMessage(payload_buffer, write_len+1, send_buffer) + sendMessage(port, send_buffer[:n]) + msg, err := OTA_UpdateHandler.WaitForNextMessageTimeout() + if err != nil { + log.Printf("Error Message not acked %v", err) + return + } else { + seqCounter := binary.LittleEndian.Uint16(msg.parsed_message[1:3]) + buff_write_index := binary.LittleEndian.Uint16(msg.parsed_message[3:5]) + log.Printf("Sequenzce Counter: %d, Update buffer Write Index: %d", seqCounter, buff_write_index) + } + update_write_index += 200 + } + + log.Printf("Update übertragen beende hier!!!") // end payload_buffer = make([]byte, 1024) send_buffer = make([]byte, 1024) @@ -386,6 +419,7 @@ func main() { _, err = OTA_UpdateHandler.WaitForNextMessageTimeout() if err != nil { log.Printf("Error Message not acked %v", err) + return } else { log.Printf("Message Waiting hat funktionioert") } diff --git a/main/message_builder.c b/main/message_builder.c index bd91a3c..2d0c9ea 100644 --- a/main/message_builder.c +++ b/main/message_builder.c @@ -20,8 +20,8 @@ bool add_byte_with_length_check(uint8_t byte, size_t write_index, uint8_t *data, int build_message(uint8_t msgid, const uint8_t *payload, size_t payload_len, uint8_t *msg_buffer, size_t msg_buffer_size) { - ESP_LOGE("BM", "payload_len %d, msg_buffer_size %d", payload_len + 4, - msg_buffer_size); + //ESP_LOGE("BM", "payload_len %d, msg_buffer_size %d", payload_len + 4, + // msg_buffer_size); if (payload_len + 4 > msg_buffer_size) { return PayloadBiggerThenBuffer; } @@ -75,6 +75,5 @@ int build_message(uint8_t msgid, const uint8_t *payload, size_t payload_len, msg_buffer[write_index++] = checksum; msg_buffer[write_index++] = EndByte; - ESP_LOGE("BM", "MESSAGE FERTIG GEBAUT"); return write_index; } diff --git a/main/message_handler.c b/main/message_handler.c index b7b9500..89baab1 100644 --- a/main/message_handler.c +++ b/main/message_handler.c @@ -46,8 +46,8 @@ void MessageBrokerTask(void *param) { while (1) { if (xQueueReceive(msg_queue, &received_msg, portMAX_DELAY)) { - ESP_LOGI(TAG, "Received message from queue: MSGID=0x%02X, Length=%u", - received_msg.msgid, received_msg.payload_len); + //ESP_LOGI(TAG, "Received message from queue: MSGID=0x%02X, Length=%u", + // received_msg.msgid, received_msg.payload_len); for (int i = 0; i < mr.num_direct_callbacks; i++) { if (mr.FunctionList[i].MSGID == received_msg.msgid) { diff --git a/main/ota_update.c b/main/ota_update.c index ba451c4..74b5248 100644 --- a/main/ota_update.c +++ b/main/ota_update.c @@ -4,6 +4,7 @@ #include "esp_log.h" #include "esp_ota_ops.h" #include "esp_partition.h" +#include "esp_system.h" #include "message_builder.h" #include "message_handler.h" #include "uart_handler.h" @@ -17,17 +18,20 @@ static uint8_t update_buffer[UPDATE_BUFFER_SIZE]; static uint16_t update_buffer_write_index; +static uint32_t update_size; static uint16_t sequenz_counter; // how often the update buffer gets written static const char *TAG = "ALOX - OTA"; static esp_ota_handle_t update_handle; -void prepare_ota_update() { +int prepare_ota_update() { const esp_partition_t *running = esp_ota_get_running_partition(); ESP_LOGI(TAG, "OTA: Running Partition: %s", running->label); + int part = 0; char partition_to_update[] = "ota_0"; if (strcmp(running->label, "ota_0") == 0) { strcpy(partition_to_update, "ota_1"); + part = 1; } const esp_partition_t *update_partition = esp_partition_find_first( @@ -36,7 +40,7 @@ void prepare_ota_update() { // Check if the partition was found if (update_partition == NULL) { ESP_LOGE(TAG, "Failed to find OTA partition: %s", partition_to_update); - return; // Or handle the error appropriately + return -1; // Or handle the error appropriately } ESP_LOGI(TAG, "Gonna write OTA Update in Partition: %s", @@ -47,11 +51,11 @@ void prepare_ota_update() { if (err != ESP_OK) { ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); esp_ota_abort(update_handle); - return; + return -2; } ESP_LOGI(TAG, "OTA update started successfully."); - // Proceed with writing the new firmware to the partition... + return part; } void start_uart_update(uint8_t msgid, const uint8_t *payload, @@ -60,10 +64,23 @@ void start_uart_update(uint8_t msgid, const uint8_t *payload, size_t send_buffer_size) { ESP_LOGI(TAG, "OTA Update Start Uart Command"); - prepare_ota_update(); + vTaskPrioritySet(NULL, 2); + + update_size = 0; + + int part = prepare_ota_update(); + + // Message: + // byte partition + // byte error + + if (part < 0) { + send_payload_buffer[1] = (part * -1) & 0xff; + } else { + send_payload_buffer[0] = part & 0xff; + } int send_payload_len = 2; - send_payload_buffer[0] = 0xff; int len = build_message(UART_OTA_START, send_payload_buffer, send_payload_len, send_buffer, send_buffer_size); if (len < 0) { @@ -81,17 +98,14 @@ void payload_uart_update(uint8_t msgid, const uint8_t *payload, size_t payload_len, uint8_t *send_payload_buffer, size_t send_payload_buffer_size, uint8_t *send_buffer, size_t send_buffer_size) { - ESP_LOGI(TAG, "OTA Update Payload Uart Command"); - - if (update_buffer_write_index < UPDATE_BUFFER_SIZE - UPDATE_PAYLOAD_SIZE) { - uint32_t write_len = MIN(UPDATE_PAYLOAD_SIZE, payload_len); - ESP_LOGI(TAG, "Writing Data to Update BUffer Sequence %d, writing Data %d", - sequenz_counter, write_len); - memcpy(&update_buffer[update_buffer_write_index], payload, write_len); - update_buffer_write_index += write_len; - } else { - ESP_LOGI(TAG, "Update Buffer full, writing it to OTA Update"); + // ESP_LOGI(TAG, "OTA Update Payload Uart Command"); + uint32_t write_len = MIN(UPDATE_PAYLOAD_SIZE, payload_len); + update_size += write_len; + if (update_buffer_write_index > UPDATE_BUFFER_SIZE - write_len) { + // ESP_LOGI(TAG, "Writing Data to Update BUffer Sequence %d, writing Data + // %d", + // sequenz_counter, write_len); // write to ota esp_err_t err = esp_ota_write(update_handle, update_buffer, update_buffer_write_index); @@ -103,9 +117,13 @@ void payload_uart_update(uint8_t msgid, const uint8_t *payload, sequenz_counter++; } + memcpy(&update_buffer[update_buffer_write_index], payload, write_len); + update_buffer_write_index += write_len; + size_t send_payload_len = 4; memcpy(send_payload_buffer, &sequenz_counter, 2); memcpy(&send_payload_buffer[2], &update_buffer_write_index, 2); + send_payload_buffer[4] = 0x00; // error int len = build_message(UART_OTA_PAYLOAD, send_payload_buffer, send_payload_len, send_buffer, send_buffer_size); @@ -125,12 +143,38 @@ void end_uart_update(uint8_t msgid, const uint8_t *payload, size_t payload_len, size_t send_payload_buffer_size, uint8_t *send_buffer, size_t send_buffer_size) { ESP_LOGI(TAG, "OTA Update End Uart Command"); - esp_err_t err = esp_ota_end(update_handle); + + esp_err_t err = + esp_ota_write(update_handle, update_buffer, update_buffer_write_index); if (err != ESP_OK) { ESP_LOGE(TAG, "GOT ESP ERROR WRITE OTA %d", err); } - int send_payload_len = 0; + err = esp_ota_end(update_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "GOT ESP ERROR WRITE OTA %d", err); + } + + ESP_LOGE(TAG, "UPDATE ENDE UPDATGE SIZE SIND %d BYTES", update_size); + + // Hol dir die zuletzt geschriebene Partition + const esp_partition_t *partition = esp_ota_get_next_update_partition(NULL); + if (partition == NULL) { + ESP_LOGE(TAG, "Failed to get updated partition"); + err = ESP_FAIL; + } + + // Setze sie als Boot-Partition + ESP_LOGE(TAG, "Setzte nächste Partition auf %s", partition->label); + err = esp_ota_set_boot_partition(partition); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_ota_set_boot_partition failed: %s", + esp_err_to_name(err)); + } + + // message ret esp_err_t + int send_payload_len = 1; + send_payload_buffer[0] = err & 0xff; int len = build_message(UART_OTA_END, send_payload_buffer, send_payload_len, send_buffer, send_buffer_size); if (len < 0) { @@ -142,6 +186,8 @@ void end_uart_update(uint8_t msgid, const uint8_t *payload, size_t payload_len, } uart_write_bytes(MASTER_UART, send_buffer, len); + + vTaskPrioritySet(NULL, 1); } void write_ota_update_from_uart_task(void *param) {} diff --git a/main/uart_handler.c b/main/uart_handler.c index 92a1600..28080d2 100644 --- a/main/uart_handler.c +++ b/main/uart_handler.c @@ -18,7 +18,7 @@ static const char *TAG = "ALOX - UART"; static QueueHandle_t parsed_message_queue; void init_uart(QueueHandle_t msg_queue_handle) { - uart_config_t uart_config = {.baud_rate = 115200, + uart_config_t uart_config = {.baud_rate = 921600, // 921600, 115200 .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, @@ -61,9 +61,9 @@ void send_message_hook(const uint8_t *buffer, size_t length) { void HandleMessageReceivedCallback(uint8_t msgid, const uint8_t *payload, size_t payload_len) { - ESP_LOGI(TAG, "GOT UART MESSAGE MSGID: %02X, Len: %u bytes \nMSG: ", msgid, + /*ESP_LOGI(TAG, "GOT UART MESSAGE MSGID: %02X, Len: %u bytes \nMSG: ", msgid, payload_len, payload); - ESP_LOG_BUFFER_HEX(TAG, payload, payload_len); + ESP_LOG_BUFFER_HEX(TAG, payload, payload_len);*/ ParsedMessage_t msg_to_send; msg_to_send.msgid = msgid;