nodemcu / nodemcu-firmware

Lua based interactive firmware for ESP8266, ESP8285 and ESP32
https://nodemcu.readthedocs.io
MIT License
7.61k stars 3.12k forks source link

Port of the rotary module to ESP32 #3625

Closed pjsg closed 2 months ago

pjsg commented 5 months ago

Fixes #3624

This is a pretty straight port of the module from the esp8266 version. I did make it object oriented.

serg3295 commented 5 months ago

I have tested the module and noticed a strange behavior. If a large amount of information is printed inside the callback function, the ESP restarts, or the rotary module freezes. If output message is short, the module works fine.

-- Test script
local CLK         = 5  -- pin a
local DT          = 18 -- pin b
local SW          = 19 -- 'NO' pushbutton switch
local eventNumber = 0

local eventType = {
  [1]  = "PRESS",
  [2]  = "LONGPRESS",
  [4]  = "RELEASE",
  [8]  = "TURN",
  [16] = "CLICK",
  [32] = "DBLCLICK",
  [63] = "ALL"
}

local sw = rotary.setup(CLK, DT, SW)
sw:on(rotary.ALL, function(type, pos, when)
  -- print(eventType[type]) -- no restart
  -- print(eventType[type], eventNumber) -- restarts after ~150 - 200 events
  print("All Events. Position=" .. pos .. " event type=" .. eventType[type] .. " time=" .. when)  -- restarts after ~20-50 events
  eventNumber = eventNumber + 1
end)
Log_1: node.restart() ets Jun 8 2016 00:22:57 rst:0xc (SW_CPU_RESET),boot:0x12 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0030,len:6940 ho 0 tail 12 room 4 load:0x40078000,len:15500 load:0x40080400,len:3844 entry 0x4008064c I (29) boot: ESP-IDF v5.0.2 2nd stage bootloader I (29) boot: compile time 21:00:08 I (29) boot: chip revision: v1.0 I (32) boot.esp32: SPI Speed : 40MHz I (37) boot.esp32: SPI Mode : DIO I (41) boot.esp32: SPI Flash Size : 4MB I (46) boot: Enabling RNG early entropy source... I (51) boot: Partition Table: I (55) boot: ## Label Usage Type ST Offset Length I (62) boot: 0 nvs WiFi data 01 02 00009000 00006000 I (69) boot: 1 phy_init RF data 01 01 0000f000 00001000 I (77) boot: 2 factory factory app 00 00 00010000 00180000 I (84) boot: 3 lfs unknown c2 01 00190000 00010000 I (92) boot: 4 storage Unknown data 01 82 001a0000 00070000 I (99) boot: End of partition table I (103) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=2ea8ch (191116) map I (181) esp_image: segment 1: paddr=0003eab4 vaddr=3ff80063 size=00008h ( 8) load I (181) esp_image: segment 2: paddr=0003eac4 vaddr=3ffb0000 size=01554h ( 5460) load I (189) esp_image: segment 3: paddr=00040020 vaddr=400d0020 size=c4058h (802904) map I (485) esp_image: segment 4: paddr=00104080 vaddr=3ffb1554 size=01f28h ( 7976) load I (489) esp_image: segment 5: paddr=00105fb0 vaddr=40080000 size=167b4h ( 92084) load I (529) esp_image: segment 6: paddr=0011c76c vaddr=400c0000 size=00064h ( 100) load I (541) boot: Loaded app from partition at offset 0x10000 I (541) boot: Disabling RNG early entropy source... I (552) cpu_start: Pro cpu up. I (552) cpu_start: Starting app cpu, entry point is 0x40081478 I (538) cpu_start: App cpu up. I (569) cpu_start: Pro cpu start user code I (569) cpu_start: cpu freq: 160000000 Hz I (569) cpu_start: Application information: I (574) cpu_start: Project name: nodemcu I (579) cpu_start: App version: 1.4.0-master_20151229-889-gcbce I (586) cpu_start: Compile time: Feb 5 2024 15:22:56 I (592) cpu_start: ELF file SHA256: 9b716118424d3445... I (598) cpu_start: ESP-IDF: v5.0.2 I (603) cpu_start: Min chip rev: v0.0 I (607) cpu_start: Max chip rev: v3.99 I (612) cpu_start: Chip rev: v1.0 I (617) heap_init: Initializing. RAM available for dynamic allocation: I (624) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM I (630) heap_init: At 3FFB7B00 len 00028500 (161 KiB): DRAM I (636) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM I (643) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM I (649) heap_init: At 400967B4 len 0000984C (38 KiB): IRAM I (657) spi_flash: detected chip: generic I (660) spi_flash: flash io: dio W (665) pcnt(legacy): legacy driver is deprecated, please migrate to `driver/pulse_cnt.h` W (673) rmt(legacy): legacy driver is deprecated, please migrate to `driver/rmt_tx.h` and/or `driver/rmt_rx.h` W (683) ADC: legacy driver is deprecated, please migrate to `esp_adc/adc_oneshot.h` I (692) cpu_start: Starting scheduler on PRO CPU. I (0) cpu_start: Starting scheduler on APP CPU. I (704) uart: queue free spaces: 3 I (824) wifi:wifi driver task: 3ffc7ef4, prio:23, stack:6656, core=0 I (824) system_api: Base MAC address is not set I (824) system_api: read default base MAC address from EFUSE I (854) wifi:wifi firmware version: 57982fe I (854) wifi:wifi certification version: v7.0 I (854) wifi:config NVS flash: enabled I (854) wifi:config nano formating: disabled I (854) wifi:Init data frame dynamic rx buffer num: 32 I (854) wifi:Init management frame dynamic rx buffer num: 32 I (864) wifi:Init management short buffer num: 32 I (864) wifi:Init dynamic tx buffer num: 32 I (874) wifi:Init static rx buffer size: 1600 I (874) wifi:Init static rx buffer num: 10 I (884) wifi:Init dynamic rx buffer num: 32 I (884) wifi_init: rx ba win: 6 I (884) wifi_init: tcpip mbox: 32 I (894) wifi_init: udp mbox: 6 I (894) wifi_init: tcp mbox: 6 I (904) wifi_init: tcp tx win: 5744 I (904) wifi_init: tcp rx win: 5744 I (904) wifi_init: tcp mss: 1440 I (914) wifi_init: WiFi IRAM OP enabled I (914) wifi_init: WiFi RX IRAM OP enabled NodeMCU ESP32 build unspecified powered by Lua 5.3.5 [5.3-int32-doublefp] on IDF v5.0.2 cannot open init.lua: No such file or directory I (41424) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 I (41424) gpio: GPIO[18]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 I (41424) gpio: GPIO[19]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 All Events. Position=1 event type=TURN time=43290794 All Events. Position=1 event type=PRESS time=43347060 All Events. Position=1 event type=RELEASE time=43476162 All Events. Position=0 event type=TURN time=43503173 All Events. Position=0 event type=CLICK time=43976162 All Events. Position=1 event type=TURN time=45502741 All Events. Position=2 event type=TURN time=45510137 All Events. Position=3 event type=TURN time=45520321 All Events. Position=4 event type=TURN time=45526621 All Events. Position=5 event type=TURN time=45536010 All Events. Position=6 event type=TURN time=45543211 All Events. Position=7 event type=TURN time=45560862 All Events. Position=8 event type=TURN time=45572447 All Events. Position=9 event type=TURN time=45599443 All Events. Position=10 event type=TURN time=45624414 All Events. Position=9 event type=TURN time=46527898 All Events. Position=8 event type=TURN time=46533794 All Events. Position=7 event type=TURN time=46542013 All Events. Position=6 event type=TURN time=46549055 All Events. Position=5 event type=TURN time=46557744 All Events. Position=4 event type=TURN time=46564080 All Events. Position=3 event type=TURN time=46571953 All Events. Position=2 event type=TURN time=46581096 All Events. Position=1 event type=TURN time=46625437 All Events. Position=2 event type=TURN time=47380063 All Events. Position=3 event type=TURN time=47452410 All Events. Position=4 event type=TURN time=47460609 All Events. Position=5 event type=TURN time=47466340 All Events. Position=6 event type=TURN time=47472378 All Events. Position=7 event type=TURN time=47480292 All Events. Position=8 event type=TURN time=47487135 All Events. Position=9 event type=TURN time=47492702 All Events. Position=10 event type=TURN time=47499684 All Events. Position=11 event type=TURN time=47507439 I (48174) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 I (48174) gpio: GPIO[18]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 I (48184) gpio: GPIO[19]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 All Events. Position=12 event type=TURN time=47515368 Guru Meditation Error: Core 0 panic'ed (StoreProhibited). Exception was unhandled. Core 0 register dump: PC : 0x4018c829 PS : 0x00060430 A0 : 0x800dd633 A1 : 0x3ffbb770 A2 : 0x00000000 A3 : 0xfffffffe A4 : 0x00000003 A5 : 0x0000000c A6 : 0x02d506e8 A7 : 0x003da226 A8 : 0x00000000 A9 : 0x3ffbb750 A10 : 0x00000000 A11 : 0x00000000 A12 : 0x00000000 A13 : 0xfffffffd A14 : 0x6868000a A15 : 0x3ffc314c SAR : 0x00000020 EXCCAUSE: 0x0000001d EXCVADDR: 0x00000003 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffffd Backtrace: 0x4018c826:0x3ffbb770 0x400dd630:0x3ffbb790 0x400f8585:0x3ffbb7c0 0x400d7a67:0x3ffbb7f0 0x4019252b:0x3ffbb810 ELF file SHA256: 9b716118424d3445 Rebooting...

