From a21283c6774cc388a60efdd824273b6fd2dc3921 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 28 Sep 2021 13:10:08 -0500 Subject: [PATCH] drv/clock: move clock driver out of contiki This refactors the code base to use pbdrv_clock_* instead of clock_* from contiki. This is a step towards eliminating contiki from the public API, which will be nice if we ever have PBIO as a stand-alone C library. It also simplifies the code a bit since it gets rid of clock_from_msec() and clock_to_msec() which were NOPs in the PBIO library anyway. clock_delay_usec() is moved into the MicroPython code (as mp_hal_delay_us()) since we want to avoid any blocking functions in PBIO. --- bricks/cityhub/mphalport.h | 9 ++- bricks/debug/mphalport.h | 9 ++- bricks/essentialhub/mphalport.h | 9 ++- bricks/ev3dev/ev3dev_mphal.c | 6 +- bricks/movehub/mphalport.h | 9 ++- bricks/nxt/mphalport.h | 9 ++- bricks/nxt/systick.c | 15 ++++ bricks/primehub/mphalport.h | 9 ++- bricks/stm32/configport.h | 2 +- bricks/stm32/mphalport.c | 22 +++++- bricks/technichub/mphalport.h | 9 ++- lib/contiki-core/sys/clock.h | 70 +------------------ lib/ev3dev/src/ev3dev_stretch/nxtcolor.c | 3 +- lib/pbio/README.md | 2 +- .../bluetooth_btstack_run_loop_contiki.c | 12 ++-- .../drv/bluetooth/bluetooth_stm32_cc2640.c | 12 ++-- lib/pbio/drv/clock/clock.h | 9 +++ lib/pbio/drv/clock/clock_linux.c | 19 ++--- lib/pbio/drv/clock/clock_nxt.c | 34 +++------ lib/pbio/drv/clock/clock_stm32.c | 50 +++++-------- lib/pbio/drv/core.c | 7 +- lib/pbio/drv/ioport/ioport_lpf2.c | 2 +- lib/pbio/drv/uart/uart_stm32f0.c | 4 +- lib/pbio/drv/uart/uart_stm32f4_ll_irq.c | 4 +- lib/pbio/drv/uart/uart_stm32l4_ll_dma.c | 4 +- lib/pbio/drv/usb/stm32_usb_serial.c | 2 +- lib/pbio/include/pbdrv/clock.h | 26 +++++++ lib/pbio/platform/city_hub/contiki-conf.h | 5 +- lib/pbio/platform/city_hub/sys.c | 2 +- lib/pbio/platform/debug/contiki-conf.h | 5 +- lib/pbio/platform/debug/sys.c | 2 +- .../platform/essential_hub/contiki-conf.h | 5 +- lib/pbio/platform/essential_hub/sys.c | 2 +- .../platform/ev3dev_stretch/contiki-conf.h | 5 +- lib/pbio/platform/move_hub/contiki-conf.h | 5 +- lib/pbio/platform/move_hub/sys.c | 2 +- lib/pbio/platform/nxt/contiki-conf.h | 5 +- lib/pbio/platform/nxt/sys.c | 2 +- lib/pbio/platform/prime_hub/contiki-conf.h | 5 +- lib/pbio/platform/prime_hub/sys.c | 2 +- lib/pbio/platform/technic_hub/contiki-conf.h | 5 +- lib/pbio/platform/technic_hub/sys.c | 2 +- lib/pbio/src/drivebase.c | 8 +-- lib/pbio/src/integrator.c | 2 - lib/pbio/src/light/animation.h | 2 +- lib/pbio/src/light/color_light.c | 8 +-- lib/pbio/src/light/color_light.h | 2 - lib/pbio/src/logger.c | 7 +- lib/pbio/src/motor_process.c | 2 +- lib/pbio/src/servo.c | 13 ++-- lib/pbio/src/trajectory.c | 2 - lib/pbio/src/uartdev.c | 4 +- lib/pbio/sys/battery.c | 16 ++--- lib/pbio/sys/bluetooth.c | 4 +- lib/pbio/sys/light.c | 4 +- lib/pbio/sys/light_matrix.c | 4 +- lib/pbio/sys/status.c | 8 ++- lib/pbio/test/clock.c | 16 +++-- lib/pbio/test/contiki-conf.h | 7 +- lib/pbio/test/drv/bluetooth.c | 24 +++---- lib/pbio/test/src/animation.c | 6 +- lib/pbio/test/src/color_light.c | 10 +-- lib/pbio/test/src/light_matrix.c | 10 +-- lib/pbio/test/src/servo.c | 4 +- lib/pbio/test/src/uartdev.c | 34 ++++----- lib/pbio/test/sys/bluetooth.c | 14 ++-- lib/pbio/test/sys/status.c | 8 +-- 67 files changed, 309 insertions(+), 332 deletions(-) create mode 100644 lib/pbio/drv/clock/clock.h create mode 100644 lib/pbio/include/pbdrv/clock.h diff --git a/bricks/cityhub/mphalport.h b/bricks/cityhub/mphalport.h index caf32d106..1363c528f 100644 --- a/bricks/cityhub/mphalport.h +++ b/bricks/cityhub/mphalport.h @@ -1,10 +1,9 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors -#include +#include void mp_hal_set_interrupt_char(int c); -#define mp_hal_ticks_ms clock_time -#define mp_hal_ticks_us clock_usecs -#define mp_hal_delay_us clock_delay_usec +#define mp_hal_ticks_ms pbdrv_clock_get_ms +#define mp_hal_ticks_us pbdrv_clock_get_us #define mp_hal_ticks_cpu() 0 diff --git a/bricks/debug/mphalport.h b/bricks/debug/mphalport.h index caf32d106..1363c528f 100644 --- a/bricks/debug/mphalport.h +++ b/bricks/debug/mphalport.h @@ -1,10 +1,9 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors -#include +#include void mp_hal_set_interrupt_char(int c); -#define mp_hal_ticks_ms clock_time -#define mp_hal_ticks_us clock_usecs -#define mp_hal_delay_us clock_delay_usec +#define mp_hal_ticks_ms pbdrv_clock_get_ms +#define mp_hal_ticks_us pbdrv_clock_get_us #define mp_hal_ticks_cpu() 0 diff --git a/bricks/essentialhub/mphalport.h b/bricks/essentialhub/mphalport.h index 5d4399be3..1363c528f 100644 --- a/bricks/essentialhub/mphalport.h +++ b/bricks/essentialhub/mphalport.h @@ -1,10 +1,9 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors -#include "sys/clock.h" +#include void mp_hal_set_interrupt_char(int c); -#define mp_hal_ticks_ms clock_time -#define mp_hal_ticks_us clock_usecs -#define mp_hal_delay_us clock_delay_usec +#define mp_hal_ticks_ms pbdrv_clock_get_ms +#define mp_hal_ticks_us pbdrv_clock_get_us #define mp_hal_ticks_cpu() 0 diff --git a/bricks/ev3dev/ev3dev_mphal.c b/bricks/ev3dev/ev3dev_mphal.c index 6b4b6c74d..2d0fc5db9 100644 --- a/bricks/ev3dev/ev3dev_mphal.c +++ b/bricks/ev3dev/ev3dev_mphal.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include "py/mphal.h" #include "py/runtime.h" @@ -125,11 +125,11 @@ void mp_hal_stdout_tx_str(const char *str) { } mp_uint_t mp_hal_ticks_ms(void) { - return clock_to_msec(clock_time()); + return pbdrv_clock_get_ms(); } mp_uint_t mp_hal_ticks_us(void) { - return clock_usecs(); + return pbdrv_clock_get_us(); } uint64_t mp_hal_time_ns(void) { diff --git a/bricks/movehub/mphalport.h b/bricks/movehub/mphalport.h index caf32d106..1363c528f 100644 --- a/bricks/movehub/mphalport.h +++ b/bricks/movehub/mphalport.h @@ -1,10 +1,9 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors -#include +#include void mp_hal_set_interrupt_char(int c); -#define mp_hal_ticks_ms clock_time -#define mp_hal_ticks_us clock_usecs -#define mp_hal_delay_us clock_delay_usec +#define mp_hal_ticks_ms pbdrv_clock_get_ms +#define mp_hal_ticks_us pbdrv_clock_get_us #define mp_hal_ticks_cpu() 0 diff --git a/bricks/nxt/mphalport.h b/bricks/nxt/mphalport.h index caf32d106..1363c528f 100644 --- a/bricks/nxt/mphalport.h +++ b/bricks/nxt/mphalport.h @@ -1,10 +1,9 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors -#include +#include void mp_hal_set_interrupt_char(int c); -#define mp_hal_ticks_ms clock_time -#define mp_hal_ticks_us clock_usecs -#define mp_hal_delay_us clock_delay_usec +#define mp_hal_ticks_ms pbdrv_clock_get_ms +#define mp_hal_ticks_us pbdrv_clock_get_us #define mp_hal_ticks_cpu() 0 diff --git a/bricks/nxt/systick.c b/bricks/nxt/systick.c index 48108ebd0..90c0c6764 100644 --- a/bricks/nxt/systick.c +++ b/bricks/nxt/systick.c @@ -9,6 +9,7 @@ #include "py/mphal.h" #include "py/mpconfig.h" +#include #include #include @@ -30,3 +31,17 @@ void mp_hal_delay_ms(mp_uint_t Delay) { systick_wait_ms(Delay); } } + +/* The inner loop takes 4 cycles. The outer 5+SPIN_COUNT*4. */ + +#define SPIN_TIME 2 /* us */ +#define SPIN_COUNT (((CLOCK_FREQUENCY * SPIN_TIME / 1000000) - 5) / 4) + +void mp_hal_delay_us(mp_uint_t t) { + #ifdef __THUMBEL__ + __asm volatile ("1: mov r1,%2\n2:\tsub r1,#1\n\tbne 2b\n\tsub %0,#1\n\tbne 1b\n" + : "=l" (t) : "0" (t), "l" (SPIN_COUNT)); + #else + #error Must be compiled in thumb mode + #endif +} diff --git a/bricks/primehub/mphalport.h b/bricks/primehub/mphalport.h index 5d4399be3..1363c528f 100644 --- a/bricks/primehub/mphalport.h +++ b/bricks/primehub/mphalport.h @@ -1,10 +1,9 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors -#include "sys/clock.h" +#include void mp_hal_set_interrupt_char(int c); -#define mp_hal_ticks_ms clock_time -#define mp_hal_ticks_us clock_usecs -#define mp_hal_delay_us clock_delay_usec +#define mp_hal_ticks_ms pbdrv_clock_get_ms +#define mp_hal_ticks_us pbdrv_clock_get_us #define mp_hal_ticks_cpu() 0 diff --git a/bricks/stm32/configport.h b/bricks/stm32/configport.h index bdd37d007..fb1b9e923 100644 --- a/bricks/stm32/configport.h +++ b/bricks/stm32/configport.h @@ -69,7 +69,7 @@ #define MICROPY_PY_SYS_STDIO_BUFFER (PYBRICKS_STM32_OPT_EXTRA_MOD) #define MICROPY_PY_URANDOM (PYBRICKS_STM32_OPT_EXTRA_MOD) #define MICROPY_PY_URANDOM_EXTRA_FUNCS (PYBRICKS_STM32_OPT_EXTRA_MOD) -#define MICROPY_PY_URANDOM_SEED_INIT_FUNC ({ extern unsigned long clock_usecs(void); clock_usecs(); }) +#define MICROPY_PY_URANDOM_SEED_INIT_FUNC ({ extern uint32_t pbdrv_clock_get_us(void); pbdrv_clock_get_us(); }) #define MICROPY_PY_USELECT (PYBRICKS_STM32_OPT_EXTRA_MOD) #define MICROPY_PY_UTIME_MP_HAL (0) #define MICROPY_MODULE_BUILTIN_INIT (1) diff --git a/bricks/stm32/mphalport.c b/bricks/stm32/mphalport.c index 1ba242caa..2d33b6890 100644 --- a/bricks/stm32/mphalport.c +++ b/bricks/stm32/mphalport.c @@ -13,21 +13,21 @@ #include "py/stream.h" // using "internal" pbdrv variable -extern volatile uint32_t clock_time_ticks; +extern volatile uint32_t pbdrv_clock_ticks; // Core delay function that does an efficient sleep and may switch thread context. // If IRQs are enabled then we must have the GIL. void mp_hal_delay_ms(mp_uint_t Delay) { if (__get_PRIMASK() == 0) { // IRQs enabled, so can use systick counter to do the delay - uint32_t start = clock_time_ticks; + uint32_t start = pbdrv_clock_ticks; // Wraparound of tick is taken care of by 2's complement arithmetic. do { // This macro will execute the necessary idle behaviour. It may // raise an exception, switch threads or enter sleep mode (waiting for // (at least) the SysTick interrupt). MICROPY_EVENT_POLL_HOOK - } while (clock_time_ticks - start < Delay); + } while (pbdrv_clock_ticks - start < Delay); } else { // IRQs disabled, so need to use a busy loop for the delay. // To prevent possible overflow of the counter we use a double loop. @@ -39,6 +39,22 @@ void mp_hal_delay_ms(mp_uint_t Delay) { } } +// delay for given number of microseconds +void mp_hal_delay_us(mp_uint_t usec) { + if (__get_PRIMASK() == 1) { + // IRQs enabled, so can use systick counter to do the delay + uint32_t start = pbdrv_clock_get_us(); + while (pbdrv_clock_get_us() - start < usec) { + } + } else { + // IRQs disabled, so need to use a busy loop for the delay + // sys freq is always a multiple of 2MHz, so division here won't lose precision + const uint32_t ucount = PBDRV_CONFIG_SYS_CLOCK_RATE / 2000000 * usec / 2; + for (uint32_t count = 0; ++count <= ucount;) { + } + } +} + #if PYBRICKS_HUB_DEBUG #include "stm32f4xx.h" diff --git a/bricks/technichub/mphalport.h b/bricks/technichub/mphalport.h index caf32d106..1363c528f 100644 --- a/bricks/technichub/mphalport.h +++ b/bricks/technichub/mphalport.h @@ -1,10 +1,9 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors -#include +#include void mp_hal_set_interrupt_char(int c); -#define mp_hal_ticks_ms clock_time -#define mp_hal_ticks_us clock_usecs -#define mp_hal_delay_us clock_delay_usec +#define mp_hal_ticks_ms pbdrv_clock_get_ms +#define mp_hal_ticks_us pbdrv_clock_get_us #define mp_hal_ticks_cpu() 0 diff --git a/lib/contiki-core/sys/clock.h b/lib/contiki-core/sys/clock.h index 610305b3e..757178266 100644 --- a/lib/contiki-core/sys/clock.h +++ b/lib/contiki-core/sys/clock.h @@ -82,48 +82,6 @@ #else #define CLOCK_SECOND (clock_time_t)32 #endif - -/** - * Converts milliseconds to clock ticks, rounding up so that the actual time - * span will be >= *msec*. - * - * \param msec the time in milliseconds - * - * \return a ::clock_time_t value - */ -static inline clock_time_t clock_from_msec(unsigned int msec) { -#if CLOCK_SECOND == 1000 - return msec; -#else - return (msec * CLOCK_SECOND + 999) / 1000; -#endif -} - -/** - * Converts clock ticks to milliseconds, rounding down so that the actual time - * span will be <= *ticks*. - * - * \param ticks a ::clock_time_t value - * - * \return the time in milliseconds - */ -static inline unsigned int clock_to_msec(clock_time_t ticks) { -#if CLOCK_SECOND == 1000 - return ticks; -#else - return ticks * 1000 / CLOCK_SECOND; -#endif -} - -/** - * Initialize the clock library. - * - * This function initializes the clock library and should be called - * from the main() function of the system. - * - */ -void clock_init(void); - /** * Get the current clock time. * @@ -131,7 +89,7 @@ void clock_init(void); * * \return The current clock time, measured in system ticks. */ -CCIF clock_time_t clock_time(void); +clock_time_t clock_time(void); /** * Get the current value of the platform seconds. @@ -141,36 +99,14 @@ CCIF clock_time_t clock_time(void); * * \return The value. */ -CCIF unsigned long clock_seconds(void); +uint32_t clock_seconds(void); /** * Get the current value of the platform microseconds. * * \return The value. */ -CCIF unsigned long clock_usecs(void); - -/** - * Set the value of the platform seconds. - * \param sec The value to set. - * - */ -void clock_set_seconds(unsigned long sec); - -/** - * Wait for a given number of ticks. - * \param t How many ticks. - * - */ -void clock_wait(clock_time_t t); - -/** - * Delay a given number of microseconds. - * \param dt How many microseconds to delay. - * - * \note Interrupts could increase the delay by a variable amount. - */ -void clock_delay_usec(uint16_t dt); +uint32_t clock_usecs(void); #endif /* CLOCK_H_ */ diff --git a/lib/ev3dev/src/ev3dev_stretch/nxtcolor.c b/lib/ev3dev/src/ev3dev_stretch/nxtcolor.c index b2af2d519..c9de004fe 100644 --- a/lib/ev3dev/src/ev3dev_stretch/nxtcolor.c +++ b/lib/ev3dev/src/ev3dev_stretch/nxtcolor.c @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -89,7 +90,7 @@ nxtcolor_t nxtcolorsensors[4]; // Simplistic nonbusy wait. May be called only once per blocking operation. pbio_error_t nxtcolor_wait(nxtcolor_t *nxtcolor, uint32_t ms) { - uint32_t now = clock_usecs(); + uint32_t now = pbdrv_clock_get_us(); // Wait for existing wait to complete if (nxtcolor->waiting) { diff --git a/lib/pbio/README.md b/lib/pbio/README.md index b85dae11c..6f60eeef8 100644 --- a/lib/pbio/README.md +++ b/lib/pbio/README.md @@ -18,7 +18,7 @@ for the various platform-specific hardware. The `include/pbdrv` directory contains the public header files for the drivers (`drv` directory). Generally, these header files should not be used outside of -this library. +this library. One common exception is clocks. The `include/pbio` directory contains the public header files for the main library (`src` directory). This is the API for user programs. diff --git a/lib/pbio/drv/bluetooth/bluetooth_btstack_run_loop_contiki.c b/lib/pbio/drv/bluetooth/bluetooth_btstack_run_loop_contiki.c index b8f634184..9f8508183 100644 --- a/lib/pbio/drv/bluetooth/bluetooth_btstack_run_loop_contiki.c +++ b/lib/pbio/drv/bluetooth/bluetooth_btstack_run_loop_contiki.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2020 The Pybricks Authors +// Copyright (c) 2020-2021 The Pybricks Authors // Contiki run loop integration for BlueKitchen BTStack. @@ -14,6 +14,8 @@ #include #include +_Static_assert(CLOCK_CONF_SECOND == 1000, "contiki clock must be configured for 1ms ticks"); + PROCESS(btstack_run_loop_contiki_process, "btstack"); static struct etimer timer; static btstack_linked_list_t timers; @@ -30,7 +32,7 @@ static void schedule_timer(void) { if (first_timer == NULL) { etimer_stop(&timer); } else { - etimer_set(&timer, clock_from_msec(first_timer->timeout - clock_to_msec(clock_time()))); + etimer_set(&timer, first_timer->timeout - pbdrv_clock_get_ms()); } PROCESS_CONTEXT_END(); @@ -57,7 +59,7 @@ static void btstack_run_loop_contiki_disable_data_source_callbacks(btstack_data_ } static void btstack_run_loop_contiki_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms) { - ts->timeout = clock_to_msec(clock_time()) + timeout_in_ms; + ts->timeout = pbdrv_clock_get_ms() + timeout_in_ms; } static void btstack_run_loop_contiki_add_timer(btstack_timer_source_t *ts) { @@ -100,7 +102,7 @@ static void btstack_run_loop_contiki_dump_timer(void) { } static uint32_t btstack_run_loop_contiki_get_time_ms(void) { - return clock_to_msec(clock_time()); + return pbdrv_clock_get_ms(); } static const btstack_run_loop_t btstack_run_loop_contiki = { @@ -153,7 +155,7 @@ PROCESS_THREAD(btstack_run_loop_contiki_process, ev, data) { // process all BTStack timers in list that have expired while (timers) { btstack_timer_source_t *ts = (void *)timers; - int32_t delta = btstack_time_delta(ts->timeout, clock_to_msec(clock_time())); + int32_t delta = btstack_time_delta(ts->timeout, pbdrv_clock_get_ms()); if (delta > 0) { // we have reached unexpired timers break; diff --git a/lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c b/lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c index 1a264a52f..71522a9fd 100644 --- a/lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c +++ b/lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c @@ -1591,7 +1591,7 @@ PROCESS_THREAD(pbdrv_bluetooth_spi_process, ev, data) { bluetooth_reset(RESET_STATE_OUT_HIGH); // not sure why we need this - etimer_set(&timer, clock_from_msec(150)); + etimer_set(&timer, 150); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)); bluetooth_reset(RESET_STATE_INPUT); @@ -1600,7 +1600,7 @@ PROCESS_THREAD(pbdrv_bluetooth_spi_process, ev, data) { if (spi_srdy) { static bool timed_out = false; - etimer_set(&timer, clock_from_msec(500)); + etimer_set(&timer, 500); while (spi_srdy) { PROCESS_WAIT_EVENT(); @@ -1610,16 +1610,16 @@ PROCESS_THREAD(pbdrv_bluetooth_spi_process, ev, data) { } if (timed_out) { - etimer_set(&timer, clock_from_msec(3000)); + etimer_set(&timer, 3000); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)); bluetooth_reset(RESET_STATE_OUT_LOW); - etimer_set(&timer, clock_from_msec(150)); + etimer_set(&timer, 150); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)); goto start; } } - etimer_set(&timer, clock_from_msec(100)); + etimer_set(&timer, 100); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)); static pbio_task_t task; @@ -1684,7 +1684,7 @@ PROCESS_THREAD(pbdrv_bluetooth_spi_process, ev, data) { // REVISIT: maybe there is a way to get individual oneshots for the // rising and falling edges of the interrupt instead of the timer hack? - etimer_set(&timer, clock_from_msec(2)); + etimer_set(&timer, 2); spi_set_mrdy(false); PROCESS_WAIT_UNTIL(!spi_srdy || (ev == PROCESS_EVENT_TIMER && etimer_expired(&timer))); diff --git a/lib/pbio/drv/clock/clock.h b/lib/pbio/drv/clock/clock.h new file mode 100644 index 000000000..0ad145b2c --- /dev/null +++ b/lib/pbio/drv/clock/clock.h @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2021 The Pybricks Authors + +#ifndef _INTERNAL_PVDRV_CLOCK_H_ +#define _INTERNAL_PVDRV_CLOCK_H_ + +void pbdrv_clock_init(void); + +#endif // _INTERNAL_PVDRV_CLOCK_H_ diff --git a/lib/pbio/drv/clock/clock_linux.c b/lib/pbio/drv/clock/clock_linux.c index e2ef9733e..e6de74565 100644 --- a/lib/pbio/drv/clock/clock_linux.c +++ b/lib/pbio/drv/clock/clock_linux.c @@ -1,36 +1,27 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #include #if PBDRV_CONFIG_CLOCK_LINUX -// TODO: replace this with test/clock.c - this probably requires reworking ev3dev-stretch background thread - -#include #include +#include #include -#include - -void clock_init(void) { +void pbdrv_clock_init(void) { } -clock_time_t clock_time(void) { +uint32_t pbdrv_clock_get_ms(void) { struct timespec time_val; clock_gettime(CLOCK_MONOTONIC_RAW, &time_val); return time_val.tv_sec * 1000 + time_val.tv_nsec / 1000000; } -unsigned long clock_usecs(void) { +uint32_t pbdrv_clock_get_us(void) { struct timespec time_val; clock_gettime(CLOCK_MONOTONIC_RAW, &time_val); return time_val.tv_sec * 1000000 + time_val.tv_nsec / 1000; } -void clock_delay_usec(uint16_t duration) { - // FIXME: is there a way to busy-wait on Linux? maybe call clock_gettime() in a loop? - usleep(duration); -} - #endif // PBDRV_CONFIG_CLOCK_LINUX diff --git a/lib/pbio/drv/clock/clock_nxt.c b/lib/pbio/drv/clock/clock_nxt.c index e0e79de51..f695b04a3 100644 --- a/lib/pbio/drv/clock/clock_nxt.c +++ b/lib/pbio/drv/clock/clock_nxt.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2019-2020 The Pybricks Authors +// Copyright (c) 2019-2021 The Pybricks Authors #include @@ -10,39 +10,21 @@ #include #include -#if CLOCK_CONF_SECOND != 1000 -#error Clock must be set to 1 msec ticks -#endif - static void clock_systick_hook(void) { etimer_request_poll(); } -clock_time_t clock_time(void) { - return systick_get_ms(); -} - -uint32_t clock_usecs(void) { - return systick_get_us(); -} - -void clock_init(void) { - // TODO: Split NXT init into clock_init etc +void pbdrv_clock_init(void) { + // TODO: Split NXT init into pbdrv_clock_init etc systick_set_hook(clock_systick_hook); } -/* The inner loop takes 4 cycles. The outer 5+SPIN_COUNT*4. */ - -#define SPIN_TIME 2 /* us */ -#define SPIN_COUNT (((CLOCK_FREQUENCY * SPIN_TIME / 1000000) - 5) / 4) +uint32_t pbdrv_clock_get_ms(void) { + return systick_get_ms(); +} -void clock_delay_usec(uint16_t t) { - #ifdef __THUMBEL__ - __asm volatile ("1: mov r1,%2\n2:\tsub r1,#1\n\tbne 2b\n\tsub %0,#1\n\tbne 1b\n" - : "=l" (t) : "0" (t), "l" (SPIN_COUNT)); - #else - #error Must be compiled in thumb mode - #endif +uint32_t pbdrv_clock_get_us(void) { + return systick_get_us(); } #endif // PBDRV_CONFIG_CLOCK_NXT diff --git a/lib/pbio/drv/clock/clock_stm32.c b/lib/pbio/drv/clock/clock_stm32.c index 956f780d3..b4418018c 100644 --- a/lib/pbio/drv/clock/clock_stm32.c +++ b/lib/pbio/drv/clock/clock_stm32.c @@ -1,38 +1,36 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2013, 2014 Damien P. George -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors + +#include #include #if PBDRV_CONFIG_CLOCK_STM32 -#include - #include STM32_H -#if CLOCK_CONF_SECOND != 1000 -#error Clock must be set to 1 msec ticks -#endif - -volatile clock_time_t clock_time_ticks; +// NB: pbdrv_clock_ticks is intended to be private, but making it static +// breaks things. +volatile uint32_t pbdrv_clock_ticks; -void clock_init(void) { +void pbdrv_clock_init(void) { // STM32 does platform-specific clock init early in SystemInit() } -clock_time_t clock_time(void) { - return clock_time_ticks; +uint32_t pbdrv_clock_get_ms(void) { + return pbdrv_clock_ticks; } // The SysTick timer counts down at 168 MHz, so we can use that knowledge // to grab a microsecond counter. -uint32_t clock_usecs(void) { +uint32_t pbdrv_clock_get_us(void) { uint32_t irq_state, counter, msec, status; irq_state = __get_PRIMASK(); __disable_irq(); counter = SysTick->VAL; - msec = clock_time_ticks; + msec = pbdrv_clock_ticks; status = SysTick->CTRL; __set_PRIMASK(irq_state); @@ -57,27 +55,11 @@ uint32_t clock_usecs(void) { return msec * 1000 + (counter * 1000) / (load + 1); } -// delay for given number of microseconds -void clock_delay_usec(uint16_t usec) { - if (__get_PRIMASK() == 1) { - // IRQs enabled, so can use systick counter to do the delay - uint32_t start = clock_usecs(); - while (clock_usecs() - start < usec) { - } - } else { - // IRQs disabled, so need to use a busy loop for the delay - // sys freq is always a multiple of 2MHz, so division here won't lose precision - const uint32_t ucount = PBDRV_CONFIG_SYS_CLOCK_RATE / 2000000 * usec / 2; - for (uint32_t count = 0; ++count <= ucount;) { - } - } -} - void SysTick_Handler(void) { - clock_time_ticks++; + pbdrv_clock_ticks++; // Read the systick control regster. This has the side effect of clearing - // the COUNTFLAG bit, which makes the logic in clock_usecs + // the COUNTFLAG bit, which makes the logic in pbdrv_clock_get_us // work properly. SysTick->CTRL; @@ -85,7 +67,7 @@ void SysTick_Handler(void) { } uint32_t HAL_GetTick(void) { - return clock_time_ticks; + return pbdrv_clock_ticks; } // We provide our own version of HAL_Delay that calls __WFI while waiting, @@ -94,9 +76,9 @@ uint32_t HAL_GetTick(void) { void HAL_Delay(uint32_t Delay) { if (__get_PRIMASK() == 0) { // IRQs enabled, so can use systick counter to do the delay - uint32_t start = clock_time_ticks; + uint32_t start = pbdrv_clock_ticks; // Wraparound of tick is taken care of by 2's complement arithmetic. - while (clock_time_ticks - start < Delay) { + while (pbdrv_clock_ticks - start < Delay) { // Enter sleep mode, waiting for (at least) the SysTick interrupt. __WFI(); } diff --git a/lib/pbio/drv/core.c b/lib/pbio/drv/core.c index 60e446cd4..962f119f9 100644 --- a/lib/pbio/drv/core.c +++ b/lib/pbio/drv/core.c @@ -6,6 +6,7 @@ #include "core.h" #include "battery/battery.h" #include "bluetooth/bluetooth.h" +#include "clock/clock.h" #include "counter/counter.h" #include "ioport/ioport.h" #include "led/led_array.h" @@ -19,9 +20,12 @@ uint32_t pbdrv_init_busy_count; /** Initializes all enabled drivers. */ void pbdrv_init(void) { - clock_init(); + // it is important that clocks go first since almost everything depends on clocks + pbdrv_clock_init(); process_init(); process_start(&etimer_process); + + // the rest of the drivers should be implemented so that init order doesn't matter pbdrv_battery_init(); pbdrv_bluetooth_init(); pbdrv_counter_init(); @@ -32,6 +36,7 @@ void pbdrv_init(void) { pbdrv_reset_init(); pbdrv_sound_init(); pbdrv_watchdog_init(); + while (pbdrv_init_busy()) { process_run(); } diff --git a/lib/pbio/drv/ioport/ioport_lpf2.c b/lib/pbio/drv/ioport/ioport_lpf2.c index 8df74e02c..815500141 100644 --- a/lib/pbio/drv/ioport/ioport_lpf2.c +++ b/lib/pbio/drv/ioport/ioport_lpf2.c @@ -423,7 +423,7 @@ PROCESS_THREAD(pbdrv_ioport_lpf2_process, ev, data) { PROCESS_BEGIN(); - etimer_set(&timer, clock_from_msec(2)); + etimer_set(&timer, 2); for (int i = 0; i < PBDRV_CONFIG_IOPORT_LPF2_NUM_PORTS; i++) { init_one(i); diff --git a/lib/pbio/drv/uart/uart_stm32f0.c b/lib/pbio/drv/uart/uart_stm32f0.c index c880a93f9..b99e7f0c2 100644 --- a/lib/pbio/drv/uart/uart_stm32f0.c +++ b/lib/pbio/drv/uart/uart_stm32f0.c @@ -82,7 +82,7 @@ pbio_error_t pbdrv_uart_read_begin(pbdrv_uart_dev_t *uart_dev, uint8_t *msg, uin uart->rx_buf_index = 0; uart->rx_result = PBIO_ERROR_AGAIN; - etimer_set(&uart->rx_timer, clock_from_msec(timeout)); + etimer_set(&uart->rx_timer, timeout); process_poll(&pbdrv_uart_process); @@ -131,7 +131,7 @@ pbio_error_t pbdrv_uart_write_begin(pbdrv_uart_dev_t *uart_dev, uint8_t *msg, ui uart->tx_buf_index = 0; uart->tx_result = PBIO_ERROR_AGAIN; - etimer_set(&uart->tx_timer, clock_from_msec(timeout)); + etimer_set(&uart->tx_timer, timeout); uart->USART->CR1 |= USART_CR1_TXEIE; diff --git a/lib/pbio/drv/uart/uart_stm32f4_ll_irq.c b/lib/pbio/drv/uart/uart_stm32f4_ll_irq.c index f4d0a4115..57cdc36d8 100644 --- a/lib/pbio/drv/uart/uart_stm32f4_ll_irq.c +++ b/lib/pbio/drv/uart/uart_stm32f4_ll_irq.c @@ -83,7 +83,7 @@ pbio_error_t pbdrv_uart_read_begin(pbdrv_uart_dev_t *uart_dev, uint8_t *msg, uin uart->read_length = length; uart->read_pos = 0; - etimer_set(&uart->read_timer, clock_from_msec(timeout)); + etimer_set(&uart->read_timer, timeout); return PBIO_SUCCESS; } @@ -121,7 +121,7 @@ pbio_error_t pbdrv_uart_write_begin(pbdrv_uart_dev_t *uart_dev, uint8_t *msg, ui uart->write_length = length; uart->write_pos = 0; - etimer_set(&uart->write_timer, clock_from_msec(timeout)); + etimer_set(&uart->write_timer, timeout); LL_USART_EnableIT_TXE(uart->pdata->uart); diff --git a/lib/pbio/drv/uart/uart_stm32l4_ll_dma.c b/lib/pbio/drv/uart/uart_stm32l4_ll_dma.c index 0873e6eb3..bc46a8a37 100644 --- a/lib/pbio/drv/uart/uart_stm32l4_ll_dma.c +++ b/lib/pbio/drv/uart/uart_stm32l4_ll_dma.c @@ -70,7 +70,7 @@ pbio_error_t pbdrv_uart_read_begin(pbdrv_uart_dev_t *uart_dev, uint8_t *msg, uin uart->read_buf = msg; uart->read_length = length; - etimer_set(&uart->rx_timer, clock_from_msec(timeout)); + etimer_set(&uart->rx_timer, timeout); return PBIO_SUCCESS; } @@ -258,7 +258,7 @@ pbio_error_t pbdrv_uart_write_begin(pbdrv_uart_dev_t *uart_dev, uint8_t *msg, ui LL_USART_ClearFlag_TC(pdata->uart); LL_USART_EnableDMAReq_TX(pdata->uart); - etimer_set(&uart->tx_timer, clock_from_msec(timeout)); + etimer_set(&uart->tx_timer, timeout); return PBIO_SUCCESS; } diff --git a/lib/pbio/drv/usb/stm32_usb_serial.c b/lib/pbio/drv/usb/stm32_usb_serial.c index b08c2a875..01be138e3 100644 --- a/lib/pbio/drv/usb/stm32_usb_serial.c +++ b/lib/pbio/drv/usb/stm32_usb_serial.c @@ -246,7 +246,7 @@ PROCESS_THREAD(pbdrv_usb_process, ev, data) { PROCESS_BEGIN(); pbdrv_stm32_usb_serial_init(); - etimer_set(&timer, clock_from_msec(5)); + etimer_set(&timer, 5); for (;;) { PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_POLL || (ev == PROCESS_EVENT_TIMER && etimer_expired(&timer))); diff --git a/lib/pbio/include/pbdrv/clock.h b/lib/pbio/include/pbdrv/clock.h new file mode 100644 index 000000000..77d0fa72c --- /dev/null +++ b/lib/pbio/include/pbdrv/clock.h @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2021 The Pybricks Authors + +/** + * @addtogroup ClockDriver Driver: Clock + * @{ + */ + +#ifndef _PBDRV_CLOCK_H_ +#define _PBDRV_CLOCK_H_ + +#include + +/** + * Gets the current clock time in milliseconds. + */ +uint32_t pbdrv_clock_get_ms(void); + +/** + * Gets the current clock time in microseconds. + */ +uint32_t pbdrv_clock_get_us(void); + +#endif /* _PBDRV_CLOCK_H_ */ + +/** @} */ diff --git a/lib/pbio/platform/city_hub/contiki-conf.h b/lib/pbio/platform/city_hub/contiki-conf.h index 4e2176379..c1dedd936 100644 --- a/lib/pbio/platform/city_hub/contiki-conf.h +++ b/lib/pbio/platform/city_hub/contiki-conf.h @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #ifndef _PBIO_CONF_H_ #define _PBIO_CONF_H_ @@ -13,4 +13,7 @@ typedef uint32_t clock_time_t; #define CLOCK_CONF_SECOND 1000 +#define clock_time pbdrv_clock_get_ms +#define clock_usecs pbdrv_clock_get_us + #endif /* _PBIO_CONF_H_ */ diff --git a/lib/pbio/platform/city_hub/sys.c b/lib/pbio/platform/city_hub/sys.c index fb9e631c9..111ea8173 100644 --- a/lib/pbio/platform/city_hub/sys.c +++ b/lib/pbio/platform/city_hub/sys.c @@ -25,7 +25,7 @@ PROCESS_THREAD(pbsys_process, ev, data) { pbsys_battery_init(); pbsys_bluetooth_init(); pbsys_hmi_init(); - etimer_set(&timer, clock_from_msec(50)); + etimer_set(&timer, 50); while (true) { PROCESS_WAIT_EVENT(); diff --git a/lib/pbio/platform/debug/contiki-conf.h b/lib/pbio/platform/debug/contiki-conf.h index 4e2176379..c1dedd936 100644 --- a/lib/pbio/platform/debug/contiki-conf.h +++ b/lib/pbio/platform/debug/contiki-conf.h @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #ifndef _PBIO_CONF_H_ #define _PBIO_CONF_H_ @@ -13,4 +13,7 @@ typedef uint32_t clock_time_t; #define CLOCK_CONF_SECOND 1000 +#define clock_time pbdrv_clock_get_ms +#define clock_usecs pbdrv_clock_get_us + #endif /* _PBIO_CONF_H_ */ diff --git a/lib/pbio/platform/debug/sys.c b/lib/pbio/platform/debug/sys.c index 79148b5a4..ca071d374 100644 --- a/lib/pbio/platform/debug/sys.c +++ b/lib/pbio/platform/debug/sys.c @@ -18,7 +18,7 @@ PROCESS_THREAD(pbsys_process, ev, data) { PROCESS_BEGIN(); pbsys_hmi_init(); - etimer_set(&timer, clock_from_msec(50)); + etimer_set(&timer, 50); while (true) { PROCESS_WAIT_EVENT(); diff --git a/lib/pbio/platform/essential_hub/contiki-conf.h b/lib/pbio/platform/essential_hub/contiki-conf.h index 4e2176379..c1dedd936 100644 --- a/lib/pbio/platform/essential_hub/contiki-conf.h +++ b/lib/pbio/platform/essential_hub/contiki-conf.h @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #ifndef _PBIO_CONF_H_ #define _PBIO_CONF_H_ @@ -13,4 +13,7 @@ typedef uint32_t clock_time_t; #define CLOCK_CONF_SECOND 1000 +#define clock_time pbdrv_clock_get_ms +#define clock_usecs pbdrv_clock_get_us + #endif /* _PBIO_CONF_H_ */ diff --git a/lib/pbio/platform/essential_hub/sys.c b/lib/pbio/platform/essential_hub/sys.c index fb9e631c9..111ea8173 100644 --- a/lib/pbio/platform/essential_hub/sys.c +++ b/lib/pbio/platform/essential_hub/sys.c @@ -25,7 +25,7 @@ PROCESS_THREAD(pbsys_process, ev, data) { pbsys_battery_init(); pbsys_bluetooth_init(); pbsys_hmi_init(); - etimer_set(&timer, clock_from_msec(50)); + etimer_set(&timer, 50); while (true) { PROCESS_WAIT_EVENT(); diff --git a/lib/pbio/platform/ev3dev_stretch/contiki-conf.h b/lib/pbio/platform/ev3dev_stretch/contiki-conf.h index ab103538c..8c0ed1bf6 100644 --- a/lib/pbio/platform/ev3dev_stretch/contiki-conf.h +++ b/lib/pbio/platform/ev3dev_stretch/contiki-conf.h @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #ifndef _PBIO_CONF_H_ #define _PBIO_CONF_H_ @@ -14,4 +14,7 @@ typedef uint32_t clock_time_t; #define CLOCK_CONF_SECOND 1000 +#define clock_time pbdrv_clock_get_ms +#define clock_usecs pbdrv_clock_get_us + #endif /* _PBIO_CONF_H_ */ diff --git a/lib/pbio/platform/move_hub/contiki-conf.h b/lib/pbio/platform/move_hub/contiki-conf.h index 0b1a6ffc3..3585e853a 100644 --- a/lib/pbio/platform/move_hub/contiki-conf.h +++ b/lib/pbio/platform/move_hub/contiki-conf.h @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #ifndef _PBIO_CONF_H_ #define _PBIO_CONF_H_ @@ -13,6 +13,9 @@ typedef uint32_t clock_time_t; #define CLOCK_CONF_SECOND 1000 +#define clock_time pbdrv_clock_get_ms +#define clock_usecs pbdrv_clock_get_us + #define PROCESS_CONF_NO_PROCESS_NAMES 1 #endif /* _PBIO_CONF_H_ */ diff --git a/lib/pbio/platform/move_hub/sys.c b/lib/pbio/platform/move_hub/sys.c index fb9e631c9..111ea8173 100644 --- a/lib/pbio/platform/move_hub/sys.c +++ b/lib/pbio/platform/move_hub/sys.c @@ -25,7 +25,7 @@ PROCESS_THREAD(pbsys_process, ev, data) { pbsys_battery_init(); pbsys_bluetooth_init(); pbsys_hmi_init(); - etimer_set(&timer, clock_from_msec(50)); + etimer_set(&timer, 50); while (true) { PROCESS_WAIT_EVENT(); diff --git a/lib/pbio/platform/nxt/contiki-conf.h b/lib/pbio/platform/nxt/contiki-conf.h index 4e2176379..c1dedd936 100644 --- a/lib/pbio/platform/nxt/contiki-conf.h +++ b/lib/pbio/platform/nxt/contiki-conf.h @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #ifndef _PBIO_CONF_H_ #define _PBIO_CONF_H_ @@ -13,4 +13,7 @@ typedef uint32_t clock_time_t; #define CLOCK_CONF_SECOND 1000 +#define clock_time pbdrv_clock_get_ms +#define clock_usecs pbdrv_clock_get_us + #endif /* _PBIO_CONF_H_ */ diff --git a/lib/pbio/platform/nxt/sys.c b/lib/pbio/platform/nxt/sys.c index b4ecad6eb..fb404bf03 100644 --- a/lib/pbio/platform/nxt/sys.c +++ b/lib/pbio/platform/nxt/sys.c @@ -22,7 +22,7 @@ PROCESS_THREAD(pbsys_process, ev, data) { pbsys_battery_init(); pbsys_hmi_init(); - etimer_set(&timer, clock_from_msec(50)); + etimer_set(&timer, 50); while (true) { PROCESS_WAIT_EVENT(); diff --git a/lib/pbio/platform/prime_hub/contiki-conf.h b/lib/pbio/platform/prime_hub/contiki-conf.h index 4e2176379..c1dedd936 100644 --- a/lib/pbio/platform/prime_hub/contiki-conf.h +++ b/lib/pbio/platform/prime_hub/contiki-conf.h @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #ifndef _PBIO_CONF_H_ #define _PBIO_CONF_H_ @@ -13,4 +13,7 @@ typedef uint32_t clock_time_t; #define CLOCK_CONF_SECOND 1000 +#define clock_time pbdrv_clock_get_ms +#define clock_usecs pbdrv_clock_get_us + #endif /* _PBIO_CONF_H_ */ diff --git a/lib/pbio/platform/prime_hub/sys.c b/lib/pbio/platform/prime_hub/sys.c index fb9e631c9..111ea8173 100644 --- a/lib/pbio/platform/prime_hub/sys.c +++ b/lib/pbio/platform/prime_hub/sys.c @@ -25,7 +25,7 @@ PROCESS_THREAD(pbsys_process, ev, data) { pbsys_battery_init(); pbsys_bluetooth_init(); pbsys_hmi_init(); - etimer_set(&timer, clock_from_msec(50)); + etimer_set(&timer, 50); while (true) { PROCESS_WAIT_EVENT(); diff --git a/lib/pbio/platform/technic_hub/contiki-conf.h b/lib/pbio/platform/technic_hub/contiki-conf.h index 4e2176379..c1dedd936 100644 --- a/lib/pbio/platform/technic_hub/contiki-conf.h +++ b/lib/pbio/platform/technic_hub/contiki-conf.h @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #ifndef _PBIO_CONF_H_ #define _PBIO_CONF_H_ @@ -13,4 +13,7 @@ typedef uint32_t clock_time_t; #define CLOCK_CONF_SECOND 1000 +#define clock_time pbdrv_clock_get_ms +#define clock_usecs pbdrv_clock_get_us + #endif /* _PBIO_CONF_H_ */ diff --git a/lib/pbio/platform/technic_hub/sys.c b/lib/pbio/platform/technic_hub/sys.c index db7b926b5..2e21e9e50 100644 --- a/lib/pbio/platform/technic_hub/sys.c +++ b/lib/pbio/platform/technic_hub/sys.c @@ -26,7 +26,7 @@ PROCESS_THREAD(pbsys_process, ev, data) { pbsys_battery_init(); pbsys_bluetooth_init(); pbsys_hmi_init(); - etimer_set(&timer, clock_from_msec(50)); + etimer_set(&timer, 50); while (true) { PROCESS_WAIT_EVENT(); diff --git a/lib/pbio/src/drivebase.c b/lib/pbio/src/drivebase.c index f8ca0e846..f214d6979 100644 --- a/lib/pbio/src/drivebase.c +++ b/lib/pbio/src/drivebase.c @@ -1,10 +1,8 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2018-2020 The Pybricks Authors -#include - #include - +#include #include #include #include @@ -68,7 +66,7 @@ static pbio_error_t drivebase_get_state(pbio_drivebase_t *db, pbio_error_t err; // Read current state of this motor: current time, speed, and position - *time_now = clock_usecs(); + *time_now = pbdrv_clock_get_us(); int32_t count_left; err = pbio_tacho_get_count(db->left->tacho, &count_left); @@ -448,7 +446,7 @@ pbio_error_t pbio_drivebase_drive(pbio_drivebase_t *db, int32_t speed, int32_t t return err; } } else { - time_now = clock_usecs(); + time_now = pbdrv_clock_get_us(); } // Initialize both controllers diff --git a/lib/pbio/src/integrator.c b/lib/pbio/src/integrator.c index f6031bf3d..f9b004722 100644 --- a/lib/pbio/src/integrator.c +++ b/lib/pbio/src/integrator.c @@ -5,8 +5,6 @@ #include #include -#include - #include #include #include diff --git a/lib/pbio/src/light/animation.h b/lib/pbio/src/light/animation.h index 3dfca1331..c7e361005 100644 --- a/lib/pbio/src/light/animation.h +++ b/lib/pbio/src/light/animation.h @@ -20,7 +20,7 @@ typedef struct _pbio_light_animation_t pbio_light_animation_t; * @param [in] animation The animation instance * @return The time to wait before calling next() again */ -typedef clock_time_t (*pbio_light_animation_next_t)(pbio_light_animation_t *animation); +typedef uint32_t (*pbio_light_animation_next_t)(pbio_light_animation_t *animation); struct _pbio_light_animation_t { /** Animation update timer. */ diff --git a/lib/pbio/src/light/color_light.c b/lib/pbio/src/light/color_light.c index 43f8726cf..6406aa02c 100644 --- a/lib/pbio/src/light/color_light.c +++ b/lib/pbio/src/light/color_light.c @@ -98,7 +98,7 @@ pbio_error_t pbio_color_light_off(pbio_color_light_t *light) { return pbio_color_light_on_hsv(light, &hsv); } -static clock_time_t pbio_color_light_blink_next(pbio_light_animation_t *animation) { +static uint32_t pbio_color_light_blink_next(pbio_light_animation_t *animation) { pbio_color_light_t *light = PBIO_CONTAINER_OF(animation, pbio_color_light_t, animation); const uint16_t *cells = light->interval_cells; @@ -112,7 +112,7 @@ static clock_time_t pbio_color_light_blink_next(pbio_light_animation_t *animatio pbio_color_hsv_t off = { 0 }; light->funcs->set_hsv(light, light->current_cell % 2 ? &light->hsv : &off); - return clock_from_msec(*interval); + return *interval; } /** @@ -141,7 +141,7 @@ void pbio_color_light_start_blink_animation(pbio_color_light_t *light, const pbi pbio_light_animation_start(&light->animation); } -static clock_time_t pbio_color_light_animate_next(pbio_light_animation_t *animation) { +static uint32_t pbio_color_light_animate_next(pbio_light_animation_t *animation) { pbio_color_light_t *light = PBIO_CONTAINER_OF(animation, pbio_color_light_t, animation); const pbio_color_compressed_hsv_t *cells = light->hsv_cells; @@ -156,7 +156,7 @@ static clock_time_t pbio_color_light_animate_next(pbio_light_animation_t *animat pbio_color_hsv_t hsv; pbio_color_hsv_expand(cell, &hsv); light->funcs->set_hsv(light, &hsv); - return clock_from_msec(light->interval); + return light->interval; } /** diff --git a/lib/pbio/src/light/color_light.h b/lib/pbio/src/light/color_light.h index 153f4b3ee..43e6954cd 100644 --- a/lib/pbio/src/light/color_light.h +++ b/lib/pbio/src/light/color_light.h @@ -3,8 +3,6 @@ #include -#include - #include #include #include diff --git a/lib/pbio/src/logger.c b/lib/pbio/src/logger.c index 3ed648f3b..2abd7ff72 100644 --- a/lib/pbio/src/logger.c +++ b/lib/pbio/src/logger.c @@ -5,8 +5,7 @@ #include #include -#include - +#include #include #include #include @@ -25,7 +24,7 @@ void pbio_logger_start(pbio_log_t *log, int32_t *buf, uint32_t len, int32_t div) log->data = buf; log->len = len; log->sample_div = div; - log->start = clock_usecs(); + log->start = pbdrv_clock_get_us(); log->active = true; } @@ -68,7 +67,7 @@ pbio_error_t pbio_logger_update(pbio_log_t *log, int32_t *buf) { } // Write time of logging - log->data[log->sampled * log->num_values] = (clock_usecs() - log->start) / 1000; + log->data[log->sampled * log->num_values] = (pbdrv_clock_get_us() - log->start) / 1000; // Write the data for (uint8_t i = NUM_DEFAULT_LOG_VALUES; i < log->num_values; i++) { diff --git a/lib/pbio/src/motor_process.c b/lib/pbio/src/motor_process.c index 349ed5bff..e27d69bad 100644 --- a/lib/pbio/src/motor_process.c +++ b/lib/pbio/src/motor_process.c @@ -58,7 +58,7 @@ PROCESS_THREAD(pbio_motor_process, ev, data) { pbio_motor_process_reset(); - etimer_set(&timer, clock_from_msec(PBIO_CONTROL_LOOP_TIME_MS)); + etimer_set(&timer, PBIO_CONTROL_LOOP_TIME_MS); for (;;) { PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)); diff --git a/lib/pbio/src/servo.c b/lib/pbio/src/servo.c index a3ab33c2a..2068222b2 100644 --- a/lib/pbio/src/servo.c +++ b/lib/pbio/src/servo.c @@ -5,8 +5,7 @@ #include #include -#include - +#include #include #include #include @@ -102,7 +101,7 @@ pbio_error_t pbio_servo_reset_angle(pbio_servo_t *srv, int32_t reset_angle, bool } // Get the old target angle that we were tracking until now - int32_t time_ref = pbio_control_get_ref_time(&srv->control, clock_usecs()); + int32_t time_ref = pbio_control_get_ref_time(&srv->control, pbdrv_clock_get_us()); int32_t count_ref, unused; pbio_trajectory_get_reference(&srv->control.trajectory, time_ref, &count_ref, &unused, &unused, &unused); int32_t target_old = pbio_control_counts_to_user(&srv->control.settings, count_ref); @@ -130,7 +129,7 @@ static pbio_error_t servo_get_state(pbio_servo_t *srv, int32_t *time_now, int32_ pbio_error_t err; // Read current state of this motor: current time, speed, and position - *time_now = clock_usecs(); + *time_now = pbdrv_clock_get_us(); err = pbio_tacho_get_count(srv->tacho, count_now); if (err != PBIO_SUCCESS) { return err; @@ -153,7 +152,7 @@ static pbio_error_t pbio_servo_actuate(pbio_servo_t *srv, pbio_actuation_t actua case PBIO_ACTUATION_BRAKE: return pbio_dcmotor_brake(srv->dcmotor); case PBIO_ACTUATION_HOLD: - return pbio_control_start_hold_control(&srv->control, clock_usecs(), control); + return pbio_control_start_hold_control(&srv->control, pbdrv_clock_get_us(), control); case PBIO_ACTUATION_DUTY: return pbio_dcmotor_set_duty_cycle_sys(srv->dcmotor, control); } @@ -307,7 +306,7 @@ pbio_error_t pbio_servo_run(pbio_servo_t *srv, int32_t speed) { return err; } } else { - time_now = clock_usecs(); + time_now = pbdrv_clock_get_us(); } // Start a timed maneuver, duration forever @@ -415,7 +414,7 @@ pbio_error_t pbio_servo_track_target(pbio_servo_t *srv, int32_t target) { } // Get the intitial state, either based on physical motor state or ongoing maneuver - int32_t time_start = clock_usecs(); + int32_t time_start = pbdrv_clock_get_us(); int32_t target_count = pbio_control_user_to_counts(&srv->control.settings, target); return pbio_control_start_hold_control(&srv->control, time_start, target_count); diff --git a/lib/pbio/src/trajectory.c b/lib/pbio/src/trajectory.c index 118ba9229..0a419fafa 100644 --- a/lib/pbio/src/trajectory.c +++ b/lib/pbio/src/trajectory.c @@ -5,8 +5,6 @@ #include #include -#include - #include #include diff --git a/lib/pbio/src/uartdev.c b/lib/pbio/src/uartdev.c index 0674588ca..b2a8ba6db 100644 --- a/lib/pbio/src/uartdev.c +++ b/lib/pbio/src/uartdev.c @@ -883,7 +883,7 @@ static PT_THREAD(pbio_uartdev_update(uartdev_port_data_t * data)) { data->tx_busy = false; // schedule baud rate change - etimer_set(&data->timer, clock_from_msec(10)); + etimer_set(&data->timer, 10); PT_WAIT_UNTIL(&data->pt, etimer_expired(&data->timer)); // change the baud rate @@ -936,7 +936,7 @@ static PT_THREAD(pbio_uartdev_update(uartdev_port_data_t * data)) { while (data->status == PBIO_UARTDEV_STATUS_DATA) { // setup keepalive timer - etimer_reset_with_new_interval(&data->timer, clock_from_msec(EV3_UART_DATA_KEEP_ALIVE_TIMEOUT)); + etimer_reset_with_new_interval(&data->timer, EV3_UART_DATA_KEEP_ALIVE_TIMEOUT); PT_WAIT_UNTIL(&data->pt, etimer_expired(&data->timer)); // make sure we are receiving data diff --git a/lib/pbio/sys/battery.c b/lib/pbio/sys/battery.c index 8847cf9ee..0feba6230 100644 --- a/lib/pbio/sys/battery.c +++ b/lib/pbio/sys/battery.c @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors // Provides battery status indication and shutdown on low battery. @@ -7,10 +7,8 @@ // TOOD: need to handle Li-ion batteries and charger for SPIKE Prime // TODO: need to handle battery pack switch and Li-ion batteries for Technic Hub and NXT -#include - #include - +#include #include // period over which the battery voltage is averaged (in milliseconds) @@ -26,7 +24,7 @@ #define LIION_LOW_MV 6800 // 3.4V per cell #define LIION_CRITICAL_MV 6000 // 3.0V per cell -static clock_time_t prev_poll_time; +static uint32_t prev_poll_time; static uint16_t avg_battery_voltage; #if PBDRV_CONFIG_BATTERY_ADC_TYPE == 1 @@ -65,7 +63,7 @@ void pbsys_battery_init(void) { avg_battery_voltage = battery_ok_mv; } - prev_poll_time = clock_time(); + prev_poll_time = pbdrv_clock_get_ms(); } /** @@ -74,12 +72,12 @@ void pbsys_battery_init(void) { * This is called periodically to update the current battery state. */ void pbsys_battery_poll(void) { - clock_time_t now; + uint32_t now; uint32_t poll_interval; uint16_t battery_voltage; - now = clock_time(); - poll_interval = clock_to_msec(now - prev_poll_time); + now = pbdrv_clock_get_ms(); + poll_interval = now - prev_poll_time; prev_poll_time = now; pbdrv_battery_get_voltage_now(&battery_voltage); diff --git a/lib/pbio/sys/bluetooth.c b/lib/pbio/sys/bluetooth.c index f3422290f..dbcc9eea5 100644 --- a/lib/pbio/sys/bluetooth.c +++ b/lib/pbio/sys/bluetooth.c @@ -201,7 +201,7 @@ static PT_THREAD(pbsys_bluetooth_monitor_status(struct pt *pt)) { // Send status periodically as well in case a notification was missed due // to bad RF environment or bug like https://crbug.com/1195592 - etimer_set(&timer, clock_from_msec(500)); + etimer_set(&timer, 500); for (;;) { // wait for status to change or timeout @@ -236,7 +236,7 @@ PROCESS_THREAD(pbsys_bluetooth_process, ev, data) { while (!pbsys_status_test(PBIO_PYBRICKS_STATUS_SHUTDOWN)) { // make sure the Bluetooth chip is in reset long enough to actually reset - etimer_set(&timer, clock_from_msec(150)); + etimer_set(&timer, 150); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)); pbdrv_bluetooth_power_on(true); diff --git a/lib/pbio/sys/light.c b/lib/pbio/sys/light.c index 8cb9bb6a2..4ebcf5948 100644 --- a/lib/pbio/sys/light.c +++ b/lib/pbio/sys/light.c @@ -191,7 +191,7 @@ static void pbsys_status_light_handle_status_change(void) { } } -static clock_time_t default_user_program_light_animation_next(pbio_light_animation_t *animation) { +static uint32_t default_user_program_light_animation_next(pbio_light_animation_t *animation) { // The brightness pattern has the form /\ through which we cycle in N steps. static uint8_t cycle = 0; const uint8_t cycle_max = 200; @@ -207,7 +207,7 @@ static clock_time_t default_user_program_light_animation_next(pbio_light_animati // This increment controls the speed of the pattern and wraps on completion cycle = (cycle + 4) % cycle_max; - return clock_from_msec(40); + return 40; } void pbsys_status_light_handle_event(process_event_t event, process_data_t data) { diff --git a/lib/pbio/sys/light_matrix.c b/lib/pbio/sys/light_matrix.c index e32776be4..b30a4f303 100644 --- a/lib/pbio/sys/light_matrix.c +++ b/lib/pbio/sys/light_matrix.c @@ -70,7 +70,7 @@ void pbsys_hub_light_matrix_init(void) { pbsys_hub_light_matrix_show_stop_sign(); } -static clock_time_t pbsys_hub_light_matrix_user_program_animation_next(pbio_light_animation_t *animation) { +static uint32_t pbsys_hub_light_matrix_user_program_animation_next(pbio_light_animation_t *animation) { // The indexes of pixels to light up static const uint8_t indexes[] = { 1, 2, 3, 8, 13, 12, 11, 6 }; @@ -92,7 +92,7 @@ static clock_time_t pbsys_hub_light_matrix_user_program_animation_next(pbio_ligh cycle += 9; } - return clock_from_msec(40); + return 40; } void pbsys_hub_light_matrix_handle_event(process_event_t event, process_data_t data) { diff --git a/lib/pbio/sys/status.c b/lib/pbio/sys/status.c index 32d3c518c..91c780d5d 100644 --- a/lib/pbio/sys/status.c +++ b/lib/pbio/sys/status.c @@ -5,9 +5,11 @@ #include #include +#include #include +#include #include #include @@ -15,7 +17,7 @@ static struct { /** Status indications as bit flags */ uint32_t flags; /** Timestamp of when status last changed */ - clock_time_t changed_time[NUM_PBIO_PYBRICKS_STATUS]; + uint32_t changed_time[NUM_PBIO_PYBRICKS_STATUS]; } pbsys_status; static void pbsys_status_update_flag(pbio_pybricks_status_t status, bool set) { @@ -27,7 +29,7 @@ static void pbsys_status_update_flag(pbio_pybricks_status_t status, bool set) { } pbsys_status.flags = new_flags; - pbsys_status.changed_time[status] = clock_time(); + pbsys_status.changed_time[status] = pbdrv_clock_get_ms(); // REVISIT: this can drop events if event queue is full process_post(PROCESS_BROADCAST, set ? PBIO_EVENT_STATUS_SET : PBIO_EVENT_STATUS_CLEARED, (process_data_t)status); @@ -78,7 +80,7 @@ bool pbsys_status_test_debounce(pbio_pybricks_status_t status, bool state, uint3 if (pbsys_status_test(status) != state) { return false; } - return (clock_time() - pbsys_status.changed_time[status]) >= clock_from_msec(ms); + return pbdrv_clock_get_ms() - pbsys_status.changed_time[status] >= ms; } /** diff --git a/lib/pbio/test/clock.c b/lib/pbio/test/clock.c index c3b10cc1f..e86e58e41 100644 --- a/lib/pbio/test/clock.c +++ b/lib/pbio/test/clock.c @@ -1,29 +1,31 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors // Clock implementation for tests. This allows tests to exactly control the // clock ticks to get repeatable tests rather than relying on a system clock. +#include + #include -static clock_time_t clock_ticks; +static uint32_t clock_ticks; /** * Increase the current clock ticks and poll etimers. * @param [in] ticks The number of ticks to add to the clock. */ -void clock_tick(clock_time_t ticks) { +void pbio_test_clock_tick(uint32_t ticks) { clock_ticks += ticks; etimer_request_poll(); } -void clock_init(void) { +void pbdrv_clock_init(void) { } -clock_time_t clock_time(void) { +uint32_t pbdrv_clock_get_ms(void) { return clock_ticks; } -unsigned long clock_usecs(void) { - return clock_to_msec(clock_ticks) * 1000UL; +uint32_t pbdrv_clock_get_us(void) { + return clock_ticks * 1000; } diff --git a/lib/pbio/test/contiki-conf.h b/lib/pbio/test/contiki-conf.h index b08782153..453a7a364 100644 --- a/lib/pbio/test/contiki-conf.h +++ b/lib/pbio/test/contiki-conf.h @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -// Copyright (c) 2018-2020 The Pybricks Authors +// Copyright (c) 2018-2021 The Pybricks Authors #ifndef _PBIO_CONF_H_ #define _PBIO_CONF_H_ @@ -14,6 +14,9 @@ typedef uint32_t clock_time_t; #define CLOCK_CONF_SECOND 1000 // extra clock function just for tests -void clock_tick(clock_time_t ticks); +void pbio_test_clock_tick(uint32_t ticks); + +#define clock_time pbdrv_clock_get_ms +#define clock_usecs pbdrv_clock_get_us #endif /* _PBIO_CONF_H_ */ diff --git a/lib/pbio/test/drv/bluetooth.c b/lib/pbio/test/drv/bluetooth.c index 8e57b25a6..bc83a84f0 100644 --- a/lib/pbio/test/drv/bluetooth.c +++ b/lib/pbio/test/drv/bluetooth.c @@ -582,12 +582,12 @@ static PT_THREAD(test_btstack_run_loop_contiki_timer(struct pt *pt)) { btstack_run_loop_add_timer(&timer_source); // should not expire early - clock_tick(9); + pbio_test_clock_tick(9); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 0); // now it should be done - clock_tick(1); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 1); @@ -603,28 +603,28 @@ static PT_THREAD(test_btstack_run_loop_contiki_timer(struct pt *pt)) { btstack_run_loop_add_timer(&timer_source_3); // should not expire early - clock_tick(4); + pbio_test_clock_tick(4); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 0); tt_want_uint_op(callback_count_2, ==, 0); tt_want_uint_op(callback_count_3, ==, 0); // only timer 2 should be called back after 5 ms - clock_tick(1); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 0); tt_want_uint_op(callback_count_2, ==, 1); tt_want_uint_op(callback_count_3, ==, 0); // then timer 1 after 10 ms - clock_tick(5); + pbio_test_clock_tick(5); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 1); tt_want_uint_op(callback_count_2, ==, 1); tt_want_uint_op(callback_count_3, ==, 0); // and finally timer 3 - clock_tick(5); + pbio_test_clock_tick(5); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 1); tt_want_uint_op(callback_count_2, ==, 1); @@ -637,27 +637,27 @@ static PT_THREAD(test_btstack_run_loop_contiki_timer(struct pt *pt)) { btstack_run_loop_set_timer(&timer_source, 15); btstack_run_loop_add_timer(&timer_source); - clock_tick(5); + pbio_test_clock_tick(5); PT_YIELD(pt); btstack_run_loop_set_timer(&timer_source_2, 10); btstack_run_loop_add_timer(&timer_source_2); - clock_tick(5); + pbio_test_clock_tick(5); PT_YIELD(pt); btstack_run_loop_set_timer(&timer_source_3, 5); btstack_run_loop_add_timer(&timer_source_3); // none should have timeout out yet - clock_tick(4); + pbio_test_clock_tick(4); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 0); tt_want_uint_op(callback_count_2, ==, 0); tt_want_uint_op(callback_count_3, ==, 0); // then all at the same time - clock_tick(1); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 1); tt_want_uint_op(callback_count_2, ==, 1); @@ -674,13 +674,13 @@ static PT_THREAD(test_btstack_run_loop_contiki_timer(struct pt *pt)) { btstack_run_loop_add_timer(&timer_source); // should not expire early - clock_tick(9); + pbio_test_clock_tick(9); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 0); btstack_run_loop_remove_timer(&timer_source); // it should have been canceled - clock_tick(1); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want_uint_op(callback_count, ==, 0); diff --git a/lib/pbio/test/src/animation.c b/lib/pbio/test/src/animation.c index ff75619c0..d3810311d 100644 --- a/lib/pbio/test/src/animation.c +++ b/lib/pbio/test/src/animation.c @@ -17,7 +17,7 @@ PROCESS_NAME(pbio_light_animation_process); static uint8_t test_animation_set_hsv_call_count; -static clock_time_t test_animation_next(pbio_light_animation_t *animation) { +static uint32_t test_animation_next(pbio_light_animation_t *animation) { test_animation_set_hsv_call_count++; return TEST_ANIMATION_TIME; } @@ -39,10 +39,10 @@ static PT_THREAD(test_light_animation(struct pt *pt)) { tt_want_uint_op(test_animation_set_hsv_call_count, ==, 1); // next() should not be called again until after a delay - clock_tick(TEST_ANIMATION_TIME - 1); + pbio_test_clock_tick(TEST_ANIMATION_TIME - 1); PT_YIELD(pt); tt_want_uint_op(test_animation_set_hsv_call_count, ==, 1); - clock_tick(1); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want_uint_op(test_animation_set_hsv_call_count, ==, 2); diff --git a/lib/pbio/test/src/color_light.c b/lib/pbio/test/src/color_light.c index 7aebe6f88..329260bf7 100644 --- a/lib/pbio/test/src/color_light.c +++ b/lib/pbio/test/src/color_light.c @@ -72,17 +72,17 @@ static PT_THREAD(test_color_light(struct pt *pt)) { // set_hsv() should not be called again until after a delay and it should // receive the next hue in the list - clock_tick(TEST_ANIMATION_TIME - 1); + pbio_test_clock_tick(TEST_ANIMATION_TIME - 1); PT_YIELD(pt); tt_want_uint_op(test_light_set_hsv_call_count, ==, 1); - clock_tick(1); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want_uint_op(test_light_set_hsv_call_count, ==, 2); // odd blink cells turns the light off (so hue doesn't matter here) tt_want_uint_op(test_light_set_hsv_last_brightness, ==, 0); // then the next animation update should wrap back to the start of the list - clock_tick(TEST_ANIMATION_TIME); + pbio_test_clock_tick(TEST_ANIMATION_TIME); PT_YIELD(pt); tt_want_uint_op(test_light_set_hsv_call_count, ==, 3); tt_want_uint_op(test_light_set_hsv_last_hue, ==, PBIO_COLOR_HUE_BLUE); @@ -98,13 +98,13 @@ static PT_THREAD(test_color_light(struct pt *pt)) { // set_hsv() should not be called again until after a delay and it should // receive the next hue in the list - clock_tick(TEST_ANIMATION_TIME); + pbio_test_clock_tick(TEST_ANIMATION_TIME); PT_YIELD(pt); tt_want_uint_op(test_light_set_hsv_call_count, ==, 2); tt_want_uint_op(test_light_set_hsv_last_hue, ==, PBIO_COLOR_HUE_MAGENTA); // then the next animation update should wrap back to the start of the list - clock_tick(TEST_ANIMATION_TIME); + pbio_test_clock_tick(TEST_ANIMATION_TIME); PT_YIELD(pt); tt_want_uint_op(test_light_set_hsv_call_count, ==, 3); tt_want_uint_op(test_light_set_hsv_last_hue, ==, PBIO_COLOR_HUE_CYAN); diff --git a/lib/pbio/test/src/light_matrix.c b/lib/pbio/test/src/light_matrix.c index 0420bbf4d..087722287 100644 --- a/lib/pbio/test/src/light_matrix.c +++ b/lib/pbio/test/src/light_matrix.c @@ -88,19 +88,19 @@ static PT_THREAD(test_light_matrix(struct pt *pt)) { // set_pixel() should not be called again until after a delay and it should // receive the next hue in the list - clock_tick(INTERVAL - 1); + pbio_test_clock_tick(INTERVAL - 1); PT_YIELD(pt); tt_want_int_op(test_light_matrix_set_pixel_last_brightness[0][0], ==, 1); - clock_tick(1); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want_int_op(test_light_matrix_set_pixel_last_brightness[0][0], !=, 1); tt_want_light_matrix_data(11, 12, 13, 14, 15, 16, 17, 18, 19); // then the next animation update should wrap back to the start of the list - clock_tick(INTERVAL - 1); + pbio_test_clock_tick(INTERVAL - 1); PT_YIELD(pt); tt_want_int_op(test_light_matrix_set_pixel_last_brightness[0][0], ==, 11); - clock_tick(1); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want_int_op(test_light_matrix_set_pixel_last_brightness[0][0], !=, 11); tt_want_light_matrix_data(1, 2, 3, 4, 5, 6, 7, 8, 9); @@ -108,7 +108,7 @@ static PT_THREAD(test_light_matrix(struct pt *pt)) { // stopping the animation should not change any pixels test_light_matrix_reset(); pbio_light_matrix_stop_animation(&test_light_matrix); - clock_tick(INTERVAL * 2); + pbio_test_clock_tick(INTERVAL * 2); PT_YIELD(pt); tt_want_light_matrix_data(0); diff --git a/lib/pbio/test/src/servo.c b/lib/pbio/test/src/servo.c index 0ab62613f..d4136c3b7 100644 --- a/lib/pbio/test/src/servo.c +++ b/lib/pbio/test/src/servo.c @@ -257,7 +257,7 @@ static PT_THREAD(test_servo_run_func(struct pt *pt, const char *name, pbio_error // write current state to log file if (pbio_logger_rows(&servo->control.log)) { - fprintf(log_file, "%d,", clock_to_msec(clock_time())); + fprintf(log_file, "%d,", pbdrv_clock_get_ms()); fprintf(log_file, "%d,", test_motor_driver.output); fprintf(log_file, "%d,", test_motor_driver.duty_cycle); @@ -269,7 +269,7 @@ static PT_THREAD(test_servo_run_func(struct pt *pt, const char *name, pbio_error servo->control.log.sampled = 0; } - clock_tick(1); + pbio_test_clock_tick(1); PT_YIELD(pt); continue; } diff --git a/lib/pbio/test/src/uartdev.c b/lib/pbio/test/src/uartdev.c index e377bf1d5..334d194ca 100644 --- a/lib/pbio/test/src/uartdev.c +++ b/lib/pbio/test/src/uartdev.c @@ -46,7 +46,7 @@ PT_THREAD(simulate_rx_msg(struct pt *pt, const uint8_t *msg, uint8_t length, boo // First uartdev reads one byte header PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.rx_msg_result == PBIO_ERROR_AGAIN; }); tt_uint_op(test_uart_dev.rx_msg_length, ==, 1); @@ -61,7 +61,7 @@ PT_THREAD(simulate_rx_msg(struct pt *pt, const uint8_t *msg, uint8_t length, boo // then read rest of message PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.rx_msg_result == PBIO_ERROR_AGAIN; }); tt_uint_op(test_uart_dev.rx_msg_length, ==, length - 1); @@ -81,7 +81,7 @@ PT_THREAD(simulate_tx_msg(struct pt *pt, const uint8_t *msg, uint8_t length, boo PT_BEGIN(pt); PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.tx_msg_result == PBIO_ERROR_AGAIN; }); tt_uint_op(test_uart_dev.tx_msg_length, ==, length); @@ -225,14 +225,14 @@ static PT_THREAD(test_boost_color_distance_sensor(struct pt *pt)) { // starting baud rate of hub PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.baud == 115200; }); // this device does not support syncing at 115200 SIMULATE_TX_MSG(msg_speed_115200); PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.baud == 2400; }); @@ -326,7 +326,7 @@ static PT_THREAD(test_boost_color_distance_sensor(struct pt *pt)) { // wait for baud rate change PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.baud == 115200; }); @@ -399,7 +399,7 @@ static PT_THREAD(test_boost_color_distance_sensor(struct pt *pt)) { int err; PT_WAIT_WHILE(pt, { - clock_tick(1); + pbio_test_clock_tick(1); (err = pbio_iodev_set_mode_begin(iodev, 1)) == PBIO_ERROR_AGAIN; }); tt_uint_op(err, ==, PBIO_SUCCESS); @@ -415,7 +415,7 @@ static PT_THREAD(test_boost_color_distance_sensor(struct pt *pt)) { SIMULATE_RX_MSG(msg88); PT_WAIT_WHILE(pt, { - clock_tick(1); + pbio_test_clock_tick(1); (err = pbio_iodev_set_mode_end(iodev)) == PBIO_ERROR_AGAIN; }); tt_uint_op(err, ==, PBIO_SUCCESS); @@ -425,7 +425,7 @@ static PT_THREAD(test_boost_color_distance_sensor(struct pt *pt)) { // also do mode 8 since it requires the extended mode flag PT_WAIT_WHILE(pt, { - clock_tick(1); + pbio_test_clock_tick(1); (err = pbio_iodev_set_mode_begin(iodev, 8)) == PBIO_ERROR_AGAIN; }); tt_uint_op(err, ==, PBIO_SUCCESS); @@ -442,7 +442,7 @@ static PT_THREAD(test_boost_color_distance_sensor(struct pt *pt)) { SIMULATE_RX_MSG(msg91); PT_WAIT_WHILE(pt, { - clock_tick(1); + pbio_test_clock_tick(1); (err = pbio_iodev_set_mode_end(iodev)) == PBIO_ERROR_AGAIN; }); tt_uint_op(err, ==, PBIO_SUCCESS); @@ -511,14 +511,14 @@ static PT_THREAD(test_boost_interactive_motor(struct pt *pt)) { // starting baud rate of hub PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.baud == 115200; }); // this device does not support syncing at 115200 SIMULATE_TX_MSG(msg_speed_115200); PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.baud == 2400; }); @@ -563,7 +563,7 @@ static PT_THREAD(test_boost_interactive_motor(struct pt *pt)) { // wait for baud rate change PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.baud == 115200; }); @@ -699,7 +699,7 @@ static PT_THREAD(test_technic_large_motor(struct pt *pt)) { // baud rate for sync messages PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.baud == 115200; }); @@ -907,7 +907,7 @@ static PT_THREAD(test_technic_xl_motor(struct pt *pt)) { // baud rate for sync messages PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); test_uart_dev.baud == 115200; }); @@ -1077,7 +1077,7 @@ pbio_error_t pbdrv_uart_read_begin(pbdrv_uart_dev_t *uart, uint8_t *msg, uint8_t test_uart_dev.rx_msg = msg; test_uart_dev.rx_msg_length = length; test_uart_dev.rx_msg_result = PBIO_ERROR_AGAIN; - etimer_set(&test_uart_dev.rx_timer, clock_from_msec(timeout)); + etimer_set(&test_uart_dev.rx_timer, timeout); return PBIO_SUCCESS; } @@ -1108,7 +1108,7 @@ pbio_error_t pbdrv_uart_write_begin(pbdrv_uart_dev_t *uart, uint8_t *msg, uint8_ test_uart_dev.tx_msg = msg; test_uart_dev.tx_msg_length = length; test_uart_dev.tx_msg_result = PBIO_ERROR_AGAIN; - etimer_set(&test_uart_dev.tx_timer, clock_from_msec(timeout)); + etimer_set(&test_uart_dev.tx_timer, timeout); return PBIO_SUCCESS; } diff --git a/lib/pbio/test/sys/bluetooth.c b/lib/pbio/test/sys/bluetooth.c index 3b38ed52c..04d6e5bf9 100644 --- a/lib/pbio/test/sys/bluetooth.c +++ b/lib/pbio/test/sys/bluetooth.c @@ -24,19 +24,19 @@ static PT_THREAD(test_bluetooth(struct pt *pt)) { // wait for the power on delay PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); pbio_test_bluetooth_get_control_state() == PBIO_TEST_BLUETOOTH_STATE_ON; }); PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); pbio_test_bluetooth_is_advertising_enabled(); }); pbio_test_bluetooth_connect(); PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); pbio_test_bluetooth_is_connected(); }); @@ -49,7 +49,7 @@ static PT_THREAD(test_bluetooth(struct pt *pt)) { uint32_t size; PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); size = strlen(test_data_1); pbsys_bluetooth_tx((const uint8_t *)test_data_1, &size) == PBIO_SUCCESS; }); @@ -63,7 +63,7 @@ static PT_THREAD(test_bluetooth(struct pt *pt)) { // this next data should get pushed in the UART buffer but wait until the // previous request is finished before sending PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); size = strlen(test_data_2); pbsys_bluetooth_tx((const uint8_t *)test_data_2, &size) == PBIO_SUCCESS; }); @@ -79,7 +79,7 @@ static PT_THREAD(test_bluetooth(struct pt *pt)) { static uint8_t rx_data[20]; PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); size = PBIO_ARRAY_SIZE(rx_data); pbsys_bluetooth_rx(rx_data, &size) == PBIO_SUCCESS; }); @@ -96,7 +96,7 @@ static PT_THREAD(test_bluetooth(struct pt *pt)) { count = pbio_test_bluetooth_get_pybricks_service_notification_count(); PT_WAIT_UNTIL(pt, { - clock_tick(1); + pbio_test_clock_tick(1); pbio_test_bluetooth_get_pybricks_service_notification_count() != count; }); diff --git a/lib/pbio/test/sys/status.c b/lib/pbio/test/sys/status.c index 19ea78332..f94524f1c 100644 --- a/lib/pbio/test/sys/status.c +++ b/lib/pbio/test/sys/status.c @@ -53,11 +53,11 @@ static PT_THREAD(test_status(struct pt *pt)) { tt_want_uint_op(last_data, ==, test_flag); // ensure that debounce works - clock_tick(clock_from_msec(9)); + pbio_test_clock_tick(9); PT_YIELD(pt); tt_want(!pbsys_status_test_debounce(test_flag, true, 10)); tt_want(!pbsys_status_test_debounce(test_flag, false, 10)); - clock_tick(clock_from_msec(1)); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want(pbsys_status_test_debounce(test_flag, true, 10)); tt_want(!pbsys_status_test_debounce(test_flag, false, 10)); @@ -85,11 +85,11 @@ static PT_THREAD(test_status(struct pt *pt)) { // ensure that debounce works last_event = PROCESS_EVENT_NONE; - clock_tick(clock_from_msec(9)); + pbio_test_clock_tick(9); PT_YIELD(pt); tt_want(!pbsys_status_test_debounce(test_flag, true, 10)); tt_want(!pbsys_status_test_debounce(test_flag, false, 10)); - clock_tick(clock_from_msec(1)); + pbio_test_clock_tick(1); PT_YIELD(pt); tt_want(!pbsys_status_test_debounce(test_flag, true, 10)); tt_want(pbsys_status_test_debounce(test_flag, false, 10));