Fix RTC synchronization after sleep, add many more tests

There were two issues with the tests

1. Incorrect print formats causing incorrect output.

2. The RTC driver was not waiting for the shadow registers to be
updated after sleeping, their reset values to be read.
This commit is contained in:
2020-04-22 09:09:31 -07:00
parent 2fa4a5e658
commit f455ce9113
20 changed files with 700 additions and 94 deletions

View File

@@ -38,12 +38,22 @@ using BSP::SystemTimer;
static BSP::Schedule::LowPowerTaskScheduler<1> g_sched;
static BSP::UsartDriver g_test_uart(USART1, g_sched);
static BSP::GpioDriver g_gpioa(GPIOA);
static BSP::GpioPin g_test_pin(g_gpioa, 6);
static BSP::time_t get_time() {
BSP::time_t time;
BSP::ReturnCode rc = SystemTimer::get_time(time);
if (rc != ReturnCode::OK) {
g_test_uart.tx_blocking("Failed while getting the time\r\n");
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
return time;
}
[[noreturn]] void main() {
g_gpioa.enable();
g_test_pin.configure_alternate_function(1);
g_test_uart.init();
g_test_uart.tx_blocking(test_start_text);
@@ -54,25 +64,14 @@ static BSP::GpioPin g_test_pin(g_gpioa, 6);
BSP::time_t now;
ReturnCode rc = SystemTimer::get_time(now);
if (rc != ReturnCode::OK) {
g_test_uart.tx_blocking("Failed while getting initial time\r\n");
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
now = get_time();
BSP::time_t end = now + Time::seconds(10);
g_test_uart.tx_blocking("GO\r\n");
char buffer[40] = { 0 };
static char buffer[40] = { 0 };
while (now < end) {
snprintf(buffer, sizeof(buffer), "%lld\r\n", BSP::Time::to_micros(now));
g_test_uart.tx_blocking(buffer);
rc = SystemTimer::get_time(now);
if (rc != ReturnCode::OK) {
g_test_uart.tx_blocking("Failed while waiting for time to pass\r\n");
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
now = get_time();
}
g_test_uart.tx_blocking("STOP\r\n");

View File

@@ -35,12 +35,10 @@ using BSP::Time;
static BSP::Schedule::LowPowerTaskScheduler<1> g_sched;
static BSP::UsartDriver g_test_uart(USART1, g_sched);
static BSP::GpioDriver g_gpioa(GPIOA);
static BSP::GpioPin g_test_pin(g_gpioa, 6);
[[noreturn]] void main() {
g_gpioa.enable();
g_test_pin.configure_alternate_function(1);
g_test_uart.init();
g_test_uart.tx_blocking(test_start_text);

89
firmware/Test/lptim.cpp Normal file
View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2020 Max Regan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "printf.h"
#include "Bsp/LowPowerTaskScheduler.h"
#include "Bsp/Drivers/GpioDriver.h"
#include "Bsp/Drivers/LptimPwm.h"
#include "Bsp/Drivers/UsartDriver.h"
#include "Bsp/Drivers/LowPower.h"
#include "Bsp/macros.h"
#include "test.h"
#include "Mcu.h"
using BSP::Time;
using BSP::ReturnCode;
using BSP::SystemTimer;
static BSP::Schedule::LowPowerTaskScheduler<1> g_sched;
static BSP::UsartDriver g_test_uart(USART1, g_sched);
static BSP::LptimPwm g_test_lptim(LPTIM2);
static BSP::GpioDriver g_gpioa(GPIOA);
static BSP::GpioDriver g_gpiob(GPIOB);
static BSP::GpioPin g_lptim_pin(g_gpiob, 2);
static BSP::time_t get_time() {
BSP::time_t time;
ReturnCode rc = SystemTimer::get_time(time);
if (rc != ReturnCode::OK) {
g_test_uart.tx_blocking("Failed while getting the time\r\n");
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
return time;
}
[[noreturn]] void main() {
g_gpioa.enable();
g_gpiob.enable();
g_lptim_pin.configure_alternate_function(1);
g_test_uart.init();
g_test_lptim.init();
BSP::RtcDriver::init();
SystemTimer::set_timer(BSP::RtcDriver::get_system_timer());
BSP::LowPower::init();
g_test_uart.tx_blocking(test_start_text);
BSP::time_t now = get_time();
BSP::time_t end = now + Time::seconds(3);
g_test_uart.tx_blocking("Running...\r\n");
while (now < end) {
now = get_time();
}
g_test_uart.tx_blocking("Going to sleep...\r\n");
BSP::time_t delay = Time::seconds(3);
BSP::RtcDriver::set_wakeup_in(delay);
end = get_time() + delay;
while (get_time() < end) {
BSP::LowPower::stop();
}
g_test_uart.tx_blocking(test_pass_text);
TEST_SPIN();
}

View File

@@ -35,12 +35,10 @@ using BSP::Time;
static BSP::Schedule::LowPowerTaskScheduler<1> g_sched;
static BSP::UsartDriver g_test_uart(USART1, g_sched);
static BSP::GpioDriver g_gpioa(GPIOA);
static BSP::GpioPin g_test_pin(g_gpioa, 6);
[[noreturn]] void main() {
g_gpioa.enable();
g_test_pin.configure_alternate_function(1);
g_test_uart.init();
g_test_uart.tx_blocking(test_start_text);

View File

@@ -0,0 +1,88 @@
/*
* Copyright (C) 2020 Max Regan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <cinttypes>
#include "printf.h"
#include "Bsp/Drivers/GpioDriver.h"
#include "Bsp/Drivers/RtcDriver.h"
#include "Bsp/Drivers/UsartDriver.h"
#include "Bsp/Drivers/LowPower.h"
#include "Bsp/LowPowerTaskScheduler.h"
#include "Bsp/macros.h"
#include "test.h"
#include "Mcu.h"
using BSP::Time;
using BSP::ReturnCode;
using BSP::SystemTimer;
using BSP::time_t;
static BSP::Schedule::LowPowerTaskScheduler<1> g_sched;
static BSP::UsartDriver g_test_uart(USART1, g_sched);
static BSP::GpioDriver g_gpioa(GPIOA);
static BSP::GpioPin g_test_pin(g_gpioa, 6);
static time_t get_time() {
time_t time;
ReturnCode rc = SystemTimer::get_time(time);
if (rc != ReturnCode::OK) {
g_test_uart.tx_blocking("Failed while getting the time\r\n");
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
return time;
}
[[noreturn]] void main() {
BSP::RtcDriver::init();
SystemTimer::set_timer(BSP::RtcDriver::get_system_timer());
BSP::LowPower::init();
g_gpioa.enable();
g_test_pin.configure_alternate_function(1);
g_test_uart.init();
g_test_uart.tx_blocking(test_start_text);
const time_t end = get_time() + Time::millis(5100);
const uint32_t pre_alarms = BSP::RtcDriver::get_alarm_count();
while (get_time() < end) {}
const uint32_t alarms = BSP::RtcDriver::get_alarm_count() - pre_alarms;
static char buffer[128] = { 0 };
snprintf(buffer, sizeof(buffer),
"Got %" PRId32 " alarms, expected around %" PRId32 "\r\n",
alarms, 5);
g_test_uart.tx_blocking(buffer);
if (alarms > 6 || alarms < 5) {
g_test_uart.tx_blocking(test_fail_text);
} else {
g_test_uart.tx_blocking(test_pass_text);
}
TEST_SPIN();
}

103
firmware/Test/set_time.cpp Normal file
View File

@@ -0,0 +1,103 @@
/*
* Copyright (C) 2020 Max Regan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "printf.h"
#include "Bsp/Drivers/GpioDriver.h"
#include "Bsp/Drivers/RtcDriver.h"
#include "Bsp/Drivers/UsartDriver.h"
#include "Bsp/LowPowerTaskScheduler.h"
#include "Bsp/macros.h"
#include "test.h"
#include "Mcu.h"
using BSP::Time;
using BSP::ReturnCode;
using BSP::SystemTimer;
using BSP::time_t;
using BSP::WallClockTime;
using BSP::RtcDriver;
static BSP::Schedule::LowPowerTaskScheduler<1> g_sched;
static BSP::UsartDriver g_test_uart(USART1, g_sched);
static BSP::GpioDriver g_gpioa(GPIOA);
static BSP::GpioPin g_test_pin(g_gpioa, 6);
static char buffer[128] = {0};
void run_case(unsigned int hours, unsigned int minutes, unsigned int seconds) {
WallClockTime t0(hours, minutes, seconds);
WallClockTime t1;
RtcDriver::set_time(t0);
RtcDriver::get_time(t1);
snprintf(buffer, sizeof(buffer),
"Testing with %d:%02d:%02d...",
hours, minutes, seconds);
g_test_uart.tx_blocking(buffer);
if (t0.get_hours_24() != hours
|| t0.get_minutes() != minutes
|| t0.get_seconds() != seconds) {
snprintf(buffer, sizeof(buffer),
"Software representation changed. Got:%d:%02d:%02d\r\n",
t0.get_hours_24(), t0.get_minutes(), t0.get_seconds());
g_test_uart.tx_blocking(buffer);
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
if (t0 != t1) {
snprintf(buffer, sizeof(buffer),
"Value from RTC does not match. Got %d:%02d:%02d\r\n",
t1.get_hours_24(), t1.get_minutes(), t1.get_seconds());
g_test_uart.tx_blocking(buffer);
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
g_test_uart.tx_blocking("OK\r\n");
}
[[noreturn]] void main() {
RtcDriver::init();
SystemTimer::set_timer(RtcDriver::get_system_timer());
BSP::LowPower::init();
g_gpioa.enable();
g_test_pin.configure_alternate_function(1);
g_test_uart.init();
g_test_uart.tx_blocking(test_start_text);
run_case(10, 0, 0);
run_case(0, 0, 0);
run_case(10, 59, 59);
run_case(12, 0, 0);
run_case(23, 59, 59);
g_test_uart.tx_blocking(test_pass_text);
TEST_SPIN();
}

View File

@@ -19,6 +19,7 @@
* THE SOFTWARE.
*/
#include <cinttypes>
#include "printf.h"
#include "Bsp/Drivers/GpioDriver.h"
@@ -67,17 +68,20 @@ static void stop_for(time_t delay) {
uint32_t pre_alarms = BSP::RtcDriver::get_alarm_count();
time_t before = get_time();
BSP::LowPower::stop();
while (pre_wakeups == BSP::RtcDriver::get_wakeup_count()) {
BSP::LowPower::stop();
}
time_t after = get_time();
uint32_t post_wakeups = BSP::RtcDriver::get_wakeup_count();
uint32_t post_alarms = BSP::RtcDriver::get_alarm_count();
static char buffer[128];
static char buffer[128] = { 0 };
snprintf(buffer, sizeof(buffer),
"Requested=%lld Actual=%lld Wakeups=%d Alarms=%d\r\n",
Time::to_millis(delay),
Time::to_millis(after - before),
"Requested=%" PRIu32 " Actual=%" PRIu32
" Wakeups=%" PRId32 " Alarms=%" PRId32 "\r\n",
(uint32_t) Time::to_millis(delay),
(uint32_t) Time::to_millis(after - before),
post_wakeups - pre_wakeups,
post_alarms - pre_alarms);
@@ -96,11 +100,17 @@ static void stop_for(time_t delay) {
g_test_uart.tx_blocking(test_start_text);
for (uint32_t i = 0; i <= 1000; i++) {
stop_for(Time::millis(5));
}
for (uint32_t delay_millis = 1; delay_millis <= 1024; delay_millis *= 2) {
stop_for(Time::millis(delay_millis));
}
stop_for(Time::seconds(10));
stop_for(Time::seconds(1));
stop_for(Time::seconds(2));
stop_for(Time::seconds(4));
stop_for(Time::seconds(60));
g_test_uart.tx_blocking(test_pass_text);

View File

@@ -35,12 +35,10 @@ using BSP::Time;
static BSP::Schedule::LowPowerTaskScheduler<1> g_sched;
static BSP::UsartDriver g_test_uart(USART1, g_sched);
static BSP::GpioDriver g_gpioa(GPIOA);
static BSP::GpioPin g_test_pin(g_gpioa, 6);
[[noreturn]] void main() {
g_gpioa.enable();
g_test_pin.configure_alternate_function(1);
g_test_uart.init();
g_test_uart.tx_blocking(test_start_text);

View File

@@ -0,0 +1,123 @@
/*
* Copyright (C) 2020 Max Regan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <cinttypes>
#include "printf.h"
#include "Bsp/Drivers/GpioDriver.h"
#include "Bsp/Drivers/RtcDriver.h"
#include "Bsp/Drivers/UsartDriver.h"
#include "Bsp/LowPowerTaskScheduler.h"
#include "Bsp/macros.h"
#include "test.h"
#include "Mcu.h"
using BSP::Time;
using BSP::ReturnCode;
using BSP::SystemTimer;
using BSP::time_t;
static BSP::Schedule::LowPowerTaskScheduler<1> g_sched;
static BSP::UsartDriver g_test_uart(USART1, g_sched);
static BSP::GpioDriver g_gpioa(GPIOA);
static BSP::GpioPin g_test_pin(g_gpioa, 6);
static time_t get_time() {
time_t time;
ReturnCode rc = SystemTimer::get_time(time);
if (rc != ReturnCode::OK) {
g_test_uart.tx_blocking("Failed while getting the time\r\n");
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
return time;
}
static void stop_for(time_t delay) {
ReturnCode rc = BSP::RtcDriver::set_wakeup_in(delay);
if (rc != ReturnCode::OK) {
g_test_uart.tx_blocking("Failed to set wakeup delay\r\n");
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
uint32_t pre_wakeups = BSP::RtcDriver::get_wakeup_count();
time_t before = get_time();
while (BSP::RtcDriver::get_wakeup_count() == pre_wakeups) {
}
time_t after = get_time();
uint32_t post_wakeups = BSP::RtcDriver::get_wakeup_count();
static char buffer[128] = { 0 };
snprintf(buffer, sizeof(buffer),
"Requested=%" PRIu32 " Actual=%" PRIu32
" Wakeups=%" PRId32 "\r\n",
(uint32_t) Time::to_millis(delay),
(uint32_t) Time::to_millis(after - before),
post_wakeups - pre_wakeups);
g_test_uart.tx_blocking(buffer);
}
static void fail_if_wakeup(time_t delay) {
uint32_t pre_wakeups = BSP::RtcDriver::get_wakeup_count();
time_t before = get_time();
while (before + delay > get_time() || BSP::RtcDriver::get_wakeup_count() != pre_wakeups) {}
if (BSP::RtcDriver::get_wakeup_count() != pre_wakeups) {
g_test_uart.tx_blocking("Got unexpected wakeup IRQ");
g_test_uart.tx_blocking(test_fail_text);
TEST_SPIN();
}
}
[[noreturn]] void main() {
BSP::RtcDriver::init();
SystemTimer::set_timer(BSP::RtcDriver::get_system_timer());
g_gpioa.enable();
g_test_pin.configure_alternate_function(1);
g_test_uart.init();
g_test_uart.tx_blocking(test_start_text);
for (uint32_t i = 0; i <= 100; i++) {
stop_for(Time::millis(5));
}
for (uint32_t delay_millis = 4; delay_millis <= 1024; delay_millis *= 2) {
stop_for(Time::millis(delay_millis));
}
stop_for(Time::seconds(2));
stop_for(Time::seconds(4));
stop_for(Time::millis(5));
fail_if_wakeup(Time::seconds(1));
g_test_uart.tx_blocking(test_pass_text);
TEST_SPIN();
}