Skip to content

Commit 1a7060a

Browse files
committed
Add us delay
1 parent 1006c56 commit 1a7060a

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

ports/stm32f4/common-hal/microcontroller/__init__.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,30 @@
4242
// This routine should work even when interrupts are disabled. Used by OneWire
4343
// for precise timing.
4444
void common_hal_mcu_delay_us(uint32_t delay) {
45-
//TODO: implement equivalent of mp_hal_delay_us(delay);
46-
//this is fairly annoying in the STM32 HAL
45+
// sys freq is always a multiple of 2MHz, so division here won't lose precision
46+
const uint32_t ucount = HAL_RCC_GetSysClockFreq() / 2000000 * delay / 2;
47+
for (uint32_t count = 0; ++count <= ucount;) {
4748
}
4849

49-
void common_hal_mcu_disable_interrupts() {
50+
volatile uint32_t nesting_count = 0;
51+
void common_hal_mcu_disable_interrupts(void) {
52+
__disable_irq();
53+
__DMB();
54+
nesting_count++;
5055
}
5156

52-
void common_hal_mcu_enable_interrupts() {
57+
void common_hal_mcu_enable_interrupts(void) {
58+
if (nesting_count == 0) {
59+
// This is very very bad because it means there was mismatched disable/enables so we
60+
// "HardFault".
61+
HardFault_Handler();
62+
}
63+
nesting_count--;
64+
if (nesting_count > 0) {
65+
return;
66+
}
67+
__DMB();
68+
__enable_irq();
5369
}
5470

5571
void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
@@ -58,7 +74,7 @@ void common_hal_mcu_on_next_reset(mcu_runmode_t runmode) {
5874
}
5975

6076
void common_hal_mcu_reset(void) {
61-
filesystem_flush();
77+
filesystem_flush(); //TODO: implement as part of flash improvements
6278
NVIC_SystemReset();
6379
}
6480

ports/stm32f4/mphalport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
#include "lib/utils/interrupt_char.h"
3434
#include "py/mpconfig.h"
3535

36-
//extern nrfx_uarte_t serial_instance;
37-
3836
extern volatile uint64_t ticks_ms;
3937

4038
#define mp_hal_ticks_ms() ((mp_uint_t) ticks_ms)

0 commit comments

Comments
 (0)