feat: 10us count in clock

This commit is contained in:
liuyihui 2023-04-27 14:43:03 +08:00
parent 602d3d0a80
commit bfe90429d2
4 changed files with 28 additions and 14 deletions

View File

@ -27,8 +27,10 @@
PORT##port->PCR[pin] |= PORT_PCR_PE(0x1); \
}
#define ticks_ms (uint32_t)(ticks_10us / 100)
extern uint32_t CORCLK;
extern uint32_t BUSCLK;
extern volatile uint32_t ms_ticks;
extern volatile uint32_t ticks_10us;
extern void SysTick_Handler(void);
extern void UART1_IRQHandler(void);

View File

@ -3,22 +3,33 @@
#include "derivative.h"
#include <stdbool.h>
extern volatile uint32_t ms_ticks;
static inline void spin(volatile uint32_t count) {
while (count--) asm("nop");
}
// t: expiration time, prd: period
static inline bool timer_expired(uint32_t *t, uint32_t prd) {
if (ms_ticks + prd < *t) *t = 0; // Time wrapped? Reset timer
if (*t == 0) *t = ms_ticks + prd; // First poll? Set expiration
if (*t > ms_ticks) return false; // Not expired yet, return
*t = (ms_ticks - *t) > prd ? ms_ticks + prd : *t + prd; // Next expiration time
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 void delay_ms(uint32_t ms) {
uint32_t start = ms_ticks;
while (ms_ticks - start < ms) asm("nop");
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");
}

View File

@ -4,10 +4,9 @@
uint32_t CORCLK = CORCLK_DEFAULT;
uint32_t BUSCLK = BUSCLK_DEFAULT;
// ms count, volatile is important!!
volatile uint32_t ms_ticks;
volatile uint32_t ticks_10us;
void SysTick_Handler(void) { ms_ticks++; }
void SysTick_Handler(void) { ticks_10us++; }
void UART1_IRQHandler(void) {
if (UART1->S1 & UART_S1_RDRF_MASK) {

View File

@ -159,6 +159,8 @@ __attribute__((naked, noreturn)) void _reset(void) {
copy_data();
clock_init();
SysTick_Config(CORCLK / 100000); // Period of systick timer : 10 us
main();
for (;;) (void)0;
}