@@ -68,6 +68,9 @@ extern void NVIC_SystemReset(void) NORETURN;
6868safe_mode_t port_init (void ) {
6969 int err = E_NO_ERROR ;
7070
71+ // 1ms tick timer
72+ SysTick_Config (SystemCoreClock / 1000 );
73+
7174 // Enable GPIO (enables clocks + common init for ports)
7275 for (int i = 0 ; i < MXC_CFG_GPIO_INSTANCES ; i ++ ) {
7376 err = MXC_GPIO_Init (0x1 << i );
@@ -122,12 +125,18 @@ void RTC_IRQHandler(void) {
122125 // Read flags to clear
123126 int flags = MXC_RTC_GetFlags ();
124127
125- if (flags & MXC_F_RTC_CTRL_SSEC_ALARM ) {
126- MXC_RTC_ClearFlags (MXC_F_RTC_CTRL_SSEC_ALARM );
127- }
128-
129- if (flags & MXC_F_RTC_CTRL_TOD_ALARM ) {
130- MXC_RTC_ClearFlags (MXC_F_RTC_CTRL_TOD_ALARM );
128+ switch (flags ) {
129+ case MXC_F_RTC_CTRL_SSEC_ALARM :
130+ MXC_RTC_ClearFlags (MXC_F_RTC_CTRL_SSEC_ALARM );
131+ break ;
132+ case MXC_F_RTC_CTRL_TOD_ALARM :
133+ MXC_RTC_ClearFlags (MXC_F_RTC_CTRL_TOD_ALARM );
134+ break ;
135+ case MXC_F_RTC_CTRL_RDY :
136+ MXC_RTC_ClearFlags (MXC_F_RTC_CTRL_RDY );
137+ break ;
138+ default :
139+ break ;
131140 }
132141
133142 tick_flag = 1 ;
@@ -145,7 +154,7 @@ void reset_port(void) {
145154}
146155
147156// Reset to the bootloader
148- // note: not implemented since max32 requires external stim ignals to
157+ // note: not implemented since max32 requires external signals to
149158// activate bootloaders
150159void reset_to_bootloader (void ) {
151160 NVIC_SystemReset ();
@@ -193,7 +202,6 @@ uint32_t port_get_saved_word(void) {
193202uint64_t port_get_raw_ticks (uint8_t * subticks ) {
194203 // Ensure we can read from ssec register as soon as we can
195204 // MXC function does cross-tick / busy checking of RTC controller
196- __disable_irq ();
197205 if (MXC_RTC -> ctrl & MXC_F_RTC_CTRL_EN ) {
198206 // NOTE: RTC_GetTime always returns BUSY if RTC is not running
199207 while ((MXC_RTC_GetTime (& sec , & subsec )) != E_NO_ERROR ) {
@@ -203,7 +211,6 @@ uint64_t port_get_raw_ticks(uint8_t *subticks) {
203211 sec = MXC_RTC -> sec ;
204212 subsec = MXC_RTC -> ssec ;
205213 }
206- __enable_irq ();
207214
208215 // Return ticks given total subseconds
209216 // ticks = TICKS/s * s + subsec/ subs/tick
@@ -220,41 +227,36 @@ uint64_t port_get_raw_ticks(uint8_t *subticks) {
220227
221228// Enable 1/1024 second tick.
222229void port_enable_tick (void ) {
223- MXC_RTC_Start ();
230+ while ( MXC_RTC_Start () == E_BUSY );
224231}
225232
226233// Disable 1/1024 second tick.
227234void port_disable_tick (void ) {
228- MXC_RTC_Stop ();
235+ while ( MXC_RTC_Stop () == E_BUSY );
229236}
230237
231238// Wake the CPU after a given # of ticks or sooner
232239void port_interrupt_after_ticks (uint32_t ticks ) {
233240 uint32_t ticks_msec = 0 ;
234- // Stop RTC & store current time & ticks
235- port_disable_tick ();
236- port_get_raw_ticks (NULL );
237241
238- ticks_msec = 1000 * ticks / TICKS_PER_SEC ;
242+ ticks_msec = ( ticks / TICKS_PER_SEC ) * 1000 ;
239243
240- while (MXC_RTC_DisableInt (MXC_F_RTC_CTRL_SSEC_ALARM_IE |
241- MXC_F_RTC_CTRL_TOD_ALARM_IE ) == E_BUSY ) {
242- }
243- ;
244+ // Disable RTC interrupts
245+ MXC_RTC_DisableInt (MXC_F_RTC_CTRL_SSEC_ALARM_IE |
246+ MXC_F_RTC_CTRL_TOD_ALARM_IE | MXC_F_RTC_CTRL_RDY_IE );
247+
248+ // Stop RTC & store current time & ticks
249+ port_get_raw_ticks (NULL );
244250
245251 // Clear the flag to be set by the RTC Handler
246252 tick_flag = 0 ;
247253
248254 // Subsec alarm is the starting/reload value of the SSEC counter.
249255 // ISR triggered when SSEC rolls over from 0xFFFF_FFFF to 0x0
250- while (MXC_RTC_SetSubsecondAlarm (MSEC_TO_SS_ALARM (ticks_msec )) == E_BUSY ) {
251- }
252- while (MXC_RTC_EnableInt (MXC_F_RTC_CTRL_SSEC_ALARM_IE ) == E_BUSY ) {
253- }
256+ while (MXC_RTC_SetSubsecondAlarm (MSEC_TO_SS_ALARM (ticks_msec )) != E_SUCCESS ) {}
254257
255- NVIC_EnableIRQ ( RTC_IRQn );
258+ MXC_RTC_EnableInt ( MXC_F_RTC_CTRL_SSEC_ALARM_IE );
256259
257- port_enable_tick ();
258260}
259261
260262void port_idle_until_interrupt (void ) {
0 commit comments