You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.0 KiB
55 lines
1.0 KiB
#include "stm32f4xx.h"
|
|
#include "tick.h"
|
|
|
|
#define TICKS_PER_MS (2)
|
|
|
|
static unsigned int __irq_per_ms = TICKS_PER_MS;
|
|
|
|
volatile uint64_t __current_mills;
|
|
|
|
static void (*__ticker)(int ms) = 0;
|
|
|
|
/* systick irq handler */
|
|
void SysTick_Handler(void)
|
|
{
|
|
__current_mills += __irq_per_ms;
|
|
if (__ticker) {
|
|
__ticker(__irq_per_ms);
|
|
}
|
|
}
|
|
|
|
uint64_t millis()
|
|
{
|
|
return __current_mills;
|
|
}
|
|
|
|
void ticks_set_callback(void (*f)(int))
|
|
{
|
|
__ticker = f;
|
|
}
|
|
|
|
void delay(unsigned int ms)
|
|
{
|
|
uint64_t end = __current_mills + ms;
|
|
|
|
while (end > __current_mills)
|
|
;
|
|
}
|
|
|
|
void ticks_init(int ms)
|
|
{
|
|
if (ms > 0) {
|
|
__irq_per_ms = ms;
|
|
}
|
|
__current_mills = 0;
|
|
/* Set systick reload value to generate 1ms interrupt */
|
|
SysTick_Config(__irq_per_ms * SystemCoreClock / 1000);
|
|
}
|
|
|
|
void ticks_close()
|
|
{
|
|
SysTick->CTRL &= ~(SysTick_CTRL_CLKSOURCE_Msk |
|
|
SysTick_CTRL_TICKINT_Msk |
|
|
SysTick_CTRL_ENABLE_Msk); /* Enable SysTick IRQ and SysTick Timer */
|
|
}
|
|
|
|
|