nodemcu-firmware icon indicating copy to clipboard operation
nodemcu-firmware copied to clipboard

rtctime.dsleep fails with integer argument on Lua 5.3 build

Open lsborg opened this issue 4 years ago • 6 comments
trafficstars

Expected behavior

rtctime.dsleep(2808000000) should put microcontroller to sleep

Actual behavior

Lua error: stdin:1: bad argument #1 to 'dsleep' (number has no integer representation) stack traceback: [C]: in function 'rtctime.dsleep' stdin:1: in main chunk [C]: in ? [C]: in ?

Test code

Provide a Minimal, Complete, and Verifiable example which will reproduce the problem.

rtctime.dsleep(2808000000)

NodeMCU startup banner

NodeMCU 3.0.0.0 built with Docker provided by frightanic.com branch: release commit: d4ae3c364bd8ae3ded8b77d35745b7f07879f5f9 release: 3.0.0-release_20210201 +1 release DTS: 202105102018 SSL: false build type: float LFS: 0x10000 bytes total capacity modules: adc,encoder,file,gpio,http,net,node,rtctime,sjson,sntp,tmr,uart,wifi build 2021-08-01 02:08 powered by Lua 5.3.5 on SDK 3.0.1-dev(fce080e)

Hardware

Nodemcu ESP8266

Other

node.dsleep(2808000000) works fine.

lsborg avatar Aug 01 '21 03:08 lsborg

rtctime.dsleep() uses integer internally: https://github.com/nodemcu/nodemcu-firmware/blob/dev/app/modules/rtctime.c#L188. Hence, the maximum is 4294967295 as documented: https://nodemcu.readthedocs.io/en/latest/modules/rtctime/#rtctimedsleep. node.dsleep() on the other hand uses long (64bit) internally: https://github.com/nodemcu/nodemcu-firmware/blob/dev/app/modules/node.c#L171. See the note about the maximum in the docs: https://nodemcu.readthedocs.io/en/latest/modules/node/#nodedsleep

However, as your 2808000000 value is below the 4294967295 maximum I don't understand what's going on here.

marcelstoer avatar Aug 01 '21 11:08 marcelstoer

Thanks for your answer @marcelstoer

New info, it works with lua 5.1

NodeMCU startup banner

NodeMCU 3.0.0.0 built with Docker provided by frightanic.com branch: release commit: d4ae3c364bd8ae3ded8b77d35745b7f07879f5f9 release: 3.0.0-release_20210201 +1 release DTS: 202105102018 SSL: false build type: float LFS: 0x10000 bytes total capacity modules: adc,encoder,file,gpio,http,net,node,rtctime,sjson,sntp,tmr,uart,wifi build 2021-08-01 23:28 powered by Lua 5.1.4 on SDK 3.0.1-dev(fce080e)

lsborg avatar Aug 01 '21 23:08 lsborg

@jmattsson is there a good reason rtctime.dsleep should not be modeled after what we have in node.dsleep (using uint64 us;)?

https://github.com/nodemcu/nodemcu-firmware/blob/949875d590e771603a73ed3a90928d48ea3dd38b/app/modules/node.c#L182-L189

marcelstoer avatar Aug 02 '21 05:08 marcelstoer

IIRC (and it's been years since I worked in this area), the sleep time limitation came from the 32bit registers used to configure the chip for wakeup.

jmattsson avatar Aug 02 '21 05:08 jmattsson

Oh, true, it even says so in the rtcmem docs. What could be the reason 2808000000 works with Lua 5.1 but not with Lua 5.3?

marcelstoer avatar Aug 02 '21 07:08 marcelstoer

Based on that error message, it looks to me like either lua_tounsigned() is still trying to fit it into a signed 32bit integer (max value 2147483648), or the lua_isinteger() is the one doing it. Is there a lua_isunsigned() in Lua 5.3?

jmattsson avatar Aug 02 '21 07:08 jmattsson