Skip to content

Commit 8cf17c7

Browse files
committed
more merging
1 parent 274d965 commit 8cf17c7

File tree

7 files changed

+161
-48
lines changed

7 files changed

+161
-48
lines changed

.gdbinit

+11
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,14 @@ define execflags
7676
end
7777
end
7878

79+
define hook-stop
80+
set $primask=1
81+
end
82+
83+
define hook-run
84+
set $primask=0
85+
end
86+
87+
define hook-continue
88+
set $primask=0
89+
end

README_BuildProcess.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ This is a partial list of definitions that can be added in a `BOARD.py` file's `
133133
* `USE_NETWORK_JS=0` - Don't include JS networking lib used for handling AT commands (default is yes if networking is enabled)
134134
* `ESPR_DCDC_ENABLE` - On NRF52 use the built-in DCDC converter (requires external hardware)
135135
* `ESPR_LSE_ENABLE` - On NRF52 use an external 32kHz Low Speed External crystal on D0/D1
136-
136+
* `ESPR_NO_LOADING_SCREEN` - Bangle.js, don't show a 'loading' screen when loading a new app
137+
* `ESPR_BOOTLOADER_SPIFLASH` - Allow bootloader to flash direct from a file in SPI flash storage
137138

138139
### chip
139140

libs/graphics/lcd_arraybuffer.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ void lcdSetPixel_ArrayBuffer_flat8(JsGraphics *gfx, int x, int y, unsigned int c
2121
unsigned int lcdGetPixel_ArrayBuffer_flat8(struct JsGraphics *gfx, int x, int y);
2222
void lcdFillRect_ArrayBuffer_flat8(JsGraphics *gfx, int x1, int y1, int x2, int y2, unsigned int col);
2323
void lcdScroll_ArrayBuffer_flat8(JsGraphics *gfx, int xdir, int ydir, int x1, int y1, int x2, int y2);
24+

scripts/hex_to_bootloader.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
var PAGESIZE = 4096;
88
var FLASH_OFFSET = 0x60300000;
99
var VERSION = 0xDEADBEEF; // VERSION! Use this to test firmware in JS land
10+
var DEBUG = false;
1011

1112
if (process.argv.length!=3) {
1213
console.error("USAGE: hex_to_bootloader.js inputfile.hex");
@@ -16,16 +17,17 @@ if (process.argv.length!=3) {
1617
var inputFile = process.argv[2];
1718

1819
var hex = require("fs").readFileSync(inputFile).toString().split("\n");
19-
var addrHi = 0;
2020
function parseLines(dataCallback) {
21+
var addrHi = 0;
2122
hex.forEach(function(hexline) {
22-
var cmd = hexline.substr(1,2);
23-
if (cmd=="02") {
24-
var subcmd = hexline.substr(7,2);
25-
if (subcmd=="02") addrHi = parseInt(hexline.substr(9,4),16) << 4; // Extended Segment Address
26-
if (subcmd=="04") addrHi = parseInt(hexline.substr(9,4),16) << 16; // Extended Linear Address
27-
} else if (cmd=="10") {
28-
var addr = addrHi + parseInt(hexline.substr(3,4),16);
23+
if (DEBUG) console.log(hexline);
24+
var bytes = hexline.substr(1,2);
25+
var addrLo = parseInt(hexline.substr(3,4),16);
26+
var cmd = hexline.substr(7,2);
27+
if (cmd=="02") addrHi = parseInt(hexline.substr(9,4),16) << 4; // Extended Segment Address
28+
else if (cmd=="04") addrHi = parseInt(hexline.substr(9,4),16) << 16; // Extended Linear Address
29+
else if (cmd=="00") {
30+
var addr = addrHi + addrLo;
2931
var data = [];
3032
for (var i=0;i<16;i++) data.push(parseInt(hexline.substr(9+(i*2),2),16));
3133
dataCallback(addr,data);
@@ -89,14 +91,21 @@ console.log(`// Data from 0x${startAddress.toString(16)} to 0x${endAddress.toStr
8991
// Work out data
9092
var headerLen = 16;
9193
var binary = new Uint8Array(headerLen + endAddress-startAddress);
92-
binary.fill(0xFF);
94+
binary.fill(0); // actually seems to assume a block is filled with 0 if not complete
9395
var bin32 = new Uint32Array(binary.buffer);
9496
parseLines(function(addr, data) {
9597
var binAddr = headerLen + addr - startAddress;
9698
binary.set(data, binAddr);
97-
//console.log("i",binAddr, data);
99+
if (DEBUG) console.log("i",addr.toString(16).padStart(8,0), data.map(x=>x.toString(16).padStart(2,0)).join(" "));
98100
//console.log("o",new Uint8Array(binary.buffer, binAddr, data.length));
99101
});
102+
103+
if (DEBUG) {
104+
for (var i=0;i<binary.length;i+=16)
105+
console.log((i+startAddress-headerLen).toString(16).padStart(8,0), Array.from(new Uint8Array(binary.buffer, i, 16)).map(x=>x.toString(16).padStart(2,0)).join(" "));
106+
process.exit(0);
107+
}
108+
100109
/* typedef struct {
101110
uint32_t address;
102111
uint32_t size;

targets/nrf5x_dfu/flash.c

+81-17
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ __attribute__( ( long_call, section(".data") ) ) static void spiFlashWriteCS(uns
6262
NRF_GPIO_PIN_SET_FAST((uint32_t)pinInfo[SPIFLASH_PIN_CS].pin);
6363
}
6464

65-
static unsigned char spiFlashStatus() {
65+
__attribute__( ( long_call, section(".data") ) ) static unsigned char spiFlashStatus() {
6666
unsigned char buf = 5;
6767
NRF_GPIO_PIN_CLEAR_FAST((uint32_t)pinInfo[SPIFLASH_PIN_CS].pin);
6868
spiFlashWrite(&buf, 1);
@@ -71,6 +71,13 @@ static unsigned char spiFlashStatus() {
7171
return buf;
7272
}
7373

74+
// Wake up the SPI Flash from deep power-down mode
75+
static void flashWakeUp() {
76+
unsigned char buf = 0xAB; // SPI Flash release from deep power-down
77+
spiFlashWriteCS(&buf,1);
78+
nrf_delay_us(50); // Wait at least 20us for Flash IC to wake up from deep power-down
79+
}
80+
7481
void spiFlashInit() {
7582
#ifdef SPIFLASH_PIN_WP
7683
nrf_gpio_pin_write_output((uint32_t)pinInfo[SPIFLASH_PIN_WP].pin, 0);
@@ -85,6 +92,11 @@ void spiFlashInit() {
8592
nrf_gpio_pin_write((uint32_t)pinInfo[SPIFLASH_PIN_RST].pin, 1);
8693
#endif
8794
nrf_delay_us(100);
95+
#ifdef SPIFLASH_SLEEP_CMD
96+
// Release from deep power-down - might need a couple of attempts...?
97+
flashWakeUp();
98+
flashWakeUp();
99+
#endif
88100
// disable lock bits
89101
// wait for write enable
90102
unsigned char buf[2];
@@ -172,7 +184,7 @@ __attribute__( ( long_call, section(".data") ) ) void xlcd_wr(int data) {
172184
}
173185
}
174186

175-
__attribute__( ( long_call, section(".data") ) ) void xlcd_rect(int x1,int y1, int x2, int y2) {
187+
__attribute__( ( long_call, section(".data") ) ) void xlcd_rect(int x1,int y1, int x2, int y2, bool white) {
176188
NRF_GPIO_PIN_WRITE_FAST(LCD_SPI_DC, 0); // command
177189
NRF_GPIO_PIN_WRITE_FAST(LCD_SPI_CS, 0);
178190
xlcd_wr(0x2A);
@@ -203,7 +215,7 @@ __attribute__( ( long_call, section(".data") ) ) void xlcd_rect(int x1,int y1, i
203215
NRF_GPIO_PIN_WRITE_FAST(LCD_SPI_CS, 0);
204216
int l = (x2+1-x1) * (y2+1-y1);
205217
for (int x=0;x<l*2;x++)
206-
xlcd_wr(0xFF);
218+
xlcd_wr(white ? 0xFF : 0);
207219
NRF_GPIO_PIN_WRITE_FAST(LCD_SPI_CS,1);
208220
}
209221

@@ -223,7 +235,7 @@ __attribute__( ( long_call, section(".data") ) ) void flashDoUpdate(FlashHeader
223235
size -= 4096;
224236
percent = (addr-header.address)*120/header.size;
225237
if (percent>120) percent=120;
226-
xlcd_rect(60,182,60+percent,188);
238+
xlcd_rect(60,182,60+percent,188,true);
227239
NRF_WDT->RR[0] = 0x6E524635; // kick watchdog
228240
}
229241
// Write
@@ -240,21 +252,28 @@ __attribute__( ( long_call, section(".data") ) ) void flashDoUpdate(FlashHeader
240252
size -= l;
241253
percent = (outaddr-header.address)*120/header.size;
242254
if (percent>120) percent=120;
243-
xlcd_rect(60,192,60+percent,198);
255+
xlcd_rect(60,192,60+percent,198,true);
244256
NRF_WDT->RR[0] = 0x6E524635; // kick watchdog
245257
}
246-
//flashEqual(header);
258+
// clear progress bar
259+
xlcd_rect(60,180,180,200,false);
260+
// re-read all data just to try and clear any caches
261+
size = header.size;
262+
outaddr = header.address;
263+
volatile int v;
264+
while (size--) {
265+
v = *(char*)outaddr;
266+
outaddr++;
267+
}
247268
// done!
248-
for (volatile int i=0;i<10000000;i++); // delay
249-
NVIC_SystemReset(); // reset!
269+
while (true) NVIC_SystemReset(); // reset!
250270
}
251271

252-
253272
void flashCheckAndRun() {
254273
spiFlashInit();
255274
FlashHeader header;
256275
spiFlashReadAddr((unsigned char *)&header, FLASH_HEADER_ADDRESS, sizeof(FlashHeader));
257-
if (header.address==0xFFFFFFFF) {
276+
if (header.address==0xFFFFFFFF || header.size==0) {
258277
// Not set - silently exit
259278
return;
260279
}
@@ -263,8 +282,9 @@ void flashCheckAndRun() {
263282
lcd_print_hex(header.size); lcd_println(" SIZE");
264283
lcd_print_hex(header.CRC); lcd_println(" CRC");
265284
lcd_print_hex(header.version); lcd_println(" VERSION");
266-
// if (header.address==0xf7000) return; // NO BOOTLOADER - FOR TESTINGs
285+
if (header.address==0xf7000) return; // NO BOOTLOADER - FOR TESTING
267286
// Calculate CRC
287+
lcd_println("CRC TEST...");
268288
unsigned char buf[256];
269289
int size = header.size;
270290
int inaddr = FLASH_HEADER_ADDRESS + sizeof(FlashHeader);
@@ -277,24 +297,68 @@ void flashCheckAndRun() {
277297
inaddr += l;
278298
size -= l;
279299
}
300+
bool isEqual = false;
280301
if (crc != header.CRC) {
281302
// CRC is wrong - exits
282303
lcd_println("CRC MISMATCH");
283304
lcd_print_hex(crc); lcd_println("");lcd_println("");
284-
nrf_delay_us(1000000);
305+
for (volatile int i=0;i<5000000;i++) NRF_WDT->RR[0] = 0x6E524635; // delay
306+
} else {
307+
// All ok - check we haven't already flashed this!
308+
lcd_println("TESTING...");
309+
isEqual = flashEqual(header);
310+
}
311+
lcd_println("REMOVE HEADER.");
312+
// Now erase the first page of flash so we don't get into a boot loop
313+
unsigned char b[20];
314+
b[0] = 0x06; // WREN
315+
spiFlashWriteCS(b,1);
316+
for (volatile int i=0;i<1000;i++);
317+
b[0] = 0x02; // Write
318+
b[1] = FLASH_HEADER_ADDRESS>>16;
319+
b[2] = FLASH_HEADER_ADDRESS>>8;
320+
b[3] = FLASH_HEADER_ADDRESS;
321+
memset(&b[4], 0, 16);
322+
spiFlashWriteCS(b,4+16); // write command plus 16 bytes of zeros
323+
// Check if flash busy
324+
while (spiFlashStatus()&1); // while 'Write in Progress'...
325+
FlashHeader header2;
326+
spiFlashReadAddr((unsigned char *)&header2, FLASH_HEADER_ADDRESS, sizeof(FlashHeader));
327+
// read a second time just in case
328+
spiFlashReadAddr((unsigned char *)&header2, FLASH_HEADER_ADDRESS, sizeof(FlashHeader));
329+
if (header2.address != 0) {
330+
lcd_println("ERASE FAIL. EXIT.");
331+
for (volatile int i=0;i<5000000;i++) NRF_WDT->RR[0] = 0x6E524635; // delay
285332
return;
286333
}
287-
// All ok - check we haven't already flashed this!
288-
if (!flashEqual(header)) {
334+
335+
if (!isEqual) {
289336
lcd_println("BINARY DIFF. FLASHING...");
290-
xlcd_rect(60,180,180,180);
291-
xlcd_rect(60,190,180,190);
292-
xlcd_rect(60,200,180,200);
337+
338+
xlcd_rect(60,180,180,180,true);
339+
xlcd_rect(60,190,180,190,true);
340+
xlcd_rect(60,200,180,200,true);
293341

294342
flashDoUpdate(header);
343+
344+
/*isEqual = flashEqual(header);
345+
if (isEqual) lcd_println("EQUAL");
346+
else lcd_println("NOT EQUAL");
347+
348+
for (volatile int i=0;i<5000000;i++) NRF_WDT->RR[0] = 0x6E524635; // delay
349+
while (true) NVIC_SystemReset(); */
295350
} else {
296351
lcd_println("BINARY MATCHES.");
297352
}
298353
}
299354

355+
// Put the SPI Flash into deep power-down mode
356+
void flashPowerDown() {
357+
spiFlashInit(); //
358+
unsigned char buf = 0xB9; // SPI Flash deep power-down
359+
spiFlashWriteCS(&buf,1);
360+
nrf_delay_us(2); // Wait at least 1us for Flash IC to enter deep power-down
361+
}
362+
363+
300364
#endif

targets/nrf5x_dfu/flash.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414

1515
#ifdef ESPR_BOOTLOADER_SPIFLASH
1616
void flashCheckAndRun();
17+
void flashPowerDown();
1718
#endif

targets/nrf5x_dfu/lcd.c

+46-20
Original file line numberDiff line numberDiff line change
@@ -577,49 +577,75 @@ static const char SPILCD_INIT_CODE[] = {
577577
0x13, 10, 0,
578578
#endif
579579
#ifdef LCD_CONTROLLER_GC9A01
580+
// CMD,DELAY,DATA_LEN,D0,D1,D2...
580581
0xfe,0,0,
581582
0xef,0,0,
582583
0xeb,0,1, 0x14,
583-
0x84,0,1, 0x40,
584+
0x84,0,1, 0x60, // 0x40->0x60 0xb5 en 20200924 james
585+
0x85,0,1, 0xFF,
586+
0x86,0,1, 0xFF,
587+
0x87,0,1, 0xFF,
588+
0x8e,0,1, 0xFF,
589+
0x8f,0,1, 0xFF,
584590
0x88,0,1, 10,
585-
0x89,0,1, 0x21,
591+
0x89,0,1, 0x23, // 0x21->0x23 spi 2data reg en
586592
0x8a,0,1, 0,
587593
0x8b,0,1, 0x80,
588594
0x8c,0,1, 1,
589-
0x8d,0,1, 1,
590-
0xb6,0,1, 0x20,
591-
0x36,0,1, 0x88, // Memory Access Control (0x48 flips upside-down)
595+
0x8d,0,1, 3, // 1->3 99 en
596+
0xb5,0,4, 0x08, 0x09, 0x14, 0x08,
597+
0xb6,0,2, 0, 0, // Positive sweep 0x20->0 GS SS 0x20
598+
#ifdef LCD_ROTATION
599+
#if (LCD_ROTATION == 0)
600+
0x36,0,1, 0x88, // Memory Access Control (no rotation)
601+
#elif (LCD_ROTATION == 90)
602+
0x36,0,1, 0x78, // Memory Access Control (rotated 90 degrees)
603+
#elif (LCD_ROTATION == 180)
604+
0x36,0,1, 0x48, // Memory Access Control (rotated 180 degrees)
605+
#elif (LCD_ROTATION == 270)
606+
0x36,0,1, 0xB8, // Memory Access Control (rotated 270 degrees)
607+
#else
608+
#error "Unexpected value defined for LCD_ROTATION - should be 0, 90, 180 or 270"
609+
#endif
610+
#else
611+
0x36,0,1, 0x88, // Memory Access Control (no rotation)
612+
#endif
592613
0x3a,0,1, 5, // could be 16/12 bit?
593614
0x90,0,4, 8, 8, 8, 8,
594615
0xbd,0,1, 6,
595616
0xbc,0,1, 0,
596617
0xff,0,3, 0x60, 1, 4,
597-
0xc3,0,1, 0x13,
598-
0xc4,0,1, 0x13,
599-
0xc9,0,1, 0x22,
618+
0xc3,0,1, 0x1d, // Power control 2: 0x13->0x1d
619+
0xc4,0,1, 0x1d, // Power control 3: 0x13->0x1d
620+
0xc9,0,1, 0x25, // Power control 4: 0x22->0x25
600621
0xbe,0,1, 0x11,
601622
0xe1,0,2, 0x10, 0xe,
602623
0xdf,0,3, 0x21, 0xc, 2,
603-
0xf0,0,6, 0x45, 9, 8, 8, 0x26, 0x2a,
604-
0xf1,0,6, 0x43, 0x70, 0x72, 0x36, 0x37, 0x6f,
605-
0xf2,0,6, 0x45, 9, 8, 8, 0x26, 0x2a,
606-
0xf3,0,6, 0x43, 0x70, 0x72, 0x36, 0x37, 0x6f,
624+
0xf0,0,6, 0x45, 9, 8, 8, 0x26, 0x2a, // Gamma 1
625+
0xf1,0,6, 0x43, 0x70, 0x72, 0x36, 0x37, 0x6f, // Gamma 2
626+
0xf2,0,6, 0x45, 9, 8, 8, 0x26, 0x2a, // Gamma 3
627+
0xf3,0,6, 0x43, 0x70, 0x72, 0x36, 0x37, 0x6f, // Gamma 4
607628
0xed,0,2, 0x1b, 0xb,
608-
0xae,0,1, 0x74,
629+
0xae,0,1, 0x77, // 0x74->0x77
609630
0xcd,0,1, 99,
610-
0x70,0,9, 7, 9, 4, 0xe, 0xf, 9, 7, 8, 3,
631+
0x70,0,9, 7, 7, 4, 0xe, 0xf, 9, 7, 8, 3, // 7,9,4... -> 7,7,4...
611632
0xe8,0,1, 0x34,
612-
0x62,0,12, 0x18, 0xd, 0x71, 0xed, 0x70, 0x70, 0x18, 0xf, 0x71, 0xef, 0x70, 0x70,
613-
99,0,12, 0x18, 0x11, 0x71, 0xf1, 0x70, 0x70, 0x18, 0x13, 0x71, 0xf3, 0x70, 0x70,
633+
0x60,0,4, 0x38, 0x0b, 0x6d, 0x6d,
634+
0x39,0,3, 0xf0, 0x6d, 0x6d,
635+
0x61,0,4, 0x38, 0xf4, 0x6d, 0x6d,
636+
0x38,0,3, 0xf7, 0x6d, 0x6d,
637+
0x62,0,12, 0x38, 0xd, 0x71, 0xed, 0x70, 0x70, 0x38, 0xf, 0x71, 0xef, 0x70, 0x70,
638+
0x63,0,12, 0x38, 0x11, 0x71, 0xf1, 0x70, 0x70, 0x38, 0x13, 0x71, 0xf3, 0x70, 0x70,
614639
100,0,7, 0x28, 0x29, 0xf1, 1, 0xf1, 0, 7,
615640
0x66,0,10, 0x3c, 0, 0xcd, 0x67, 0x45, 0x45, 0x10, 0, 0, 0,
616641
0x67,0,10, 0, 0x3c, 0, 0, 0, 1, 0x54, 0x10, 0x32, 0x98,
617642
0x74,0,7, 0x10, 0x85, 0x80, 0, 0, 0x4e, 0,
618643
0x98,0,2, 0x3e, 7,
619-
0x35,0,0,
620-
0x21,10,0,
621-
0x11,20,0,
622-
0x29,10,0,
644+
0x99,0,2, 0x3e, 7, // bvee 2x
645+
0x35,0,1, 0,
646+
0x21,5,0,
647+
0x11,5,0,
648+
0x29,5,0,
623649
0x2c,0,0,
624650
#endif
625651
// End

0 commit comments

Comments
 (0)