Skip to content

Commit deb413e

Browse files
committed
Merge pull request #345 from dhylands/stmhal-systick-cleanup
stmhal - More systick cleanup. Fix HAL_Delay
2 parents 536dde2 + 9db719b commit deb413e

File tree

6 files changed

+30
-65
lines changed

6 files changed

+30
-65
lines changed

stmhal/hal/src/stm32f4xx_hal.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,13 @@ uint32_t HAL_GetTick(void)
281281
*/
282282
void HAL_Delay(__IO uint32_t Delay)
283283
{
284-
uint32_t timingdelay;
285-
286-
timingdelay = HAL_GetTick() + Delay;
287-
while(HAL_GetTick() < timingdelay)
288-
{
284+
uint32_t start = HAL_GetTick();
285+
286+
// Note that the following works (due to the magic of 2's complement numbers)
287+
// even when Delay causes wraparound.
288+
289+
while (HAL_GetTick() - start <= Delay) {
290+
__WFI(); // enter sleep mode, waiting for interrupt
289291
}
290292
}
291293

stmhal/main.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ void flash_error(int n) {
7272
for (int i = 0; i < n; i++) {
7373
led_state(PYB_LED_R1, 1);
7474
led_state(PYB_LED_R2, 0);
75-
sys_tick_delay_ms(250);
75+
HAL_Delay(250);
7676
led_state(PYB_LED_R1, 0);
7777
led_state(PYB_LED_R2, 1);
78-
sys_tick_delay_ms(250);
78+
HAL_Delay(250);
7979
}
8080
led_state(PYB_LED_R2, 0);
8181
}
@@ -242,8 +242,6 @@ int main(void) {
242242
#endif
243243
#endif
244244

245-
// basic sub-system init
246-
sys_tick_init();
247245
#if 0
248246
pendsv_init();
249247
#endif
@@ -275,16 +273,16 @@ int main(void) {
275273
while (1) {
276274
led_state(led, 1);
277275
usart_tx_strn_cooked(pyb_usart_global_debug, "on\n", 3);
278-
sys_tick_delay_ms(100);
276+
HAL_Delay(100);
279277
led_state(led, 0);
280278
usart_tx_strn_cooked(pyb_usart_global_debug, "off\n", 4);
281-
sys_tick_delay_ms(100);
279+
HAL_Delay(100);
282280
led_state(led, 1);
283281
usart_tx_strn_cooked(pyb_usart_global_debug, "on\n", 3);
284-
sys_tick_delay_ms(100);
282+
HAL_Delay(100);
285283
led_state(led, 0);
286284
usart_tx_strn_cooked(pyb_usart_global_debug, "off\n", 4);
287-
sys_tick_delay_ms(700);
285+
HAL_Delay(700);
288286

289287
led = (led % 4) + 1;
290288
}
@@ -359,7 +357,7 @@ int main(void) {
359357
reset_filesystem = false;
360358
break;
361359
}
362-
sys_tick_delay_ms(10);
360+
HAL_Delay(10);
363361
}
364362
}
365363
#endif
@@ -375,7 +373,7 @@ int main(void) {
375373

376374
// LED on to indicate creation of LFS
377375
led_state(PYB_LED_R2, 1);
378-
uint32_t stc = sys_tick_counter;
376+
uint32_t start_tick = HAL_GetTick();
379377

380378
res = f_mkfs("0:", 0, 0);
381379
if (res == FR_OK) {
@@ -397,7 +395,7 @@ int main(void) {
397395
f_close(&fp);
398396

399397
// keep LED on for at least 200ms
400-
sys_tick_wait_at_least(stc, 200);
398+
sys_tick_wait_at_least(start_tick, 200);
401399
led_state(PYB_LED_R2, 0);
402400
} else {
403401
__fatal_error("could not access LFS");
@@ -421,7 +419,7 @@ int main(void) {
421419

422420
// LED on to indicate creation of boot.py
423421
led_state(PYB_LED_R2, 1);
424-
uint32_t stc = sys_tick_counter;
422+
uint32_t start_tick = HAL_GetTick();
425423

426424
FIL fp;
427425
f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
@@ -431,7 +429,7 @@ int main(void) {
431429
f_close(&fp);
432430

433431
// keep LED on for at least 200ms
434-
sys_tick_wait_at_least(stc, 200);
432+
sys_tick_wait_at_least(start_tick, 200);
435433
led_state(PYB_LED_R2, 0);
436434
}
437435
}
@@ -530,7 +528,7 @@ int main(void) {
530528
}
531529
accel_read_nack();
532530
usb_hid_send_report(data);
533-
sys_tick_delay_ms(15);
531+
HAL_Delay(15);
534532
}
535533
}
536534
#endif

stmhal/pybmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ STATIC mp_obj_t pyb_millis(void) {
116116
STATIC MP_DEFINE_CONST_FUN_OBJ_0(pyb_millis_obj, pyb_millis);
117117

118118
STATIC mp_obj_t pyb_delay(mp_obj_t count) {
119-
sys_tick_delay_ms(mp_obj_get_int(count));
119+
HAL_Delay(mp_obj_get_int(count));
120120
return mp_const_none;
121121
}
122122

stmhal/pyexec.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int stdin_rx_chr(void) {
5959
if (pyb_usart_global_debug != PYB_USART_NONE && usart_rx_any(pyb_usart_global_debug)) {
6060
return usart_rx_char(pyb_usart_global_debug);
6161
}
62-
sys_tick_delay_ms(1);
62+
HAL_Delay(1);
6363
#if 0
6464
if (storage_needs_flush()) {
6565
storage_flush();
@@ -142,7 +142,7 @@ int readline(vstr_t *line, const char *prompt) {
142142
} else {
143143
escape = 0;
144144
}
145-
sys_tick_delay_ms(1);
145+
HAL_Delay(1);
146146
}
147147
}
148148

stmhal/systick.c

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,15 @@
22
#include "misc.h"
33
#include "systick.h"
44

5-
void sys_tick_init(void) {
6-
// SysTick_Config is now called from HAL_RCC_ClockConfig, which is called
7-
// from SystemClock_Config
8-
9-
// SysTick_Config sets the SysTick_IRQn to be the lowest priority, but
10-
// we want it to be the highest priority, so fix things here.
11-
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
12-
}
13-
14-
void sys_tick_delay_ms(uint32_t delay_ms) {
15-
sys_tick_wait_at_least(HAL_GetTick(), delay_ms);
16-
}
17-
18-
// waits until at least delay_ms milliseconds have passed from the sampling of stc
19-
// handles overflow properl
20-
// assumes stc was taken from HAL_GetTick() some time before calling this function
21-
// eg stc <= HAL_GetTick() for the case of no wrap around of HAL_GetTick()
22-
void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms) {
23-
// stc_wait is the value of HAL_GetTick() that we wait for
24-
uint32_t stc_wait = stc + delay_ms;
25-
if (stc_wait < stc) {
26-
// stc_wait wrapped around
27-
while (stc <= HAL_GetTick() || HAL_GetTick() < stc_wait) {
28-
__WFI(); // enter sleep mode, waiting for interrupt
29-
}
30-
} else {
31-
// stc_wait did not wrap around
32-
while (stc <= HAL_GetTick() && HAL_GetTick() < stc_wait) {
33-
__WFI(); // enter sleep mode, waiting for interrupt
34-
}
35-
}
5+
bool sys_tick_has_passed(uint32_t start_tick, uint32_t delay_ms) {
6+
return HAL_GetTick() - start_tick >= delay_ms;
367
}
378

38-
bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms) {
39-
// stc_wait is the value of HAL_GetTick() that we wait for
40-
uint32_t stc_wait = stc + delay_ms;
41-
if (stc_wait < stc) {
42-
// stc_wait wrapped around
43-
return !(stc <= HAL_GetTick() || HAL_GetTick() < stc_wait);
44-
} else {
45-
// stc_wait did not wrap around
46-
return !(stc <= HAL_GetTick() && HAL_GetTick() < stc_wait);
9+
// waits until at least delay_ms milliseconds have passed from the sampling of
10+
// startTick. Handles overflow properly. Assumes stc was taken from
11+
// HAL_GetTick() some time before calling this function.
12+
void sys_tick_wait_at_least(uint32_t start_tick, uint32_t delay_ms) {
13+
while (!sys_tick_has_passed(start_tick, delay_ms)) {
14+
__WFI(); // enter sleep mode, waiting for interrupt
4715
}
4816
}

stmhal/systick.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
void sys_tick_init(void);
2-
void SysTick_Handler(void);
3-
void sys_tick_delay_ms(uint32_t delay_ms);
41
void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms);
52
bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy