-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFreeRTOS_tick_config.c
90 lines (75 loc) · 2.02 KB
/
FreeRTOS_tick_config.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "demo.h"
/* ARM Generic Timer */
#define CORE0_TIMER_IRQCNTL ((volatile uint32_t *)(0x40000040))
static uint32_t timer_cntfrq = 0;
static uint32_t timer_tick = 0;
void enable_cntv(void)
{
uint32_t cntv_ctl;
cntv_ctl = 1;
asm volatile ("msr cntv_ctl_el0, %0" :: "r" (cntv_ctl));
}
/*-----------------------------------------------------------*/
void write_cntv_tval(uint32_t val)
{
asm volatile ("msr cntv_tval_el0, %0" :: "r" (val));
return;
}
/*-----------------------------------------------------------*/
uint32_t read_cntfrq(void)
{
uint32_t val;
asm volatile ("mrs %0, cntfrq_el0" : "=r" (val));
return val;
}
/*-----------------------------------------------------------*/
void init_timer(void)
{
timer_cntfrq = timer_tick = read_cntfrq();
write_cntv_tval(timer_cntfrq); // clear cntv interrupt and set next 1 sec timer.
return;
}
/*-----------------------------------------------------------*/
void timer_set_tick_rate_hz(uint32_t rate)
{
timer_tick = timer_cntfrq / rate ;
write_cntv_tval(timer_tick);
}
/*-----------------------------------------------------------*/
void vConfigureTickInterrupt( void )
{
/* init timer device. */
init_timer();
/* set tick rate. */
timer_set_tick_rate_hz(configTICK_RATE_HZ);
/* timer interrupt routing. */
*CORE0_TIMER_IRQCNTL = 1 << 3; /* nCNTVIRQ routing to CORE0.*/
/* start & enable interrupts in the timer. */
enable_cntv();
}
/*-----------------------------------------------------------*/
void vClearTickInterrupt( void )
{
write_cntv_tval(timer_tick); // clear cntv interrupt and set next timer.
return;
}
/*-----------------------------------------------------------*/
void vApplicationIRQHandler( uint32_t ulCORE0_INT_SRC )
{
uint32_t ulInterruptID;
ulInterruptID = ulCORE0_INT_SRC & 0x0007FFFFUL;
/* call handler function */
if(ulInterruptID & (1 << 3))
{
/* Generic Timer */
FreeRTOS_Tick_Handler();
}
if(ulInterruptID & (1 << 8))
{
/* Peripherals */
irq_handler();
}
}