@@ -147,9 +147,10 @@ __attribute__((used, naked)) void Reset_Handler(void) {
147147 __enable_irq ();
148148 main ();
149149}
150-
151150#endif //end H7 specific code
152151
152+ // Low power clock variables
153+ static volatile uint32_t systick_ms ;
153154static RTC_HandleTypeDef _hrtc ;
154155
155156#if BOARD_HAS_LOW_SPEED_CRYSTAL
@@ -159,7 +160,7 @@ static uint32_t rtc_clock_frequency = LSI_VALUE;
159160#endif
160161
161162safe_mode_t port_init (void ) {
162- HAL_Init ();
163+ HAL_Init (); // Turns on SysTick
163164 __HAL_RCC_SYSCFG_CLK_ENABLE ();
164165
165166 #if (CPY_STM32F4 )
@@ -182,13 +183,38 @@ safe_mode_t port_init(void) {
182183 HAL_RTC_Init (& _hrtc );
183184 HAL_NVIC_EnableIRQ (RTC_Alarm_IRQn );
184185
186+ // Turn off SysTick
187+ SysTick -> CTRL = 0 ;
188+
185189 return NO_SAFE_MODE ;
186190}
187191
192+ void HAL_Delay (uint32_t delay_ms ) {
193+ if (SysTick -> CTRL != 0 ) {
194+ // SysTick is on, so use it
195+ uint32_t tickstart = systick_ms ;
196+ while (systick_ms - tickstart < delay_ms ) {
197+ }
198+ } else {
199+ mp_hal_delay_ms (delay_ms );
200+ }
201+ }
202+
203+ uint32_t HAL_GetTick () {
204+ if (SysTick -> CTRL != 0 ) {
205+ return systick_ms ;
206+ } else {
207+ uint8_t subticks ;
208+ uint32_t result = (uint32_t )port_get_raw_ticks (& subticks );
209+ return result ;
210+ }
211+ }
212+
213+
188214void SysTick_Handler (void ) {
215+ systick_ms += 1 ;
189216 // Read the CTRL register to clear the SysTick interrupt.
190217 SysTick -> CTRL ;
191- HAL_IncTick ();
192218}
193219
194220void reset_port (void ) {
0 commit comments