Day: September 27, 2022

How to get NuttX RTOS working on old ESP32-Devkit with 2MB SPIFLASH

After compiling and flash NuttX with wapi example:
$ ./tools/configure.sh esp32-devkitc:wapi

$ make -j flash ESPTOOL_PORT=/dev/ttyUSB0


I got this error:

I (29) boot: ESP-IDF v5.0-dev-1181-g0132079fa5 2nd stage bootloader
I (29) boot: compile time 14:26:53
I (29) boot: chip revision: 1
I (34) boot_comm: chip revision: 1, min. bootloader chip revision: 0
I (41) boot.esp32: SPI Speed      : 40MHz
I (45) boot.esp32: SPI Mode       : DIO
I (50) boot.esp32: SPI Flash Size : 2MB
I (54) boot: Enabling RNG early entropy source...
I (60) boot: Partition Table:
I (63) boot: ## Label            Usage          Type ST Offset   Length
I (71) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (78) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (86) boot:  2 factory          factory app      00 00 00010000 00100000
I (93) boot: End of partition table
I (97) boot_comm: chip revision: 1, min. application chip revision: 0
I (104) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=13030h ( 77872) map
I (141) esp_image: segment 1: paddr=00023058 vaddr=3ffbca40 size=02284h (  8836) load
I (145) esp_image: segment 2: paddr=000252e4 vaddr=40080000 size=0ad34h ( 44340) load
I (165) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=6c2c0h (443072) map
I (326) esp_image: segment 4: paddr=0009c2e8 vaddr=4008ad34 size=01730h (  5936) load
I (335) boot: Loaded app from partition at offset 0x10000
I (335) boot: Disabling RNG early entropy source...
�ABup_assert: Assertion failed at file:chip/esp32_spiflash.c line: 2011 task: nsh_main
xtensa_registerdump:    PC: 400d76e5    PS: 00060f20
xtensa_registerdump:    A0: 800d7019    A1: 3ffe1350    A2: 00000000    A3: 3ffb25fe
xtensa_registerdump:    A4: 20000000    A5: 3ffafc90    A6: 3ffb04a8    A7: fffffffc
xtensa_registerdump:    A8: 800f6ac8    A9: 3ffe12c0   A10: 00000054   A11: 3f4009b4
xtensa_registerdump:   A12: 3ffe13a0   A13: 3ffe1380   A14: 00000008   A15: 00000000
xtensa_registerdump:   SAR: 00000004 CAUSE: 3ffe1380 VADDR: 00000008
xtensa_registerdump:  LBEG: 4000c46c  LEND: 4000c477  LCNT: 00000000
xtensa_dump_stack: IRQ Stack:
xtensa_dump_stack: sp:     3ffe1370
xtensa_dump_stack:   base: 3ffbca40
xtensa_dump_stack:   size: 00000800
xtensa_dump_stack:   used: 00000000
xtensa_dump_stack: ERROR: IRQ Stack pointer is not within the stack
xtensa_dump_stack: User Stack:
xtensa_dump_stack: sp:     3ffe1370
xtensa_dump_stack:   base: 3ffe0570
xtensa_dump_stack:   size: 00000fc0
xtensa_dump_stack:   used: 000004d8
xtensa_stackdump: 3ffe1360: 800d68c2 3ffe13a0 00000000 3f4009b4 00000000 3ffe1380 00000008 deadbeef
xtensa_stackdump: 3ffe1380: 3ffafd00 3ffe1370 3ffb23e4 00000002 800d445c 3ffe13c0 3ffbec84 000007db
xtensa_stackdump: 3ffe13a0: 3f400a57 3ffbec84 000007db 3ffafd7c 80084a60 3ffe13e0 3ffbec84 000007db
xtensa_stackdump: 3ffe13c0: 00060920 00000041 00000000 00000000 800f4d52 3ffe1400 00280000 000b0000
xtensa_stackdump: 3ffe13e0: 00000064 3ffe13b0 3ffbf060 3ffae700 800f4d10 3ffe1420 00000000 3ffe1600
xtensa_stackdump: 3ffe1400: 00000000 3ffae700 3f401c4d 3f401c51 800f4ce6 3ffe1440 00000000 00000035
xtensa_stackdump: 3ffe1420: 3f402360 00000000 00000003 00000000 800e9590 3ffe1460 00000000 0000007e
xtensa_stackdump: 3ffe1440: 3ffafc90 3ffafc70 3ffafd00 00000000 800d7b47 3ffe1480 ffffffe7 00000000
xtensa_stackdump: 3ffe1460: 3ffafcb0 3ffafc90 3ffb04a8 fffffffc 800d7b00 3ffe14a0 00000000 3ffe14c0
xtensa_stackdump: 3ffe1480: 00000000 3ffe15a0 00060020 3ffb04a0 800d4498 3ffe14c0 00000001 3ffe0550
xtensa_stackdump: 3ffe14a0: 00000000 3ffafcd0 00000002 00000000 800d290f 3ffe14f0 400d7ae0 00000001
xtensa_stackdump: 3ffe14c0: 00000064 00000000 00000000 00000000 00000064 3ffe15dc 3ffafdb0 3ffb0368
xtensa_stackdump: 3ffe14e0: 00000000 3ffe1510 00000000 00000000 3ffe0550 3f400420 00000000 3ffb00dc
xtensa_stackdump: 3ffe1500: 00000000 3ffe1530 00000000 00000000 00000000 00000000 00000000 00000000
xtensa_showtasks:    PID    PRI     STACK      USED   FILLED    COMMAND
xtensa_showtasks:   ----   ----      2048         0     0.0%    irq
xtensa_dump_task:      0      0      3040       512    16.8%    Idle Task
xtensa_dump_task:      1    100      4032       376     9.3%    lpwork 0x3ffb04a0
xtensa_dump_task:      2    100      4032      1336    33.1%    nsh_main

