-
Notifications
You must be signed in to change notification settings - Fork 16
/
FirmwareUpdate.ino
146 lines (119 loc) · 2.7 KB
/
FirmwareUpdate.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* sakura.io firmware update example
*
* For detailed command information please see the datasheet.
* https://sakura.io/developer/pdf/sakura_module_datasheet_latest.pdf
*/
#include <SakuraIO.h>
//SakuraIO_SPI sakuraio(10);
SakuraIO_I2C sakuraio;
uint8_t updateFirmware();
void setup() {
pinMode(13, OUTPUT);
Serial.begin(9600);
delay(3000);
if(updateFirmware() == 0){
Serial.println("Update successfull");
}else{
Serial.println("Update failure");
}
}
uint8_t updateFirmware(){
uint8_t ret;
char version[33] = {0};
// Check module
uint16_t productId = sakuraio.getProductID();
switch(productId){
case 0x0001:
Serial.println("SCM-LTE-Beta");
break;
case 0x0002:
Serial.println("SCM-LTE-01");
break;
case 0x0003:
Serial.println("SCM-LTE-01 Rev.B");
break;
default:
Serial.println("Unknown Product ID");
break;
}
delay(1000);
// Waiting for online
Serial.println("Waiting for online");
while(1){
Serial.print('.');
Serial.flush();
delay(2000);
if((sakuraio.getConnectionStatus() & 0x80) != 0x00){
break;
}
}
Serial.println("");
Serial.println("Online");
Serial.println("Get current version");
ret = sakuraio.getFirmwareVersion(version);
if((ret = sakuraio.getFirmwareVersion(version)) != CMD_ERROR_NONE){
Serial.print("Error code=");
Serial.println(ret);
return 1;
}
Serial.print("Current: ");
Serial.println(version);
delay(1000);
// Request unlock
Serial.println("Unlock");
if((ret = sakuraio.unlock()) != CMD_ERROR_NONE){
Serial.print("Error code=");
Serial.println(ret);
return 1;
}
delay(1000);
// Request firmware update
Serial.println("Starting update");
if((ret = sakuraio.updateFirmware()) != CMD_ERROR_NONE){
Serial.print("Error code=");
Serial.println(ret);
return 1;
}
delay(1000);
// Check update status
uint8_t errCode = 0x00;
Serial.println("Waiting for update");
while(1){
delay(1000);
Serial.print('.');
Serial.flush();
uint8_t updateStatus = sakuraio.getFirmwareUpdateStatus();
if(updateStatus == 0xff || (updateStatus & 0x80) != 0x00){
continue;
}else if(updateStatus == 0x00){
// Success
break;
}else{
// Error
errCode = updateStatus & 0x3f;
break;
}
}
Serial.println("");
switch(errCode){
case 0x00:
return 0;
break;
case 0x01:
Serial.println("Already updated");
return 0;
break;
default:
Serial.print("Error code=");
Serial.println(errCode);
break;
}
return 1;
}
void loop() {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}