Working OTA Update over UART to the Master

This commit is contained in:
simon 2025-08-03 22:52:01 +02:00
parent 3abdd8816c
commit 3b560799af
5 changed files with 112 additions and 33 deletions

View File

@ -69,7 +69,6 @@ type OTASyncManager struct {
func (ot *OTASyncManager) WaitForNextMessageTimeout() (*MessageReceive, error) { func (ot *OTASyncManager) WaitForNextMessageTimeout() (*MessageReceive, error) {
select { select {
case msg := <-ot.NewOTAMessage: case msg := <-ot.NewOTAMessage:
log.Printf("OTASyncManager MessageReceive %v", msg)
return &msg, nil return &msg, nil
case <-time.After(ot.TimeoutMessage): case <-time.After(ot.TimeoutMessage):
return nil, fmt.Errorf("Message Timeout") 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) { 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 { if payload_len != 4 {
fmt.Printf("Payload should be 4 is %v", payload_len) fmt.Printf("Payload should be 4 is %v", payload_len)
return return
@ -206,7 +205,6 @@ func message_receive_failed_callback(mr MessageReceive) {
} }
func parseByte(mr *MessageReceive, pbyte byte) { func parseByte(mr *MessageReceive, pbyte byte) {
fmt.Printf("Parsing %v", pbyte)
addByteToRawBuffer(mr, pbyte) addByteToRawBuffer(mr, pbyte)
switch mr.state { switch mr.state {
case WAITING_FOR_START_BYTE: case WAITING_FOR_START_BYTE:
@ -279,6 +277,10 @@ func buildMessage(payloadBuffer []byte, payload_len int, sendBuffer []byte) int
writeIndex++ writeIndex++
checksum ^= b checksum ^= b
} }
if checksum == START_BYTE || checksum == ESCAPE_BYTE || checksum == END_BYTE {
sendBuffer[writeIndex] = ESCAPE_BYTE
writeIndex++
}
sendBuffer[writeIndex] = checksum sendBuffer[writeIndex] = checksum
writeIndex++ writeIndex++
sendBuffer[writeIndex] = END_BYTE sendBuffer[writeIndex] = END_BYTE
@ -310,11 +312,12 @@ func main() {
OTA_MessageCounter: 0, OTA_MessageCounter: 0,
OTA_PayloadMessageSequence: 0, OTA_PayloadMessageSequence: 0,
NewOTAMessage: make(chan MessageReceive), NewOTAMessage: make(chan MessageReceive),
TimeoutMessage: time.Millisecond * 1000, TimeoutMessage: time.Millisecond * 30000,
} }
mode := &serial.Mode{ mode := &serial.Mode{
BaudRate: 115200, //BaudRate: 115200,
BaudRate: 921600,
} }
port, err := serial.Open("/dev/ttyUSB0", mode) port, err := serial.Open("/dev/ttyUSB0", mode)
if err != nil { if err != nil {
@ -367,15 +370,45 @@ func main() {
payload_buffer[0] = UART_OTA_START payload_buffer[0] = UART_OTA_START
n := buildMessage(payload_buffer, 1, send_buffer) n := buildMessage(payload_buffer, 1, send_buffer)
sendMessage(port, send_buffer[:n]) sendMessage(port, send_buffer[:n])
_, err = OTA_UpdateHandler.WaitForNextMessageTimeout() msg, err := OTA_UpdateHandler.WaitForNextMessageTimeout()
if err != nil { if err != nil {
log.Printf("Error Message not acked %v", err) log.Printf("Error Message not acked %v", err)
} else { } 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 // 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 // end
payload_buffer = make([]byte, 1024) payload_buffer = make([]byte, 1024)
send_buffer = make([]byte, 1024) send_buffer = make([]byte, 1024)
@ -386,6 +419,7 @@ func main() {
_, err = OTA_UpdateHandler.WaitForNextMessageTimeout() _, err = OTA_UpdateHandler.WaitForNextMessageTimeout()
if err != nil { if err != nil {
log.Printf("Error Message not acked %v", err) log.Printf("Error Message not acked %v", err)
return
} else { } else {
log.Printf("Message Waiting hat funktionioert") log.Printf("Message Waiting hat funktionioert")
} }

View File

@ -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, int build_message(uint8_t msgid, const uint8_t *payload, size_t payload_len,
uint8_t *msg_buffer, size_t msg_buffer_size) { uint8_t *msg_buffer, size_t msg_buffer_size) {
ESP_LOGE("BM", "payload_len %d, msg_buffer_size %d", payload_len + 4, //ESP_LOGE("BM", "payload_len %d, msg_buffer_size %d", payload_len + 4,
msg_buffer_size); // msg_buffer_size);
if (payload_len + 4 > msg_buffer_size) { if (payload_len + 4 > msg_buffer_size) {
return PayloadBiggerThenBuffer; 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++] = checksum;
msg_buffer[write_index++] = EndByte; msg_buffer[write_index++] = EndByte;
ESP_LOGE("BM", "MESSAGE FERTIG GEBAUT");
return write_index; return write_index;
} }

View File

@ -46,8 +46,8 @@ void MessageBrokerTask(void *param) {
while (1) { while (1) {
if (xQueueReceive(msg_queue, &received_msg, portMAX_DELAY)) { if (xQueueReceive(msg_queue, &received_msg, portMAX_DELAY)) {
ESP_LOGI(TAG, "Received message from queue: MSGID=0x%02X, Length=%u", //ESP_LOGI(TAG, "Received message from queue: MSGID=0x%02X, Length=%u",
received_msg.msgid, received_msg.payload_len); // received_msg.msgid, received_msg.payload_len);
for (int i = 0; i < mr.num_direct_callbacks; i++) { for (int i = 0; i < mr.num_direct_callbacks; i++) {
if (mr.FunctionList[i].MSGID == received_msg.msgid) { if (mr.FunctionList[i].MSGID == received_msg.msgid) {

View File

@ -4,6 +4,7 @@
#include "esp_log.h" #include "esp_log.h"
#include "esp_ota_ops.h" #include "esp_ota_ops.h"
#include "esp_partition.h" #include "esp_partition.h"
#include "esp_system.h"
#include "message_builder.h" #include "message_builder.h"
#include "message_handler.h" #include "message_handler.h"
#include "uart_handler.h" #include "uart_handler.h"
@ -17,17 +18,20 @@
static uint8_t update_buffer[UPDATE_BUFFER_SIZE]; static uint8_t update_buffer[UPDATE_BUFFER_SIZE];
static uint16_t update_buffer_write_index; 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 uint16_t sequenz_counter; // how often the update buffer gets written
static const char *TAG = "ALOX - OTA"; static const char *TAG = "ALOX - OTA";
static esp_ota_handle_t update_handle; 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(); const esp_partition_t *running = esp_ota_get_running_partition();
ESP_LOGI(TAG, "OTA: Running Partition: %s", running->label); ESP_LOGI(TAG, "OTA: Running Partition: %s", running->label);
int part = 0;
char partition_to_update[] = "ota_0"; char partition_to_update[] = "ota_0";
if (strcmp(running->label, "ota_0") == 0) { if (strcmp(running->label, "ota_0") == 0) {
strcpy(partition_to_update, "ota_1"); strcpy(partition_to_update, "ota_1");
part = 1;
} }
const esp_partition_t *update_partition = esp_partition_find_first( const esp_partition_t *update_partition = esp_partition_find_first(
@ -36,7 +40,7 @@ void prepare_ota_update() {
// Check if the partition was found // Check if the partition was found
if (update_partition == NULL) { if (update_partition == NULL) {
ESP_LOGE(TAG, "Failed to find OTA partition: %s", partition_to_update); 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", ESP_LOGI(TAG, "Gonna write OTA Update in Partition: %s",
@ -47,11 +51,11 @@ void prepare_ota_update() {
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err));
esp_ota_abort(update_handle); esp_ota_abort(update_handle);
return; return -2;
} }
ESP_LOGI(TAG, "OTA update started successfully."); 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, 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) { size_t send_buffer_size) {
ESP_LOGI(TAG, "OTA Update Start Uart Command"); 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; int send_payload_len = 2;
send_payload_buffer[0] = 0xff;
int len = build_message(UART_OTA_START, send_payload_buffer, send_payload_len, int len = build_message(UART_OTA_START, send_payload_buffer, send_payload_len,
send_buffer, send_buffer_size); send_buffer, send_buffer_size);
if (len < 0) { 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 payload_len, uint8_t *send_payload_buffer,
size_t send_payload_buffer_size, uint8_t *send_buffer, size_t send_payload_buffer_size, uint8_t *send_buffer,
size_t send_buffer_size) { size_t send_buffer_size) {
ESP_LOGI(TAG, "OTA Update Payload Uart Command"); // 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); uint32_t write_len = MIN(UPDATE_PAYLOAD_SIZE, payload_len);
ESP_LOGI(TAG, "Writing Data to Update BUffer Sequence %d, writing Data %d", update_size += write_len;
sequenz_counter, write_len); if (update_buffer_write_index > UPDATE_BUFFER_SIZE - write_len) {
memcpy(&update_buffer[update_buffer_write_index], payload, write_len); // ESP_LOGI(TAG, "Writing Data to Update BUffer Sequence %d, writing Data
update_buffer_write_index += write_len; // %d",
} else { // sequenz_counter, write_len);
ESP_LOGI(TAG, "Update Buffer full, writing it to OTA Update");
// write to ota // write to ota
esp_err_t err = esp_err_t err =
esp_ota_write(update_handle, update_buffer, update_buffer_write_index); 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++; sequenz_counter++;
} }
memcpy(&update_buffer[update_buffer_write_index], payload, write_len);
update_buffer_write_index += write_len;
size_t send_payload_len = 4; size_t send_payload_len = 4;
memcpy(send_payload_buffer, &sequenz_counter, 2); memcpy(send_payload_buffer, &sequenz_counter, 2);
memcpy(&send_payload_buffer[2], &update_buffer_write_index, 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, int len = build_message(UART_OTA_PAYLOAD, send_payload_buffer,
send_payload_len, send_buffer, send_buffer_size); 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_payload_buffer_size, uint8_t *send_buffer,
size_t send_buffer_size) { size_t send_buffer_size) {
ESP_LOGI(TAG, "OTA Update End Uart Command"); 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) { if (err != ESP_OK) {
ESP_LOGE(TAG, "GOT ESP ERROR WRITE OTA %d", err); 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, int len = build_message(UART_OTA_END, send_payload_buffer, send_payload_len,
send_buffer, send_buffer_size); send_buffer, send_buffer_size);
if (len < 0) { 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); uart_write_bytes(MASTER_UART, send_buffer, len);
vTaskPrioritySet(NULL, 1);
} }
void write_ota_update_from_uart_task(void *param) {} void write_ota_update_from_uart_task(void *param) {}

View File

@ -18,7 +18,7 @@ static const char *TAG = "ALOX - UART";
static QueueHandle_t parsed_message_queue; static QueueHandle_t parsed_message_queue;
void init_uart(QueueHandle_t msg_queue_handle) { 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, .data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE, .parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1, .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, void HandleMessageReceivedCallback(uint8_t msgid, const uint8_t *payload,
size_t payload_len) { 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); payload_len, payload);
ESP_LOG_BUFFER_HEX(TAG, payload, payload_len); ESP_LOG_BUFFER_HEX(TAG, payload, payload_len);*/
ParsedMessage_t msg_to_send; ParsedMessage_t msg_to_send;
msg_to_send.msgid = msgid; msg_to_send.msgid = msgid;