36 lines
1.4 KiB
C
36 lines
1.4 KiB
C
#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
|
|
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
|
|
return true; // Expired, return true
|
|
}
|
|
|
|
static inline bool timer_expired_10us(uint32_t *t, uint32_t prd) {
|
|
if (ticks_10us + prd < *t) *t = 0; // Time wrapped? Reset timer
|
|
if (*t == 0) *t = ticks_10us + prd; // First poll? Set expiration
|
|
if (*t > ticks_10us) return false; // Not expired yet, return
|
|
*t = (ticks_10us - *t) > prd ? ticks_10us + prd : *t + prd; // Next expiration time
|
|
return true; // Expired, return true
|
|
}
|
|
|
|
static inline void delay_ms(uint32_t ms) {
|
|
uint32_t start = ticks_ms;
|
|
while (ticks_ms - start < ms) asm("nop");
|
|
}
|
|
|
|
static inline void delay_10us(uint32_t us) {
|
|
uint32_t start = ticks_10us;
|
|
while (ticks_10us - start < us) asm("nop");
|
|
}
|