-
Notifications
You must be signed in to change notification settings - Fork 1
/
Eve_BT81x_Flash.ino
365 lines (312 loc) · 10.1 KB
/
Eve_BT81x_Flash.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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <stdlib.h>
#include "Eve2_81x.h"
#include "MatrixEve2Conf.h" // Header for EVE Display configuration settings
#include "process.h"
#include "Arduino_AL.h"
File myFile;
char LogBuf[WorkBuffSz];
void setup()
{
uint8_t FlashStatus;
// MyDelay(3000); // This is just here to give us time to power cycle after a code update
// Initializations. Order is important
GlobalInit();
FT81x_Init();
SD_Init();
FlashAttach(); // Attach flash
FlashLoad(); // Conditionally copy output.bin to Eve attached flash
if(!FlashFast()) // Set flash to fast mode - QSPI
{
// Let's say that if fast mode fails, then probably the flash is corrupted so let's reload flash
if(SD.exists("transfer.fin"))
{
SD.remove("transfer.fin");
MyDelay(50);
}
FlashLoad(); // Copy output.bin to Eve attached flash
}
wr8(REG_PWM_DUTY + RAM_REG, 128); // set backlight
if ( FlashGetFileParms() ) // Stuff the file list buffer from flash
{
uint8_t Index = 2; // The first two files that we parse are always the blob and the map
while(1) // main loop which actually loops unlike loop() which does not.
{
MakeScreen_Bitmap(Index++);
if(Index > 7)
Index = 2;
MyDelay(5000);
}
}
else
{
Log("Bad flash parsing");
while(1); // Can not continue if the flash is not right (unless this is a thing and you could reload here potentially)
}
}
// ************************************************************************************
// Following are wrapper functions for C++ Arduino functions so that they may be *
// called from outside of C++ files. These are also your opportunity to use a common *
// name for your hardware functions - no matter the hardware. In Arduino-world you *
// interact with hardware using Arduino built-in functions which are all C++ and so *
// your "abstraction layer" must live in this xxx.ino file where C++ works. *
// *
// This is also an alternative to ifdef-elif hell. A different target *
// processor or compiler will include different files for hardware abstraction, but *
// the core "library" files remain unaltered - and clean. Applications built on top *
// of the libraries need not know which processor or compiler they are running / *
// compiling on (in general and within reason) *
// ************************************************************************************
void GlobalInit(void)
{
Wire.begin(); // Setup I2C bus
Serial.begin(115200); // Setup serial port for debug
while (!Serial) {;} // Wait for serial port to connect.
// Matrix Orbital Eve display interface initialization
pinMode(EvePDN_PIN, OUTPUT); // Pin setup as output for Eve PDN pin.
SetPin(EvePDN_PIN, 0); // Apply a resetish condition on Eve
pinMode(EveChipSelect_PIN, OUTPUT); // SPI CS Initialization
SetPin(EveChipSelect_PIN, 1); // Deselect Eve
pinMode(EveAudioEnable_PIN, OUTPUT); // Audio Enable PIN
SetPin(EveAudioEnable_PIN, 0); // Disable Audio
SPI.begin(); // Enable SPI
// Log("Startup\n");
}
// Send a single byte through SPI
void SPI_WriteByte(uint8_t data)
{
SPI.beginTransaction(SPISettings(SPISpeed, MSBFIRST, SPI_MODE0));
digitalWrite(EveChipSelect_PIN, LOW);
SPI.transfer(data);
digitalWrite(EveChipSelect_PIN, HIGH);
SPI.endTransaction();
}
// Send a series of bytes (contents of a buffer) through SPI
void SPI_WriteBuffer(uint8_t *Buffer, uint32_t Length)
{
SPI.beginTransaction(SPISettings(SPISpeed, MSBFIRST, SPI_MODE0));
digitalWrite(EveChipSelect_PIN, LOW);
SPI.transfer(Buffer, Length);
digitalWrite(EveChipSelect_PIN, HIGH);
SPI.endTransaction();
}
// Send a byte through SPI as part of a larger transmission. Does not enable/disable SPI CS
void SPI_Write(uint8_t data)
{
// Log("W-0x%02x\n", data);
SPI.transfer(data);
}
// Read a series of bytes from SPI and store them in a buffer
void SPI_ReadBuffer(uint8_t *Buffer, uint32_t Length)
{
uint8_t a = SPI.transfer(0x00); // dummy read
while (Length--)
{
*(Buffer++) = SPI.transfer(0x00);
}
}
// Enable SPI by activating chip select line
void SPI_Enable(void)
{
SPI.beginTransaction(SPISettings(SPISpeed, MSBFIRST, SPI_MODE0));
digitalWrite(EveChipSelect_PIN, LOW);
}
// Disable SPI by deasserting the chip select line
void SPI_Disable(void)
{
digitalWrite(EveChipSelect_PIN, HIGH);
SPI.endTransaction();
}
void Eve_Reset_HW(void)
{
// Reset Eve
SetPin(EvePDN_PIN, 0); // Set the Eve PDN pin low
MyDelay(50); // delay
SetPin(EvePDN_PIN, 1); // Set the Eve PDN pin high
MyDelay(100); // delay
}
void DebugPrint(char *str)
{
Serial.print(str);
}
// A millisecond delay wrapper for the Arduino function
void MyDelay(uint32_t DLY)
{
uint32_t wait;
wait = millis() + DLY; while(millis() < wait);
}
// Externally accessible abstraction for millis()
uint32_t MyMillis(void)
{
return millis();
}
// An abstracted pin write that may be called from outside this file.
void SetPin(uint8_t pin, bool state)
{
digitalWrite(pin, state);
}
// An abstracted pin read that may be called from outside this file.
uint8_t ReadPin(uint8_t pin)
{
return(digitalRead(pin));
}
//================================== SD Card Functions ====================================
void SD_Init(void)
{
// Log("Initializing SD card...\n");
if (!SD.begin(SDChipSelect_PIN))
{
Log("SD initialization failed!\n");
return;
}
// Log("SD initialization done\n");
}
// Read the touch digitizer calibration matrix values from the Eve and write them to a file
void SaveTouchMatrix(void)
{
uint8_t count = 0;
uint32_t data;
uint32_t address = REG_TOUCH_TRANSFORM_A + RAM_REG;
// Log("Enter SaveTouchMatrix\n");
// If the file exists already from previous run, then delete it.
if(SD.exists("tmatrix.txt"))
{
SD.remove("tmatrix.txt");
MyDelay(50);
}
FileOpen("tmatrix.txt", FILEWRITE);
if(!myFileIsOpen())
{
// Log("No create file\n");
FileClose();
return false;
}
do
{
data = rd32(address + (count * 4));
// Log("TM%dw: 0x%08lx\n", count, data);
FileWrite(data & 0xff); // Little endian file storage to match Eve
FileWrite((data >> 8) & 0xff);
FileWrite((data >> 16) & 0xff);
FileWrite((data >> 24) & 0xff);
count++;
}while(count < 6);
FileClose();
// Log("Matrix Saved\n\n");
}
// Read the touch digitizer calibration matrix values from a file and write them to the Eve.
bool LoadTouchMatrix(void)
{
uint8_t count = 0;
uint32_t data;
uint32_t address = REG_TOUCH_TRANSFORM_A + RAM_REG;
FileOpen("tmatrix.txt", FILEREAD);
if(!myFileIsOpen())
{
// Log("tmatrix.txt not open\n");
FileClose();
return false;
}
do
{
data = FileReadByte() + ((uint32_t)FileReadByte() << 8) + ((uint32_t)FileReadByte() << 16) + ((uint32_t)FileReadByte() << 24);
// Log("TM%dr: 0x%08lx\n", count, data);
wr32(address + (count * 4), data);
count++;
}while(count < 6);
FileClose();
// Log("Matrix Loaded \n\n");
return true;
}
// ************************************************************************************
// Following are abstracted file operations for Arduino. This is possible by using a *
// global pointer to a single file. It is enough for our needs and it hides file *
// handling details within the abstraction. *
// ************************************************************************************
bool FileExists(char *filename)
{
if(SD.exists(filename))
return true;
else
return false;
}
void FileOpen(char *filename, uint8_t mode)
{
// Since one also loses access to defined values like FILE_READ from outside the .ino
// I have been forced to make up values and pass them here (mode) where I can use the
// Arduino defines.
switch(mode)
{
case FILEREAD:
myFile = SD.open(filename, FILE_READ);
break;
case FILEWRITE:
myFile = SD.open(filename, FILE_WRITE);
break;
default:;
}
}
void FileClose(void)
{
myFile.close();
// if(myFileIsOpen())
// {
// Log("Failed to close file\n");
// }
}
// Read a single byte from a file
uint8_t FileReadByte(void)
{
return(myFile.read());
}
// Read bytes from a file into a provided buffer
void FileReadBuf(uint8_t *data, uint32_t NumBytes)
{
myFile.read(data, NumBytes);
}
void FileWrite(uint8_t data)
{
myFile.write(data);
}
// Write a string of characters to a file
// MaxChars does not include the null terminator of the source string.
// We make no attempt to detect the usage of MaxChars and simply truncate the output
void FileWriteStr(uint8_t *str, uint16_t MaxChars)
{
int16_t count = 0;
FileOpen("pidlog.txt", FILEWRITE);
if(!myFileIsOpen())
{
Log("No file\n");
FileClose();
return;
}
// Write out the string until the terminator or until we've written the max
while( (count < MaxChars) && str[count] )
{
FileWrite(str[count]);
count++;
}
FileClose();
}
uint32_t FileSize(void)
{
return(myFile.size());
}
uint32_t FilePosition(void)
{
return(myFile.position());
}
bool FileSeek(uint32_t offset)
{
return(myFile.seek(offset));
}
bool myFileIsOpen(void)
{
if(myFile)
return true;
else
return false;
}