1 |
// **************************************************************************************
|
2 |
// el Comandante guerilla.bplaced.net
|
3 |
// --------------------------------------------------------------------------------------
|
4 |
// Information:
|
5 |
// VSPI sd-card interface, Custom VSPI Pins, flash Winbond W25Q32FVSIG chip (4 MB)
|
6 |
// Read datas from a file on sd-card and flash datas to Winbond 25Q32FVSIG chip (4 MB)
|
7 |
// Written for the esp32 device, should work with any other mcu with SPI- Interface
|
8 |
// Status:
|
9 |
// Bricked Edision Proton T265 LED DVB-T receiver after firmware flash failure
|
10 |
// Goal:
|
11 |
// Bring the Edision Proton T265 LED DVB-T receiver back on stage
|
12 |
// --------------------------------------------------------------------------------------
|
13 |
// Firmware download:
|
14 |
// https://www.edision.gr/de/detail/proton-t265-led
|
15 |
// --------------------------------------------------------------------------------------
|
16 |
// Details:
|
17 |
// - Download the firmware
|
18 |
// - Copy the firmware file (renamed to: flashdat.bin) to a FAT32 formatted sd-card
|
19 |
// - Connect a sd-card breakout modul to the esp32 device
|
20 |
// - Insert the sd-card with the firmware file
|
21 |
// - Connect the Winbond W25Q32FVSIG chip (4 MB) to the esp32 device using the adapter
|
22 |
// - Start the Arduino IDE and load this ino file
|
23 |
// - Upload the file to the esp32 device
|
24 |
// - Be patient, flashing and verifying could take a few minutes
|
25 |
// **************************************************************************************
|
26 |
|
27 |
#include <SPI.h>
|
28 |
#include <SD.h>
|
29 |
|
30 |
// ######################################################################################
|
31 |
// Change values in this area to match your envoirement and desires
|
32 |
// --------------------------------------------------------------------------------------
|
33 |
// Winbond DataFlash commands
|
34 |
#define PAGEPROG 0x02
|
35 |
#define READSTATUS 0x05
|
36 |
#define READDATA 0x03
|
37 |
#define WRITEENABLE 0x06
|
38 |
#define CHIPERASE 0x60
|
39 |
#define READID 0x90
|
40 |
|
41 |
// Custom VSPI Pins for the SD-Card breakout board and the flash ram - must match the wired hardware
|
42 |
#define sck 27
|
43 |
#define miso 32
|
44 |
#define mosi 33
|
45 |
#define cs 2 // chip select sd-card breakout board
|
46 |
|
47 |
// Custom Pins for the flash chip - must match the wired hardware
|
48 |
#define sckF 21
|
49 |
#define misoF 22
|
50 |
#define mosiF 23
|
51 |
#define csF 4 // chip select flash ram chip
|
52 |
// ######################################################################################
|
53 |
|
54 |
// Vars
|
55 |
uint32_t errors;
|
56 |
uint8_t errorSDCard;
|
57 |
uint8_t errorFlash;
|
58 |
uint8_t filedata;
|
59 |
uint8_t flashdata;
|
60 |
uint32_t filesize;
|
61 |
uint32_t i;
|
62 |
uint32_t vspiBusFrequency = 2000000; // >= 4000000 sd-card mount could fail
|
63 |
File file; // file object
|
64 |
String szFilepath = "/flashdat.bin"; // file to load
|
65 |
|
66 |
// Create a new SPI class on HSPI or VSPI
|
67 |
SPIClass vspi = SPIClass(VSPI);
|
68 |
|
69 |
|
70 |
// ======================================================================================
|
71 |
// Simple DataFlash - see http://www.technoblogy.com/show?2JMU
|
72 |
//
|
73 |
// David Johnson-Davies - www.technoblogy.com - 17th July 2019
|
74 |
//
|
75 |
// CC BY 4.0 Licensed under a Creative Commons Attribution 4.0 International license:
|
76 |
// http://creativecommons.org/licenses/by/4.0/
|
77 |
// --------------------------------------------------------------------------------------
|
78 |
// Adapter circuit modified (without diodes):
|
79 |
// https://onecircuit.blogspot.com/2021/06/0000-0000-0110-0111.html
|
80 |
// --------------------------------------------------------------------------------------
|
81 |
|
82 |
class DF {
|
83 |
public:
|
84 |
boolean Setup();
|
85 |
void BeginRead(unsigned long addr);
|
86 |
void BeginWrite(void);
|
87 |
uint8_t ReadByte(void);
|
88 |
void WriteByte(uint8_t data);
|
89 |
void EndRead(void);
|
90 |
void EndWrite(void);
|
91 |
private:
|
92 |
unsigned long addr;
|
93 |
uint8_t Read(void);
|
94 |
void Write(uint8_t);
|
95 |
void Busy(void);
|
96 |
void WriteEnable(void);
|
97 |
};
|
98 |
|
99 |
boolean DF::Setup () {
|
100 |
uint8_t manID, devID;
|
101 |
//pinMode(power, OUTPUT); digitalWrite(power, HIGH);
|
102 |
digitalWrite(csF, HIGH); pinMode(csF, OUTPUT);
|
103 |
pinMode(sckF, OUTPUT);
|
104 |
pinMode(mosiF, OUTPUT);
|
105 |
pinMode(misoF, INPUT);
|
106 |
digitalWrite(sckF, LOW); digitalWrite(mosiF, HIGH);
|
107 |
delay(1);
|
108 |
digitalWrite(csF, LOW);
|
109 |
delay(100);
|
110 |
Write(READID); Write(0);Write(0);Write(0);
|
111 |
manID = Read();
|
112 |
devID = Read();
|
113 |
digitalWrite(csF, HIGH);
|
114 |
return (devID == 0x15); // Found correct device
|
115 |
}
|
116 |
|
117 |
void DF::Write (uint8_t data) {
|
118 |
shiftOut(mosiF, sckF, MSBFIRST, data);
|
119 |
}
|
120 |
|
121 |
void DF::Busy () {
|
122 |
digitalWrite(csF, 0);
|
123 |
Write(READSTATUS);
|
124 |
while (Read() & 1 != 0);
|
125 |
digitalWrite(csF, 1);
|
126 |
}
|
127 |
|
128 |
void DF::WriteEnable () {
|
129 |
digitalWrite(csF, 0);
|
130 |
Write(WRITEENABLE);
|
131 |
digitalWrite(csF, 1);
|
132 |
}
|
133 |
|
134 |
void DF::BeginRead (unsigned long start) {
|
135 |
addr = start;
|
136 |
Busy();
|
137 |
digitalWrite(csF, 0);
|
138 |
Write(READDATA);
|
139 |
Write(addr>>16);
|
140 |
Write(addr>>8);
|
141 |
Write(addr);
|
142 |
}
|
143 |
|
144 |
uint8_t DF::Read () {
|
145 |
return shiftIn(misoF, sckF, MSBFIRST);
|
146 |
}
|
147 |
|
148 |
void DF::EndRead(void) {
|
149 |
digitalWrite(csF, 1);
|
150 |
}
|
151 |
|
152 |
void DF::BeginWrite () {
|
153 |
addr = 0;
|
154 |
Busy();
|
155 |
// Erase DataFlash
|
156 |
WriteEnable();
|
157 |
digitalWrite(csF, 0);
|
158 |
Write(CHIPERASE);
|
159 |
digitalWrite(csF, 1);
|
160 |
Busy();
|
161 |
}
|
162 |
|
163 |
uint8_t DF::ReadByte () {
|
164 |
return Read();
|
165 |
}
|
166 |
|
167 |
void DF::WriteByte (uint8_t data) {
|
168 |
// New page
|
169 |
if ((addr & 0xFF) == 0) {
|
170 |
digitalWrite(csF, 1);
|
171 |
Busy();
|
172 |
WriteEnable();
|
173 |
digitalWrite(csF, 0);
|
174 |
Write(PAGEPROG);
|
175 |
Write(addr>>16);
|
176 |
Write(addr>>8);
|
177 |
Write(0);
|
178 |
}
|
179 |
Write(data);
|
180 |
addr++;
|
181 |
}
|
182 |
|
183 |
void DF::EndWrite (void) {
|
184 |
digitalWrite(csF, 1);
|
185 |
}
|
186 |
|
187 |
DF DataFlash;
|
188 |
|
189 |
|
190 |
|
191 |
// Setup
|
192 |
void setup()
|
193 |
{
|
194 |
// serial monitor - init
|
195 |
Serial.begin(115200);
|
196 |
//while (!Serial);
|
197 |
delay (2000);
|
198 |
Serial.println("-----------------------------------------------------------");
|
199 |
Serial.println("Testing");
|
200 |
Serial.println("-----------------------------------------------------------");
|
201 |
Serial.println("Serial Monitor: Okay");
|
202 |
|
203 |
Serial.println("-----------------------------------------------------------");
|
204 |
Serial.println("Test SD-Card");
|
205 |
Serial.println("-----------------------------------------------------------");
|
206 |
// sd-card - vspi init
|
207 |
digitalWrite(cs, HIGH); // slave select pin csSD (sd-card)
|
208 |
vspi.begin(sck, miso, mosi, cs); // init sd-card spi communication protocol
|
209 |
Serial.println("SD-Card VSPI: Okay");
|
210 |
|
211 |
// sd-card - mount
|
212 |
errorSDCard = 0;
|
213 |
if (!SD.begin(cs, vspi, vspiBusFrequency))
|
214 |
{Serial.println("SD-Card Mount: Error"); errorSDCard++;}
|
215 |
else
|
216 |
{Serial.println("SD-Card Mount: Okay");}
|
217 |
|
218 |
// sd-card - check
|
219 |
if(SD.cardType() == CARD_NONE)
|
220 |
{Serial.println("SD-Card Type: None"); errorSDCard++;}
|
221 |
else
|
222 |
{Serial.println("SD-Card Type: Okay");}
|
223 |
|
224 |
// sd-card - open file and read filesize
|
225 |
file = SD.open(szFilepath, FILE_READ);
|
226 |
if (file)
|
227 |
{Serial.print("File Open: "); Serial.println(szFilepath); filesize = file.size(); Serial.print("File Size: "); Serial.println(filesize);}
|
228 |
else
|
229 |
{Serial.print("File Open: Error "); errorSDCard++;}
|
230 |
|
231 |
filedata = file.read();
|
232 |
Serial.print("File Byte 0: "); Serial.println(filedata, HEX);
|
233 |
filedata = file.read();
|
234 |
Serial.print("File Byte 1: "); Serial.println(filedata, HEX);
|
235 |
file.seek(filesize);
|
236 |
filedata = file.read();
|
237 |
Serial.print("File Byte "); Serial.print(filesize, DEC); Serial.print(": "); Serial.println(filedata, HEX);
|
238 |
file.seek(0);
|
239 |
filedata = file.read();
|
240 |
Serial.print("File Byte 0: "); Serial.println(filedata, HEX);
|
241 |
|
242 |
Serial.println("-----------------------------------------------------------");
|
243 |
Serial.println("Test Flash");
|
244 |
Serial.println("-----------------------------------------------------------");
|
245 |
// flash ram - setup
|
246 |
errorFlash = 0;
|
247 |
if (!DataFlash.Setup())
|
248 |
{Serial.println("Flash chip: Not found"); errorFlash++;}
|
249 |
else
|
250 |
{Serial.println("Flash chip: Found");}
|
251 |
|
252 |
// flash write test
|
253 |
filedata = 0x4B;
|
254 |
Serial.print("Flash Write Test: "); Serial.println(filedata, HEX);
|
255 |
DataFlash.BeginWrite();
|
256 |
DataFlash.WriteByte(filedata);
|
257 |
DataFlash.EndWrite();
|
258 |
|
259 |
// flash read test
|
260 |
DataFlash.BeginRead(0);
|
261 |
filedata = 0;
|
262 |
filedata = DataFlash.ReadByte();
|
263 |
Serial.print("Flash Read Test: "); Serial.println(filedata, HEX);
|
264 |
DataFlash.EndRead();
|
265 |
|
266 |
Serial.println("-----------------------------------------------------------");
|
267 |
Serial.println("Test Result");
|
268 |
Serial.println("-----------------------------------------------------------");
|
269 |
if (errorSDCard == 0 && errorFlash == 0)
|
270 |
{Serial.println("Okay");}
|
271 |
else
|
272 |
{Serial.println("Error"); return;}
|
273 |
|
274 |
Serial.println("");
|
275 |
|
276 |
Serial.println("-----------------------------------------------------------");
|
277 |
Serial.println("Flashing...");
|
278 |
Serial.println("-----------------------------------------------------------");
|
279 |
delay (1000);
|
280 |
|
281 |
// file pointer
|
282 |
file.seek(0);
|
283 |
|
284 |
// flash write begin
|
285 |
DataFlash.BeginWrite();
|
286 |
// flash write loop
|
287 |
for (i = 0; i < filesize; i++)
|
288 |
{
|
289 |
filedata = file.read(); // read byte from file
|
290 |
DataFlash.WriteByte(filedata); // write byte to flash
|
291 |
}
|
292 |
// flash write end
|
293 |
DataFlash.EndWrite();
|
294 |
|
295 |
// empty line
|
296 |
Serial.println("");
|
297 |
|
298 |
Serial.println("-----------------------------------------------------------");
|
299 |
Serial.println("Verifying...");
|
300 |
Serial.println("-----------------------------------------------------------");
|
301 |
delay (1000);
|
302 |
|
303 |
// file pointer
|
304 |
file.seek(0);
|
305 |
|
306 |
// flash read begin
|
307 |
DataFlash.BeginRead(0);
|
308 |
|
309 |
// flash read loop
|
310 |
errors = 0;
|
311 |
for (i = 0; i < filesize; i++)
|
312 |
{
|
313 |
filedata = file.read();
|
314 |
flashdata = DataFlash.ReadByte();
|
315 |
//Serial.print("Verify: "); Serial.print("File = "); Serial.print(filedata, HEX); Serial.print(" --- Flash = "); Serial.println(flashdata, HEX);
|
316 |
if (flashdata != filedata)
|
317 |
{errors++;}
|
318 |
}
|
319 |
|
320 |
// flash read end
|
321 |
DataFlash.EndRead();
|
322 |
|
323 |
// empty line
|
324 |
Serial.println("");
|
325 |
|
326 |
Serial.println("-----------------------------------------------------------");
|
327 |
Serial.println("End of program");
|
328 |
Serial.println("-----------------------------------------------------------");
|
329 |
// file close
|
330 |
file.close();
|
331 |
Serial.print("Errors: "); Serial.println(errors);
|
332 |
}
|
333 |
|
334 |
|
335 |
|
336 |
// Loop
|
337 |
void loop()
|
338 |
{
|
339 |
// notting to do
|
340 |
}
|