From c9cd875567444a106b627cef5a046c5a527f13b1 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Tue, 24 Aug 2021 22:33:08 +0200 Subject: [PATCH 1/5] Update machine_rtc.c --- ports/esp8266/machine_rtc.c | 82 +++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 4 deletions(-) diff --git a/ports/esp8266/machine_rtc.c b/ports/esp8266/machine_rtc.c index 38049ce724939..6e01d6c3f3d0a 100644 --- a/ports/esp8266/machine_rtc.c +++ b/ports/esp8266/machine_rtc.c @@ -25,6 +25,7 @@ */ #include +#include #include #include "py/runtime.h" @@ -42,7 +43,9 @@ typedef struct _pyb_rtc_obj_t { #define MEM_USER_MAGIC_ADDR (MEM_CAL_ADDR + 1) #define MEM_USER_LEN_ADDR (MEM_USER_MAGIC_ADDR + 1) #define MEM_USER_DATA_ADDR (MEM_USER_LEN_ADDR + 1) -#define MEM_USER_MAXLEN (512 - (MEM_USER_DATA_ADDR - MEM_DELTA_ADDR) * 4) +#define MEM_USER_MAXLEN (512 - (MEM_USER_DATA_ADDR - MEM_DELTA_ADDR) * 4) // 492 +#define MEM_USER_SLOTS_SIZE 4 /* Slots have 4 bytes, see https://nodemcu.readthedocs.io/en/release/modules/rtcmem/ */ +#define MEM_USER_SLOTS (MEM_USER_MAXLEN/MEM_USER_SLOTS_SIZE) // singleton RTC object STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}}; @@ -166,6 +169,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_datetime_obj, 1, 2, pyb_rtc_d STATIC mp_obj_t pyb_rtc_memory(size_t n_args, const mp_obj_t *args) { uint8_t rtcram[MEM_USER_MAXLEN]; uint32_t len; + uint32_t idx; if (n_args == 1) { // read RTC memory @@ -174,8 +178,9 @@ STATIC mp_obj_t pyb_rtc_memory(size_t n_args, const mp_obj_t *args) { system_rtc_mem_read(MEM_USER_DATA_ADDR, rtcram, (len + 3) & ~3); return mp_obj_new_bytes(rtcram, len); - } else { - // write RTC memory + } + else if (n_args == 2) { + // write RTC memory: [data] mp_buffer_info_t bufinfo; mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); @@ -196,9 +201,78 @@ STATIC mp_obj_t pyb_rtc_memory(size_t n_args, const mp_obj_t *args) { return mp_const_none; } + else if (n_args == 3) { + // read addressed memory slot: [32bit slot ID, number of slots] + +// idx = atoi(args[1]); // Linker error: undefined reference to `atoi' + idx = (uint32_t)(args[1]); + +// len = atoi(args[2]); // Linker error: undefined reference to `atoi' + len = (uint32_t)(args[2]); + + /* For some reason idx and len somehow get multiplied by 2 and +1! + We need to revert this. Since indx is always a multiple of 2, we savely can divide it again by 2 */ + idx = (idx - 1) / 2; // Slot ID, 0..122 + len = (len - 1) / 2; // Number of 4-byte slots, 0..123 + + if (idx > MEM_USER_SLOTS) { // idx must be 0..122 + mp_raise_ValueError(MP_ERROR_TEXT("index out of range")); + } + + if ((idx + len) > MEM_USER_SLOTS) { + mp_raise_ValueError(MP_ERROR_TEXT("read beyond valid range")); + } + + if (len == 0) { + mp_raise_ValueError(MP_ERROR_TEXT("must read at least 1 slot")); + } + + system_rtc_mem_read(MEM_USER_DATA_ADDR + idx, rtcram, len * 4); + return mp_obj_new_bytes(rtcram, len * 4); + } + else if (n_args == 4) { + // write addressed memory slot: [32bit slot ID, number of slots, data] + +// idx = atoi(args[1]); // Linker error: undefined reference to `atoi' + idx = (uint32_t)(args[1]); + +// len = atoi(args[2]); // Linker error: undefined reference to `atoi' + len = (uint32_t)(args[2]); + + /* For some reason idx and len somehow get multiplied by 2 and +1! + We need to revert this. Since indx is always a multiple of 2, we savely can divide it again by 2 */ + idx = (idx - 1) / 2; // Slot ID, 0..122 + len = (len - 1) / 2; // Number of 4-byte slots, 0..123 + + if (idx > MEM_USER_SLOTS) { // idx must be 0..122 + mp_raise_ValueError(MP_ERROR_TEXT("index out of range")); + } + + if ((idx + len) > MEM_USER_SLOTS) { + mp_raise_ValueError(MP_ERROR_TEXT("write beyond valid range")); + } + + if (len == 0) { + mp_raise_ValueError(MP_ERROR_TEXT("must write at least 1 slot")); + } + + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); + + int i = 0; + for (; i < bufinfo.len; i++) { + rtcram[i] = ((uint8_t *)bufinfo.buf)[i]; + } + system_rtc_mem_write(MEM_USER_DATA_ADDR + idx, rtcram, len * 4); + + return mp_const_none; + } + else { + return mp_const_none; + } } -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_memory_obj, 1, 2, pyb_rtc_memory); +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_rtc_memory_obj, 1, 4, pyb_rtc_memory); STATIC mp_obj_t pyb_rtc_alarm(mp_obj_t self_in, mp_obj_t alarm_id, mp_obj_t time_in) { (void)self_in; // unused From 8fa478f813df50b596c889fe04ada0b7b984e773 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Tue, 24 Aug 2021 22:47:08 +0200 Subject: [PATCH 2/5] Update machine_rtc.c --- ports/esp8266/machine_rtc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/esp8266/machine_rtc.c b/ports/esp8266/machine_rtc.c index 6e01d6c3f3d0a..d40a0b2440982 100644 --- a/ports/esp8266/machine_rtc.c +++ b/ports/esp8266/machine_rtc.c @@ -204,10 +204,10 @@ STATIC mp_obj_t pyb_rtc_memory(size_t n_args, const mp_obj_t *args) { else if (n_args == 3) { // read addressed memory slot: [32bit slot ID, number of slots] -// idx = atoi(args[1]); // Linker error: undefined reference to `atoi' +// idx = atoi(args[1]); // Linker error: undefined reference to 'atoi' idx = (uint32_t)(args[1]); -// len = atoi(args[2]); // Linker error: undefined reference to `atoi' +// len = atoi(args[2]); // Linker error: undefined reference to 'atoi' len = (uint32_t)(args[2]); /* For some reason idx and len somehow get multiplied by 2 and +1! @@ -233,10 +233,10 @@ STATIC mp_obj_t pyb_rtc_memory(size_t n_args, const mp_obj_t *args) { else if (n_args == 4) { // write addressed memory slot: [32bit slot ID, number of slots, data] -// idx = atoi(args[1]); // Linker error: undefined reference to `atoi' +// idx = atoi(args[1]); // Linker error: undefined reference to 'atoi' idx = (uint32_t)(args[1]); -// len = atoi(args[2]); // Linker error: undefined reference to `atoi' +// len = atoi(args[2]); // Linker error: undefined reference to 'atoi' len = (uint32_t)(args[2]); /* For some reason idx and len somehow get multiplied by 2 and +1! From bb61ea3a9d8e9e7f296a571c0932381e55ae05eb Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Tue, 24 Aug 2021 22:55:11 +0200 Subject: [PATCH 3/5] Add rtc.memory() documention Simple copy of https://github.com/karfas/micropython/commit/9ec4b836a98d7711c3266b036b6746ed44c6a7ba Copyright by @heikokue --- docs/library/machine.RTC.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/library/machine.RTC.rst b/docs/library/machine.RTC.rst index be2be2eee5eea..f6425ab3f09f6 100644 --- a/docs/library/machine.RTC.rst +++ b/docs/library/machine.RTC.rst @@ -74,6 +74,26 @@ Methods - ``handler`` is the function to be called when the callback is triggered. - ``wake`` specifies the sleep mode from where this interrupt can wake up the system. + +.. method:: RTC.memory([data]) + + Stores ``data`` (byte literal) into RTC memory, which is kept stored in deep sleep mode of the ESP8266 or ESP32. + The data is stored together with a magic word to detect that the RTC memory is valid. + An uninitialized or cleared RTC memory has no magic word and will deliver ``b''``. + Without ``data`` the method delivers the RTC memory content. + In the ESP8266 are max. 492 bytes and in the ESP32 are max. 2048 Bytes storeable by this method. + + Example:: + + import machine + rtc = machine.RTC() + writedata = b'test' + rtc.memory(writedata) # this command writes writedata into the RTC memory + readdata = rtc.memory() # this command puts the RTC memory into readdata + print(readdata) + + Availability: ESP8266, ESP32 + Constants --------- From 85d37b02aa1ff8a45d4e13e9c0f35d5d413986ab Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Tue, 24 Aug 2021 22:56:40 +0200 Subject: [PATCH 4/5] Update machine.RTC.rst Simple copy of karfas@9ec4b83 Copyright by @heikokue --- docs/library/machine.RTC.rst | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/docs/library/machine.RTC.rst b/docs/library/machine.RTC.rst index be2be2eee5eea..f6425ab3f09f6 100644 --- a/docs/library/machine.RTC.rst +++ b/docs/library/machine.RTC.rst @@ -74,6 +74,26 @@ Methods - ``handler`` is the function to be called when the callback is triggered. - ``wake`` specifies the sleep mode from where this interrupt can wake up the system. + +.. method:: RTC.memory([data]) + + Stores ``data`` (byte literal) into RTC memory, which is kept stored in deep sleep mode of the ESP8266 or ESP32. + The data is stored together with a magic word to detect that the RTC memory is valid. + An uninitialized or cleared RTC memory has no magic word and will deliver ``b''``. + Without ``data`` the method delivers the RTC memory content. + In the ESP8266 are max. 492 bytes and in the ESP32 are max. 2048 Bytes storeable by this method. + + Example:: + + import machine + rtc = machine.RTC() + writedata = b'test' + rtc.memory(writedata) # this command writes writedata into the RTC memory + readdata = rtc.memory() # this command puts the RTC memory into readdata + print(readdata) + + Availability: ESP8266, ESP32 + Constants --------- From ad0f2b89dda85039c1b9ae72aad8b47ee3d2d1e2 Mon Sep 17 00:00:00 2001 From: CaCO3 Date: Tue, 24 Aug 2021 23:00:51 +0200 Subject: [PATCH 5/5] extended documentation --- docs/library/machine.RTC.rst | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/docs/library/machine.RTC.rst b/docs/library/machine.RTC.rst index f6425ab3f09f6..41a760e5807ad 100644 --- a/docs/library/machine.RTC.rst +++ b/docs/library/machine.RTC.rst @@ -94,6 +94,22 @@ Methods Availability: ESP8266, ESP32 +.. method:: RTC.memory(idx, len, [data]) + + Same as `RTC.memory([data])` but allows slot wise access to the memory. + On the ESP8266, 122 slots with 4bytes each are available. + The memory stays compatible with the block read/write functionality. + + Example:: + + import machine + rtc = machine.RTC() + writedata = b'\x20\x30\x40\x50' + rtc.memory(0, 1, writedata) # this command writes writedata into the first slot of the RTC memory + readdata = rtc.memory(0, 1) # this command puts the content of SLot 0 of the RTC memory into readdata + print(readdata) + + Availability: ESP8266 Constants --------- 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