*****
Sometimes the module stops processing events, but the ESP responds to commands

Log2: All Events. Position=0 event type=PRESS time=5273872 All Events. Position=0 event type=RELEASE time=5433321 All Events. Position=0 event type=CLICK time=5933321 All Events. Position=0 event type=PRESS time=5999769 All Events. Position=0 event type=RELEASE time=6177546 All Events. Position=0 event type=PRESS time=6516123 All Events. Position=0 event type=RELEASE time=6697773 All Events. Position=0 event type=DBLCLICK time=6697773 All Events. Position=0 event type=PRESS time=7002153 All Events. Position=0 event type=RELEASE time=7148530 All Events. Position=0 event type=PRESS time=7437229 All Events. Position=0 event type=RELEASE time=7591457 All Events. Position=0 event type=DBLCLICK time=7591457 All Events. Position=0 event type=PRESS time=7909872 All Events. Position=0 event type=RELEASE time=8057767 All Events. Position=0 event type=PRESS time=8366288 All Events. Position=0 event type=RELEASE time=8519719 All Events. Position=0 event type=DBLCLICK time=8519719 All Events. Position=0 event type=PRESS time=8816157 All Events. Position=0 event type=RELEASE time=8989804 All Events. Position=0 event type=PRESS time=9271649 All Events. Position=0 event type=RELEASE time=9440399 All Events. Position=0 event type=DBLCLICK time=9440399 All Events. Position=0 event type=PRESS time=9709663 All Events. Position=0 event type=RELEASE time=9881721 All Events. Position=0 event type=PRESS time=10202613 All Events. Position=0 event type=RELEASE time=10357388 All Events. Position=0 event type=DBLCLICK time=10357388 All Events. Position=0 event type=PRESS time=10664506 All Events. Position=0 event type=RELEASE time=10819630 All Events. Position=0 event type=PRESS time=11113301 All Events. Position=0 event type=RELEASE time=11293918 All Events. Position=0 event type=DBLCLICK time=11293918 All Events. Position=0 event type=PRESS time=11595329 All Events. Position=0 event type=RELEASE time=11753979 All Events. Position=0 event type=CLICK time=12253979 All Events. Position=0 event type=PRESS time=12806025 I (13964) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 I (13964) gpio: GPIO[18]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 I (13974) gpio: GPIO[19]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0 All Events. Position=0 event type=LONGPRESS time=13306025 =node.heap() 206892 local p,r,c=print,string.rep,0;local function f(m,t)p(r('=',9)..m..r('=',9))for k,v in pairs(t)do p(k,v)if type(k)=='number'then c=c+1 end end;return c end;f(' _G ',_G)f(' package.loaded ',_G.package.loaded)p('Regs:',f(' Regs ',debug.getregistry())) ========= _G ========= package table: 0x3ffccf30 _G table: 0x3ffc2588 _VERSION Lua 5.3 io table: 0x3ffcd360 require function: 0x3ffcd288 ========= package.loaded ========= package table: 0x3ffccf30 _G table: 0x3ffc2588 io table: 0x3ffcd360 ========= Regs ========= 1 thread: 0x3ffc1fb4 2 table: 0x3ffc2588 3 table: 0x3ffc5274 4 10 _IO_output file (0x3ffaeb44) pulsecnt.pctr table: 0x3f408928 LUABOX table: 0x3ffc55b8 5 4 sjson.decoder table: 0x3f406cb0 7 6 onerror function: 0x400dc178 9 8 _IO_input file (0x3ffaeadc) net.tcpsocket table: 0x3f405d50 stdin Pipe: 0x3ffc5060 sjson.encoder table: 0x3f406d00 FILE* table: 0x3ffcd6d8 8 7 net.tcpserver table: 0x3f405dc0 6 5 _LOADED table: 0x3ffc4db0 0 9 net.udpsocket table: 0x3f405ca0 rotary.switch table: 0x3f4069ec mqtt.mt table: 0x3f4056a8 tmr.timer table: 0x3f40716c _PRELOAD table: 0x3ffcd264 http.context table: 0x3f404e50 Regs: 10
jmattsson commented 5 months ago

