Skip to content

Commit 1e97d2c

Browse files
authored
Fix RTC Back In Time Events (#336)
Implement RTC Syncronization Apply sub-second shifting to the SHIFTR RTC peripheral register to set the milliseconds being reported from the remote source. Address all possible rollover edge cases when shifting the milliseconds. Prior to this change, the milliseconds were never being set. This addresses the back in time error that is occurring on Bristlemouth nodes. Adds Mutex Protection To RTC Registers Prevents shared resources from being acessed from multiple tasks while unprotected in the RTC module. This allows for the the get and set operation to occur at the same time without any potential race conditions Disable use of the RTC shadow register and perform double read on RTC registers when getting RTC time Prevents race condition where a rollover in time could occur between reading the Time and Date registers creating an improper RTC time
1 parent 1c37660 commit 1e97d2c

File tree

5 files changed

+216
-95
lines changed

5 files changed

+216
-95
lines changed

src/apps/bridge/sensor_drivers/aanderaaSensor.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#include "aanderaaSensor.h"
2-
#include "aanderaa_data_msg.h"
2+
#include "aanderaa_current_meter_msg.h"
33
#include "app_config.h"
4+
#include "app_util.h"
45
#include "avgSampler.h"
5-
#include "spotter.h"
6-
#include "pubsub.h"
76
#include "bridgeLog.h"
87
#include "device_info.h"
8+
#include "pubsub.h"
99
#include "reportBuilder.h"
1010
#include "semphr.h"
11+
#include "spotter.h"
1112
#include "stm32_rtc.h"
1213
#include "topology_sampler.h"
13-
#include "app_util.h"
1414
#include <new>
1515

1616
// TODO - get this from the sensor node itself
@@ -36,11 +36,12 @@ void AanderaaSensor::aanderaSubCallback(uint64_t node_id, const char *topic, uin
3636
(void)version;
3737
printf("Aanderaa data received from node %016" PRIx64 " On topic: %.*s\n", node_id, topic_len,
3838
topic);
39-
Aanderaa_t *aanderaa = static_cast<Aanderaa_t *>(sensorControllerFindSensorById(node_id, SENSOR_TYPE_AANDERAA));
39+
Aanderaa_t *aanderaa =
40+
static_cast<Aanderaa_t *>(sensorControllerFindSensorById(node_id, SENSOR_TYPE_AANDERAA));
4041
if (aanderaa && aanderaa->type == SENSOR_TYPE_AANDERAA) {
4142
if (xSemaphoreTake(aanderaa->_mutex, portMAX_DELAY)) {
42-
static AanderaaDataMsg::Data d;
43-
if (AanderaaDataMsg::decode(d, data, data_len) == CborNoError) {
43+
static AanderaaCurrentMeterMsg::Data d;
44+
if (AanderaaCurrentMeterMsg::decode(d, data, data_len) == CborNoError) {
4445
char *log_buf = static_cast<char *>(pvPortMalloc(SENSOR_LOG_BUF_SIZE));
4546
configASSERT(log_buf);
4647
aanderaa->abs_speed_cm_s.addSample(d.abs_speed_cm_s);
@@ -58,12 +59,14 @@ void AanderaaSensor::aanderaSubCallback(uint64_t node_id, const char *topic, uin
5859
uint32_t sensor_reading_time_millis = d.header.sensor_reading_time_ms % 1000U;
5960

6061
uint32_t current_timestamp = pdTICKS_TO_MS(xTaskGetTickCount());
61-
if ((current_timestamp - aanderaa->last_timestamp > DEFAULT_AANDERAA_READING_PERIOD_MS + 30000u) ||
62+
if ((current_timestamp - aanderaa->last_timestamp >
63+
DEFAULT_AANDERAA_READING_PERIOD_MS + 30000u) ||
6264
aanderaa->reading_count == 1U) {
6365
printf("Updating aanderaa %016" PRIx64 " node position, current_time = %" PRIu32
6466
", last_time = %" PRIu32 ", reading count: %" PRIu32 "\n",
6567
node_id, current_timestamp, aanderaa->last_timestamp, aanderaa->reading_count);
66-
aanderaa->node_position = topology_sampler_get_node_position(node_id, pdTICKS_TO_MS(5000));
68+
aanderaa->node_position =
69+
topology_sampler_get_node_position(node_id, pdTICKS_TO_MS(5000));
6770
}
6871
aanderaa->last_timestamp = current_timestamp;
6972

src/apps/bristleback_apps/aanderaa/user_code/user_code.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
#include "user_code.h"
1+
#include "LineParser.h"
22
#include "OrderedKVPLineParser.h"
3-
#include "aanderaa_data_msg.h"
3+
#include "aanderaa_current_meter_msg.h"
44
#include "app_util.h"
55
#include "array_utils.h"
66
#include "avgSampler.h"
77
#include "bsp.h"
8+
#include "configuration.h"
89
#include "debug.h"
910
#include "device_info.h"
1011
#include "lwip/inet.h"
@@ -443,11 +444,11 @@ void loop(void) {
443444
}
444445

445446
// Publish individual reading.
446-
static AanderaaDataMsg::Data d;
447+
static AanderaaCurrentMeterMsg::Data d;
447448
size_t bufsize =
448449
sizeof(payload_buffer); // Re-use the payload buffer since we don't need it anymore.
449450
size_t encoded_len = 0;
450-
d.header.version = AanderaaDataMsg::VERSION;
451+
d.header.version = AanderaaCurrentMeterMsg::VERSION;
451452
d.header.reading_uptime_millis = uptimeGetMs();
452453
d.abs_speed_cm_s = getDoubleOrNaN(parser.getValue(ABS_SPEED));
453454
d.abs_speed_cm_s = getDoubleOrNaN(parser.getValue(ABS_SPEED));
@@ -468,8 +469,8 @@ void loop(void) {
468469
d.header.reading_time_utc_ms = (rtcGetMicroSeconds(&datetime) / 1e3);
469470
}
470471

471-
if (AanderaaDataMsg::encode(d, reinterpret_cast<uint8_t *>(payload_buffer), bufsize,
472-
&encoded_len) == CborNoError) {
472+
if (AanderaaCurrentMeterMsg::encode(d, reinterpret_cast<uint8_t *>(payload_buffer), bufsize,
473+
&encoded_len) == CborNoError) {
473474
bm_pub_wl(aanderaaTopic, aanderaaTopicStrLen, reinterpret_cast<uint8_t *>(payload_buffer),
474475
encoded_len, 0, BM_COMMON_PUB_SUB_VERSION);
475476
} else {
@@ -505,7 +506,7 @@ static void spoof_aanderaa() {
505506
fake_stats_timer_start = uptimeGetMs();
506507
SensorWatchdog::SensorWatchdogPet(AANDERAA_WATCHDOG_ID);
507508
// Publish individual reading.
508-
static AanderaaDataMsg::Data d;
509+
static AanderaaCurrentMeterMsg::Data d;
509510
size_t bufsize =
510511
sizeof(payload_buffer); // Re-use the payload buffer since we don't need it anymore.
511512
size_t encoded_len = 0;
@@ -514,7 +515,7 @@ static void spoof_aanderaa() {
514515
d.header.reading_time_utc_ms = (rtcGetMicroSeconds(&datetime) / 1e3);
515516
srand(d.header.reading_time_utc_ms & 0xFFFFFFFF);
516517
}
517-
d.header.version = AanderaaDataMsg::VERSION;
518+
d.header.version = AanderaaCurrentMeterMsg::VERSION;
518519
d.header.reading_uptime_millis = uptimeGetMs();
519520
// Fill with random values in the range of 0-data from sensor log.
520521
d.abs_speed_cm_s = 50 + (rand() % 10);
@@ -530,8 +531,8 @@ static void spoof_aanderaa() {
530531
d.std_tilt_deg = 5 + (rand() % 30);
531532
d.temperature_deg_c = 20 + (rand() % 5);
532533

533-
if (AanderaaDataMsg::encode(d, reinterpret_cast<uint8_t *>(payload_buffer), bufsize,
534-
&encoded_len) == CborNoError) {
534+
if (AanderaaCurrentMeterMsg::encode(d, reinterpret_cast<uint8_t *>(payload_buffer), bufsize,
535+
&encoded_len) == CborNoError) {
535536
bm_pub_wl(aanderaaTopic, aanderaaTopicStrLen, reinterpret_cast<uint8_t *>(payload_buffer),
536537
encoded_len, 0, BM_COMMON_PUB_SUB_VERSION);
537538
} else {

src/apps/rs232_expander_apps/aanderaa/user_code/user_code.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "user_code.h"
22
#include "OrderedKVPLineParser.h"
3-
#include "aanderaa_data_msg.h"
3+
#include "aanderaa_current_meter_msg.h"
44
#include "app_util.h"
55
#include "array_utils.h"
66
#include "avgSampler.h"
@@ -407,11 +407,11 @@ void loop(void) {
407407
}
408408

409409
// Publish individual reading.
410-
static AanderaaDataMsg::Data d;
410+
static AanderaaCurrentMeterMsg::Data d;
411411
size_t bufsize =
412412
sizeof(payload_buffer); // Re-use the payload buffer since we don't need it anymore.
413413
size_t encoded_len = 0;
414-
d.header.version = AanderaaDataMsg::VERSION;
414+
d.header.version = AanderaaCurrentMeterMsg::VERSION;
415415
d.header.reading_uptime_millis = uptimeGetMs();
416416
d.abs_speed_cm_s = getDoubleOrNaN(parser.getValue(ABS_SPEED));
417417
d.abs_speed_cm_s = getDoubleOrNaN(parser.getValue(ABS_SPEED));
@@ -432,8 +432,8 @@ void loop(void) {
432432
d.header.reading_time_utc_ms = (rtcGetMicroSeconds(&datetime) / 1e3);
433433
}
434434

435-
if (AanderaaDataMsg::encode(d, reinterpret_cast<uint8_t *>(payload_buffer), bufsize,
436-
&encoded_len) == CborNoError) {
435+
if (AanderaaCurrentMeterMsg::encode(d, reinterpret_cast<uint8_t *>(payload_buffer), bufsize,
436+
&encoded_len) == CborNoError) {
437437
bm_pub_wl(aanderaaTopic, aanderaaTopicStrLen, reinterpret_cast<uint8_t *>(payload_buffer),
438438
encoded_len, 0, BM_COMMON_PUB_SUB_VERSION);
439439
} else {
@@ -469,7 +469,7 @@ static void spoof_aanderaa() {
469469
fake_stats_timer_start = uptimeGetMs();
470470
SensorWatchdog::SensorWatchdogPet(AANDERAA_WATCHDOG_ID);
471471
// Publish individual reading.
472-
static AanderaaDataMsg::Data d;
472+
static AanderaaCurrentMeterMsg::Data d;
473473
size_t bufsize =
474474
sizeof(payload_buffer); // Re-use the payload buffer since we don't need it anymore.
475475
size_t encoded_len = 0;
@@ -478,7 +478,7 @@ static void spoof_aanderaa() {
478478
d.header.reading_time_utc_ms = (rtcGetMicroSeconds(&datetime) / 1e3);
479479
srand(d.header.reading_time_utc_ms & 0xFFFFFFFF);
480480
}
481-
d.header.version = AanderaaDataMsg::VERSION;
481+
d.header.version = AanderaaCurrentMeterMsg::VERSION;
482482
d.header.reading_uptime_millis = uptimeGetMs();
483483
// Fill with random values in the range of 0-data from sensor log.
484484
d.abs_speed_cm_s = 50 + (rand() % 10);
@@ -494,8 +494,8 @@ static void spoof_aanderaa() {
494494
d.std_tilt_deg = 5 + (rand() % 30);
495495
d.temperature_deg_c = 20 + (rand() % 5);
496496

497-
if (AanderaaDataMsg::encode(d, reinterpret_cast<uint8_t *>(payload_buffer), bufsize,
498-
&encoded_len) == CborNoError) {
497+
if (AanderaaCurrentMeterMsg::encode(d, reinterpret_cast<uint8_t *>(payload_buffer), bufsize,
498+
&encoded_len) == CborNoError) {
499499
bm_pub_wl(aanderaaTopic, aanderaaTopicStrLen, reinterpret_cast<uint8_t *>(payload_buffer),
500500
encoded_len, 0, BM_COMMON_PUB_SUB_VERSION);
501501
} else {

0 commit comments

Comments
 (0)