Skip to content

Commit c39e35a

Browse files
committed
Start adding non voltaile memory support so that we can keep track of the LoRaWAN device nonce for OTA join.
1 parent 1b595f4 commit c39e35a

File tree

6 files changed

+188
-6
lines changed

6 files changed

+188
-6
lines changed

ports/zephyr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ set(MICROPY_SOURCE_PORT
5050
zephyr_storage.c
5151
mpthreadport.c
5252
modlorawan.c
53+
nvmc.c
5354
)
5455
list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/)
5556

ports/zephyr/boards/nrf52840dk_nrf52840.conf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,5 @@ CONFIG_LORAMAC_REGION_EU868=y
3232

3333
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=8192
3434

35-
CONFIG_FLASH=y
36-
CONFIG_FLASH_PAGE_LAYOUT=y
37-
CONFIG_NVS=y
38-
CONFIG_MPU_ALLOW_FLASH_WRITE=y
39-
4035
CONFIG_TEST_RANDOM_GENERATOR=y
4136

ports/zephyr/modlorawan.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ static mp_obj_t mp_lorawan_init(void) {
106106
lorawan_register_downlink_callback(&downlink_cb);
107107
lorawan_register_dr_changed_callback(lorwan_datarate_changed);
108108

109-
modlorawan_dev_nonce = 1;
109+
modlorawan_dev_nonce = 100;
110110

111111
return mp_const_none;
112112
}

ports/zephyr/nvmc.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024 Daniel Campora on behalf of REMOTE TECH LTD
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdbool.h>
28+
#include <stdlib.h>
29+
#include <string.h>
30+
#include <stdint.h>
31+
32+
#include <zephyr/kernel.h>
33+
#include <zephyr/sys/reboot.h>
34+
#include <zephyr/device.h>
35+
#include <zephyr/drivers/flash.h>
36+
#include <zephyr/storage/flash_map.h>
37+
#include <zephyr/fs/nvs.h>
38+
39+
#include "nvmc.h"
40+
41+
42+
// declare private constants
43+
44+
#define NVS_PARTITION storage_partition
45+
#define NVS_PARTITION_DEVICE FIXED_PARTITION_DEVICE(NVS_PARTITION)
46+
#define NVS_PARTITION_OFFSET FIXED_PARTITION_OFFSET(NVS_PARTITION)
47+
48+
49+
// declare private functions
50+
51+
52+
// declare private data
53+
54+
static struct nvs_fs nvmc_fs;
55+
56+
57+
// define public functions
58+
59+
void nvmc_init (void) {
60+
61+
int rc = 0;
62+
struct flash_pages_info info;
63+
64+
/* define the nvs file system by settings with:
65+
* sector_size equal to the pagesize,
66+
* 3 sectors
67+
* starting at NVS_PARTITION_OFFSET
68+
*/
69+
nvmc_fs.flash_device = NVS_PARTITION_DEVICE;
70+
if (!device_is_ready(nvmc_fs.flash_device)) {
71+
printk("Flash device %s is not ready\n", nvmc_fs.flash_device->name);
72+
return;
73+
}
74+
nvmc_fs.offset = NVS_PARTITION_OFFSET;
75+
rc = flash_get_page_info_by_offs(nvmc_fs.flash_device, nvmc_fs.offset, &info);
76+
if (rc) {
77+
printk("Unable to get page info, rc=%d\n", rc);
78+
return;
79+
}
80+
nvmc_fs.sector_size = info.size;
81+
nvmc_fs.sector_count = 3U;
82+
83+
rc = nvs_mount(&nvmc_fs);
84+
if (rc) {
85+
printk("Flash Init failed, rc=%d\n", rc);
86+
return;
87+
}
88+
89+
uint32_t signature = 0;
90+
if (!nvmc_get (E_NVM_SIGNATURE, &signature) || signature != NVMC_SIGNAUTRE_VALUE) {
91+
if (!nvmc_set (E_NVM_SIGNATURE, NVMC_SIGNAUTRE_VALUE)) {
92+
printk("NVM signature set failed!\n");
93+
}
94+
95+
if (!nvmc_set (E_NVM_CHARGE_COMPLETED, false)) {
96+
printk("NVM charge completed clear failed!\n");
97+
}
98+
99+
if (!nvmc_set (E_NVM_BATTERY_EMPTY, false)) {
100+
printk("NVM charge depleted clear failed!\n");
101+
}
102+
}
103+
}
104+
105+
bool nvmc_set (teNvmElement element, uint32_t value) {
106+
uint16_t id = element + 1;
107+
108+
if (nvs_write(&nvmc_fs, id, &value, sizeof(value)) == sizeof(value)) {
109+
return true;
110+
}
111+
return false;
112+
}
113+
114+
bool nvmc_get (teNvmElement element, uint32_t *value) {
115+
uint16_t id = element + 1;
116+
117+
if (nvs_read(&nvmc_fs, id, value, sizeof(*value)) > 0) {
118+
return true;
119+
}
120+
return false;
121+
}

ports/zephyr/nvmc.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2024 Daniel Campora on behalf of REMOTE TECH LTD
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef NVMC_H
28+
#define NVMC_H
29+
30+
#include <stdint.h>
31+
32+
33+
// define constants
34+
35+
#define NVMC_SIGNAUTRE_VALUE 0x5791ABCD
36+
37+
38+
// define types
39+
40+
typedef enum {
41+
42+
E_NVM_SIGNATURE = 0,
43+
E_NVM_CHARGE_COMPLETED,
44+
E_NVM_BLE_ON_BOOT, // Not used anymore.
45+
E_NVM_BATTERY_EMPTY,
46+
47+
} teNvmElement;
48+
49+
50+
// declare public functions
51+
52+
void nvmc_init (void);
53+
54+
bool nvmc_set (teNvmElement element, uint32_t value);
55+
56+
bool nvmc_get (teNvmElement element, uint32_t *value);
57+
58+
59+
#endif // NVMC_H
60+

ports/zephyr/prj.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,8 @@ CONFIG_NET_BUF_POOL_USAGE=y
7979
CONFIG_MICROPY_CONFIGFILE="mpconfigport.h"
8080
CONFIG_MICROPY_VFS_FAT=y
8181
CONFIG_MICROPY_VFS_LFS2=y
82+
83+
CONFIG_FLASH=y
84+
CONFIG_FLASH_PAGE_LAYOUT=y
85+
CONFIG_NVS=y
86+
CONFIG_MPU_ALLOW_FLASH_WRITE=y

0 commit comments

Comments
 (0)