39
39
#include "supervisor/filesystem.h"
40
40
#include "supervisor/shared/safe_mode.h"
41
41
42
- // This routine should work even when interrupts are disabled. Used by OneWire
43
- // for precise timing.
42
+ #include "stm32f4xx_hal.h"
43
+
44
+ //tested divisor value for busy loop in us delay
45
+ #define LOOP_TICKS 12
46
+
47
+ STATIC uint32_t get_us (void ) {
48
+ uint32_t ticks_per_us = HAL_RCC_GetSysClockFreq ()/1000000 ;
49
+ uint32_t micros , sys_cycles ;
50
+ do {
51
+ micros = ticks_ms ;
52
+ sys_cycles = SysTick -> VAL ; //counts backwards
53
+ } while (micros != ticks_ms ); //try again if ticks_ms rolled over
54
+ return (micros * 1000 ) + (ticks_per_us * 1000 - sys_cycles ) / ticks_per_us ;
55
+ }
56
+
44
57
void common_hal_mcu_delay_us (uint32_t delay ) {
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 ;) {
58
+ if (__get_PRIMASK () == 0x00000000 ) {
59
+ //by default use ticks_ms
60
+ uint32_t start = get_us ();
61
+ while (get_us ()- start < delay ) {
62
+ __asm__ __volatile__("nop" );
63
+ }
64
+ } else {
65
+ //when SysTick is disabled, approximate with busy loop
66
+ const uint32_t ucount = HAL_RCC_GetSysClockFreq () / 1000000 * delay / LOOP_TICKS ;
67
+ (void )start ;
68
+ for (uint32_t count = 0 ; ++ count <= ucount ;) {
69
+ }
70
+ }
48
71
}
49
72
50
73
volatile uint32_t nesting_count = 0 ;
74
+
51
75
void common_hal_mcu_disable_interrupts (void ) {
52
76
__disable_irq ();
53
77
__DMB ();
@@ -58,7 +82,7 @@ void common_hal_mcu_enable_interrupts(void) {
58
82
if (nesting_count == 0 ) {
59
83
// This is very very bad because it means there was mismatched disable/enables so we
60
84
// "HardFault".
61
- HardFault_Handler ( );
85
+ asm( "bkpt" );
62
86
}
63
87
nesting_count -- ;
64
88
if (nesting_count > 0 ) {
0 commit comments