Skip to content

Instantly share code, notes, and snippets.

@gemfarmer gemfarmer/SDFlash2.ino
Last active Mar 14, 2019

Embed
What would you like to do?
0000 0a08 f40e 0a08 0000 0500 0600 0501
0402 ea03 0000 0000 f80b 0a08 0948 0a49
08b5 8842 05d0 094a 8242 02d0 121a 00f0
9ffd 0748 074a 0021 121a 00f0 a4fd 0648
08bd 00bf 0003 0020 640e 0a08 6803 0020
6803 0020 d008 0020 d008 0020 70b5 00f0
09fb 074b 074c 1e46 e41a a408 0025 a542
04d0 56f8 2530 9847 0135 f8e7 70bd 00bf
240e 0a08 540e 0a08 00f0 12b8 08b5 00f0
1ff8 bde8 0840 00f0 afba 00f0 f9b8 0078
b0fa 80f0 4009 7047 0123 0370 7047 0000
08b5 00f0 c9f9 4ff4 1651 00f0 abf9 00f0
c3f9 bde8 0840 0149 00f0 4cbb 0c0c 0a08
08b5 00f0 b9f9 bde8 0840 0149 00f0 42bb
1d0c 0a08 00f0 98b8 08b5 024b 1b68 1b68
0193 08bd d801 0608 08b5 024b 1b68 5b68
0193 08bd d801 0608 08b5 024b 1b68 1b69
0193 08bd d801 0608 08b5 024b 1b68 5b69
0193 08bd d801 0608 08b5 024b 1b68 9b69
0193 08bd d801 0608 08b5 024b 1b68 db69
0193 08bd d801 0608 08b5 024b 1b68 1b6a
0193 08bd d801 0608 08b5 024b 1b68 9b69
0193 08bd c401 0608 08b5 024b 1b68 5b6a
0193 08bd c401 0608 08b5 024b 1b68 9b6a
0193 08bd c401 0608 08b5 024b 1b68 db6a
0193 08bd c401 0608 08b5 024b 1b68 1b6b
0193 08bd c401 0608 08b5 024b 1b68 5b6b
0193 08bd c401 0608 08b5 024b 1b68 1b6c
0193 08bd c401 0608 08b5 024b 1b68 1b6b
0193 08bd d001 0608 08b5 024b 1b68 db6e
0193 08bd b801 0608 08b5 024b 1b68 5b68
0193 08bd 9c01 0608 08b5 024b 1b68 5b69
0193 08bd 9c01 0608 08b5 024b 1b68 db69
0193 08bd b401 0608 08b5 024b 1b68 1b68
0193 08bd b001 0608 08b5 024b 1b68 db6b
0193 08bd cc01 0608 08b5 024b 1b68 1b69
0193 08bd c801 0608 08b5 024b 1b68 5b68
0193 08bd a401 0608 08b5 034b 1b68 d3f8
9030 0193 08bd 0000 a401 0608 08b5 034b
1b68 d3f8 9c30 0193 08bd 0000 a401 0608
08b5 024b 1b68 1b68 0193 08bd a001 0608
08b5 024b 1b68 5b68 0193 08bd a001 0608
7047 0022 1146 1046 fff7 c6bf 0348 044b
044a 0549 0360 00f0 ffba 00bf 6c03 0020
280c 0a08 6803 0020 a102 0a08 7047 4174
7047 007c fff7 70bf 007c fff7 4dbf 007c
fff7 5abf 007c fff7 4fbf 007c fff7 5cbf
10b5 0446 fff7 d1fe 2046 10bd 70b5 457c
0446 0e46 25b9 007c fff7 56ff 0028 05dd
3146 207c bde8 7040 fff7 26bf 2846 70bd
10b5 0446 0846 1146 0022 6260 4ff4 7a72
a260 054a 2074 2260 0122 6274 1a46 fff7
0bff 2046 10bd 00bf 340c 0a08 38b5 0c4d
2968 11f0 0104 11d1 2846 fff7 a0fe 68b1
084a 2146 084b 0948 fff7 daff 2846 fff7
9bfe 074a 0749 0548 00f0 9efa 0348 38bd
0804 0020 0c04 0020 8403 0020 7003 0020
6803 0020 cd02 0a08 4174 7047 7047 0021
007c fff7 c1be 0121 007c fff7 bdbe 007c
fff7 b2be 007c fff7 a7be 38b5 0446 007c
0d46 fff7 a9fe 0028 01dc 607c 20b1 2946
207c fff7 b1fe 0120 38bd 007c fff7 b4be
10b5 0446 fff7 59fe 2046 10bd 10b5 0446
0023 6360 4ff4 7a73 a360 054b 0846 2360
0123 2174 6374 1146 fff7 6efe 2046 10bd
640c 0a08 0022 007c fff7 6ebe 10b5 0446
1422 0021 00f0 affb 054b 2046 6360 054b
e360 40f2 0113 2381 8123 2382 10bd 00bf
1505 0020 9404 0020 30b5 0e4d 87b0 01a8
fff7 e4ff 2968 11f0 0104 10d1 2846 fff7
1efe 60b1 01aa 2146 0748 fff7 bfff 2846
fff7 1afe 054a 0649 0348 00f0 1dfa 0248
07b0 30bd 9004 0020 1806 0020 6803 0020
9d03 0a08 10b5 0446 1422 0021 00f0 73fb
054b 2046 6360 054b e360 40f2 0113 2381
8123 2382 10bd 00bf c806 0020 3006 0020
10b5 0e4c 86b0 01a8 fff7 e4ff 2368 db07
10d4 2046 fff7 e3fd 60b1 01aa 0121 0848
fff7 84ff 2046 fff7 dffd 064a 0649 0448
00f0 e2f9 0248 06b0 10bd 00bf 2c06 0020
b406 0020 6803 0020 9d03 0a08 7047 10b5
0446 fff7 c2fd 2046 10bd f8b5 0f46 00f1
0806 051d 0024 16f8 011d 0a22 3846 00f0
4af9 ae42 0444 07d0 002c f4d0 2e21 3846
00f0 14f9 0444 eee7 2046 f8bd 10b5 9df8
0840 44ea 0323 43ea 0242 42ea 0161 0423
4160 0375 10bd 0000 37b5 044d 0560 9df8
1850 0095 fff7 eaff 03b0 30bd 940c 0a08
10b5 8368 0446 13b1 0322 0146 9847 2046
10bd 0000 08b5 144b 33b1 fff7 55ff 0368
1b69 9847 0028 16dc 104b 33b1 fff7 cefe
0368 1b69 9847 0028 10dc 0d4b 0bb1 aff3
0080 0c4b 7bb1 fff7 7bff 0368 1b69 9847
0028 06dc 08bd aff3 0080 e5e7 aff3 0080
ebe7 aff3 0080 08bd 0000 0000 0000 0000
0000 0000 0000 0000 08b5 fff7 cbff fff7
fbfd 014b 1860 08bd 0c08 0020 0248 034a
0349 121a 00f0 acba 0040 0240 0440 0240
600e 0a08 07b5 0023 0093 1a46 6ff0 7701
fff7 1cfe 03b0 5df8 04fb 0000 07b5 4388
0a2b 08d0 502b 09d1 094b 1b68 3bb1 03b0
5df8 04eb 1847 fff7 e5ff 06e0 0023 0093
1a46 6ff0 7701 fff7 01fe 03b0 5df8 04fb
cc07 0020 38b5 104c 104d 2368 ab42 104b
0cbf 0122 0022 1a70 02d0 fff7 bfff 2560
fff7 aafd 00f0 80fa 0a4b 1bb1 fff7 a4fd
aff3 0080 0021 0748 fff7 befd bde8 3840
0021 0548 fff7 d0bd 0040 0240 1e1c 279a
d007 0020 0000 0000 3d06 0a08 10b5 0446
fff7 56ff 2046 10bd 0348 0023 8360 034a
0349 00f0 e9b8 00bf d407 0020 6803 0020
cd06 0a08 024b 1a68 024b 1a60 7047 00bf
0003 0020 e407 0020 014a 024b 1a60 7047
840d 0a08 e807 0020 f8b5 0646 0d46 8f18
0024 bd42 0cd0 3368 15f8 011b 9b68 3046
9847 0028 01db 0444 f3e7 002c 08bf 0446
2046 f8bd 70b5 0546 0c46 0846 49b1 00f0
4bfa 2b68 0246 2146 2846 bde8 7040 db68
1847 70bd 08b5 fff7 edff 08bd 0368 9b68
1847 012a 98bf 0a22 30b5 0b46 8bb0 0021
8df8 2410 0df1 2304 b3fb f2f5 05fb 1233
dbb2 092b 94bf 3033 3733 dbb2 2146 04f8
0139 2b46 002d efd1 fff7 ccff 0bb0 30bd
10b4 22b9 0368 c9b2 9b68 10bc 1847 d2b2
10bc fff7 d6bf fff7 f3bf 0000 4ff6 9c73
37b5 0d4c 0025 6380 4ff6 ff73 2846 a380
2570 fff7 31fd 02a8 40f8 045d 2a46 2946
fff7 f2fc 019b 1e2b 04d1 0323 2370 1123
6580 a380 03b0 30bd ec07 0020 7047 10b5
0446 fff7 42fc 2046 10bd 0000 70b5 0446
0e46 074b 0025 0360 40f8 0c5f fff7 ccfc
3046 2671 fff7 e8fc a560 2046 70bd 00bf
b80d 0a08 13b5 084c 0023 1a46 1946 0093
2046 fff7 89fe 2046 044a 0549 02b0 bde8
1040 00f0 29b8 00bf f407 0020 6803 0020
0d05 0a08 70b5 0c4c 0c4e 0d4d 0021 2046
fff7 ccff 2046 0b4c 3246 2946 00f0 14f8
0121 2046 fff7 c2ff 3246 2946 2046 bde8
7040 00f0 09b8 00bf 1008 0020 6803 0020
0d08 0a08 2008 0020 0b46 0146 1846 00f0
49b9 0000 184b 1a68 d107 5cbf 0122 1a60
164b 1a68 d207 5cbf 0122 1a60 144b 1a68
d007 5cbf 0122 1a60 124b 1a68 d107 5cbf
0122 1a60 104b 1a68 d207 5cbf 0122 1a60
0e4b 1a68 d007 5cbf 0122 1a60 0c4b 1a68
d107 5cbf 0122 1a60 0a4b 1a68 d207 5cbf
0122 1a60 7047 00bf 4c08 0020 4808 0020
4408 0020 4008 0020 3c08 0020 3808 0020
3408 0020 3008 0020 244b 1a68 d007 5cbf
0122 1a60 224b 1a68 d107 5cbf 0122 1a60
204b 1a68 d207 5cbf 0122 1a60 1e4b 1a68
d007 5cbf 0122 1a60 1c4b 1a68 d107 5cbf
0122 1a60 1a4b 1a68 d207 5cbf 0122 1a60
184b 1a68 d007 5cbf 0122 1a60 164b 1a68
d107 5cbf 0122 1a60 144b 1a68 d207 5cbf
0122 1a60 124b 1a68 d007 5cbf 0122 1a60
104b 1a68 d107 5cbf 0122 1a60 0e4b 1a68
d207 5cbf 0122 1a60 7047 00bf 7c08 0020
7808 0020 7408 0020 7008 0020 6c08 0020
6808 0020 6408 0020 6008 0020 5c08 0020
5808 0020 5408 0020 5008 0020 244b 1a68
d007 5cbf 0122 1a60 224b 1a68 d107 5cbf
0122 1a60 204b 1a68 d207 5cbf 0122 1a60
1e4b 1a68 d007 5cbf 0122 1a60 1c4b 1a68
d107 5cbf 0122 1a60 1a4b 1a68 d207 5cbf
0122 1a60 184b 1a68 d007 5cbf 0122 1a60
164b 1a68 d107 5cbf 0122 1a60 144b 1a68
d207 5cbf 0122 1a60 124b 1a68 d007 5cbf
0122 1a60 104b 1a68 d107 5cbf 0122 1a60
0e4b 1a68 d207 5cbf 0122 1a60 7047 00bf
ac08 0020 a808 0020 a408 0020 a008 0020
9c08 0020 9808 0020 9408 0020 9008 0020
8c08 0020 8808 0020 8408 0020 8008 0020
184b 1a68 d107 5cbf 0122 1a60 164b 1a68
d207 5cbf 0122 1a60 144b 1a68 d007 5cbf
0122 1a60 124b 1a68 d107 5cbf 0122 1a60
104b 1a68 d207 5cbf 0122 1a60 0e4b 1a68
d007 5cbf 0122 1a60 0c4b 1a68 d107 5cbf
0122 1a60 0a4b 1a68 d207 5cbf 0122 1a60
7047 00bf cc08 0020 c808 0020 c408 0020
c008 0020 bc08 0020 b808 0020 b408 0020
b008 0020 10b5 054c 1346 2cb1 0a46 0146
0220 aff3 0080 10bd 2046 10bd 0000 0000
10b5 431e 0a44 9142 04d0 11f8 014b 03f8
014f f8e7 10bd 0346 0244 9342 02d0 03f8
011b fae7 7047 0000 38b5 124b 0546 1c68
a36b d3b9 1820 fff7 6bfb 43f2 0e33 a063
0380 4af6 cd33 4380 41f2 3423 8380 4ef2
6d63 c380 4df6 ec63 0381 0523 4381 0b23
8381 0122 0023 c0e9 0423 a36b 0022 1d61
5a61 38bd 6403 0020 0346 13f8 012b 002a
fbd1 181a 0138 7047 1d00 0a08 5d00 0a08
8900 0a08 8d00 0a08 0000 0000 4669 726d
7761 7265 2066 6c61 7368 6564 0021 0000
0000 0000 0000 0000 a302 0a08 0000 0000
0000 0000 cd02 0a08 f102 0a08 fd02 0a08
1907 0a08 d902 0a08 e502 0a08 df02 0a08
eb02 0a08 cf02 0a08 d302 0a08 0000 0000
0000 0000 9d03 0a08 e103 0a08 bb03 0a08
1907 0a08 b503 0a08 9f03 0a08 a703 0a08
db03 0a08 af03 0a08 9903 0a08 0000 0000
0000 0000 1b05 0a08 0d05 0a08 0f05 0a08
5a00 252b 3033 643a 2530 3275 0025 592d
256d 2d25 6454 2548 3a25 4d3a 2553 257a
0061 7363 7469 6d65 0066 006c 6e00 666e
0063 6f64 6500 6465 7461 696c 000d 0a00
2530 3130 7520 005d 2000 2564 002c 2000
2829 3a20 0020 5b00 636f 6465 203d 2000
2569 0064 6574 6169 6c73 203d 2000 6261
7564 0053 6572 6961 6c00 5553 4253 6572
6961 6c31 0074 7970 6500 7061 7261 6d00
636d 6400 6964 0068 6e64 0073 7472 6d00
6669 6c74 006c 766c 0061 6464 4861 6e64
6c65 7200 7265 6d6f 7665 4861 6e64 6c65
7200 656e 756d 4861 6e64 6c65 7273 004a
534f 4e53 7472 6561 6d4c 6f67 4861 6e64
6c65 7200 6170 7000 6e6f 6e65 0074 7261
6365 0069 6e66 6f00 7761 726e 0065 7272
6f72 0070 616e 6963 0061 6c6c 0000 0000
0000 0000 0000 0000 0d08 0a08 0f08 0a08
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
4300 0000 e500 0a08 ad02 0a08 d906 0a08
f506 0a08 0907 0a08 cd07 0a08 4508 0a08
7508 0a08 c508 0a08 4909 0a08 0d0a 0a08
d10a 0a08 0000 0000 0000 0000 0000 0000
0000 0000 c10c 0a08 0000 0000 c00d 0a08
e00d 0a08 000e 0a08 0000 0000 0000 0000
0000 0000 0000 0000 200e 0a08 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0403 0020 ffff ffff
0000 66d1 16fa 55f6 5905 0ed8 d01c a1fa
8ec7 987b 6704 59f4 460b f85e 694b c7d0
2632 2800 a0a1 e699
// This #include statement was automatically added by the Particle IDE.
#include "Particle.h"
#include "system_update.h"
int fileSize;
void poll();
void setupOpenLog(void);
void readFirmware(char *fileName);
void updateFirmwareFromFile(uint8_t* file_bytes);
void readDisk();
void gotoCommandMode(void);
int getFileSize(char *fileName);
struct sDCardStatus {
bool hasBeenSetup;
bool getFirmware;
};
sDCardStatus SDCardStatus = {0, 0};
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("I'm alive");
delay(1000);
setupOpenLog(); //Resets logger and waits for the '<' I'm alive character
Serial.println("OpenLog online");
}
void loop() {
poll();
SDCardStatus.getFirmware = true;
}
void poll() {
if (SDCardStatus.getFirmware == 1 ) {
//Now let's read back
gotoCommandMode(); //Puts OpenLog in command mode
readFirmware("conf_ph.bin");
//Now let's read back
readDisk(); //Check the size and stats of the SD card
Serial.println("Yay!");
}
}
//Setups up the software serial, resets OpenLog so we know what state it's in, and waits
//for OpenLog to come online and report '<' that it is ready to receive characters to record
void setupOpenLog(void) {
if (SDCardStatus.hasBeenSetup == 0) {
gotoCommandMode(); //Puts OpenLog in command mode
Serial1.print("reset");
Serial1.write(13); //This is \r
Serial.println("soft reset");
while(1) {
if(Serial1.available())
if(Serial1.read() == '<') break;
}
SDCardStatus.hasBeenSetup = true;
}
}
int getFileSize(char *fileName) {
//Old way
Serial1.print("size ");
Serial1.print(fileName);
Serial1.write(13); //This is \r
//The OpenLog echos the commands we send it by default so we have 'read log823.txt\r' sitting
//in the RX buffer. Let's try to not print this.
while(1) {
if(Serial1.available())
if(Serial1.read() == '\r') break;
}
Serial.write("size buffer cleared");
String totalString = "";
for(int timeOut = 0 ; timeOut < 1000 ; timeOut++) {
while(Serial1.available()) {
char tempString[100];
int spot = 0;
while(Serial1.available()) {
tempString[spot++] = Serial1.read();
if(spot > 98) break;
}
tempString[spot] = '\0';
Serial.write(tempString);
totalString += tempString;
timeOut = 0;
}
delay(1);
}
Serial.write("fileLength ");
Serial.write(totalString);
fileSize = totalString.toInt();
return fileSize;
}
//Reads the contents of a given file and dumps it to the serial terminal
//This function assumes the OpenLog is in command mode
void readFirmware(char *fileName) {
Serial.write("Read Firmware");
int sizeFile = getFileSize(fileName);
//Old way
Serial1.print("read ");
Serial1.print(fileName);
Serial1.write(13); //This is \r
//The OpenLog echos the commands we send it by default so we have 'read log823.txt\r' sitting
//in the RX buffer. Let's try to not print this.
while(1) {
if(Serial1.available())
if(Serial1.read() == '\r') break;
}
//This will listen for characters coming from OpenLog and print them to the terminal
//This relies heavily on the SoftSerial buffer not overrunning. This will probably not work
//above 38400bps.
//This loop will stop listening after 1 second of no characters received
char file_bytes[sizeFile];
for(int _timeOut = 0 ; _timeOut < 2000 ; _timeOut++) {
while(Serial1.available()) {
// char tempString[100];
int spot = 0;
while(Serial1.available()) {
file_bytes[spot++] = Serial1.read();
if(spot > sizeFile) break;
}
Serial.write(file_bytes);
_timeOut = 0;
}
delay(1);
}
uint8_t file_bytes_ = *file_bytes;
updateFirmwareFromFile(&file_bytes_);
}
void updateFirmwareFromFile(uint8_t* file_bytes) {
delay(5000);
FileTransfer::Descriptor file;
Serial.printlnf("starting flash size=%d", fileSize);
file.file_length = fileSize;
file.file_address = 0; // Automatically set to HAL_OTA_FlashAddress if store is FIRMWARE
file.chunk_address = 0;
file.chunk_size = 0; // use default
file.store = FileTransfer::Store::FIRMWARE;
int result = Spark_Prepare_For_Firmware_Update(file, 0, NULL);
if (result != 0) {
Serial.printlnf("prepare failed %d", result);
return;
}
Serial.printlnf("chunk_size=%d file_address=0x%x", file.chunk_size, file.file_address);
if (file.chunk_size == 0) {
file.chunk_size = 512;
}
// Note that Spark_Prepare_For_Firmware_Update sets file.file_address so it's not really zero here
// even though it's what we initialize it to above!
file.chunk_address = file.file_address;
size_t offset = 0;
while(offset < fileSize) {
if (file.chunk_size > (fileSize - offset)) {
file.chunk_size = (fileSize - offset);
}
Serial.printlnf("chunk_address=0x%x chunk_size=%d (%d < %d == %s)", file.chunk_address, file.chunk_size, offset, fileSize, (offset < fileSize) ? "continue" : "break");
result = Spark_Save_Firmware_Chunk(file, &file_bytes[offset], NULL);
if (result != 0) {
Serial.printlnf("save chunk failed %d", result);
return;
}
file.chunk_address += file.chunk_size;
offset += file.chunk_size;
}
result = Spark_Finish_Firmware_Update(file, 0x1, NULL);
if (result != 0) {
Serial.printlnf("finish failed %d", result);
return;
}
Serial.printlnf("update complete");
}
//Check the stats of the SD card via 'disk' command
//This function assumes the OpenLog is in command mode
void readDisk() {
//Old way
Serial1.print("disk");
Serial1.write(13); //This is \r
//New way
//OpenLog.print("read ");
//OpenLog.println(filename); //regular println works with OpenLog v2.51 and above
//The OpenLog echos the commands we send it by default so we have 'disk\r' sitting
//in the RX buffer. Let's try to not print this.
while(1) {
if(Serial1.available())
if(Serial1.read() == '\r') break;
}
//This will listen for characters coming from OpenLog and print them to the terminal
//This relies heavily on the SoftSerial buffer not overrunning. This will probably not work
//above 38400bps.
//This loop will stop listening after 1 second of no characters received
for(int timeOut = 0 ; timeOut < 1000 ; timeOut++) {
while(Serial1.available()) {
char tempString[100];
int spot = 0;
while(Serial1.available()) {
tempString[spot++] = Serial1.read();
if(spot > 98) break;
}
tempString[spot] = '\0';
Serial.write(tempString); //Take the string from OpenLog and push it to the Arduino terminal
timeOut = 0;
}
delay(1);
}
//This is not perfect. The above loop will print the '.'s from the log file. These are the two escape characters
//recorded before the third escape character is seen.
//It will also print the '>' character. This is the OpenLog telling us it is done reading the file.
//This function leaves OpenLog in command mode
}
//This function pushes OpenLog into command mode
void gotoCommandMode(void) {
//Send three control z to enter OpenLog command mode
//Works with Arduino v1.0
Serial1.write(26);
Serial1.write(26);
Serial1.write(26);
Serial1.write(13); //This is \r
Serial.println("go to command mode");
//Wait for OpenLog to respond with '>' to indicate we are in command mode
while(1) {
if(Serial1.available())
if(Serial1.read() == '>') break;
}
Serial.println("in command mode");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.