Skip to content

Commit 739d29d

Browse files
committed
pbio/drv/clock: Add hooks for mphal.
By abstracting away the requirements for mp_hal_delay_ms we should be able to share the definition of mp_hal_delay_ms between platforms. The clock driver is a good place for this since most implementations already have their own blocking wait for when IRQs are disabled.
1 parent bc09f31 commit 739d29d

10 files changed

Lines changed: 76 additions & 112 deletions

File tree

bricks/_common/sources.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ PBIO_SRC_C = $(addprefix lib/pbio/,\
144144
drv/clock/clock_stm32.c \
145145
drv/clock/clock_test.c \
146146
drv/clock/clock_ev3.c \
147-
drv/clock/clock_virtual.c \
148147
drv/core.c \
149148
drv/counter/counter_ev3.c \
150149
drv/counter/counter_stm32f0_gpio_quad_enc.c \

bricks/_common_stm32/mphalport.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <contiki.h>
1010

11+
#include <pbdrv/clock.h>
1112
#include <pbdrv/config.h>
1213
#include <pbio/main.h>
1314
#include <pbsys/bluetooth.h>
@@ -23,7 +24,7 @@ extern volatile uint32_t pbdrv_clock_ticks;
2324
// Core delay function that does an efficient sleep and may switch thread context.
2425
// If IRQs are enabled then we must have the GIL.
2526
void mp_hal_delay_ms(mp_uint_t Delay) {
26-
if (__get_PRIMASK() == 0) {
27+
if (pbdrv_clock_is_ticking()) {
2728
// IRQs enabled, so can use systick counter to do the delay
2829
uint32_t start = pbdrv_clock_ticks;
2930
// Wraparound of tick is taken care of by 2's complement arithmetic.
@@ -35,12 +36,7 @@ void mp_hal_delay_ms(mp_uint_t Delay) {
3536
} while (pbdrv_clock_ticks - start < Delay);
3637
} else {
3738
// IRQs disabled, so need to use a busy loop for the delay.
38-
// To prevent possible overflow of the counter we use a double loop.
39-
const uint32_t count_1ms = PBDRV_CONFIG_SYS_CLOCK_RATE / 4000;
40-
for (mp_uint_t i = 0; i < Delay; i++) {
41-
for (uint32_t count = 0; ++count <= count_1ms;) {
42-
}
43-
}
39+
pbdrv_clock_busy_delay_ms(Delay);
4440
}
4541
}
4642

lib/pbio/drv/clock/clock_ev3.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,13 @@ uint32_t pbdrv_clock_get_100us(void) {
119119
return pbdrv_clock_get_ms() * 10;
120120
}
121121

122+
void pbdrv_clock_busy_delay_ms(uint32_t ms) {
123+
// TODO
124+
}
125+
126+
bool pbdrv_clock_is_ticking(void) {
127+
// TODO
128+
return true;
129+
}
130+
122131
#endif // PBDRV_CONFIG_CLOCK_TIAM1808

lib/pbio/drv/clock/clock_linux.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,14 @@ uint32_t pbdrv_clock_get_us(void) {
112112
return time_val.tv_sec * 1000000 + time_val.tv_nsec / 1000;
113113
}
114114

115+
void pbdrv_clock_busy_delay_ms(uint32_t ms) {
116+
uint32_t start = pbdrv_clock_get_ms();
117+
while (pbdrv_clock_get_ms() - start < ms) {
118+
}
119+
}
120+
121+
bool pbdrv_clock_is_ticking(void) {
122+
return true;
123+
}
124+
115125
#endif // PBDRV_CONFIG_CLOCK_LINUX

lib/pbio/drv/clock/clock_none.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ uint32_t pbdrv_clock_get_100us(void) {
2222
return 0;
2323
}
2424

25+
void pbdrv_clock_busy_delay_ms(uint32_t ms) {
26+
}
27+
28+
bool pbdrv_clock_is_ticking(void) {
29+
return false;
30+
}
31+
2532
#endif // PBDRV_CONFIG_CLOCK_NONE

lib/pbio/drv/clock/clock_nxt.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ void nx_systick_wait_ms(uint32_t ms) {
9898
}
9999
}
100100

101+
void pbdrv_clock_busy_delay_ms(uint32_t ms) {
102+
nx_systick_wait_ms(ms);
103+
}
104+
101105
void nx_systick_wait_ns(uint32_t ns) {
102106
volatile uint32_t x = (ns >> 7) + 1;
103107

@@ -106,4 +110,9 @@ void nx_systick_wait_ns(uint32_t ns) {
106110
}
107111
}
108112

113+
bool pbdrv_clock_is_ticking(void) {
114+
// TODO
115+
return true;
116+
}
117+
109118
#endif // PBDRV_CONFIG_CLOCK_NXT

lib/pbio/drv/clock/clock_stm32.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,14 @@ void HAL_Delay(uint32_t Delay) {
112112
}
113113
}
114114

115+
void pbdrv_clock_busy_delay_ms(uint32_t ms) {
116+
HAL_Delay(ms);
117+
}
118+
119+
bool pbdrv_clock_is_ticking(void) {
120+
// Init already completed in SystemInit(), so we just need to check if
121+
// interrupts are enabled.
122+
return __get_PRIMASK() == 0;
123+
}
124+
115125
#endif // PBDRV_CONFIG_CLOCK_STM32

lib/pbio/drv/clock/clock_test.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,12 @@ uint32_t pbdrv_clock_get_us(void) {
4141
return clock_ticks * 1000;
4242
}
4343

44+
void pbdrv_clock_busy_delay_ms(uint32_t ms) {
45+
}
46+
47+
bool pbdrv_clock_is_ticking(void) {
48+
return true;
49+
}
50+
4451

4552
#endif // PBDRV_CONFIG_CLOCK_TEST

lib/pbio/drv/clock/clock_virtual.c

Lines changed: 0 additions & 104 deletions
This file was deleted.

lib/pbio/include/pbdrv/clock.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef _PBDRV_CLOCK_H_
1010
#define _PBDRV_CLOCK_H_
1111

12+
#include <stdbool.h>
1213
#include <stdint.h>
1314

1415
/**
@@ -36,6 +37,26 @@ uint32_t pbdrv_clock_get_us(void);
3637
*/
3738
void pbdrv_clock_busy_delay_us(uint32_t us);
3839

40+
/**
41+
* Busy wait delay for several milliseconds. May not be accurate.
42+
*
43+
* NB: Should not be used in any driver code. This exists only as a hook for
44+
* APIs that need a blocking delay to work when interrupts could be disabled.
45+
*
46+
* @param [in] ms The number of milliseconds to delay.
47+
*/
48+
void pbdrv_clock_busy_delay_ms(uint32_t ms);
49+
50+
/**
51+
* Tests if the millisecond clock is ticking.
52+
*
53+
* On embedded systems, this means that the clock was initialized and IRQs are
54+
* enabled.
55+
*
56+
* @return True if the clock is ticking, false otherwise.
57+
*/
58+
bool pbdrv_clock_is_ticking(void);
59+
3960
#endif /* _PBDRV_CLOCK_H_ */
4061

4162
/** @} */

0 commit comments

Comments
 (0)