Skip to content

Commit 33bdd21

Browse files
committed
Merge branch 'master' of github.com:micropython/micropython
Conflicts: stmhal/main.c
2 parents b30c02a + deb413e commit 33bdd21

6 files changed

Lines changed: 30 additions & 64 deletions

File tree

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 & 13 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
}
@@ -248,7 +248,6 @@ int main(void) {
248248
#endif
249249

250250
// basic sub-system init
251-
sys_tick_init();
252251
pendsv_init();
253252
led_init();
254253

@@ -278,16 +277,16 @@ int main(void) {
278277
while (1) {
279278
led_state(led, 1);
280279
usart_tx_strn_cooked(pyb_usart_global_debug, "on\n", 3);
281-
sys_tick_delay_ms(100);
280+
HAL_Delay(100);
282281
led_state(led, 0);
283282
usart_tx_strn_cooked(pyb_usart_global_debug, "off\n", 4);
284-
sys_tick_delay_ms(100);
283+
HAL_Delay(100);
285284
led_state(led, 1);
286285
usart_tx_strn_cooked(pyb_usart_global_debug, "on\n", 3);
287-
sys_tick_delay_ms(100);
286+
HAL_Delay(100);
288287
led_state(led, 0);
289288
usart_tx_strn_cooked(pyb_usart_global_debug, "off\n", 4);
290-
sys_tick_delay_ms(700);
289+
HAL_Delay(700);
291290

292291
led = (led % 4) + 1;
293292
}
@@ -362,7 +361,7 @@ int main(void) {
362361
reset_filesystem = false;
363362
break;
364363
}
365-
sys_tick_delay_ms(10);
364+
HAL_Delay(10);
366365
}
367366
}
368367
#endif
@@ -378,7 +377,7 @@ int main(void) {
378377

379378
// LED on to indicate creation of LFS
380379
led_state(PYB_LED_R2, 1);
381-
uint32_t stc = sys_tick_counter;
380+
uint32_t start_tick = HAL_GetTick();
382381

383382
res = f_mkfs("0:", 0, 0);
384383
if (res == FR_OK) {
@@ -400,7 +399,7 @@ int main(void) {
400399
f_close(&fp);
401400

402401
// keep LED on for at least 200ms
403-
sys_tick_wait_at_least(stc, 200);
402+
sys_tick_wait_at_least(start_tick, 200);
404403
led_state(PYB_LED_R2, 0);
405404
} else {
406405
__fatal_error("could not access LFS");
@@ -424,7 +423,7 @@ int main(void) {
424423

425424
// LED on to indicate creation of boot.py
426425
led_state(PYB_LED_R2, 1);
427-
uint32_t stc = sys_tick_counter;
426+
uint32_t start_tick = HAL_GetTick();
428427

429428
FIL fp;
430429
f_open(&fp, "0:/boot.py", FA_WRITE | FA_CREATE_ALWAYS);
@@ -434,7 +433,7 @@ int main(void) {
434433
f_close(&fp);
435434

436435
// keep LED on for at least 200ms
437-
sys_tick_wait_at_least(stc, 200);
436+
sys_tick_wait_at_least(start_tick, 200);
438437
led_state(PYB_LED_R2, 0);
439438
}
440439
}
@@ -550,7 +549,7 @@ int main(void) {
550549
}
551550
accel_read_nack();
552551
usb_hid_send_report(data);
553-
sys_tick_delay_ms(15);
552+
HAL_Delay(15);
554553
}
555554
}
556555
#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)