Skip to content

Commit

Permalink
Add OTA routine for UNO WiFi rev2
Browse files Browse the repository at this point in the history
  • Loading branch information
facchinm committed Aug 9, 2019
1 parent a567034 commit 0723542
Showing 1 changed file with 82 additions and 1 deletion.
83 changes: 82 additions & 1 deletion main/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,87 @@ int deleteFile(const uint8_t command[], uint8_t response[]) {
return 0;
}

#include <driver/uart.h>

int applyOTA(const uint8_t command[], uint8_t response[]) {
#ifdef UNO_WIFI_REV2

const char* filename = "/fs/UPDATE.BIN";
FILE* updateFile = fopen(filename, "rb");

// init uart and write update to 4809
uart_config_t uart_config;

uart_config.baud_rate = 115200;
uart_config.data_bits = UART_DATA_8_BITS;
uart_config.parity = UART_PARITY_DISABLE;
uart_config.stop_bits = UART_STOP_BITS_1;
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
uart_config.rx_flow_ctrl_thresh = 122;
uart_config.use_ref_tick = true;

uart_param_config(UART_NUM_1, &uart_config);

uart_set_pin(UART_NUM_1,
1, // tx
3, // rx
UART_PIN_NO_CHANGE, // rts
UART_PIN_NO_CHANGE); //cts

uart_driver_install(UART_NUM_1, 1024, 0, 20, NULL, 0);

struct stat st;
stat(filename, &st);

int retries = 0;

size_t remaining = st.st_size % 1024;
for (int i=0; i<st.st_size; i++) {
uint8_t c;
uint8_t d;

fread(&c, 1, 1, updateFile);
retries = 0;
while (retries == 0 || (c != d && retries < 100)) {
uart_write_bytes(UART_NUM_1, (const char*)&c, 1);
uart_read_bytes(UART_NUM_1, &d, 1, 10);
retries++;
}
if (retries >= 100) {
goto exit;
}
}
// send remaining bytes (to reach page size) as 0xFF
for (int i=0; i<remaining + 10; i++) {
uint8_t c = 0xFF;
uint8_t d;
retries = 0;
while (retries == 0 || (c != d && retries < 100)) {
uart_write_bytes(UART_NUM_1, (const char*)&c, 1);
uart_read_bytes(UART_NUM_1, &d, 1, 10);
retries++;
}
}

// delay a bit before restarting, in case the flashing isn't yet over
delay(200);

pinMode(19, OUTPUT);
digitalWrite(19, HIGH);
delay(200);
digitalWrite(19, LOW);
pinMode(19, INPUT);

exit:
fclose(updateFile);
unlink(filename);

return 0;
#else
return 0;
#endif
}

int existsFile(const uint8_t command[], uint8_t response[]) {
char filename[32 + 1];
size_t len;
Expand Down Expand Up @@ -1125,7 +1206,7 @@ const CommandHandlerType commandHandlers[] = {
setPinMode, setDigitalWrite, setAnalogWrite, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,

// 0x60 -> 0x6f
writeFile, readFile, deleteFile, existsFile, downloadFile, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
writeFile, readFile, deleteFile, existsFile, downloadFile, applyOTA, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
};

#define NUM_COMMAND_HANDLERS (sizeof(commandHandlers) / sizeof(commandHandlers[0]))
Expand Down

0 comments on commit 0723542

Please sign in to comment.