Skip to content

Commit 89738e8

Browse files
committed
stmhal: Rename sys_tick ticks/delay functions to corresp. mp_hal ones.
The renames are: HAL_Delay -> mp_hal_delay_ms sys_tick_udelay -> mp_hal_delay_us sys_tick_get_microseconds -> mp_hal_ticks_us And mp_hal_ticks_ms is added to provide the full set of timing functions. Also, a separate HAL_Delay function is added which differs slightly from mp_hal_delay_ms and is intended for use only by the ST HAL functions.
1 parent 6ab5512 commit 89738e8

7 files changed

Lines changed: 39 additions & 26 deletions

File tree

stmhal/gccollect.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ mp_uint_t gc_helper_get_regs_and_sp(mp_uint_t *regs);
3939
void gc_collect(void) {
4040
// get current time, in case we want to time the GC
4141
#if 0
42-
uint32_t start = sys_tick_get_microseconds();
42+
uint32_t start = mp_hal_ticks_us();
4343
#endif
4444

4545
// start the GC
@@ -66,7 +66,7 @@ void gc_collect(void) {
6666

6767
#if 0
6868
// print GC info
69-
uint32_t ticks = sys_tick_get_microseconds() - start;
69+
uint32_t ticks = mp_hal_ticks_us() - start;
7070
gc_info_t info;
7171
gc_info(&info);
7272
printf("GC@%lu %lums\n", start, ticks);

stmhal/modpyb.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "py/obj.h"
3535
#include "py/gc.h"
3636
#include "py/builtin.h"
37+
#include "py/mphal.h"
3738
#include "lib/utils/pyexec.h"
3839
#include "lib/oofatfs/ff.h"
3940
#include "lib/oofatfs/diskio.h"
@@ -112,7 +113,7 @@ STATIC mp_obj_t pyb_micros(void) {
112113
// We want to "cast" the 32 bit unsigned into a small-int. This means
113114
// copying the MSB down 1 bit (extending the sign down), which is
114115
// equivalent to just using the MP_OBJ_NEW_SMALL_INT macro.
115-
return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds());
116+
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us());
116117
}
117118
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
118119

@@ -128,7 +129,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_micros_obj, pyb_micros);
128129
/// # Perform some operation
129130
STATIC mp_obj_t pyb_elapsed_micros(mp_obj_t start) {
130131
uint32_t startMicros = mp_obj_get_int(start);
131-
uint32_t currMicros = sys_tick_get_microseconds();
132+
uint32_t currMicros = mp_hal_ticks_us();
132133
return MP_OBJ_NEW_SMALL_INT((currMicros - startMicros) & 0x3fffffff);
133134
}
134135
STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_elapsed_micros_obj, pyb_elapsed_micros);

stmhal/mphalport.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,7 @@ void mp_hal_set_interrupt_char(int c); // -1 to disable
2727

2828
// timing functions
2929

30-
#include "stmhal/systick.h"
31-
32-
#define mp_hal_delay_ms HAL_Delay
33-
#define mp_hal_delay_us(us) sys_tick_udelay(us)
34-
#define mp_hal_delay_us_fast(us) sys_tick_udelay(us)
35-
#define mp_hal_ticks_ms HAL_GetTick
36-
#define mp_hal_ticks_us() sys_tick_get_microseconds()
30+
#define mp_hal_delay_us_fast(us) mp_hal_delay_us(us)
3731

3832
extern bool mp_hal_ticks_cpu_enabled;
3933
void mp_hal_ticks_cpu_enable(void);

stmhal/sdcard.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828

2929
#include "py/nlr.h"
3030
#include "py/runtime.h"
31+
#include "py/mphal.h"
3132
#include "lib/oofatfs/ff.h"
3233
#include "extmod/vfs_fat.h"
33-
#include "mphalport.h"
3434

3535
#include "sdcard.h"
3636
#include "pin.h"

