From f455ce911302702984b1b857944d74ef12e63b6b Mon Sep 17 00:00:00 2001 From: Max Regan Date: Wed, 22 Apr 2020 09:09:31 -0700 Subject: [PATCH] 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. --- .dir-locals.el | 10 +- .gitlab-ci.yml | 21 +++- firmware/Bsp/Drivers/LptimPwm.cpp | 4 +- firmware/Bsp/Drivers/RtcDriver.cpp | 56 ++++++---- firmware/Bsp/Drivers/UsartDriver.cpp | 1 - firmware/Bsp/Time.h | 11 ++ firmware/Makefile | 11 +- firmware/Test/clock.cpp | 31 +++--- firmware/Test/fail.cpp | 2 - firmware/Test/lptim.cpp | 89 +++++++++++++++ firmware/Test/pass.cpp | 2 - firmware/Test/periodic_alarms.cpp | 88 +++++++++++++++ firmware/Test/set_time.cpp | 103 +++++++++++++++++ firmware/Test/stop.cpp | 22 +++- firmware/Test/timeout.cpp | 2 - firmware/Test/wakeup_irq.cpp | 123 +++++++++++++++++++++ firmware/cmd.jlink | 2 +- test/Dockerfile | 25 +++++ test/README.md | 33 ++++++ test/src/tr_test/test.py | 158 ++++++++++++++++++++++----- 20 files changed, 700 insertions(+), 94 deletions(-) create mode 100644 firmware/Test/lptim.cpp create mode 100644 firmware/Test/periodic_alarms.cpp create mode 100644 firmware/Test/set_time.cpp create mode 100644 firmware/Test/wakeup_irq.cpp create mode 100644 test/Dockerfile create mode 100644 test/README.md diff --git a/.dir-locals.el b/.dir-locals.el index 94de7d7..6e1184b 100644 --- a/.dir-locals.el +++ b/.dir-locals.el @@ -1,3 +1,7 @@ -;; -((nil . ((projectile-project-compilation-cmd . "make -C firmware/ BOARD=devboard") - (python-shell-interpreter . "python3")))) +(;; Tell projectile about how to compile this project + (nil . ((projectile-project-compilation-cmd . "make -C firmware/ BOARD=devboard") + (projectile-project-test-cmd . "test/src/tr_test/test.py") + (python-shell-interpreter . "python3") + (gud-gdb-command-name . "arm-none-eabi-gdb -i=mi"))) + ;; Automatically run python-black on python buffers when they are saved + (python-mode . ((eval . (python-black-on-save-mode))))) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index db7fd1e..467c440 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,10 +1,10 @@ -image: registry.gitlab.maxregan.me/max/arm-build-image:latest +variables: + BOARD: "devboard" + GIT_SUBMODULE_STRATEGY: "recursive" build: + image: registry.gitlab.maxregan.me/max/arm-build-image:latest stage: build - variables: - BOARD: "devboard" - GIT_SUBMODULE_STRATEGY: "recursive" script: - make -C ./firmware/ artifacts: @@ -13,3 +13,16 @@ build: - "firmware/Test/*.bin" - "firmware/Application/*.elf" - "firmware/Application/*.bin" + +test: + dependencies: + - build + tags: + - tr-hw + - privileged + image: registry.gitlab.maxregan.me/max/timely-reference/test-exec + script: + - test/src/tr_test/test.py --junitxml=test-report.xml + artifacts: + reports: + junit: test-report.xml diff --git a/firmware/Bsp/Drivers/LptimPwm.cpp b/firmware/Bsp/Drivers/LptimPwm.cpp index faa2d78..50bbf49 100644 --- a/firmware/Bsp/Drivers/LptimPwm.cpp +++ b/firmware/Bsp/Drivers/LptimPwm.cpp @@ -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)) {} diff --git a/firmware/Bsp/Drivers/RtcDriver.cpp b/firmware/Bsp/Drivers/RtcDriver.cpp index 43986d3..5857105 100644 --- a/firmware/Bsp/Drivers/RtcDriver.cpp +++ b/firmware/Bsp/Drivers/RtcDriver.cpp @@ -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() /*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) /*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 diff --git a/firmware/Bsp/Drivers/UsartDriver.cpp b/firmware/Bsp/Drivers/UsartDriver.cpp index 0c1b618..30bdaf1 100644 --- a/firmware/Bsp/Drivers/UsartDriver.cpp +++ b/firmware/Bsp/Drivers/UsartDriver.cpp @@ -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 diff --git a/firmware/Bsp/Time.h b/firmware/Bsp/Time.h index fb66385..71e861a 100644 --- a/firmware/Bsp/Time.h +++ b/firmware/Bsp/Time.h @@ -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; }; + } diff --git a/firmware/Makefile b/firmware/Makefile index 7122f41..1fd7966 100644 --- a/firmware/Makefile +++ b/firmware/Makefile @@ -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 diff --git a/firmware/Test/clock.cpp b/firmware/Test/clock.cpp index 1d7f549..a3d7f5d 100644 --- a/firmware/Test/clock.cpp +++ b/firmware/Test/clock.cpp @@ -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"); diff --git a/firmware/Test/fail.cpp b/firmware/Test/fail.cpp index a751d25..4087741 100644 --- a/firmware/Test/fail.cpp +++ b/firmware/Test/fail.cpp @@ -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); diff --git a/firmware/Test/lptim.cpp b/firmware/Test/lptim.cpp new file mode 100644 index 0000000..b9cdab3 --- /dev/null +++ b/firmware/Test/lptim.cpp @@ -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(); +} diff --git a/firmware/Test/pass.cpp b/firmware/Test/pass.cpp index 4545796..71bfbd5 100644 --- a/firmware/Test/pass.cpp +++ b/firmware/Test/pass.cpp @@ -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); diff --git a/firmware/Test/periodic_alarms.cpp b/firmware/Test/periodic_alarms.cpp new file mode 100644 index 0000000..760c333 --- /dev/null +++ b/firmware/Test/periodic_alarms.cpp @@ -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 +#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(); +} diff --git a/firmware/Test/set_time.cpp b/firmware/Test/set_time.cpp new file mode 100644 index 0000000..89736d3 --- /dev/null +++ b/firmware/Test/set_time.cpp @@ -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(); +} diff --git a/firmware/Test/stop.cpp b/firmware/Test/stop.cpp index 8c7b482..09a0b94 100644 --- a/firmware/Test/stop.cpp +++ b/firmware/Test/stop.cpp @@ -19,6 +19,7 @@ * THE SOFTWARE. */ +#include #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); diff --git a/firmware/Test/timeout.cpp b/firmware/Test/timeout.cpp index 00aecc5..9d3f879 100644 --- a/firmware/Test/timeout.cpp +++ b/firmware/Test/timeout.cpp @@ -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); diff --git a/firmware/Test/wakeup_irq.cpp b/firmware/Test/wakeup_irq.cpp new file mode 100644 index 0000000..64001e6 --- /dev/null +++ b/firmware/Test/wakeup_irq.cpp @@ -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 +#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(); +} diff --git a/firmware/cmd.jlink b/firmware/cmd.jlink index 6f67f64..215e5e4 100644 --- a/firmware/cmd.jlink +++ b/firmware/cmd.jlink @@ -1,6 +1,6 @@ exitonerror 1 h r -loadbin ./watch.bin 0x8000000 +loadbin Application/main.bin 0x8000000 g q diff --git a/test/Dockerfile b/test/Dockerfile new file mode 100644 index 0000000..6673e50 --- /dev/null +++ b/test/Dockerfile @@ -0,0 +1,25 @@ +FROM ubuntu:20.04 + +RUN apt-get update && \ + apt-get upgrade -y && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y \ + curl \ + sigrok-cli \ + python3 \ + python3-pip \ + libncurses5 \ + && \ + rm -rf /var/lib/apt/lists/* + +RUN cd /tmp && \ + curl 'https://www.segger.com/downloads/jlink/JLink_Linux_x86_64.deb' \ + -H 'Content-Type: application/x-www-form-urlencoded' \ + --data 'accept_license_agreement=accepted&submit=Download+software' \ + --output JLink_Linux_x86_64.deb && \ + dpkg -i ./JLink_Linux_x86_64.deb && \ + rm ./JLink_Linux_x86_64.deb + +RUN pip3 install \ + pytest \ + pylink-square \ + pyserial \ diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..ae958a6 --- /dev/null +++ b/test/README.md @@ -0,0 +1,33 @@ +This directory contains the test code and other requirements in order to run the hardware integration tests for the Timely Reference. + +The Docker image uses SEGGER's JLink software tools, and requires accepting their [license](https://www.segger.com/downloads/jlink/JLink_Linux_x86_64.deb). This may be swapped out for [OpenOCD](https://sourceforge.net/p/openocd/code/ci/master/tree/) in the future. + +Of course, the tools can also be installed directly into the host- Docker provides a (more) consistent runtime environment and forces documentation of setup steps. + +### Hardware Requirements + +Current: + +- STMicroelectronics [Nucleo-L412-RB-P](https://www.st.com/en/evaluation-tools/nucleo-l412rb-p.html) +- SEGGER [J-Link](https://www.segger.com/products/debug-probes/j-link/) (Any model should do) +- FTDI [FT232R USB-Serial Breakout](https://www.ftdichip.com/Products/ICs/FT232R.htm). Tested with [this one from Amazon](https://www.amazon.com/gp/product/B075N82CDL) +- Sigrok-supported [FX2 logic analyzer](https://sigrok.org/wiki/Fx2lafw#Cypress_FX2_vs._FX2LP). Tested with [Lcsoft Mini Board](https://sigrok.org/wiki/Lcsoft_Mini_Board) and another no-name board. Other Sigrok-supported could be supported easily. + +The current hardware detection mechanisms may fail if more than one these devices is connected. + +### Execution + +First, build the docker image. Depending on your execution environment, you may need to escalate privileges in order to run `docker`. + +``` +cd +docker build -t tr-test test/ +``` + +Then, execute the tests. The tests require access to the USB hardware components, so their files need to be volume-mounted in, and the container must be given privilege in order to access the devices. + +``` +docker run --privileged -it -v /dev/bus/usb:/dev/bus/usb -v $(pwd):/build-dir tr-test /build-dir/test/src/tr_test/test.py +``` + +The tests also accept other arguments accepted by [pytest](https://docs.pytest.org/en/latest/usage.html). diff --git a/test/src/tr_test/test.py b/test/src/tr_test/test.py index e0d967a..5f0204c 100755 --- a/test/src/tr_test/test.py +++ b/test/src/tr_test/test.py @@ -20,7 +20,6 @@ # THE SOFTWARE. import pytest -import subprocess import serial import serial.tools.list_ports import logging @@ -29,6 +28,7 @@ import time import os import sys import re +import subprocess PYTEST_DIR = os.path.dirname(os.path.abspath(__file__)) DEFAULT_FW_DIR = os.path.abspath(PYTEST_DIR + "../../../../firmware/") @@ -49,19 +49,18 @@ def logger(): @pytest.fixture def context_factory(): + def create_context( + fw_rel_path: str, + mcu: str = "STM32L412RB", + addr: int = 0x8000000, + leave_halted: bool = False, + ): + ports = [ + p + for p in serial.tools.list_ports.comports() + if p.product == "FT232R USB UART" + ] - def create_context(fw_rel_path: str, - mcu: str = "STM32L412RB", - addr: int = 0x8000000, - leave_halted: bool = False): - - proc = subprocess.run(["make", "BOARD=devboard", fw_rel_path], - cwd=DEFAULT_FW_DIR, - capture_output=True, - check=True) - logging.info("Make process stdout: {}".format(proc.stdout)) - - ports = serial.tools.list_ports.comports() if len(ports) == 0: raise RuntimeError("No serial devices found") if len(ports) > 1: @@ -82,9 +81,7 @@ def context_factory(): assert jlink.halted() jlink.reset(halt=True) - serial_dev = serial.Serial(port=ports[0].device, - baudrate=115200, - timeout=1) + serial_dev = serial.Serial(port=ports[0].device, baudrate=115200, timeout=2) if leave_halted: return serial_dev, jlink @@ -93,15 +90,15 @@ def context_factory(): while True: try: logging.info("Waiting for firmware to start...") - assert serial_dev \ - .read_until(TEST_START_TEXT) \ - .endswith(TEST_START_TEXT), \ - "Timed out starting test firmware application" + assert serial_dev.read_until(TEST_START_TEXT).endswith( + TEST_START_TEXT + ), "Timed out starting test firmware application" logging.debug("Test execution started") except serial.serialutil.SerialException: continue break return serial_dev, jlink + return create_context @@ -131,15 +128,28 @@ def test_meta_nostart(context_factory, logger): def test_watch(context_factory, logger): - serial_dev, jlink = context_factory("Application/main.bin", - leave_halted=True) + serial_dev, jlink = context_factory("Application/main.bin", leave_halted=True) jlink.reset(halt=False) +def test_set_time(context_factory, logger): + serial_dev, jlink = context_factory("Test/set_time.bin") + text = serial_dev.read_until(TEST_PASS_TEXT) + print("Text:", text.decode()) + assert text.endswith(TEST_PASS_TEXT) + +def test_periodic_alarms(context_factory, logger): + serial_dev, jlink = context_factory("Test/periodic_alarms.bin") + serial_dev.timeout = 6 + text = serial_dev.read_until(TEST_PASS_TEXT) + print("Text:", text.decode()) + assert text.endswith(TEST_PASS_TEXT) + + def test_clock(context_factory, logger): serial_dev, jlink = context_factory("Test/clock.bin") EXPECTED_RUNTIME = 10 - TOLERANCE = .1 + TOLERANCE = 0.1 serial_dev.timeout = EXPECTED_RUNTIME * 1.2 @@ -148,23 +158,53 @@ def test_clock(context_factory, logger): start_text = serial_dev.read_until(START_MARKER) start = time.monotonic() + print("Start text:", start_text) + assert start_text.endswith(START_MARKER) + end_text = serial_dev.read_until(END_MARKER) end = time.monotonic() + print("Time:", end - start) + assert end_text.endswith(END_MARKER) delta = end - start - logger.debug("Serial text: {}".format(start_text)) - logger.debug("Serial text: {}".format(end_text)) + logger.info("Serial text: {}".format(start_text)) + logger.info("Serial text: {}".format(end_text)) logger.info("Start time: {}".format(start)) logger.info("End time: {}".format(end)) logger.info("Delta time: {}".format(delta)) # TODO: Using a single pin, instead of UART, would make this more # accurate. Add support via sigrok. - assert start_text.endswith(START_MARKER) - assert end_text.endswith(END_MARKER) assert (delta - EXPECTED_RUNTIME) < TOLERANCE +def test_wakeup_irq(context_factory, logger): + serial_dev, jlink = context_factory("Test/wakeup_irq.bin") + serial_dev.timeout = 65 + + pattern = re.compile("Requested=(\\d*) Actual=(\\d*) Wakeups=(\\d*)") + while True: + line = serial_dev.readline() + if line == TEST_PASS_TEXT: + break + + line = line.decode("ascii", errors="ignore") + print(line.strip()) + match = pattern.match(line) + assert match + req = int(match.group(1)) + actual = int(match.group(2)) + wakeups = int(match.group(3)) + delta = req - actual + + assert wakeups == 1, "Expected one wakeup per line of test output" + if req < 32000: + assert abs(delta < req * (2.0 / 100.0)) or (delta <= 1) + else: + # Delays > 32sec have reduced resolution (1 sec) + assert abs(delta) < 1000 + + def test_stop(context_factory, logger): serial_dev, jlink = context_factory("Test/stop.bin") serial_dev.timeout = 65 @@ -189,6 +229,70 @@ def test_stop(context_factory, logger): # Delays > 32sec have reduced resolution (1 sec) assert abs(delta) < 1000 +"sigrok-cli -C D3 -d fx2lafw -c samplerate=1M --time 1s -P timing:data=D3" + + +def measure_frequency( + period: float, + pin_name: str, + executable: str = "sigrok-cli", + driver_name: str = "fx2lafw", +): + + cmd = [ + executable, + "-C", + pin_name, + "-d", + driver_name, + "-c", + "samplerate=1M", + "--time", + "{}ms".format(int(period * 1000)), + "-P", + "timing:data={}".format(pin_name), + "-A", + "timing=time", + ] + + print("sigrok-cli cmd {}".format(cmd)) + proc = subprocess.run(cmd, capture_output=True, check=True) + lines = proc.stdout.decode("utf-8").split("\n") + reg = re.compile(".*:\\W(\\d+.\\d+)\\W(\\w+)") + periods = [] + for line in lines: + m = reg.match(line) + if not m: + break + num = float(m.groups(1)[0]) + units = m.groups(1)[1] + if units == "ms": + periods.append(num / 1000) + elif units == "μs": + periods.append(num / 1000000) + else: + assert False + + return periods[::2], periods[1:][::2] + +def test_lptim(context_factory, logger): + serial_dev, jlink = context_factory("Test/lptim.bin") + state0_periods, state1_periods = measure_frequency(1, "D1") + num_periods = min(len(state0_periods), len(state1_periods)) + periods = [state0_periods[i] + state1_periods[i] for i in range(num_periods)] + freqs = list(map(lambda x: 1 / x, periods)) + assert ( + periods + ), "No LPTIM changes detected, is the right analyzer being used? Is the device connected?" + + min_f = min(freqs) + max_f = max(freqs) + avg_f = sum(freqs) / len(freqs) + print("min:{}, max:{}, avg:{}".format(min_f, max_f, avg_f)) + assert abs(avg_f - 50) < 0.25 + assert min_f > 49 + assert max_f < 51 + def main(): pytest.main(sys.argv)