Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions boards/nrf52840_s140_v6.ld
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@ MEMORY
{
FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0xED000 - 0x26000

/* To keep data in RAM across resets */
PERSISTENT_RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 8

/* SRAM required by Softdevice depend on
* - Attribute Table Size (Number of Services and Characteristics)
* - Vendor UUID count
* - Max ATT MTU
* - Concurrent connection peripheral + central + secure links
* - Event Len, HVN queue, Write CMD queue
*/
RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x20040000 - 0x20006000
RAM (rwx) : ORIGIN = 0x20006000 + 8, LENGTH = 0x20040000 - 0x20006000 - 8
}

SECTIONS
{
. = ALIGN(4);
.persistent (NOLOAD) :
{
KEEP(*(.persistent_magic))
KEEP(*(.persistent_data))
. = ALIGN(4);
} > PERSISTENT_RAM

. = ALIGN(4);
.svc_data :
{
Expand All @@ -33,6 +44,6 @@ SECTIONS
KEEP(*(.fs_data))
PROVIDE(__stop_fs_data = .);
} > RAM
} INSERT AFTER .data;
}

INCLUDE "nrf52_common.ld"
15 changes: 13 additions & 2 deletions boards/nrf52840_s140_v6_extrafs.ld
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@ MEMORY
{
FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0xD4000 - 0x26000

/* To keep data in RAM across resets */
PERSISTENT_RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 8

/* SRAM required by Softdevice depend on
* - Attribute Table Size (Number of Services and Characteristics)
* - Vendor UUID count
* - Max ATT MTU
* - Concurrent connection peripheral + central + secure links
* - Event Len, HVN queue, Write CMD queue
*/
RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x20040000 - 0x20006000
RAM (rwx) : ORIGIN = 0x20006000 + 8, LENGTH = 0x20040000 - 0x20006000 - 8
}

SECTIONS
{
. = ALIGN(4);
.persistent (NOLOAD) :
{
KEEP(*(.persistent_magic))
KEEP(*(.persistent_data))
. = ALIGN(4);
} > PERSISTENT_RAM

. = ALIGN(4);
.svc_data :
{
Expand All @@ -33,6 +44,6 @@ SECTIONS
KEEP(*(.fs_data))
PROVIDE(__stop_fs_data = .);
} > RAM
} INSERT AFTER .data;
}

INCLUDE "nrf52_common.ld"
15 changes: 13 additions & 2 deletions boards/nrf52840_s140_v7.ld
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,29 @@ MEMORY
{
FLASH (rx) : ORIGIN = 0x27000, LENGTH = 0xED000 - 0x27000

/* To keep data in RAM across resets */
PERSISTENT_RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 8

/* SRAM required by Softdevice depend on
* - Attribute Table Size (Number of Services and Characteristics)
* - Vendor UUID count
* - Max ATT MTU
* - Concurrent connection peripheral + central + secure links
* - Event Len, HVN queue, Write CMD queue
*/
RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x20040000 - 0x20006000
RAM (rwx) : ORIGIN = 0x20006000 + 8, LENGTH = 0x20040000 - 0x20006000 - 8
}

SECTIONS
{
. = ALIGN(4);
.persistent (NOLOAD) :
{
KEEP(*(.persistent_magic))
KEEP(*(.persistent_data))
. = ALIGN(4);
} > PERSISTENT_RAM

. = ALIGN(4);
.svc_data :
{
Expand All @@ -33,6 +44,6 @@ SECTIONS
KEEP(*(.fs_data))
PROVIDE(__stop_fs_data = .);
} > RAM
} INSERT AFTER .data;
}

INCLUDE "nrf52_common.ld"
12 changes: 10 additions & 2 deletions boards/nrf52840_s140_v7_extrafs.ld
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ MEMORY
* - Concurrent connection peripheral + central + secure links
* - Event Len, HVN queue, Write CMD queue
*/
RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x20040000 - 0x20006000
RAM (rwx) : ORIGIN = 0x20006000 + 8, LENGTH = 0x20040000 - 0x20006000 - 8
}

SECTIONS
{
. = ALIGN(4);
.persistent (NOLOAD) :
{
KEEP(*(.persistent_magic))
KEEP(*(.persistent_data))
. = ALIGN(4);
} > PERSISTENT_RAM

. = ALIGN(4);
.svc_data :
{
Expand All @@ -33,6 +41,6 @@ SECTIONS
KEEP(*(.fs_data))
PROVIDE(__stop_fs_data = .);
} > RAM
} INSERT AFTER .data;
}

