2023-04-17 21:56:42 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "derivative.h"
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
static inline void spin(volatile uint32_t count) {
|
|
|
|
while (count--) asm("nop");
|
|
|
|
}
|
|
|
|
|
|
|
|
// t: expiration time, prd: period
|
2023-04-27 14:43:03 +08:00
|
|
|
static inline bool timer_expired_ms(uint32_t *t, uint32_t prd) {
|
|
|
|
if (ticks_ms + prd < *t) *t = 0; // Time wrapped? Reset timer
|
|
|
|
if (*t == 0) *t = ticks_ms + prd; // First poll? Set expiration
|
|
|
|
if (*t > ticks_ms) return false; // Not expired yet, return
|
|
|
|
*t = (ticks_ms - *t) > prd ? ticks_ms + prd : *t + prd; // Next expiration time
|
2023-04-17 21:56:42 +08:00
|
|
|
return true; // Expired, return true
|
|
|
|
}
|
|
|
|
|
2023-04-28 18:11:09 +08:00
|
|
|
static inline bool timer_expired_100us(uint32_t *t, uint32_t prd) {
|
|
|
|
if (ticks_100us + prd < *t) *t = 0; // Time wrapped? Reset timer
|
|
|
|
if (*t == 0) *t = ticks_100us + prd; // First poll? Set expiration
|
|
|
|
if (*t > ticks_100us) return false; // Not expired yet, return
|
|
|
|
*t = (ticks_100us - *t) > prd ? ticks_100us + prd : *t + prd; // Next expiration time
|
|
|
|
return true; // Expired, return true
|
2023-04-27 14:43:03 +08:00
|
|
|
}
|
|
|
|
|
2023-04-17 21:56:42 +08:00
|
|
|
static inline void delay_ms(uint32_t ms) {
|
2023-04-27 14:43:03 +08:00
|
|
|
uint32_t start = ticks_ms;
|
|
|
|
while (ticks_ms - start < ms) asm("nop");
|
|
|
|
}
|
|
|
|
|
2023-04-28 18:11:09 +08:00
|
|
|
static inline void delay_100us(uint32_t us) {
|
|
|
|
uint32_t start = ticks_100us;
|
|
|
|
while (ticks_100us - start < us) asm("nop");
|
2023-04-17 21:56:42 +08:00
|
|
|
}
|