/
arduino.c
208 lines (191 loc) · 5.12 KB
/
arduino.c
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <esp8266.h>
#include "driver/uart.h"
#include "arduino.h"
static uint8 rcvBuffer[ARDUINO_PAGE_SIZE + 20];
static uint8 rcvBufferCounter = 0;
uint8 pageBuffer[ARDUINO_PAGE_SIZE];
static bool in_progress = false;
arduinoUpdateState_t arduinoUpdateState;
static ETSTimer commsTimeout;
void ICACHE_FLASH_ATTR arduinoCommsTimeout(){
arduinoUpdateState.state = IDLE;
rcvBufferCounter = 0;
}
void ICACHE_FLASH_ATTR setTimeout(){
//Schedule disconnect/connect
os_timer_disarm(&commsTimeout);
os_timer_setfn(&commsTimeout, arduinoCommsTimeout, NULL);
os_timer_arm(&commsTimeout, 500, 0);
}
void ICACHE_FLASH_ATTR arduinoConnect(){
if(arduinoUpdateState.state == CONNECT){
// Send the "connect" command
// '0 '
uart0_sendStr("0 ");
//Schedule another attempt if it's not ready yet
os_timer_disarm(&commsTimeout);
os_timer_setfn(&commsTimeout, arduinoConnect, NULL);
os_timer_arm(&commsTimeout, 300, 0);
}else{
os_timer_disarm(&commsTimeout);
}
}
void ICACHE_FLASH_ATTR arduinoLoadAddress(int addr){
// Send the "load address" command
// U
// addr >> 1 & 0xFF
// (addr >> 9) & 0xFF
// ' '
uint8 temp[] = "U\0\0 ";
temp[1] = (addr >> 1) & 0xFF;
temp[2] = (addr >> 9) & 0xFF;
uart0_tx_buffer(temp, 4);
setTimeout();
}
void ICACHE_FLASH_ATTR arduinoProgramPage(uint8 * data, int len){
// Send the "program page" command
// d
// len & 0xFF
// (len >> 8) & 0xFF
// 'F'
// data
// ' '
uint8 temp[] = "d\0\0F";
temp[1] = (len >> 8) & 0xFF;
temp[2] = len & 0xFF;
uart0_tx_buffer(temp, 4);
uart0_tx_buffer(data, len);
uart0_sendStr(" ");
setTimeout();
}
void ICACHE_FLASH_ATTR arduinoReadPage(int len){
// Send the "read page" command
// t
// len & 0xFF
// (len >> 8) & 0xFF
// 'F '
uint8 temp[] = "t\0\0F ";
temp[1] = (len >> 8) & 0xFF;
temp[2] = len & 0xFF;
uart0_tx_buffer(temp, 5);
setTimeout();
}
void ICACHE_FLASH_ATTR arduinoReset(){
// Causes a delay to reset the Arduino
for(int i=0; i<50; i++){
GPIO_OUTPUT_SET(GPIO_ID_PIN(5), 0);
}
GPIO_OUTPUT_SET(GPIO_ID_PIN(5), 1);
}
void ICACHE_FLASH_ATTR arduinoUpdate(){
int addr;
switch (arduinoUpdateState.state){
case CONNECT:
//connect to the arduino
arduinoConnect();
break;
case LOAD_PROG_ADDRESS:
//load address
addr = arduinoUpdateState.page * ARDUINO_PAGE_SIZE;
arduinoLoadAddress(addr);
break;
case PROGRAM_PAGE:
//program page
spi_flash_read(ARDUINO_FLASH_LOCATION + (ARDUINO_PAGE_SIZE * arduinoUpdateState.page), (uint32 *) &pageBuffer, ARDUINO_PAGE_SIZE);
arduinoProgramPage(pageBuffer, ARDUINO_PAGE_SIZE);
break;
case LOAD_READ_ADDRESS:
//load address
addr = arduinoUpdateState.page * ARDUINO_PAGE_SIZE;
arduinoLoadAddress(addr);
break;
case READ_PAGE:
//read page
arduinoReadPage(ARDUINO_PAGE_SIZE);
break;
case IDLE:
return;
}
}
bool ICACHE_FLASH_ATTR arduinoBeginUpdate(){
if(arduinoUpdateState.state == IDLE){
arduinoUpdateState.page = 0;
arduinoUpdateState.state = CONNECT;
in_progress = true;
//toggle the reset pin
arduinoReset();
//start the update process
arduinoUpdate();
return true;
}else{
return false;
}
}
char ICACHE_FLASH_ATTR arduinoGetStatus(){
// Return how many pages have been sent so far to track progress
return 0;
}
bool ICACHE_FLASH_ATTR arduinoUpdating(){
return arduinoUpdateState.state != IDLE;
}
int ICACHE_FLASH_ATTR arduinoPagesFlashed(){
return arduinoUpdateState.page;
}
bool ICACHE_FLASH_ATTR okMsg(){
return (rcvBufferCounter >= 2 && rcvBuffer[0] == 20 && rcvBuffer[1] == 16);
}
void ICACHE_FLASH_ATTR arduinoHandleData(uint8 incoming){
rcvBuffer[rcvBufferCounter++] = incoming;
// check if we have an appropriate response
switch (arduinoUpdateState.state){
case CONNECT:
if(okMsg()){
arduinoUpdateState.state = LOAD_PROG_ADDRESS;
rcvBufferCounter = 0;
arduinoUpdate();
}
break;
case LOAD_PROG_ADDRESS:
if(okMsg()){
arduinoUpdateState.state = PROGRAM_PAGE;
rcvBufferCounter = 0;
arduinoUpdate();
}
break;
case PROGRAM_PAGE:
if(okMsg()){
arduinoUpdateState.state = LOAD_READ_ADDRESS;
rcvBufferCounter = 0;
arduinoUpdate();
}
break;
case LOAD_READ_ADDRESS:
if(okMsg()){
arduinoUpdateState.state = READ_PAGE;
rcvBufferCounter = 0;
arduinoUpdate();
}
break;
case READ_PAGE:
if (rcvBufferCounter >= (2 + ARDUINO_PAGE_SIZE) && rcvBuffer[0] == 20 && rcvBuffer[1 + ARDUINO_PAGE_SIZE] == 16) {
//it's a full packet with a page worth of data, let's check it matches what's in the buffer we sent
if(memcmp(&rcvBuffer[1], &pageBuffer, ARDUINO_PAGE_SIZE) == 0){
// it is correct
if(++arduinoUpdateState.page >= ARDUINO_MAX_PAGE){
os_timer_disarm(&commsTimeout);
arduinoUpdateState.state = IDLE;
}else{
arduinoUpdateState.state = LOAD_PROG_ADDRESS;
rcvBufferCounter = 0;
arduinoUpdate();
}
}else{
os_timer_disarm(&commsTimeout);
arduinoUpdateState.state = IDLE;
}
}
break;
case IDLE:
return;
}
}