esp32/machine_rtc: Make RTC.memory size and availability configurable.

The compile-time configuration value MICROPY_HW_RTC_USER_MEM_MAX can now be
used to define the amount of memory set aside for RTC.memory().  If this
value is configured to zero then the RTC.memory functionality is not
included in the build.
This commit is contained in:
Josh Lloyd 2019-08-12 13:21:47 +12:00 committed by Damien George
parent 57c18fdd38
commit ed2314f35a
1 changed files with 25 additions and 6 deletions

View File

@ -44,17 +44,29 @@ typedef struct _machine_rtc_obj_t {
mp_obj_base_t base; mp_obj_base_t base;
} machine_rtc_obj_t; } machine_rtc_obj_t;
#define MEM_MAGIC 0x75507921
/* There is 8K of rtc_slow_memory, but some is used by the system software /* There is 8K of rtc_slow_memory, but some is used by the system software
If the USER_MAXLEN is set to high, the following compile error will happen: If the MICROPY_HW_RTC_USER_MEM_MAX is set too high, the following compile error will happen:
region `rtc_slow_seg' overflowed by N bytes region `rtc_slow_seg' overflowed by N bytes
The current system software allows almost 4096 to be used. The current system software allows almost 4096 to be used.
To avoid running into issues if the system software uses more, 2048 was picked as a max length To avoid running into issues if the system software uses more, 2048 was picked as a max length
You can also change this max length at compile time by defining MICROPY_HW_RTC_USER_MEM_MAX
either on your make line, or in your board config.
If MICROPY_HW_RTC_USER_MEM_MAX is set to 0, the RTC.memory() functionality will be not
be compiled which frees some extra flash and RTC memory.
*/ */
#define MEM_USER_MAXLEN 2048 #ifndef MICROPY_HW_RTC_USER_MEM_MAX
#define MICROPY_HW_RTC_USER_MEM_MAX 2048
#endif
// Optionally compile user memory functionality if the size of memory is greater than 0
#if MICROPY_HW_RTC_USER_MEM_MAX > 0
#define MEM_MAGIC 0x75507921
RTC_DATA_ATTR uint32_t rtc_user_mem_magic; RTC_DATA_ATTR uint32_t rtc_user_mem_magic;
RTC_DATA_ATTR uint32_t rtc_user_mem_len; RTC_DATA_ATTR uint32_t rtc_user_mem_len;
RTC_DATA_ATTR uint8_t rtc_user_mem_data[MEM_USER_MAXLEN]; RTC_DATA_ATTR uint8_t rtc_user_mem_data[MICROPY_HW_RTC_USER_MEM_MAX];
#endif
// singleton RTC object // singleton RTC object
STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}}; STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
@ -118,19 +130,23 @@ STATIC mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
mp_obj_t args[2] = {self_in, date}; mp_obj_t args[2] = {self_in, date};
machine_rtc_datetime_helper(2, args); machine_rtc_datetime_helper(2, args);
#if MICROPY_HW_RTC_USER_MEM_MAX > 0
if (rtc_user_mem_magic != MEM_MAGIC) { if (rtc_user_mem_magic != MEM_MAGIC) {
rtc_user_mem_magic = MEM_MAGIC; rtc_user_mem_magic = MEM_MAGIC;
rtc_user_mem_len = 0; rtc_user_mem_len = 0;
} }
#endif
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init); STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);
#if MICROPY_HW_RTC_USER_MEM_MAX > 0
STATIC mp_obj_t machine_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) { STATIC mp_obj_t machine_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) {
if (n_args == 1) { if (n_args == 1) {
// read RTC memory // read RTC memory
uint32_t len = rtc_user_mem_len; uint32_t len = rtc_user_mem_len;
uint8_t rtcram[MEM_USER_MAXLEN]; uint8_t rtcram[MICROPY_HW_RTC_USER_MEM_MAX];
memcpy( (char *) rtcram, (char *) rtc_user_mem_data, len); memcpy( (char *) rtcram, (char *) rtc_user_mem_data, len);
return mp_obj_new_bytes(rtcram, len); return mp_obj_new_bytes(rtcram, len);
} else { } else {
@ -138,7 +154,7 @@ STATIC mp_obj_t machine_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ); mp_get_buffer_raise(args[1], &bufinfo, MP_BUFFER_READ);
if (bufinfo.len > MEM_USER_MAXLEN) { if (bufinfo.len > MICROPY_HW_RTC_USER_MEM_MAX) {
mp_raise_ValueError("buffer too long"); mp_raise_ValueError("buffer too long");
} }
memcpy( (char *) rtc_user_mem_data, (char *) bufinfo.buf, bufinfo.len); memcpy( (char *) rtc_user_mem_data, (char *) bufinfo.buf, bufinfo.len);
@ -147,11 +163,14 @@ STATIC mp_obj_t machine_rtc_memory(mp_uint_t n_args, const mp_obj_t *args) {
} }
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_memory_obj, 1, 2, machine_rtc_memory); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_memory_obj, 1, 2, machine_rtc_memory);
#endif
STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = { STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_rtc_init_obj) }, { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_rtc_init_obj) },
{ MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) }, { MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) },
#if MICROPY_HW_RTC_USER_MEM_MAX > 0
{ MP_ROM_QSTR(MP_QSTR_memory), MP_ROM_PTR(&machine_rtc_memory_obj) }, { MP_ROM_QSTR(MP_QSTR_memory), MP_ROM_PTR(&machine_rtc_memory_obj) },
#endif
}; };
STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);