Skip to content

Commit be9ead9

Browse files
committed
cleaned up, added erase_all to inisetup.fwupdate(fn, erase_all=False, safe_check=True, verbose=True)
1 parent 3fa4c73 commit be9ead9

File tree

8 files changed

+123
-144
lines changed

8 files changed

+123
-144
lines changed

lib/littlefs/lfs2.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3239,9 +3239,11 @@ static int lfs2_file_rawsync(lfs2_t *lfs2, lfs2_file_t *file) {
32393239
}
32403240
#endif
32413241

3242+
#ifdef MICROPY_DFU
32423243
extern uint32_t dfu_blks[256];
32433244
extern uint8_t dfu_offs[256];
32443245
extern int dfu_nblks;
3246+
#endif
32453247
static lfs2_ssize_t lfs2_file_flushedread(lfs2_t *lfs2, lfs2_file_t *file,
32463248
void *buffer, lfs2_size_t size) {
32473249
uint8_t *data = buffer;
@@ -3263,10 +3265,12 @@ static lfs2_ssize_t lfs2_file_flushedread(lfs2_t *lfs2, lfs2_file_t *file,
32633265
int err = lfs2_ctz_find(lfs2, NULL, &file->cache,
32643266
file->ctz.head, file->ctz.size,
32653267
file->pos, &file->block, &file->off);
3266-
if(dfu_nblks==0 || (dfu_nblks>0 && dfu_blks[(dfu_nblks-1)%256]!=file->block)){ //DEBUG
3268+
#ifdef MICROPY_DFU
3269+
if(dfu_nblks==0 || (dfu_nblks>0 && dfu_blks[(dfu_nblks-1)%256]!=file->block)){ // DFU hook
32673270
dfu_blks[dfu_nblks%256] = file->block;
32683271
dfu_offs[(dfu_nblks++)%256] = file->off;
32693272
}
3273+
#endif
32703274
if (err) {
32713275
return err;
32723276
}

ports/esp8266/boards/ESP8266_GENERIC/mpconfigboard.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define MICROPY_VFS (1)
1515

1616
#define MICROPY_PY_CRYPTOLIB (1)
17+
#define MICROPY_DFU (1)
1718

1819
#elif defined(MICROPY_ESP8266_1M)
1920

ports/esp8266/boards/ESP8266_GENERIC/mpconfigboard.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ifeq ($(BOARD_VARIANT),)
22
LD_FILES = boards/esp8266_2MiB.ld
33

4-
MICROPY_ESPNOW ?= 0
4+
MICROPY_ESPNOW ?= 1
55
MICROPY_PY_BTREE ?= 1
66
MICROPY_VFS_FAT ?= 1
77
MICROPY_VFS_LFS2 ?= 1

ports/esp8266/boards/esp8266_common.ld

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ SECTIONS
182182

183183
/* for -mforce-l32 */
184184
*/*.o(.rodata*)
185+
*modespnow.o(.text.espnow_del_peer .text.espnow_add_peer .text.espnow_config)
185186

186187
_irom0_text_end = ABSOLUTE(.);
187188
} >irom0_0_seg :irom0_0_phdr

ports/esp8266/modesp.c

Lines changed: 34 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_set_native_code_location_obj, esp_set_nativ
339339

340340
#endif
341341

342-
343-
344-
345-
346-
342+
/*
347343
#include <stdarg.h>
348344
int mp_vprintf(const mp_print_t *print, const char *fmt, va_list args);
349345
STATIC void _strncat(char *data, const char *str, size_t len){
@@ -379,83 +375,28 @@ STATIC mp_obj_t esp_showMemory(mp_obj_t _start, mp_obj_t _size) {
379375
return mp_const_none;
380376
}
381377
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_showMemory_obj, esp_showMemory);
378+
*/
382379

383380
uint32_t dfu_blks[256];
384381
uint8_t dfu_offs[256];
385382
int dfu_nblks = -1;
386383
uint32_t dfu_fwsize = 0;
387384

388-
// STATIC int MP_FASTCODE(verify_erase)(size_t sector, uint8_t byte){
389-
// char *ptr = FLASH_START+sector*FLASH_SEC_SIZE;
390-
// int ret = 0;
391-
// for(int x=0; x<FLASH_SEC_SIZE; x++)
392-
// if(ptr[x]!=byte) ret++;
393-
// return ret;
394-
// }
395-
void inline sync_all(){
396-
__asm__ volatile ("memw");
397-
__asm__ volatile ("isync");
385+
/* Firmware update codes must be put into IRAM, or will crash while flashing halfway! */
386+
void MP_FASTCODE(erase_sector)(int sector) {
387+
char *ptr = FLASH_START+sector*FLASH_SEC_SIZE;
388+
for(char *PTR=ptr+FLASH_SEC_SIZE; ptr<PTR; ptr++)
389+
if(*ptr!=0xff){SPIEraseSector(sector);break;}
398390
}
399-
// void MP_FASTCODE(ram_erase_sector)(size_t sector){
400-
// // spi_flash_check_wr_protect();
401-
// // Cache_Read_Disable_2();
402-
// SPIEraseSector(sector);
403-
// // Cache_Read_Enable_2();
404-
// }
405-
// void MP_FASTCODE(ram_read_flash)(size_t src, void *dst, size_t sz){
406-
// Cache_Read_Disable_2();
407-
// // uart_tx_one_char(0, '1');
408-
// // sync_all();
409-
// SPIRead(src, dst, sz);
410-
// // uart_tx_one_char(0, '2');
411-
// // sync_all();
412-
// Cache_Read_Enable_2();
413-
// // uart_tx_one_char(0, '3');
414-
// // sync_all();
415-
// }
416-
// void MP_FASTCODE(ram_write_flash)(size_t dst, void *src, size_t sz){
417-
// spi_flash_check_wr_protect();
418-
// // sync_all();
419-
// // uart_tx_one_char(0, '1');
420-
// Cache_Read_Disable_2();
421-
// // sync_all();
422-
// // uart_tx_one_char(0, '2');
423-
// SPIWrite(dst, src, sz);
424-
// // sync_all();
425-
// // uart_tx_one_char(0, '3');
426-
// Cache_Read_Enable_2();
427-
// // sync_all();
428-
// // uart_tx_one_char(0, '4');
429-
// }
430-
void MP_FASTCODE(DFU)(char *dfu_buf, int n_erase) {
431-
for(int x=0; x<n_erase; x++){
432-
SPIEraseSector(x);
391+
void MP_FASTCODE(DFU)(char *dfu_buf, int n_fwblks, int n_erase) {
392+
for(int x=0; x<n_fwblks; x++){
393+
erase_sector(x);
433394
// uart_tx_one_char(0, 'E');
434395
}
435396

436397
int cur_write_pos = 0;
437398
int cur_write_sz;
438399
int remaining_sz = dfu_fwsize;
439-
/*
440-
memset(dfu_buf, 0, FLASH_SEC_SIZE);
441-
for(int x=0xff; x>=0; x--){
442-
while(verify_erase(x, 0xff)){
443-
// ram_write_flash(x*FLASH_SEC_SIZE, dfu_buf, FLASH_SEC_SIZE);
444-
ram_erase_sector(x);
445-
while(verify_erase(x, 0xff)){
446-
uart_tx_one_char(0, 'f');
447-
wdt_feed();
448-
}
449-
uart_tx_one_char(0, 'x');
450-
}
451-
uart_tx_one_char(0, '.');
452-
uart_tx_one_char(0, ' ');
453-
wdt_feed();
454-
}
455-
uart_tx_one_char(0, 'O');
456-
uart_tx_one_char(0, 'K');
457-
uart_tx_one_char(0, '\n'); */
458-
459400

460401
for(int x=0; x<dfu_nblks && remaining_sz>0; x++){
461402
cur_write_sz = FLASH_SEC_SIZE - dfu_offs[x];
@@ -473,72 +414,47 @@ void MP_FASTCODE(DFU)(char *dfu_buf, int n_erase) {
473414
remaining_sz -= cur_write_sz;
474415
wdt_feed();
475416
}
476-
// uart_tx_one_char(0, 'O');
477-
// uart_tx_one_char(0, 'K');
478-
// uart_tx_one_char(0, '\r');
479-
// uart_tx_one_char(0, '\n');
480-
sync_all();
417+
418+
for(int x=n_fwblks; x<n_erase; x++){
419+
erase_sector(x);
420+
// uart_tx_one_char(0, 'E');
421+
}
422+
423+
asm ("memw");
424+
asm ("isync");
481425
// rom_phy_reset_req();
482426
system_restart_core();
483427
// return cur_write_pos;
484428
}
485-
STATIC mp_obj_t esp_DFU(void) {
486-
int state;
429+
STATIC mp_obj_t esp_DFU(mp_obj_t erase_all) {
430+
extern char flashchip;
431+
SpiFlashChip *flash = (SpiFlashChip *)(&flashchip + 4);
487432
char *buf = malloc(FLASH_SEC_SIZE);
488433
if(buf==NULL){
489-
mp_hal_stdout_tx_str("Not enough memory!\r\n");
434+
mp_raise_OSError(MP_ENOMEM);
490435
return mp_const_none;
491436
}
492-
/*
493-
buf[0] = 0;
494-
_sprintf(&buf[0], "DFU(): %p\r\n", DFU);
495-
mp_hal_stdout_tx_str(buf);
496-
buf[0] = 0;
497-
_sprintf(&buf[0], "verify_erase(): %p\r\n", verify_erase);
498-
mp_hal_stdout_tx_str(buf);
499-
buf[0] = 0;
500-
_sprintf(&buf[0], "ram_erase_sector(): %p\r\n", ram_erase_sector);
501-
mp_hal_stdout_tx_str(buf);
502-
*/
437+
503438
ets_wdt_disable();
504-
state = disable_irq();
505-
DFU(buf, dfu_fwsize/FLASH_SEC_SIZE+(dfu_fwsize%FLASH_SEC_SIZE?1:0));
439+
disable_irq();
440+
DFU(buf, dfu_fwsize/FLASH_SEC_SIZE+(dfu_fwsize%FLASH_SEC_SIZE?1:0), mp_obj_is_true(erase_all)?(flash->chip_size/FLASH_SEC_SIZE):0);
506441

507442
// It will NEVER reach here even if flash failed
508443
// enable_irq(state);
509444
// ets_wdt_enable();
510445

511446
return mp_const_none;
512447
}
513-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_DFU_obj, esp_DFU);
448+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp_DFU_obj, esp_DFU);
514449

515-
STATIC mp_obj_t esp_setn(mp_obj_t cnter, mp_obj_t fsize) {
516-
dfu_nblks = mp_obj_get_int(cnter);
450+
STATIC mp_obj_t esp_set_dfu(mp_obj_t counter, mp_obj_t fsize) {
451+
dfu_nblks = mp_obj_get_int(counter);
517452
dfu_fwsize = mp_obj_get_int(fsize);
518453
return mp_const_none;
519454
}
520-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_setn_obj, esp_setn);
455+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_set_dfu_obj, esp_set_dfu);
521456

522-
STATIC mp_obj_t esp_getn() {
523-
return mp_obj_new_int(dfu_nblks);
524-
}
525-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_getn_obj, esp_getn);
526-
527-
// STATIC mp_obj_t esp_ram_read_flash(mp_obj_t offset, mp_obj_t size) {
528-
// int sz = mp_obj_get_int(size);
529-
// char *buf = malloc(sz);
530-
// if(!buf){
531-
// mp_raise_OSError(MP_ENOMEM);
532-
// return mp_const_none;
533-
// }
534-
// ram_read_flash(mp_obj_get_int(offset), buf, sz);
535-
// mp_obj_t ret_bytes = mp_obj_new_bytes(buf, sz);
536-
// free(buf);
537-
// return ret_bytes;
538-
// }
539-
// STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp_ram_read_flash_obj, esp_ram_read_flash);
540-
541-
STATIC mp_obj_t esp_showblks() {
457+
STATIC mp_obj_t esp_get_blks() {
542458
if(dfu_nblks<=0)
543459
return mp_obj_new_list(0, NULL), mp_obj_new_list(0, NULL);
544460
mp_obj_t ret1 = mp_obj_new_list(0, NULL);
@@ -552,7 +468,7 @@ STATIC mp_obj_t esp_showblks() {
552468
mp_obj_list_append(ret, ret2);
553469
return ret;
554470
}
555-
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_showblks_obj, esp_showblks);
471+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(esp_get_blks_obj, esp_get_blks);
556472

557473
STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
558474
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp) },
@@ -568,11 +484,9 @@ STATIC const mp_rom_map_elem_t esp_module_globals_table[] = {
568484
{ MP_ROM_QSTR(MP_QSTR_flash_user_start), MP_ROM_PTR(&esp_flash_user_start_obj) },
569485

570486
{ MP_ROM_QSTR(MP_QSTR_DFU), MP_ROM_PTR(&esp_DFU_obj) },
571-
{ MP_ROM_QSTR(MP_QSTR_showMemory), MP_ROM_PTR(&esp_showMemory_obj) },
572-
{ MP_ROM_QSTR(MP_QSTR_setn), MP_ROM_PTR(&esp_setn_obj) },
573-
{ MP_ROM_QSTR(MP_QSTR_getn), MP_ROM_PTR(&esp_getn_obj) },
574-
{ MP_ROM_QSTR(MP_QSTR_showblks), MP_ROM_PTR(&esp_showblks_obj) },
575-
// { MP_ROM_QSTR(MP_QSTR_ram_read_flash), MP_ROM_PTR(&esp_ram_read_flash_obj) },
487+
// { MP_ROM_QSTR(MP_QSTR_showMemory), MP_ROM_PTR(&esp_showMemory_obj) },
488+
{ MP_ROM_QSTR(MP_QSTR_set_dfu), MP_ROM_PTR(&esp_set_dfu_obj) },
489+
{ MP_ROM_QSTR(MP_QSTR_get_blks), MP_ROM_PTR(&esp_get_blks_obj) },
576490

577491
#if MICROPY_ESP8266_APA102
578492
{ MP_ROM_QSTR(MP_QSTR_apa102_write), MP_ROM_PTR(&esp_apa102_write_obj) },

ports/esp8266/modules/apa102.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# APA102 driver for MicroPython on ESP8266
2+
# MIT license; Copyright (c) 2016 Robert Foss, Daniel Busch
3+
4+
from esp import apa102_write
5+
from neopixel import NeoPixel
6+
7+
8+
class APA102(NeoPixel):
9+
ORDER = (0, 1, 2, 3)
10+
11+
def __init__(self, clock_pin, data_pin, n, bpp=4):
12+
super().__init__(data_pin, n, bpp)
13+
self.clock_pin = clock_pin
14+
self.clock_pin.init(clock_pin.OUT)
15+
16+
def write(self):
17+
apa102_write(self.clock_pin, self.pin, self.buf)

ports/esp8266/modules/inisetup.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,37 @@
33
from flashbdev import bdev
44

55

6-
def fwupdate(fn, erase_all=False, verbose=False):
6+
def fwupdate(fn, erase_all=False, safe_check=True, verbose=True):
77
import esp
88
fw_size = rem_fs = os.stat(fn)[6]
9-
esp.setn(0, fw_size)
9+
esp.set_dfu(0, fw_size)
1010
with open(fn, 'rb') as fp:
11-
while fp.read(4096):pass
11+
while fp.read(4096):
12+
pass
13+
14+
if safe_check:
15+
blks, offs = esp.get_blks()
16+
esp.set_dfu(-1, fw_size)
17+
if verbose:
18+
print(f'Verifying sector data:{blks} {offs}')
19+
with open(fn, 'rb') as fp:
20+
for ii, blkid in enumerate(blks):
21+
sz = min(rem_fs, 4096-offs[ii])
22+
L2 = esp.flash_read(esp.flash_user_start()+blkid*4096+offs[ii], sz)
23+
L1 = fp.read(sz)
24+
if L1 != L2:
25+
raise Exception('Data is different at N={ii} blkid={blkid}')
26+
del L1, L2
27+
rem_fs -= sz
28+
if verbose:
29+
print(f'{ii}/{len(blks)}', end='\r')
30+
esp.set_dfu(len(blks), fw_size)
31+
del blks, offs
32+
if verbose:
33+
print('Success, starting firmware update ...')
34+
35+
esp.DFU(erase_all)
1236

13-
blks, offs = esp.showblks()
14-
nblks = esp.getn()
15-
assert nblks == len(blks)
16-
esp.setn(-1, fw_size)
17-
if verbose: print(f'Verifying sector data:{blks} {offs}')
18-
with open(fn, 'rb') as fp:
19-
for ii,blkid in enumerate(blks):
20-
sz = min(rem_fs, 4096-offs[ii])
21-
L2 = esp.flash_read(esp.flash_user_start()+blkid*4096+offs[ii], sz)
22-
L1 = fp.read(sz)
23-
if L1!=L2:
24-
if verbose: print(f'Data is different at {ii} {blkid}')
25-
return
26-
del L1, L2
27-
rem_fs -= sz
28-
if verbose: print(f'{ii}/{len(blks)}', end='\r')
29-
if verbose: print('Success, starting firmware update, please wait ...')
30-
esp.setn(nblks, fw_size)
31-
esp.DFU()
3237

3338
def wifi():
3439
import binascii

ports/esp8266/modules/port_diag.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import esp
2+
import uctypes
3+
import network
4+
import lwip
5+
6+
7+
def main():
8+
ROM = uctypes.bytearray_at(0x40200000, 16)
9+
fid = esp.flash_id()
10+
11+
print("FlashROM:")
12+
print("Flash ID: %x (Vendor: %x Device: %x)" % (fid, fid & 0xFF, fid & 0xFF00 | fid >> 16))
13+
14+
print("Flash bootloader data:")
15+
SZ_MAP = {0: "512KB", 1: "256KB", 2: "1MB", 3: "2MB", 4: "4MB"}
16+
FREQ_MAP = {0: "40MHZ", 1: "26MHZ", 2: "20MHz", 0xF: "80MHz"}
17+
print("Byte @2: %02x" % ROM[2])
18+
print(
19+
"Byte @3: %02x (Flash size: %s Flash freq: %s)"
20+
% (ROM[3], SZ_MAP.get(ROM[3] >> 4, "?"), FREQ_MAP.get(ROM[3] & 0xF))
21+
)
22+
print("Firmware checksum:")
23+
print(esp.check_fw())
24+
25+
print("\nNetworking:")
26+
print("STA ifconfig:", network.WLAN(network.STA_IF).ifconfig())
27+
print("AP ifconfig:", network.WLAN(network.AP_IF).ifconfig())
28+
print("Free WiFi driver buffers of type:")
29+
for i, comm in enumerate(
30+
("1,2 TX", "4 Mngmt TX(len: 0x41-0x100)", "5 Mngmt TX (len: 0-0x40)", "7", "8 RX")
31+
):
32+
print("%d: %d (%s)" % (i, esp.esf_free_bufs(i), comm))
33+
print("lwIP PCBs:")
34+
lwip.print_pcbs()
35+
36+
37+
main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy