diff --git a/components/esp_ota/CMakeLists.txt b/components/esp_ota/CMakeLists.txt new file mode 100644 index 0000000..dad7834 --- /dev/null +++ b/components/esp_ota/CMakeLists.txt @@ -0,0 +1,5 @@ +idf_component_register(SRCS "esp_ota.c" + INCLUDE_DIRS "." + REQUIRES driver + REQUIRES app_update + ) diff --git a/components/esp_ota/esp_ota.c b/components/esp_ota/esp_ota.c new file mode 100644 index 0000000..94bddf6 --- /dev/null +++ b/components/esp_ota/esp_ota.c @@ -0,0 +1,109 @@ +#include "esp_ota.h" +static const char *TAG = "OTA"; + +static void __attribute__((noreturn)) task_fatal_error(void) +{ + ESP_LOGE(TAG, "Exiting task due to fatal error..."); + (void)vTaskDelete(NULL); + + while (1) { + ; + } +} + +void Update_App_error(esp_ota_handle_t update_handle) +{ + esp_ota_abort(update_handle); + task_fatal_error(); +} + +void Update_App(uint8_t * buffer) +{ + + uint32_t binsize = 0; + binsize = (buffer[0] << 24) | (buffer[1]<<16) | (buffer[2]<<8) | (buffer[3]<<0); + ESP_LOGI(TAG, "buffer: %d %d %d %ld",buffer[0],buffer[1],buffer[2],binsize); + esp_err_t err; + /* update handle : set by esp_ota_begin(), must be freed via esp_ota_end() */ + esp_ota_handle_t update_handle = 0 ; + const esp_partition_t *update_partition = NULL; + + ESP_LOGI(TAG, "Starting OTA example"); + + const esp_partition_t *configured = esp_ota_get_boot_partition(); + const esp_partition_t *running = esp_ota_get_running_partition(); + + if (configured != running) { + ESP_LOGI(TAG, "Configured OTA boot partition at offset 0x%lx, but running from offset 0x%lx", + configured->address, running->address); + ESP_LOGW(TAG, "(This can happen if either the OTA boot data or preferred boot image become corrupted somehow.)"); + } + ESP_LOGI(TAG, "Running partition type %d subtype %d (offset 0x%ld)", + running->type, running->subtype, running->address); + + update_partition = esp_ota_get_next_update_partition(NULL); + assert(update_partition != NULL); + ESP_LOGI(TAG, "Writing to partition subtype %d at offset 0x%lx", + update_partition->subtype, update_partition->address); + + //esp_app_desc_t new_app_info; + esp_app_desc_t running_app_info; + if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) { + ESP_LOGI(TAG, "Running firmware version: %s", running_app_info.version); + } + + const esp_partition_t* last_invalid_app = esp_ota_get_last_invalid_partition(); + esp_app_desc_t invalid_app_info; + if (esp_ota_get_partition_description(last_invalid_app, &invalid_app_info) == ESP_OK) { + ESP_LOGI(TAG, "Last invalid firmware version: %s", invalid_app_info.version); + } + + uint8_t* code; + uint8_t buf[256]; + uint8_t index = 0; + uint32_t max_size = 0; + uint32_t checksum = 0; + uint32_t Rem_mem = 0; + uint16_t read_len = 256; + uint16_t i =0; + + ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size()); + + max_size = binsize; + err = esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_ota_begin failed (%s)", esp_err_to_name(err)); + esp_ota_abort(update_handle); + task_fatal_error(); + } + ESP_LOGI(TAG, "esp_ota_begin succeeded"); + + { + err = esp_ota_write( update_handle, (const void *)&buffer[4], max_size); + if (err != ESP_OK) { + esp_ota_abort(update_handle); + task_fatal_error(); + } + } + ESP_LOGI(TAG, "%ld %ld",binsize,max_size); + //free(code); + ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size()); + err = esp_ota_end(update_handle); + if (err != ESP_OK) { + if (err == ESP_ERR_OTA_VALIDATE_FAILED) { + ESP_LOGE(TAG, "Image validation failed, image is corrupted"); + } else { + ESP_LOGE(TAG, "esp_ota_end failed (%s)!", esp_err_to_name(err)); + } + task_fatal_error(); + } + + err = esp_ota_set_boot_partition(update_partition); + if (err != ESP_OK) { + ESP_LOGE(TAG, "esp_ota_set_boot_partition failed (%s)!", esp_err_to_name(err)); + + task_fatal_error(); + } + ESP_LOGI(TAG, "Prepare to restart system!"); + esp_restart(); +} \ No newline at end of file diff --git a/components/esp_ota/esp_ota.h b/components/esp_ota/esp_ota.h new file mode 100644 index 0000000..3319ce6 --- /dev/null +++ b/components/esp_ota/esp_ota.h @@ -0,0 +1,17 @@ +#ifndef ESP_OTA_H +#define ESP_OTA_H + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" +#include +#include +#include "esp_check.h" +#include +#include "esp_partition.h" +#include "esp_ota_ops.h" + +void Update_App(uint8_t * buffer); + +#endif + diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index ec5648c..897d3af 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -18,5 +18,4 @@ idf_component_register(SRCS "main.c" "Receiver.c" "flasher_tool.c" INCLUDE_DIRS "GUI/" INCLUDE_DIRS "file_system/" INCLUDE_DIRS "wifi/") - #spiffs_create_partition_image(storage spiffs_image FLASH_IN_PROJECT) diff --git a/main/Receiver.c b/main/Receiver.c index 54a0e6c..2add3d0 100644 --- a/main/Receiver.c +++ b/main/Receiver.c @@ -1,6 +1,8 @@ #include "public.h" #include "esp_log.h" #include "stream.h" +#include "tusb_cdc_acm.h" +#include "esp_ota.h" static const char *TAG = "RECV"; @@ -14,44 +16,15 @@ typedef struct void Connection_test(uint8_t *buffer); void Transfer_file(uint8_t *buffer); - +void Upgrade_app(uint8_t *buffer); const DECODERCMD Cmd_Receiver[] = { - {0x01,0x01,0x05,Connection_test}, - {0x02,0x01,0x03,Transfer_file}, + {0x01,0x01,5,Connection_test}, + {0x02,0x01,3,Transfer_file}, + {0x03,0x01,3,Upgrade_app}, {0,0,0,NULL}, }; -void HandleCmd(const DECODERCMD* pCmd,uint8_t ch) -{ - uint8_t i,j, result; - uint8_t PrnCmd[16]; - - result = 0; - for(i = 0; i < 256 ; i++) - { - if (pCmd[i].Funcode == 0) { - break; - } else if(pCmd[i].Funcode == ch) { - - if (pCmd[i].waitcount > 0) - { - if (WaitPrnBufBytes(&uart_interface_data,pCmd[i].waitcount, 1000, PrnCmd) != pCmd[i].waitcount) - { - result = 0; - } - } - if (pCmd[i].codefunc != NULL) - { - pCmd[i].codefunc(&PrnCmd[0]); - } - } - - } - - -} - // CRC16 (Modbus) calculation function uint16_t crc16_modbus(const uint8_t *data, uint16_t len) { uint16_t crc = 0xFFFF; @@ -86,6 +59,8 @@ void Report_transmission_progress(uint32_t file_size,uint32_t have_load) send_buff[6] = (uint8_t)(tran_schedule * 100); // usb_serial_jtag_write_bytes(&send_buff[0],(6 + data_len + 2),20 / portTICK_PERIOD_MS); // ESP_LOGI(TAG,"buff size:%d %.2f %d",send_buff[6],tran_schedule,data_len); + tinyusb_cdcacm_write_queue(0, &send_buff[0], (6 + data_len + 2)); + tinyusb_cdcacm_write_flush(0, 0); } @@ -107,6 +82,8 @@ void Connection_test(uint8_t *buffer) memcpy(&send_buff[6],&off_line_tool,sizeof(Device_info_t)); if (buffer[0] == 0x01) { // usb_serial_jtag_write_bytes(&send_buff[0],(6 + sizeof(Device_info_t) + 2),20 / portTICK_PERIOD_MS); + tinyusb_cdcacm_write_queue(0, &send_buff[0], (6 + sizeof(Device_info_t) + 2)); + tinyusb_cdcacm_write_flush(0, 0); } // ESP_LOGI(TAG,"buff size:%d %d",sizeof(Device_info_t),data_len); } @@ -131,7 +108,7 @@ void Transfer_file(uint8_t *buffer) char file_name[64]; // ESP_LOGI(TAG,"Transfer_file"); data_len = buffer[1] << 8 | buffer[2]; - ESP_LOGI(TAG,"data_len:%d",data_len); + // ESP_LOGI(TAG,"data_len:%d",data_len); data_buff = malloc(data_len +10); if(buffer[0] == 0x01) {//文件名 if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len) @@ -205,6 +182,93 @@ void Transfer_file(uint8_t *buffer) } +void Upgrade_app(uint8_t *buffer) +{ + uint16_t data_len = 0; + uint16_t crc_calc = 0xffff; + uint16_t crc_received = 0; + static uint32_t file_size = 0; + static uint32_t have_load = 0; + char* data_buff = NULL; + char file_name[64]; + // ESP_LOGI(TAG,"Upgrade_app"); + data_len = buffer[1] << 8 | buffer[2]; + data_buff = malloc(data_len + 10); + if(data_buff == NULL)return; + static uint8_t* updata; + if(buffer[0] == 0x01) { + //获取升级文件的大小 + if (WaitPrnBufBytes(&uart_interface_data,data_len,1000,(uint8_t*)&data_buff[0]) == data_len) + { + file_size = data_buff[0] | data_buff[1] << 8 | data_buff[2] << 16 | data_buff[3] << 24; + data_buff[data_len -1] = 0; + data_buff[data_len -2] = 0; + sprintf(&file_name[0],"/spiffs/%s",&data_buff[4]); + ESP_LOGI(TAG,"file name:%s file size:%ld",file_name,file_size); + if (updata == NULL) + { + updata = malloc(file_size+128); + updata[0] = data_buff[3]; + updata[1] = data_buff[2]; + updata[2] = data_buff[1]; + updata[3] = data_buff[0]; + have_load = 4; + if (updata == NULL) { + ESP_LOGE(TAG,"The upgrade failed because the memory was insufficient"); + } + } + + } + }else if(buffer[0] == 0x02) { + //获取升级文件的数据 + if (WaitPrnBufBytes(&uart_interface_data,data_len,3000,(uint8_t*)&data_buff[0]) == data_len) { + memcpy(&updata[have_load],&data_buff[2],(data_len - 4)); + have_load += data_len - 4; + Report_transmission_progress(file_size,have_load); + if(data_buff[0] == 0xff && data_buff[1] == 0xff) + { + have_load = 0; + ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]); + Update_App(&updata[0]); + } + // ESP_LOGI(TAG,"data_buff[0]:%X %X",data_buff[0],data_buff[1]); + crc_calc = crc16_modbus((uint8_t*)&data_buff[2],data_len-4); + } + } + +} + +void HandleCmd(const DECODERCMD* pCmd,uint8_t ch) +{ + uint8_t i,j, result; + uint8_t PrnCmd[16]; + + result = 0; + for(i = 0; i < 256 ; i++) + { + if (pCmd[i].Funcode == 0) { + break; + } else if(pCmd[i].Funcode == ch) { + + if (pCmd[i].waitcount > 0) + { + if (WaitPrnBufBytes(&uart_interface_data,pCmd[i].waitcount, 1000, PrnCmd) != pCmd[i].waitcount) + { + result = 0; + } + } + if (pCmd[i].codefunc != NULL) + { + pCmd[i].codefunc(&PrnCmd[0]); + } + } + + } + + +} + + void Read_CMD_task(void *pvParameter) { uint8_t ch; diff --git a/main/usb_serial_task.c b/main/usb_serial_task.c index 7c70fcb..eb058bb 100644 --- a/main/usb_serial_task.c +++ b/main/usb_serial_task.c @@ -25,15 +25,33 @@ void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event) /* read */ esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size); if (ret == ESP_OK) { - ESP_LOGI(TAG, "Data from channel %d:", itf); - ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_INFO); + // ESP_LOGI(TAG, "Data from channel %d:", itf); + // ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_INFO); + + if(frameStreamRecEmptyLen(&uart_interface_data) < 1024*4) + { + // ESP_LOGE(TAG,"frameStream Be almost full:%d",len); + frameStreamWrite(&uart_interface_data,&buf[0],rx_size); + while (1) + { + vTaskDelay(10/ portTICK_PERIOD_MS); + if(frameStreamRecEmptyLen(&uart_interface_data) > 1024*6) + { + // ESP_LOGE(TAG,"frameStream: %ld",frameStreamRecEmptyLen(&uart_interface_data)); + break; + } + } + }else{ + frameStreamWrite(&uart_interface_data,&buf[0],rx_size); + } + } else { ESP_LOGE(TAG, "Read error"); } /* write back */ - tinyusb_cdcacm_write_queue(itf, buf, rx_size); - tinyusb_cdcacm_write_flush(itf, 0); + // tinyusb_cdcacm_write_queue(itf, buf, rx_size); + // tinyusb_cdcacm_write_flush(itf, 0); } void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event) @@ -221,7 +239,7 @@ void usb_serial_task(void *pvParameters) CDC_EVENT_LINE_STATE_CHANGED, &tinyusb_cdc_line_state_changed_callback)); ESP_LOGI(TAG, "USB initialization DONE"); - + vTaskDelete(NULL); }