INCLUDE "nrf52_common.ld"
6 changes: 6 additions & 0 deletions src/helpers/ArduinoHelpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdint.h>

extern "C" {
__attribute__((section(".persistent_magic"))) uint32_t persistent_magic;
__attribute__((section(".persistent_data"))) uint32_t persistent_time;
}
42 changes: 40 additions & 2 deletions src/helpers/ArduinoHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,57 @@
#include <Mesh.h>
#include <Arduino.h>

#ifdef NRF52_PLATFORM
#define CLOCK_MAGIC_NUM 0xAA55CC33
#define RTC_TIME_MIN 1772323200 // 1 Mar 2026

extern uint32_t persistent_magic;
extern uint32_t persistent_time;
#endif

class VolatileRTCClock : public mesh::RTCClock {
uint32_t base_time;
uint64_t accumulator;
unsigned long prev_millis;

public:
VolatileRTCClock() { base_time = 1715770351; accumulator = 0; prev_millis = millis(); } // 15 May 2024, 8:50pm
VolatileRTCClock() {
#ifdef NRF52_PLATFORM
if (persistent_magic == CLOCK_MAGIC_NUM && persistent_time >= RTC_TIME_MIN) {
base_time = persistent_time;
} else {
base_time = RTC_TIME_MIN;
}
#else
base_time = 1715770351;
#endif

accumulator = 0;
prev_millis = millis();
}

uint32_t getCurrentTime() override { return base_time + accumulator/1000; }
void setCurrentTime(uint32_t time) override { base_time = time; accumulator = 0; prev_millis = millis(); }

void setCurrentTime(uint32_t time) override {
base_time = time;
accumulator = 0;
prev_millis = millis();

#ifdef NRF52_PLATFORM
persistent_magic = CLOCK_MAGIC_NUM;
persistent_time = time;
#endif
}

void tick() override {
unsigned long now = millis();
accumulator += (now - prev_millis);
prev_millis = now;

#ifdef NRF52_PLATFORM
persistent_magic = CLOCK_MAGIC_NUM;
persistent_time = getCurrentTime();
#endif
}
};

Expand Down
1 change: 1 addition & 0 deletions variants/gat562_30s_mesh_kit/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extends = nrf52_base
board = rak4631
board_check = true
board_build.ldscript = boards/nrf52840_s140_v6.ld
build_flags = ${nrf52_base.build_flags}
${sensor_base.build_flags}
-I variants/gat562_30s_mesh_kit
Expand Down
1 change: 1 addition & 0 deletions variants/gat562_mesh_tracker_pro/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extends = nrf52_base
board = rak4631
board_check = true
board_build.ldscript = boards/nrf52840_s140_v6.ld
build_flags = ${nrf52_base.build_flags}
${sensor_base.build_flags}
-I variants/gat562_mesh_tracker_pro
Expand Down
1 change: 1 addition & 0 deletions variants/promicro/platformio.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[Promicro]
extends = nrf52_base
board = promicro_nrf52840
board_build.ldscript = boards/nrf52840_s140_v6.ld
build_flags = ${nrf52_base.build_flags}
-I variants/promicro
-D PROMICRO
Expand Down
1 change: 1 addition & 0 deletions variants/rak3401/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extends = nrf52_base
board = rak3401
board_check = true
board_build.ldscript = boards/nrf52840_s140_v6.ld
build_flags = ${nrf52_base.build_flags}
${sensor_base.build_flags}
-I variants/rak3401
Expand Down
1 change: 1 addition & 0 deletions variants/rak4631/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extends = nrf52_base
board = rak4631
board_check = true
board_build.ldscript = boards/nrf52840_s140_v6.ld
extra_scripts = ${nrf52_base.extra_scripts}
post:variants/rak4631/fix_bsec_lib.py
build_flags = ${nrf52_base.build_flags}
Expand Down
1 change: 1 addition & 0 deletions variants/rak_wismesh_tag/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
extends = nrf52_base
board = rak4631
board_check = true
board_build.ldscript = boards/nrf52840_s140_v6.ld
build_flags = ${nrf52_base.build_flags}
${sensor_base.build_flags}
-I variants/rak_wismesh_tag
Expand Down