It is happening because the MTD partitions are located at OFFSET+SIZE that went over the 2MB Flash:

CONFIG_ESP32_STORAGE_MTD_OFFSET=0x180000
CONFIG_ESP32_STORAGE_MTD_SIZE=0x80000
...
CONFIG_ESP32_WIFI_MTD_OFFSET=0x280000
CONFIG_ESP32_WIFI_MTD_SIZE=0xb0000

See 2MB means 0x200000 then the partitions are going beyond that.

An option to fix it is reducing the partition size and moving the WIFI_MTD_OFFSET to below 0x200000:

CONFIG_ESP32_STORAGE_MTD_OFFSET=0x180000
CONFIG_ESP32_STORAGE_MTD_SIZE=0x10000
...
CONFIG_ESP32_WIFI_MTD_OFFSET=0x190000
CONFIG_ESP32_WIFI_MTD_SIZE=0x10000

Now everything is working as expected:

nsh> wapi psk wlan0 MyPassword 3
nsh> wapi essid wlan0 MyRouterName 1
nsh> renew wlan0
nsh> ifconfig
wlan0   Link encap:Ethernet HWaddr ac:67:b2:53:8b:ec at UP
        inet addr:192.168.0.7 DRaddr:192.168.0.1 Mask:255.255.255.0
                                                                        
             IPv4   TCP   UDP  ICMP                                     
Received     0004  0000  0002  0002                                     
Dropped      0000  0000  0000  0000                                     
  IPv4        VHL: 0000   Frg: 0000
  Checksum   0000  0000  0000  ----
  TCP         ACK: 0000   SYN: 0000
              RST: 0000  0000
  Type       0000  ----  ----  0000
Sent         0004  0000  0002  0002
  Rexmit     ----  0000  ----  ----
nsh> ping 8.8.8.8
PING 8.8.8.8 56 bytes of data
56 bytes from 8.8.8.8: icmp_seq=0 time=50.0 ms
56 bytes from 8.8.8.8: icmp_seq=1 time=40.0 ms
56 bytes from 8.8.8.8: icmp_seq=2 time=50.0 ms
56 bytes from 8.8.8.8: icmp_seq=3 time=50.0 ms
56 bytes from 8.8.8.8: icmp_seq=4 time=60.0 ms
56 bytes from 8.8.8.8: icmp_seq=5 time=50.0 ms
56 bytes from 8.8.8.8: icmp_seq=6 time=40.0 ms
56 bytes from 8.8.8.8: icmp_seq=7 time=40.0 ms
56 bytes from 8.8.8.8: icmp_seq=8 time=40.0 ms
56 bytes from 8.8.8.8: icmp_seq=9 time=50.0 ms
10 packets transmitted, 10 received, 0% packet loss, time 10100 ms
rtt min/avg/max/mdev = 40.000/47.000/60.000/6.403 ms