EXCVADDR: 0x00000003 That's effectively a null pointer dereference crash. Probably into the second element of a struct with a 32bit first element.

If you use idf.py monitor you'll probably get that stack backtrace decoded and the offending function pinpointed.

pjsg commented 4 months ago

What is interesting is that something is reconfiguring the GPIO just before the final print. I wonder if you try and initialize the rotary module twice on the same set of gpios, then maybe it dies. I'm away from my dev environment at the moment, but I'll definitely try this when I get back to it. Thanks for trying this....

On Thu, Feb 8, 2024 at 4:47 PM J Mattsson @.***> wrote:

EXCVADDR: 0x00000003 That's effectively a null pointer dereference crash. Probably into the second element of a struct with a 32bit first element.

If you use idf.py monitor you'll probably get that stack backtrace decoded and the offending function pinpointed.

— Reply to this email directly, view it on GitHub https://github.com/nodemcu/nodemcu-firmware/pull/3625#issuecomment-1935158380, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALQLTKILQTZDB6XHYOIUSTYSVW3JAVCNFSM6AAAAABCXTWHCOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZVGE2TQMZYGA . You are receiving this because you authored the thread.Message ID: @.***>

serg3295 commented 4 months ago

Second calling rotary.setup() causes the module to hang.

Log 3: NodeMCU ESP32 build unspecified powered by Lua 5.3.5 [5.3-int32-doublefp] on IDF v5.0.2 cannot open init.lua: No such file or directory I (5054) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 I (5054) gpio: GPIO[18]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 I (5064) gpio: GPIO[19]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 TURN TURN local sw = rotary.setup(5,18, 19) I (12504) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 I (12504) gpio: GPIO[18]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 I (12514) gpio: GPIO[19]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:3 COMMENT: The module is not responding to events at this moment =node.heap() 200328
serg3295 commented 4 months ago

