feat: 10us count in clock
This commit is contained in:
parent
602d3d0a80
commit
bfe90429d2
@ -27,8 +27,10 @@
|
|||||||
PORT##port->PCR[pin] |= PORT_PCR_PE(0x1); \
|
PORT##port->PCR[pin] |= PORT_PCR_PE(0x1); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define ticks_ms (uint32_t)(ticks_10us / 100)
|
||||||
|
|
||||||
extern uint32_t CORCLK;
|
extern uint32_t CORCLK;
|
||||||
extern uint32_t BUSCLK;
|
extern uint32_t BUSCLK;
|
||||||
extern volatile uint32_t ms_ticks;
|
extern volatile uint32_t ticks_10us;
|
||||||
extern void SysTick_Handler(void);
|
extern void SysTick_Handler(void);
|
||||||
extern void UART1_IRQHandler(void);
|
extern void UART1_IRQHandler(void);
|
||||||
|
@ -3,22 +3,33 @@
|
|||||||
#include "derivative.h"
|
#include "derivative.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
extern volatile uint32_t ms_ticks;
|
|
||||||
|
|
||||||
static inline void spin(volatile uint32_t count) {
|
static inline void spin(volatile uint32_t count) {
|
||||||
while (count--) asm("nop");
|
while (count--) asm("nop");
|
||||||
}
|
}
|
||||||
|
|
||||||
// t: expiration time, prd: period
|
// t: expiration time, prd: period
|
||||||
static inline bool timer_expired(uint32_t *t, uint32_t prd) {
|
static inline bool timer_expired_ms(uint32_t *t, uint32_t prd) {
|
||||||
if (ms_ticks + prd < *t) *t = 0; // Time wrapped? Reset timer
|
if (ticks_ms + prd < *t) *t = 0; // Time wrapped? Reset timer
|
||||||
if (*t == 0) *t = ms_ticks + prd; // First poll? Set expiration
|
if (*t == 0) *t = ticks_ms + prd; // First poll? Set expiration
|
||||||
if (*t > ms_ticks) return false; // Not expired yet, return
|
if (*t > ticks_ms) return false; // Not expired yet, return
|
||||||
*t = (ms_ticks - *t) > prd ? ms_ticks + prd : *t + prd; // Next expiration time
|
*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
|
return true; // Expired, return true
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void delay_ms(uint32_t ms) {
|
static inline void delay_ms(uint32_t ms) {
|
||||||
uint32_t start = ms_ticks;
|
uint32_t start = ticks_ms;
|
||||||
while (ms_ticks - start < ms) asm("nop");
|
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");
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,9 @@
|
|||||||
uint32_t CORCLK = CORCLK_DEFAULT;
|
uint32_t CORCLK = CORCLK_DEFAULT;
|
||||||
uint32_t BUSCLK = BUSCLK_DEFAULT;
|
uint32_t BUSCLK = BUSCLK_DEFAULT;
|
||||||
|
|
||||||
// ms count, volatile is important!!
|
volatile uint32_t ticks_10us;
|
||||||
volatile uint32_t ms_ticks;
|
|
||||||
|
|
||||||
void SysTick_Handler(void) { ms_ticks++; }
|
void SysTick_Handler(void) { ticks_10us++; }
|
||||||
|
|
||||||
void UART1_IRQHandler(void) {
|
void UART1_IRQHandler(void) {
|
||||||
if (UART1->S1 & UART_S1_RDRF_MASK) {
|
if (UART1->S1 & UART_S1_RDRF_MASK) {
|
||||||
|
@ -159,6 +159,8 @@ __attribute__((naked, noreturn)) void _reset(void) {
|
|||||||
copy_data();
|
copy_data();
|
||||||
clock_init();
|
clock_init();
|
||||||
|
|
||||||
|
SysTick_Config(CORCLK / 100000); // Period of systick timer : 10 us
|
||||||
|
|
||||||
main();
|
main();
|
||||||
for (;;) (void)0;
|
for (;;) (void)0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user