2013-10-19 14:40:54 +01:00
|
|
|
#include <stm32f4xx.h>
|
|
|
|
#include "misc.h"
|
|
|
|
#include "systick.h"
|
|
|
|
|
|
|
|
volatile uint32_t sys_tick_counter;
|
|
|
|
|
2013-10-23 20:39:20 +01:00
|
|
|
void sys_tick_init(void) {
|
2013-10-19 14:40:54 +01:00
|
|
|
// sys-tick interrupt called at 1ms intervals
|
|
|
|
sys_tick_counter = 0;
|
|
|
|
SysTick_Config(SystemCoreClock / 1000);
|
2013-10-19 18:13:48 +01:00
|
|
|
NVIC_SetPriority(SysTick_IRQn, 0); // make it highest priority
|
2013-10-19 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// called on SysTick interrupt
|
2013-10-23 20:39:20 +01:00
|
|
|
void SysTick_Handler(void) {
|
2013-10-19 14:40:54 +01:00
|
|
|
sys_tick_counter++;
|
2013-11-04 23:05:48 +00:00
|
|
|
// hack!
|
2013-11-30 15:02:49 +00:00
|
|
|
//void audio_drain(void);
|
|
|
|
//audio_drain();
|
2013-10-19 14:40:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void sys_tick_delay_ms(uint32_t delay_ms) {
|
|
|
|
sys_tick_wait_at_least(sys_tick_counter, delay_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
// waits until at least delay_ms milliseconds have passed from the sampling of sys_tick_counter in stc
|
|
|
|
// handles overflow properl
|
|
|
|
// assumes stc was taken from sys_tick_counter some time before calling this function
|
|
|
|
// eg stc <= sys_tick_counter for the case of no wrap around of sys_tick_counter
|
|
|
|
void sys_tick_wait_at_least(uint32_t stc, uint32_t delay_ms) {
|
|
|
|
// stc_wait is the value of sys_tick_counter that we wait for
|
|
|
|
uint32_t stc_wait = stc + delay_ms;
|
|
|
|
if (stc_wait < stc) {
|
|
|
|
// stc_wait wrapped around
|
|
|
|
while (stc <= sys_tick_counter || sys_tick_counter < stc_wait) {
|
2013-12-06 00:42:13 +00:00
|
|
|
__WFI(); // enter sleep mode, waiting for interrupt
|
2013-10-19 14:40:54 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// stc_wait did not wrap around
|
|
|
|
while (stc <= sys_tick_counter && sys_tick_counter < stc_wait) {
|
2013-12-06 00:42:13 +00:00
|
|
|
__WFI(); // enter sleep mode, waiting for interrupt
|
2013-10-19 14:40:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool sys_tick_has_passed(uint32_t stc, uint32_t delay_ms) {
|
|
|
|
// stc_wait is the value of sys_tick_counter that we wait for
|
|
|
|
uint32_t stc_wait = stc + delay_ms;
|
|
|
|
if (stc_wait < stc) {
|
|
|
|
// stc_wait wrapped around
|
|
|
|
return !(stc <= sys_tick_counter || sys_tick_counter < stc_wait);
|
|
|
|
} else {
|
|
|
|
// stc_wait did not wrap around
|
|
|
|
return !(stc <= sys_tick_counter && sys_tick_counter < stc_wait);
|
|
|
|
}
|
|
|
|
}
|