I found the issue. If rotary object is global the module works fine. local sw = rotary.setup() causes rebooting after a few events. I ran module under debugger. Result is:

Screenshot ![11-02-24_18-24](https://github.com/nodemcu/nodemcu-firmware/assets/75196080/788fadf0-b6d3-4377-b3f9-005eeb004efe)
pjsg commented 4 months ago

Ok. It is something in the GC that is causing the problem. I can definitely take a look at that

On Sun, Feb 11, 2024, 07:33 serg3295 @.***> wrote:

I found the issue. If rotary object is global the module works fine. local sw = rotary.setup() causes rebooting after a few events. I ran module under debugger. Result is: Screenshot

11-02-24_18-24.png (view on web) https://github.com/nodemcu/nodemcu-firmware/assets/75196080/788fadf0-b6d3-4377-b3f9-005eeb004efe

— Reply to this email directly, view it on GitHub https://github.com/nodemcu/nodemcu-firmware/pull/3625#issuecomment-1937786862, or unsubscribe https://github.com/notifications/unsubscribe-auth/AALQLTKPRABCKX7Z5QDJRL3YTDQDTAVCNFSM6AAAAABCXTWHCOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMZXG44DMOBWGI . You are receiving this because you authored the thread.Message ID: @.***>

pjsg commented 2 months ago

Sorry for the delay. This should be fixed now. Now, if you lose access to the switch object it will continue to work. If you want to get rid of the object entirely, you have to :close it first and then lose access to it.

serg3295 commented 2 months ago

I have tested the latest version of module. It works fine now. Thank you.