This repository has been archived on 2024-11-23. You can view files and clone it, but cannot push or open issues or pull requests.
SSTV-Decoder/include/systick.h

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_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
}
static inline void delay_ms(uint32_t ms) {
uint32_t start = ticks_ms;
while (ticks_ms - start < ms) asm("nop");
}
static inline void delay_100us(uint32_t us) {
uint32_t start = ticks_100us;
while (ticks_100us - start < us) asm("nop");
}