I'm fetching firmware file from Web Server for OTA using GSM Module Cavli C16Qs. Cavli C16Qs is Connected with ESP32 via ESP Serial1.
I Configure HTTP Connection with cavli C16Qs to server using AT COMMAND. I also able to download file from server. Esp32 is getting file from ATSerial(ESP32 Serial1). My Firmware file size is 250KB. So i need to append that file. I tried but File is corrupted.
The size of the file being appended is the same as my BIN file. But when I am read that file, the data in that file is corrupted.
If any normal words are appended or used in read, they are being appended correctly. But when I try to append my firmware file it is not appending correctly. it is corrupted.
I'm use Partition Scheme: 16MB flash (3MB APP/9.9MB FATFS) for Append file.
I'm append file using FFat file system, And 16mb file partition.
so basically Issue why I'm not able to append data of file properly?
- #include "FS.h"
- #include "FFat.h"
- #include <freertos/FreeRTOS.h>
- #include <freertos/task.h>
- #include <freertos/queue.h>
- #include "soc/timer_group_struct.h"
- #include "soc/timer_group_reg.h"
- #define BUFFER_SIZE 256
- #define QUEUE_SIZE 300
- unsigned long lastSerial1Time = 0;
- QueueHandle_t QueueHandle;
- File FF;
- struct DataPacket {
- uint8_t data[BUFFER_SIZE];
- size_t bytesRead;
- };
- bool packet_start = false;
- String currentSTR = "";
- #define FORMAT_FFAT true
- void dataprocesa(void *pvParameters);
- void TaskReadFromSerial(void *pvParameters);
- void checkSPIFFSFreeSpace();
- void listDir(fs::FS &fs, const char *dirname, uint8_t levels);
- void readFile(fs::FS &fs, const char *path);
- void feedTheDog();
- void checkSPIFFSFreeSpace() {
- size_t totalBytes = FFat.totalBytes();
- size_t usedBytes = FFat.usedBytes();
- size_t freeBytes = totalBytes - usedBytes;
- Serial.print("Total space: ");
- Serial.print(totalBytes);
- Serial.println(" bytes");
- Serial.print("Used space: ");
- Serial.print(usedBytes);
- Serial.println(" bytes");
- Serial.print("Free space: ");
- Serial.print(freeBytes);
- Serial.println(" bytes");
- }
- void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
- Serial.printf("Listing directory: %s\r\n", dirname);
- File root = fs.open(dirname);
- if (!root) {
- Serial.println("- failed to open directory");
- return;
- }
- if (!root.isDirectory()) {
- Serial.println(" - not a directory");
- return;
- }
- File file = root.openNextFile();
- while (file) {
- if (file.isDirectory()) {
- Serial.print(" DIR : ");
- Serial.println(file.name());
- if (levels) {
- listDir(fs, file.path(), levels - 1);
- }
- } else {
- Serial.print(" FILE: ");
- Serial.print(file.name());
- Serial.print("\tSIZE: ");
- Serial.println(file.size());
- }
- file = root.openNextFile();
- }
- }
- void readFile(fs::FS &fs, const char *path) {
- Serial.printf("Reading file: %s\r\n", path);
- File file = fs.open(path);
- if (!file || file.isDirectory()) {
- Serial.println("- failed to open file for reading");
- return;
- }
- Serial.println("- read from file:");
- while (file.available()) {
- Serial.write(file.read());
- }
- file.close();
- }
- void feedTheDog() {
- // feed dog 0
- TIMERG0.wdt_wprotect = TIMG_WDT_WKEY_VALUE; // write enable
- TIMERG0.wdt_feed = 1; // feed dog
- TIMERG0.wdt_wprotect = 0; // write protect
- // feed dog 1
- TIMERG1.wdt_wprotect = TIMG_WDT_WKEY_VALUE; // write enable
- TIMERG1.wdt_feed = 1; // feed dog
- TIMERG1.wdt_wprotect = 0; // write protect
- }
- void setup() {
- pinMode(PWRKEY, OUTPUT);
- digitalWrite(PWRKEY, LOW);
- delay(550);
- digitalWrite(PWRKEY, HIGH);
- pinMode(RST_PIL, OUTPUT);
- digitalWrite(RST_PIL, LOW);
- delay(150);
- digitalWrite(RST_PIL, HIGH);
- Serial.begin(230400);
- Serial1.begin(230400, SERIAL_8N1, ESP_GPIO_RX, ESP_GPIO_TX);
- pinMode(2, OUTPUT);
- digitalWrite(2, LOW);
- delay(500);
- digitalWrite(2, HIGH);
- if (FORMAT_FFAT) {
- FFat.format();
- }
- if (!FFat.begin(true)) { // Format SPIFFS if mount fails
- Serial.println("SPIFFS Mount Failed");
- return;
- }
- checkSPIFFSFreeSpace();
- // listDir(FFat, "/", 0);
- Serial.println("Setup complete");
- Serial.println("Start Configuration of Task");
- QueueHandle = xQueueCreate(QUEUE_SIZE, sizeof(DataPacket));
- if (QueueHandle == NULL) {
- Serial.println("Queue could not be created. Halt.");
- while (1) delay(1000);
- }
- xTaskCreatePinnedToCore(
- dataprocesa, "Task Write To Serial",
- 8192, NULL, 1, NULL, 0);
- xTaskCreatePinnedToCore(
- TaskReadFromSerial, "Task Read From Serial",
- 8192, NULL, 2, NULL, 1);
- }
- void loop() {
- delay(1000);
- }
- void dataprocesa(void *pvParameters) {
- DataPacket receivedBuffer;
- int datalen = 0;
- FF = FFat.open("/file.bin", FILE_WRITE);
- if (!FF) {
- Serial.println("Failed to open file for appending");
- return;
- }
- for (;;) {
- if (QueueHandle != NULL) {
- if (xQueueReceive(QueueHandle, &receivedBuffer, portMAX_DELAY) == pdPASS) {
- FF.write(receivedBuffer.data, receivedBuffer.bytesRead);
- } else {
- Serial.println("The `dataprocesa` was unable to receive data from the Queue");
- }
- }
- }
- }
- void TaskReadFromSerial(void *pvParameters) {
- int pcktlen = 0;
- DataPacket dataBuffer;
- for (;;) {
- feedTheDog();
- // Read one byte at a time from Serial1
- while (Serial1.available()) {
- String header = Serial1.readStringUntil('\n');
- if (header.startsWith("+CIPRECEIVE:")) {
- pcktlen = header.substring(header.lastIndexOf(',') + 1).toInt();
- while (pcktlen > 0) {
- dataBuffer.bytesRead = Serial1.readBytes(dataBuffer.data, min(BUFFER_SIZE, pcktlen));
- pcktlen -= dataBuffer.bytesRead;
- int ret = xQueueSend(QueueHandle, &dataBuffer, portMAX_DELAY);
- if (ret != pdTRUE) {
- Serial.println("The `TaskReadFromSerial` was unable to send data into the Queue");
- }
- }
- }
- lastSerial1Time = millis();
- }
- // Echo any user input from Serial to Cavli module via Serial1
- while (Serial.available()) {
- char c = Serial.read();
- Serial1.write(c);
- }
- // Check for timeout to close the file
- if (millis() - lastSerial1Time >= 30000) {
- FF.close(); // Close the file
- delay(500);
- checkSPIFFSFreeSpace();
- Serial.println("File closed due to Serial1 timeout.");
- delay(1000);
- readFile(FFat, "/file.bin");
- }
- vTaskDelay(1);
- }
- }