libretiny-eu / libretiny

PlatformIO development platform for IoT modules
http://docs.libretiny.eu/
MIT License
383 stars 55 forks source link

WR02 crashes at "Starting WiFi scan" #146

Closed Sonic-Amiga closed 1 year ago

Sonic-Amiga commented 1 year ago

Module: Tuya WR02 Device YML:

esphome:
  name: yndx-0007

libretiny:
  board: generic-rtl8710bn-2mb-788k
  framework:
    version: latest
  # verbose/trace/debug/info/warn/error/fatal
  loglevel: debug
  # suppress chip's SDK log messages
  # (same as LT_UART_SILENT_ALL above)
  sdk_silent: false
  # disable SWD/JTAG so that all GPIOs can be used
  # set to false if you want to attach a debugger
  gpio_recover: false

logger:
  hardware_uart: "SERIAL2"
api:
  password: ""
ota:
  password: ""

wifi:
  ssid: "MyWifi"
  password: "TopSecret"
  ap:
    ssid: "yndx-0007"
    password: ""

captive_portal:
web_server:

Boot log:

I [      0.000] LibreTiny v1.1.0 on generic-rtl8710bn-2mb-788k, compiled at Jul  2 2023 23:08:55, GCC 10.3.1 (-Os)
I [      0.000] Reset reason: 1
▒I [      0.002] Preferences initialized
▒+L▒&▒[▒RW+[▒▒V▒.▒&L▒W'HS▒R-+▒W++K▒▒m▒▒j5R▒[C][ota:477]: There have been 5 suspected unsuccessful boot attempts.
[D][lt.preferences:104]: Saving 1 preferences to flash...
[D][lt.preferences:132]: Saving 1 preferences to flash: 0 cached, 1 written, 0 failed
[I][app:029]: Running through setup()...
[C][wifi:038]: Setting up WiFi...
[C][wifi:039]:   Local MAC: 70:89:76:DC:31:2D
D [      0.054] WIFI: Mode changing 0 -> 1
I [      0.059] WIFI: Initializing LwIP
interface 0 is initialized
                          interface 1 is initialized
                                                    D [      0.068] WIFI: WLAN: wlan0=0, wlan1=0

Initializing WIFI ...
LDO Mode, BD_Info: 0

LDO Mode, BD_Info: 0

WIFI initialized
                D [      0.233] WIFI: WLAN: wlan0=1, wlan1=0
D [      0.238] WIFI: WLAN: wlan0=1, wlan1=0
D [      0.253] WIFI: WiFi sleep mode 0
[D][wifi:392]: Starting scan...
D [      0.261] WIFI: WLAN: wlan0=1, wlan1=0
D [      0.266] WIFI: WLAN: wlan0=1, wlan1=0
I [      0.271] WIFI: Starting WiFi scan
RTL8195A[HAL]: Hard Fault Error!!!!
RTL8195A[HAL]: R0 = 0x0
RTL8195A[HAL]: R1 = 0x50
RTL8195A[HAL]: R2 = 0x0
RTL8195A[HAL]: R3 = 0x0
RTL8195A[HAL]: R12 = 0xa5a5a5a5
RTL8195A[HAL]: LR = 0x802f3b3
RTL8195A[HAL]: PC = 0x802fed4
RTL8195A[HAL]: PSR = 0x21000000
RTL8195A[HAL]: BFAR = 0xe000ed38
RTL8195A[HAL]: CFSR = 0x10000
RTL8195A[HAL]: HFSR = 0x40000000
RTL8195A[HAL]: DFSR = 0x0
RTL8195A[HAL]: AFSR = 0x0
RTL8195A[HAL]: PriMask 0x0
RTL8195A[HAL]: BasePri 0x50
RTL8195A[HAL]: SVC priority: 0x00
RTL8195A[HAL]: PendSVC priority: 0xf0
RTL8195A[HAL]: Systick priority: 0xf0

According to addr2line, PC points at:

D:\Projects\YandexSocket\libretiny-esphome>C:\Users\pavel\.platformio\packages\toolchain-gccarmnoneeabi\bin\arm-none-eabi-addr2line -e .esphome\build\yndx-0007\.pioenvs\yndx-0007\raw_firmware.ota1.elf 0x802fed4
C:\Users\pavel\.platformio\packages\library-freertos\FreeRTOS\Source/tasks.c:1673

And LR is:

D:\Projects\YandexSocket\libretiny-esphome>C:\Users\pavel\.platformio\packages\toolchain-gccarmnoneeabi\bin\arm-none-eabi-addr2line -e .esphome\build\yndx-0007\.pioenvs\yndx-0007\raw_firmware.ota1.elf 0x802f3b3
C:\Users\pavel\.platformio\packages\library-freertos-port\realtek-ambz/port.c:402

C code at these locations is nothing suspicious, it's not a nullptr dereference:

 802fecc:   4d1e        ldr r5, [pc, #120]  ; (802ff48 <xTaskResumeAll+0xb8>)
 802fece:   2601        movs    r6, #1
 802fed0:   6823        ldr r3, [r4, #0]
 802fed2:   bb3b        cbnz    r3, 802ff24 <xTaskResumeAll+0x94>
 802fed4:   4b1c        ldr r3, [pc, #112]  ; (802ff48 <xTaskResumeAll+0xb8>)   // <---- This is the crash location
 802fed6:   681c        ldr r4, [r3, #0]
 802fed8:   2c01        cmp r4, #1
 802feda:   d1e3        bne.n   802fea4 <xTaskResumeAll+0x14>

And LR location is:

0802f3ac <vPortEnterCritical>:
 802f3ac:   b508        push    {r3, lr}
 802f3ae:   f7ff fff0   bl  802f392 <ulPortSetInterruptMask>
 802f3b2:   4a04        ldr r2, [pc, #16]   ; (802f3c4 <vPortEnterCritical+0x18>) // <--- here
 802f3b4:   6813        ldr r3, [r2, #0]
 802f3b6:   3301        adds    r3, #1
 802f3b8:   6013        str r3, [r2, #0]
 802f3ba:   f3bf 8f4f   dsb sy
 802f3be:   f3bf 8f6f   isb sy
 802f3c2:   bd08        pop {r3, pc}
 802f3c4:   10005ba4    .word   0x10005ba4

ulPortSetInterruptMask is very straightforward:

 0802f392 <ulPortSetInterruptMask>:
 802f392:   f3ef 8011   mrs r0, BASEPRI
 802f396:   f04f 0150   mov.w   r1, #80 ; 0x50
 802f39a:   f381 8811   msr BASEPRI, r1
 802f39e:   4770        bx  lr
 802f3a0:   2000        movs    r0, #0

So how come we jump into xTaskResumeAll() right after ulPortSetInterruptMask () completes ? Does it have to do with interrupts, and xTaskResumeAll() is a part of scheduler ?

Sonic-Amiga commented 1 year ago

The root cause appeared bad power supply. I powered the module from "VCCIO" pin on my FT232 board (after setting 3.3V of course), and that was the mistake. The board yields enough power for flashing and basic functions, but it's not enough for the radio. Solution: use separate power.