Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions bricks/_common_stm32/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,17 @@
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.
// We must have the GIL.
void mp_hal_delay_ms(mp_uint_t Delay) {
if (pbdrv_clock_is_ticking()) {
// IRQs enabled, so can use systick counter to do the delay
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 (pbdrv_clock_ticks - start < Delay);
} else {
// IRQs disabled, so need to use a busy loop for the delay.
pbdrv_clock_busy_delay_ms(Delay);
}
// Use systick counter to do the delay
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 (pbdrv_clock_ticks - start < Delay);
}

uintptr_t mp_hal_stdio_poll(uintptr_t poll_flags) {
Expand Down
4 changes: 2 additions & 2 deletions bricks/ev3/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include "py/stream.h"

// Core delay function that does an efficient sleep and may switch thread context.
// If IRQs are enabled then we must have the GIL.
// We must have the GIL.
void mp_hal_delay_ms(mp_uint_t Delay) {
// IRQs enabled, so can use systick counter to do the delay
// Use systick counter to do the delay
uint32_t start = pbdrv_clock_get_ms();
// Wraparound of tick is taken care of by 2's complement arithmetic.
do {
Expand Down
30 changes: 10 additions & 20 deletions bricks/nxt/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,18 @@
#include "py/mpconfig.h"
#include "py/stream.h"

// TODO
static bool interrupts_get(void) {
return true;
}

// Core delay function that does an efficient sleep and may switch thread context.
// If IRQs are enabled then we must have the GIL.
// We must have the GIL.
void mp_hal_delay_ms(mp_uint_t Delay) {
if (interrupts_get()) {
// IRQs enabled, so can use systick counter to do the delay
uint32_t start = pbdrv_clock_get_ms();
// 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 (pbdrv_clock_get_ms() - start < Delay);
} else {
// IRQs disabled, so need to use a busy loop for the delay.
nx_systick_wait_ms(Delay);
}
// Use systick counter to do the delay
uint32_t start = pbdrv_clock_get_ms();
// 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 (pbdrv_clock_get_ms() - start < Delay);
}

// delay for given number of microseconds
Expand Down
9 changes: 0 additions & 9 deletions lib/pbio/drv/clock/clock_ev3.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,4 @@ uint32_t pbdrv_clock_get_100us(void) {
return pbdrv_clock_get_ms() * 10;
}

void pbdrv_clock_busy_delay_ms(uint32_t ms) {
// TODO
}

bool pbdrv_clock_is_ticking(void) {
// TODO
return true;
}

#endif // PBDRV_CONFIG_CLOCK_TIAM1808
10 changes: 0 additions & 10 deletions lib/pbio/drv/clock/clock_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,4 @@ uint32_t pbdrv_clock_get_us(void) {
return time_val.tv_sec * 1000000 + time_val.tv_nsec / 1000;
}

void pbdrv_clock_busy_delay_ms(uint32_t ms) {
uint32_t start = pbdrv_clock_get_ms();
while (pbdrv_clock_get_ms() - start < ms) {
}
}

bool pbdrv_clock_is_ticking(void) {
return true;
}

#endif // PBDRV_CONFIG_CLOCK_LINUX
7 changes: 0 additions & 7 deletions lib/pbio/drv/clock/clock_none.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,4 @@ uint32_t pbdrv_clock_get_100us(void) {
return 0;
}

void pbdrv_clock_busy_delay_ms(uint32_t ms) {
}

bool pbdrv_clock_is_ticking(void) {
return false;
}

#endif // PBDRV_CONFIG_CLOCK_NONE
9 changes: 0 additions & 9 deletions lib/pbio/drv/clock/clock_nxt.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ void nx_systick_wait_ms(uint32_t ms) {
}
}

void pbdrv_clock_busy_delay_ms(uint32_t ms) {
nx_systick_wait_ms(ms);
}

void nx_systick_wait_ns(uint32_t ns) {
volatile uint32_t x = (ns >> 7) + 1;

Expand All @@ -110,9 +106,4 @@ void nx_systick_wait_ns(uint32_t ns) {
}
}

bool pbdrv_clock_is_ticking(void) {
// TODO
return true;
}

#endif // PBDRV_CONFIG_CLOCK_NXT
12 changes: 2 additions & 10 deletions lib/pbio/drv/clock/clock_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ uint32_t HAL_GetTick(void) {
// We provide our own version of HAL_Delay that calls __WFI while waiting,
// and works when interrupts are disabled. This function is intended to be
// used only by the ST HAL functions.
//
// TODO: Is anything actually still calling this?
void HAL_Delay(uint32_t Delay) {
if (__get_PRIMASK() == 0) {
// IRQs enabled, so can use systick counter to do the delay
Expand All @@ -112,14 +114,4 @@ void HAL_Delay(uint32_t Delay) {
}
}

void pbdrv_clock_busy_delay_ms(uint32_t ms) {
HAL_Delay(ms);
}

bool pbdrv_clock_is_ticking(void) {
// Init already completed in SystemInit(), so we just need to check if
// interrupts are enabled.
return __get_PRIMASK() == 0;
}

#endif // PBDRV_CONFIG_CLOCK_STM32
7 changes: 0 additions & 7 deletions lib/pbio/drv/clock/clock_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,5 @@ uint32_t pbdrv_clock_get_us(void) {
return clock_ticks * 1000;
}

void pbdrv_clock_busy_delay_ms(uint32_t ms) {
}

bool pbdrv_clock_is_ticking(void) {
return true;
}


#endif // PBDRV_CONFIG_CLOCK_TEST
20 changes: 0 additions & 20 deletions lib/pbio/include/pbdrv/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,6 @@ uint32_t pbdrv_clock_get_us(void);
*/
void pbdrv_clock_busy_delay_us(uint32_t us);

/**
* Busy wait delay for several milliseconds. May not be accurate.
*
* NB: Should not be used in any driver code. This exists only as a hook for
* APIs that need a blocking delay to work when interrupts could be disabled.
*
* @param [in] ms The number of milliseconds to delay.
*/
void pbdrv_clock_busy_delay_ms(uint32_t ms);

/**
* Tests if the millisecond clock is ticking.
*
* On embedded systems, this means that the clock was initialized and IRQs are
* enabled.
*
* @return True if the clock is ticking, false otherwise.
*/
bool pbdrv_clock_is_ticking(void);

#endif /* _PBDRV_CLOCK_H_ */

/** @} */