-
Notifications
You must be signed in to change notification settings - Fork 2
/
onbrightFlasher.cpp
287 lines (223 loc) · 7.39 KB
/
onbrightFlasher.cpp
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
OnbrightFlasher.cpp - implementation
Copyright (c) 2023 Jonathan Armstrong. All right reserved.
*/
// include this library's description file
#include "onbrightFlasher.h"
// choose whether to use SoftWire or Wire library
#include "projectDefs.h"
//
#if defined(USE_SOFTWIRE_LIBRARY) && defined(USE_WIRE_LIBRARY)
#error Please uncomment either USE_SOFTWIRE_LIBRARY or USE_WIRE_LIBRARY but not both.
#elif defined(USE_SOFTWIRE_LIBRARY)
// softwire I2C
#include <SoftWire.h>
extern SoftWire Wire;
#elif defined(USE_SOFTWAREWIRE_LIBRARY)
#include <SoftwareWire.h>
extern SoftwareWire Wire;
#elif defined (USE_WIRE_LIBRARY)
#include <Wire.h>
#endif
// Constructor /////////////////////////////////////////////////////////////////
// Function that handles the creation and setup of instances
// //FIXME: need to do anything here?
//OnbrightFlasher::OnbrightFlasher(void)
//{
//}
// Public Methods //////////////////////////////////////////////////////////////
// Functions available in Wiring sketches, this library, and other libraries
// Private Methods /////////////////////////////////////////////////////////////
// Functions only available to other functions in this library
bool OnbrightFlasher::onbrightHandshake(void)
{
// store result of i2c operation (success, ack, nack, timeout, etc.)
byte result;
unsigned int index;
// this is the only ack we take into account to decide success/failure
bool gotFirstAck = false;
// indicate an address write with no actual data write as per captured protocol
Wire.beginTransmission(DEVICE_ADDRESS);
result = Wire.endTransmission();
// we set maximum retries based on typical amount seen in traces
for (index = 0; index < MAX_HANDSHAKE_RETRIES; index++)
{
Wire.beginTransmission(RESET_CHIP);
result = Wire.endTransmission();
// at first we will receive nacks, however
// if we received ack, proceed with the rest of the handshake
if (result == 0)
{
Wire.beginTransmission(HANDSHAKE01);
result = Wire.endTransmission();
Wire.beginTransmission(HANDSHAKE02);
result = Wire.endTransmission();
// no actual read seems to be performed
Wire.requestFrom(DEVICE_ADDRESS, 1);
result = Wire.endTransmission();
//Serial.print("Retried times: ");
//Serial.println(index);
//Serial.println("Tried handshake 01+02+Read");
//Serial.print("Result: ");
//Serial.println(result);
// let calling function know we succeeded with handshake
gotFirstAck = true;
// break out of loop
index = MAX_HANDSHAKE_RETRIES;
}
}
return gotFirstAck;
}
// should be 0x0A for OnBright OBS38S003 8051 based microcontroller
byte OnbrightFlasher::readChipType(unsigned char& chipType)
{
// used to check for nack/ack, success, etc.
byte result;
//byte numBytes;
// check chip type
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.write(READ_CONFIG_BYTE);
Wire.write(CHIP_TYPE_BYTE);
result = Wire.endTransmission();
// this returns number of bytes so could use that
Wire.requestFrom(DATA_ADDRESS, 1);
while(Wire.available() > 0)
{
chipType = Wire.read();
}
return result;
}
void OnbrightFlasher::resetMCU(void)
{
// FIXME: reset command not able to return any indication of success/failure?
// for example, no ack or nack
//byte result = 0;
// we do not actually read anything
Wire.requestFrom(RESET_CHIP, 1);
//return result;
}
// apparently clock stretching is problematic
// relevant here?
// [https://learn.adafruit.com/working-with-i2c-devices/clock-stretching]
byte OnbrightFlasher::eraseChip(void)
{
byte result;
// FIXME: this works but is timing out on ESP32 with Wire library, why?
// erase chip
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.write(ERASE_CHIP);
result = Wire.endTransmission();
return result;
}
// common fuses as a check
// address zero should read 10 (0xA) which is the chip type
byte OnbrightFlasher::readConfigByte(const unsigned char address, unsigned char &configByte)
{
byte result;
//
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.write(READ_CONFIG_BYTE);
Wire.write(address);
result = Wire.endTransmission();
Wire.requestFrom(DATA_ADDRESS, 1);
while(Wire.available() > 0)
{
configByte = Wire.read();
}
return result;
}
byte OnbrightFlasher::writeConfigByte(const unsigned char address, const unsigned char configByte)
{
byte result;
unsigned int index;
// we save to write twice according to traces from official programmer
for (index = 0; index < 2; index++)
{
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.write(WRITE_CONFIG_BYTE);
Wire.write(address);
Wire.endTransmission();
Wire.beginTransmission(DATA_ADDRESS);
Wire.write(configByte);
result = Wire.endTransmission();
}
return result;
}
// TODO: might want something that works across entire block size in the future
// read single byte from flash
// there are 16 blocks * 512 bytes = 8192 bytes total flash memory
byte OnbrightFlasher::readFlashByte(const unsigned int flashAddress, unsigned char &flashByte)
{
const uint8_t highByte = (flashAddress >> 8) & 0xff;
const uint8_t lowByte = flashAddress & 0xff;
byte result;
//
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.write(READ_FLASH);
Wire.write(highByte);
Wire.write(lowByte);
result = Wire.endTransmission();
Wire.requestFrom(DATA_ADDRESS, 1);
while(Wire.available() > 0)
{
flashByte = Wire.read();
}
return result;
}
// TODO:
byte OnbrightFlasher::writeFlashByte(const unsigned int flashAddress, const unsigned char flashByte)
{
const uint8_t highByte = (flashAddress >> 8) & 0xff;
const uint8_t lowByte = flashAddress & 0xff;
byte result;
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.write(WRITE_FLASH);
Wire.write(highByte);
Wire.write(lowByte);
Wire.endTransmission();
Wire.beginTransmission(DATA_ADDRESS);
Wire.write(flashByte);
result = Wire.endTransmission();
return result;
}
byte OnbrightFlasher::readFlashBlock(const unsigned int flashAddress, unsigned char (&flashbyte)[], const unsigned int length)
{
byte result;
unsigned int currentAddress;
unsigned int index;
for (index = 0; index < length; index++)
{
currentAddress = flashAddress + index;
result = readFlashByte(currentAddress, flashbyte[index]);
//#if defined(ESP8266)
// ugly hack, try using yield() instead
//ESP.wdtFeed();
yield();
//#endif
}
return result;
}
byte OnbrightFlasher::writeFlashBlock(const unsigned int flashAddress, unsigned char* flashbyte, const unsigned int length)
{
byte result;
unsigned int currentAddress;
unsigned int index;
for (index = 0; index < length; index++)
{
currentAddress = flashAddress + index;
result = writeFlashByte(currentAddress, flashbyte[index]);
}
return result;
}
byte OnbrightFlasher::readConfigBlock(const unsigned char flashAddress, unsigned char (&flashbyte)[], const unsigned char length)
{
byte result;
unsigned int currentAddress;
unsigned int index;
for (index = 0; index < length; index++)
{
currentAddress = flashAddress + index;
result = readConfigByte(currentAddress, flashbyte[index]);
}
return result;
}