waveblender/timer.c
Daniele Lacamera 22ff79dcdf Initial import
2020-04-06 21:08:01 +02:00

57 lines
1.2 KiB
C

#include <stdint.h>
#include "system.h"
static uint32_t master_clock = 0;
/* Timer 4: Use val 60000 with PSC 1400 for 1s tick (84 Mhz) */
/* Timer 4: Use val 52500 with PSC 200 for 1/8 s tick (84 Mhz) */
#define TMR4_INIT_VAL 52500
#define TMR4_INIT_PSC 200
void timer_init(void)
{
uint32_t val = 0;
uint32_t psc = 1;
uint32_t err = 0;
nvic_irq_enable(NVIC_TIM4_IRQN);
nvic_irq_setprio(NVIC_TIM4_IRQN, 0);
APB1_CLOCK_RST |= TIM4_APB1_CLOCK_ER_VAL;
__asm__ volatile ("dmb");
APB1_CLOCK_RST &= ~TIM4_APB1_CLOCK_ER_VAL;
APB1_CLOCK_ER |= TIM4_APB1_CLOCK_ER_VAL;
TIM4_CR1 = 0;
__asm__ volatile ("dmb");
TIM4_PSC = TMR4_INIT_PSC;
TIM4_ARR = TMR4_INIT_VAL;
TIM4_CR1 |= TIM_CR1_CLOCK_ENABLE;
TIM4_DIER |= TIM_DIER_UIE;
__asm__ volatile ("dmb");
}
static volatile uint32_t tim4_ticks = 0;
void isr_tim1(void)
{
TIM1_SR &= ~TIM_SR_UIF;
}
void isr_tim3(void)
{
TIM3_SR &= ~TIM_SR_UIF;
}
void isr_tim4(void)
{
TIM4_SR &= ~TIM_SR_UIF;
tim4_ticks++;
}
void gettime(uint32_t *seconds, uint32_t *microseconds)
{
*microseconds = ((TIM4_CNT * TMR4_INIT_PSC) / 84) + (tim4_ticks & 0x07) * 125000;
*seconds = (tim4_ticks >> 3);
}