Skip to content

Commit e2a7d8f

Browse files
committed
Add OTA routine for UNO WiFi rev2
1 parent 1f5709b commit e2a7d8f

File tree

1 file changed

+82
-1
lines changed

1 file changed

+82
-1
lines changed

main/CommandHandler.cpp

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,87 @@ int deleteFile(const uint8_t command[], uint8_t response[]) {
10591059
return 0;
10601060
}
10611061

1062+
#include <driver/uart.h>
1063+
1064+
int applyOTA(const uint8_t command[], uint8_t response[]) {
1065+
#ifdef UNO_WIFI_REV2
1066+
1067+
const char* filename = "/fs/UPDATE.BIN";
1068+
FILE* updateFile = fopen(filename, "rb");
1069+
1070+
// init uart and write update to 4809
1071+
uart_config_t uart_config;
1072+
1073+
uart_config.baud_rate = 115200;
1074+
uart_config.data_bits = UART_DATA_8_BITS;
1075+
uart_config.parity = UART_PARITY_DISABLE;
1076+
uart_config.stop_bits = UART_STOP_BITS_1;
1077+
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
1078+
uart_config.rx_flow_ctrl_thresh = 122;
1079+
uart_config.use_ref_tick = true;
1080+
1081+
uart_param_config(UART_NUM_1, &uart_config);
1082+
1083+
uart_set_pin(UART_NUM_1,
1084+
1, // tx
1085+
3, // rx
1086+
UART_PIN_NO_CHANGE, // rts
1087+
UART_PIN_NO_CHANGE); //cts
1088+
1089+
uart_driver_install(UART_NUM_1, 1024, 0, 20, NULL, 0);
1090+
1091+
struct stat st;
1092+
stat(filename, &st);
1093+
1094+
int retries = 0;
1095+
1096+
size_t remaining = st.st_size % 1024;
1097+
for (int i=0; i<st.st_size; i++) {
1098+
uint8_t c;
1099+
uint8_t d;
1100+
1101+
fread(&c, 1, 1, updateFile);
1102+
retries = 0;
1103+
while (retries == 0 || (c != d && retries < 100)) {
1104+
uart_write_bytes(UART_NUM_1, (const char*)&c, 1);
1105+
uart_read_bytes(UART_NUM_1, &d, 1, 10);
1106+
retries++;
1107+
}
1108+
if (retries >= 100) {
1109+
goto exit;
1110+
}
1111+
}
1112+
// send remaining bytes (to reach page size) as 0xFF
1113+
for (int i=0; i<remaining + 10; i++) {
1114+
uint8_t c = 0xFF;
1115+
uint8_t d;
1116+
retries = 0;
1117+
while (retries == 0 || (c != d && retries < 100)) {
1118+
uart_write_bytes(UART_NUM_1, (const char*)&c, 1);
1119+
uart_read_bytes(UART_NUM_1, &d, 1, 10);
1120+
retries++;
1121+
}
1122+
}
1123+
1124+
// delay a bit before restarting, in case the flashing isn't yet over
1125+
delay(200);
1126+
1127+
pinMode(19, OUTPUT);
1128+
digitalWrite(19, HIGH);
1129+
delay(200);
1130+
digitalWrite(19, LOW);
1131+
pinMode(19, INPUT);
1132+
1133+
exit:
1134+
fclose(updateFile);
1135+
unlink(filename);
1136+
1137+
return 0;
1138+
#else
1139+
return 0;
1140+
#endif
1141+
}
1142+
10621143
int existsFile(const uint8_t command[], uint8_t response[]) {
10631144
char filename[32 + 1];
10641145
size_t len;
@@ -1125,7 +1206,7 @@ const CommandHandlerType commandHandlers[] = {
11251206
setPinMode, setDigitalWrite, setAnalogWrite, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
11261207

11271208
// 0x60 -> 0x6f
1128-
writeFile, readFile, deleteFile, existsFile, downloadFile, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1209+
writeFile, readFile, deleteFile, existsFile, downloadFile, applyOTA, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
11291210
};
11301211

11311212
#define NUM_COMMAND_HANDLERS (sizeof(commandHandlers) / sizeof(commandHandlers[0]))

0 commit comments

Comments
 (0)