Skip to content

Commit 6a6d142

Browse files
committed
Get device clock synchronization working. Use random value for DevNonce.
1 parent ac28f4f commit 6a6d142

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

ports/zephyr/boards/nrf52840dk_nrf52840.conf

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ CONFIG_BT_CENTRAL=y
77
CONFIG_MICROPY_HEAP_SIZE=98304
88
CONFIG_MAIN_STACK_SIZE=8192
99

10+
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
11+
CONFIG_TEST_RANDOM_GENERATOR=y
12+
1013
CONFIG_MICROPY_CONFIGFILE="mpconfigport_full.h"
1114

1215
# CONFIG_DYNAMIC_THREAD=y
1316
CONFIG_THREAD_CUSTOM_DATA=y
1417
CONFIG_THREAD_MONITOR=y
1518
CONFIG_THREAD_STACK_INFO=y
16-
19+
CONFIG_THREAD_NAME=y
1720

1821
CONFIG_NEWLIB_LIBC=y
1922
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
@@ -24,13 +27,24 @@ CONFIG_LORA_SX126X=y
2427
CONFIG_SPI=y
2528

2629
CONFIG_LORA=y
27-
CONFIG_LORA_LOG_LEVEL_DBG=y
2830

31+
# Random number generator required for several LoRaWAN services
32+
CONFIG_ENTROPY_GENERATOR=y
33+
34+
# LoRaWAN application layer
2935
CONFIG_LORAWAN=y
30-
CONFIG_LORAWAN_LOG_LEVEL_DBG=y
3136
CONFIG_LORAMAC_REGION_EU868=y
32-
33-
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=8192
34-
35-
CONFIG_TEST_RANDOM_GENERATOR=y
36-
37+
CONFIG_LORAWAN_NVM_SETTINGS=y
38+
39+
# LoRaWAN services required for FUOTA
40+
CONFIG_LORAWAN_SERVICES=y
41+
CONFIG_LORAWAN_APP_CLOCK_SYNC=y
42+
CONFIG_LORAWAN_REMOTE_MULTICAST=y
43+
CONFIG_LORAWAN_FRAG_TRANSPORT=y
44+
45+
# Flash driver to store firmware image
46+
CONFIG_FLASH=y
47+
CONFIG_FLASH_MAP=y
48+
CONFIG_FLASH_PAGE_LAYOUT=y
49+
CONFIG_STREAM_FLASH=y
50+
CONFIG_IMG_MANAGER=y

ports/zephyr/main.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
#include "modmachine.h"
6161
#include "modzephyr.h"
6262

63-
#include "nvmc.h"
6463

6564
static char heap[MICROPY_HEAP_SIZE];
6665

@@ -89,8 +88,6 @@ void init_zephyr(void) {
8988
net_if_ipv6_addr_add(net_if_get_default(), &in6addr_my, NET_ADDR_MANUAL, 0);
9089
#endif
9190
#endif
92-
93-
nvmc_init();
9491
}
9592

9693
#if MICROPY_VFS

ports/zephyr/modlorawan.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
#include <zephyr/device.h>
4040
#include <zephyr/lorawan/lorawan.h>
4141
#include <zephyr/kernel.h>
42-
43-
#include "nvmc.h"
42+
#include <zephyr/random/random.h>
4443

4544

4645
#define DEBUG_printf(...) printk("LoRa: " __VA_ARGS__)
@@ -65,6 +64,14 @@ static bool modlorawan_init_done = false;
6564

6665
static void dl_callback(uint8_t port, bool data_pending, int16_t rssi, int8_t snr, uint8_t len, const uint8_t *hex_data) {
6766
DEBUG_printf("Port %d, Pending %d, RSSI %ddB, SNR %ddBm\n", port, data_pending, rssi, snr);
67+
68+
// if (len > 0) {
69+
// printf("Data: ");
70+
// for (int i = 0; i < len; i++) {
71+
// printf("%02x ", hex_data[i]);
72+
// }
73+
// printf("\n");
74+
// }
6875
}
6976

7077
static void lorwan_datarate_changed(enum lorawan_datarate dr) {
@@ -106,9 +113,7 @@ static mp_obj_t mp_lorawan_init(void) {
106113
lorawan_register_downlink_callback(&downlink_cb);
107114
lorawan_register_dr_changed_callback(lorwan_datarate_changed);
108115

109-
uint32_t nonce;
110-
nvmc_get(E_NVM_LORAWAN_DEV_NONCE, &nonce);
111-
modlorawan_dev_nonce = (uint16_t)nonce;
116+
modlorawan_dev_nonce = sys_rand16_get();
112117
DEBUG_printf("Dev Nonce is %d\n", modlorawan_dev_nonce);
113118

114119
modlorawan_init_done = true;
@@ -133,18 +138,19 @@ static mp_obj_t mp_lorawan_join(void) {
133138
join_cfg.otaa.nwk_key = app_key;
134139
join_cfg.otaa.dev_nonce = modlorawan_dev_nonce++;
135140

136-
if (modlorawan_dev_nonce == 0) {
137-
modlorawan_dev_nonce = 1;
138-
}
139-
nvmc_set(E_NVM_LORAWAN_DEV_NONCE, modlorawan_dev_nonce);
140-
141141
DEBUG_printf("Joining network over OTAA\n");
142142
ret = lorawan_join(&join_cfg);
143143
if (ret < 0) {
144144
DEBUG_printf("ERROR: lorawan_join_network failed: %d\n", ret);
145145
mp_raise_OSError(MP_EFAULT);
146146
}
147147

148+
/*
149+
* Clock synchronization is required to schedule the multicast session
150+
* in class C mode. It can also be used independent of FUOTA.
151+
*/
152+
lorawan_clock_sync_run();
153+
148154
return mp_const_none;
149155
}
150156
static MP_DEFINE_CONST_FUN_OBJ_0(mp_lorawan_join_obj, mp_lorawan_join);

ports/zephyr/modtime.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,23 @@
2626
*/
2727

2828
#include <zephyr/kernel.h>
29+
#include <zephyr/lorawan/lorawan.h>
2930

3031
#include "py/obj.h"
3132

33+
34+
#define LORAWAN_GPS_TO_UNIX_TIME_OFFSET 315964735
35+
36+
3237
static mp_obj_t mp_time_time_get(void) {
33-
/* The absence of FP support is deliberate. The Zephyr port uses
34-
* single precision floats so the fraction component will start to
35-
* lose precision on devices with a long uptime.
36-
*/
37-
return mp_obj_new_int(k_uptime_get() / 1000);
38+
uint32_t gps_time;
39+
if (lorawan_clock_sync_get(&gps_time) < 0) {
40+
/* The absence of FP support is deliberate. The Zephyr port uses
41+
* single precision floats so the fraction component will start to
42+
* lose precision on devices with a long uptime.
43+
*/
44+
return mp_obj_new_int(k_uptime_get() / 1000);
45+
} else {
46+
return mp_obj_new_int(gps_time + LORAWAN_GPS_TO_UNIX_TIME_OFFSET);
47+
}
3848
}

ports/zephyr/nvmc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void nvmc_init (void) {
9292
printk("NVM signature set failed!\n");
9393
}
9494

95-
if (!nvmc_set (E_NVM_LORAWAN_DEV_NONCE, 1)) {
95+
if (!nvmc_set (E_NVM_LORAWAN_DEV_NONCE, 0)) {
9696
printk("NVM charge completed clear failed!\n");
9797
}
9898
}

0 commit comments

Comments
 (0)