stmhal/stm32_it.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ void SysTick_Handler(void) {
298298
uwTick += 1;
299299

300300
// Read the systick control regster. This has the side effect of clearing
301-
// the COUNTFLAG bit, which makes the logic in sys_tick_get_microseconds
301+
// the COUNTFLAG bit, which makes the logic in mp_hal_ticks_us
302302
// work properly.
303303
SysTick->CTRL;
304304

stmhal/systick.c

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,36 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include STM32_HAL_H
28-
29-
#include "py/obj.h"
27+
#include "py/mphal.h"
3028
#include "irq.h"
3129
#include "systick.h"
3230
#include "pybthread.h"
3331

34-
// We provide our own version of HAL_Delay that calls __WFI while waiting, in
35-
// order to reduce power consumption.
36-
// Note: Upon entering this function we may or may not have the GIL.
32+
extern __IO uint32_t uwTick;
33+
34+
// We provide our own version of HAL_Delay that calls __WFI while waiting,
35+
// and works when interrupts are disabled. This function is intended to be
36+
// used only by the ST HAL functions.
3737
void HAL_Delay(uint32_t Delay) {
3838
if (query_irq() == IRQ_STATE_ENABLED) {
3939
// IRQs enabled, so can use systick counter to do the delay
40-
extern __IO uint32_t uwTick;
40+
uint32_t start = uwTick;
41+
// Wraparound of tick is taken care of by 2's complement arithmetic.
42+
while (uwTick - start < Delay) {
43+
// Enter sleep mode, waiting for (at least) the SysTick interrupt.
44+
__WFI();
45+
}
46+
} else {
47+
// IRQs disabled, use mp_hal_delay_ms routine.
48+
mp_hal_delay_ms(Delay);
49+
}
50+
}
51+
52+
// Core delay function that does an efficient sleep and may switch thread context.
53+
// Note: Upon entering this function we may or may not have the GIL.
54+
void mp_hal_delay_ms(mp_uint_t Delay) {
55+
if (query_irq() == IRQ_STATE_ENABLED) {
56+
// IRQs enabled, so can use systick counter to do the delay
4157
uint32_t start = uwTick;
4258
// Wraparound of tick is taken care of by 2's complement arithmetic.
4359
while (uwTick - start < Delay) {
@@ -64,11 +80,11 @@ void HAL_Delay(uint32_t Delay) {
6480
}
6581

6682
// delay for given number of microseconds
67-
void sys_tick_udelay(uint32_t usec) {
83+
void mp_hal_delay_us(mp_uint_t usec) {
6884
if (query_irq() == IRQ_STATE_ENABLED) {
6985
// IRQs enabled, so can use systick counter to do the delay
70-
uint32_t start = sys_tick_get_microseconds();
71-
while (sys_tick_get_microseconds() - start < usec) {
86+
uint32_t start = mp_hal_ticks_us();
87+
while (mp_hal_ticks_us() - start < usec) {
7288
}
7389
} else {
7490
// IRQs disabled, so need to use a busy loop for the delay
@@ -92,11 +108,15 @@ void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
92108
}
93109
}
94110

111+
mp_uint_t mp_hal_ticks_ms(void) {
112+
return uwTick;
113+
}
114+
95115
// The SysTick timer counts down at 168 MHz, so we can use that knowledge
96116
// to grab a microsecond counter.
97117
//
98118
// We assume that HAL_GetTickis returns milliseconds.
99-
uint32_t sys_tick_get_microseconds(void) {
119+
mp_uint_t mp_hal_ticks_us(void) {
100120
mp_uint_t irq_state = disable_irq();
101121
uint32_t counter = SysTick->VAL;
102122
uint32_t milliseconds = HAL_GetTick();

stmhal/systick.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,5 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
void sys_tick_udelay(uint32_t usec);
2827
void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms);
2928
bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms);
30-
uint32_t sys_tick_get_microseconds(void);

0 commit comments

Comments
 (0)