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

@@ -82,8 +82,8 @@ void LptimPwm::init_lptim()
/*!< Produce a 60Hz, signal with minimal "high" time. The display
only needs 2us of "high" time on EXTCOMM, and it draws a fair
amount of power. */
LPTIM1->ARR = 0x4FF;
LPTIM1->CMP = 0x4FE;
LPTIM1->ARR = 0x27F;
LPTIM1->CMP = 0x27E;
while(!(LPTIM1->ISR & LPTIM_ISR_ARROK)) {}
while(!(LPTIM1->ISR & LPTIM_ISR_CMPOK)) {}

View File

@@ -33,6 +33,9 @@ RtcDriver::RtcSystemTimer RtcDriver::m_sys_timer;
ReturnCode RtcDriver::init()
{
m_alarm_count = 0;
m_wakeup_count = 0;
init_hw();
return ReturnCode::OK;
@@ -110,14 +113,16 @@ ReturnCode RtcDriver::init_hw()
#elif defined(STM32L4XX)
uint32_t temp = RCC->CSR;
//SET(RCC->CSR, RCC_CSR_RTCRST);
// Reset the backup domain (includes the RTC)
SET(RCC->BDCR, RCC_BDCR_BDRST);
CLR(RCC->BDCR, RCC_BDCR_BDRST);
SET(RCC->APB1ENR1, RCC_APB1ENR1_PWREN);
SET(PWR->CR1, PWR_CR1_DBP);
SET(temp, RCC_BDCR_LSEON);
while (!(RCC->BDCR & RCC_BDCR_LSERDY)) {}
// while (!(RCC->BDCR & RCC_BDCR_LSERDY)) {}
SET_TO(temp, RCC_BDCR_RTCSEL, RCC_BDCR_RTCSEL_0);
SET(temp, RCC_BDCR_RTCEN);
@@ -141,8 +146,8 @@ ReturnCode RtcDriver::init_hw()
/*<! Load initial date and time */
// 12-Hour format
SET(RTC->CR, RTC_CR_FMT);
// 24-Hour format
CLR(RTC->CR, RTC_CR_FMT);
uint32_t time = 0;
SET(time, RTC_TR_PM);
@@ -163,7 +168,7 @@ ReturnCode RtcDriver::init_hw()
// Enable Wakeup irq, we may/will use them later
SET(RTC->CR, RTC_CR_WUTIE);
NVIC_EnableIRQ(RTC_IRQn);
NVIC_SetPriority(RTC_IRQn, 0);
NVIC_SetPriority(RTC_IRQn, 1);
#elif defined(STM32L4XX)
CLR(RTC->ICSR, RTC_ICSR_INIT);
@@ -173,15 +178,22 @@ ReturnCode RtcDriver::init_hw()
// Enable Wakeup irq, we may/will use them later
SET(RTC->CR, RTC_CR_WUTIE);
NVIC_EnableIRQ(RTC_WKUP_IRQn);
NVIC_SetPriority(RTC_WKUP_IRQn, 1);
NVIC_SetPriority(RTC_WKUP_IRQn, 0);
#else
#error "Unsupported device type"
#endif
enable_periodic_alarm();
disable_rtc_write();
// Disable the wakeup timer. This can be leftover from an old firmware/reset
//SET(EXTI->SWIER1, EXTI_SWIER1_SWI18);
// Clear the interrupt in the RTC
SET(RTC->SCR, RTC_SCR_CWUTF);
// Disable the Wakeup timer (its periodic, but we use it as a
// one-shot timer
CLR(RTC->CR, RTC_CR_WUTE);
disable_rtc_write();
return ReturnCode::OK;
}
@@ -206,10 +218,6 @@ ReturnCode RtcDriver::get_time(BSP::WallClockTime &wall_time)
seconds += 10 * STM32_GET_FIELD(time, RTC_TR_ST);
seconds += STM32_GET_FIELD(time, RTC_TR_SU);
if (STM32_GET_FIELD(time, RTC_TR_PM)) {
hours += 12;
}
new (&wall_time) BSP::WallClockTime(hours, minutes, seconds);
return ReturnCode::OK;
@@ -231,14 +239,9 @@ ReturnCode RtcDriver::set_time(const BSP::WallClockTime &wall_time)
/*<! Load initial date and time */
// 12-Hour format
SET(RTC->CR, RTC_CR_FMT);
uint32_t time = 0;
SET_TO(time, RTC_TR_PM, wall_time.get_is_pm() << RTC_TR_PM_Pos);
SET_TO(time, RTC_TR_HT, wall_time.get_hours_12_tens() << RTC_TR_HT_Pos);
SET_TO(time, RTC_TR_HU, wall_time.get_hours_12_ones() << RTC_TR_HU_Pos);
SET_TO(time, RTC_TR_HT, wall_time.get_hours_24_tens() << RTC_TR_HT_Pos);
SET_TO(time, RTC_TR_HU, wall_time.get_hours_24_ones() << RTC_TR_HU_Pos);
SET_TO(time, RTC_TR_MNT, wall_time.get_minutes_tens() << RTC_TR_MNT_Pos);
SET_TO(time, RTC_TR_MNU, wall_time.get_minutes_ones() << RTC_TR_MNU_Pos);
SET_TO(time, RTC_TR_ST, wall_time.get_seconds_tens() << RTC_TR_ST_Pos);
@@ -247,11 +250,11 @@ ReturnCode RtcDriver::set_time(const BSP::WallClockTime &wall_time)
#if defined(STM32L0XX)
CLR(RTC->ISR, RTC_ISR_INIT);
while (!(RTC->ISR & RTC_ISR_INITF)) {}
while (!(RTC->ISR & RTC_ISR_INITF)) {} // FIXME: this is probably inverted
while (!(RTC->ISR & RTC_ISR_RSF)) {}
#elif defined(STM32L4XX)
CLR(RTC->ICSR, RTC_ICSR_INIT);
while (!(RTC->ICSR & RTC_ICSR_INITF)) {}
while ((RTC->ICSR & RTC_ICSR_INITF)) {}
while (!(RTC->ICSR & RTC_ICSR_RSF)) {}
#else
#error "Unsupported device type"
@@ -310,6 +313,7 @@ ReturnCode RtcDriver::set_wakeup_in(BSP::time_t wakeup_delay)
SET_TO(RTC->WUTR, RTC_WUTR_WUT, delay_cycles - 1);
SET_TO(RTC->CR, RTC_CR_WUCKSEL, wucksel << RTC_CR_WUCKSEL_Pos);
SET(RTC->SCR, RTC_SCR_CWUTF);
SET(RTC->CR, RTC_CR_WUTE);
disable_rtc_write();
@@ -322,7 +326,10 @@ BSP::time_t RtcDriver::RtcSystemTimer::get_time()
uint32_t new_secs, old_secs, ssr;
uint64_t new_timer_ticks, new_millis;
while (!(RTC->ICSR & RTC_ICSR_WUTWF)) {}
enable_rtc_write();
RTC->ICSR = ~(RTC_ICSR_INIT | RTC_ICSR_RSF);
disable_rtc_write();
while (!(RTC->ICSR & RTC_ICSR_RSF)) {}
do {
__disable_irq();
@@ -395,9 +402,9 @@ extern "C" void RTC_IRQHandler()
static void irq_handler() {
SET(EXTI->PR1, EXTI_PR1_PIF20);
if (RTC->SR & RTC_ICSR_WUTWF) {
SET(EXTI->PR1, EXTI_PR1_PIF20);
if (RTC->SR & RTC_SR_WUTF) {
RtcDriver::increment_wakeup_count();
// Clear the interrupt in the RTC
SET(RTC->SCR, RTC_SCR_CWUTF);
@@ -423,6 +430,7 @@ extern "C" void RTC_ALARM_IRQHandler()
{
irq_handler();
}
#else
#error "Unsupported device type"
#endif

View File

@@ -54,7 +54,6 @@ void UsartDriver::init()
#error "Unknown device family"
#endif
// Set TX (PA9) to output, push/pull
SET_TO(GPIOA->AFR[1], GPIO_AFRH_AFSEL9, 7u << GPIO_AFRH_AFSEL9_Pos); //AF7 (USART1_TX)
SET_TO(GPIOA->MODER, GPIO_MODER_MODE9, 2u << GPIO_MODER_MODE9_Pos); // Alternate Function

View File

@@ -96,6 +96,16 @@ public:
, m_seconds(seconds)
{}
inline bool operator ==(const WallClockTime &t2) const {
return m_hours == t2.m_hours
&& m_minutes == t2.m_minutes
&& m_seconds == t2.m_seconds;
}
inline bool operator !=(const WallClockTime &t2) const {
return !operator==(t2);
}
static inline uint8_t hour24_to_hour12(uint8_t hour24) {
if (hour24 == 0) {
return 12;
@@ -220,4 +230,5 @@ private:
uint8_t m_minutes;
uint8_t m_seconds;
};
}

View File

@@ -83,7 +83,7 @@ S_SOURCES := $(call find_important, $(SOURCEDIR), '*.s')
SPP_SOURCES := Bsp/Mcu/$(DEVICE_TYPE).S
SOURCES = $(C_SOURCES) $(S_SOURCES) $(SPP_SOURCES) $(CPP_SOURCES)
APPS := ./Application/main ./Test/pass ./Test/fail ./Test/timeout ./Test/clock ./Test/stop ./Test/no_start
APPS := ./Application/main ./Test/pass ./Test/fail ./Test/timeout ./Test/clock ./Test/stop ./Test/no_start ./Test/lptim ./Test/set_time ./Test/periodic_alarms ./Test/wakeup_irq
APP_ELFS = $(addsuffix .elf, $(APPS))
APP_MAPS = $(addsuffix .map, $(APPS))
APP_BINS = $(addsuffix .bin, $(APPS))
@@ -98,7 +98,7 @@ OBJS = $(filter-out $(APP_OBJS), $(ALL_OBJS))
LINKER_SCRIPT ?= Bsp/Mcu/$(DEVICE_TYPE).ld
OUTPUT_NAME ?= watch
OUTPUT_NAME ?= Application/main
OUTPUT_BIN ?= $(OUTPUT_NAME).bin
OUTPUT_ELF ?= $(OUTPUT_NAME).elf
OUTPUT_MAP ?= $(OUTPUT_NAME).map
@@ -121,7 +121,7 @@ CPU_FLAGS = -mthumb -mcpu=cortex-m0plus -mfloat-abi=soft
CFLAGS = -Wall -Wextra -pedantic -Werror -Wcast-align
CXX_FLAGS = -Wsuggest-override -Wsuggest-final-methods -Wsuggest-final-types
# Debug/optimization
CFLAGS += -Os -ggdb
CFLAGS += -Os -g3
CFLAGS += -fdata-sections -ffunction-sections
# Architecture
CFLAGS += $(CPU_FLAGS)
@@ -132,6 +132,9 @@ CFLAGS += -D$(DEVICE_FAMILY_DEFINE) -D$(DEVICE_TYPE_DEFINE) -D$(DEVICE_LINE_DEFI
# CFLAGS += -DPRINTF_DISABLE_SUPPORT_FLOAT -DPRINTF_DISABLE_SUPPORT_EXPONENTIAL -DPRINTF_DISABLE_SUPPORT_PTRDIFF_T
# Consider this one if we are running short on flash, it saved about 1K
# CFLAGS += -DPRINTF_DISABLE_SUPPORT_LONG_LONG
CFLAGS += -D__STDC_FORMAT_MACROS
# Includes
CFLAGS += -I./Bsp/Mcu/
CFLAGS += -I./ThirdParty/stm32/$(DEVICE_LINE)/Include
@@ -229,7 +232,7 @@ flash: $(OUTPUT_BIN)
jlink: $(OUTPUT_BIN)
@echo "FLASH $(OUTPUT_BIN)"
JLinkExe -device $$(echo $(DEVICE_TYPE) | tr '[:lower:]' '[:upper:]') -if SWD \
-speed auto -autoconnect 1 -CommanderScript flash.jlink
-speed auto -autoconnect 1 -CommanderScript cmd.jlink
.PHONY: clean

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();
}

View File

@@ -1,6 +1,6 @@
exitonerror 1
h
r
loadbin ./watch.bin 0x8000000
loadbin Application/main.bin 0x8000000
g
q