-
Notifications
You must be signed in to change notification settings - Fork 0
/
pit.c
109 lines (95 loc) · 1.64 KB
/
pit.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* pit.c
*
* Created on: 25/07/2019
* Author: Evandro Teixeira
*/
#include "pit.h"
/**
* @brief
*/
irqTask pit_irq[PIT_NUMBER_CHANNEL][PIT_NUMBER_INDEX] = {NULL};
static uint8_t index_ch[PIT_NUMBER_INDEX] = {0};
/**
* @brief
* @param valeu
* @param ch
* @return
*/
bool pit_init(uint32_t value,pit_channel_t ch)
{
if(ch < PIT_NUMBER_INDEX)
{
// Enable PIT clock
SIM_SCGC6 |= SIM_SCGC6_PIT_MASK;
// Turn on PIT
PIT_MCR = 0;
// Configure PIT to produce an interrupt every 1s
//PIT_LDVAL0 = value;
PIT_LDVAL_REG(PIT,ch) = value;
// Enable interrupt and enable timer
//PIT_TCTRL0 |= PIT_TCTRL_TIE_MASK | PIT_TCTRL_TEN_MASK;
PIT_TCTRL_REG(PIT,ch) |= PIT_TCTRL_TIE_MASK | PIT_TCTRL_TEN_MASK;
PIT_TCTRL_REG(PIT,ch) &= ~PIT_TCTRL_TEN_MASK;
// Enable External Interrupt
NVIC_EnableIRQ(PIT_IRQn);
return true;
}
else
{
return false;
}
}
/**
* @brief
* @param
*/
void pit_start(pit_channel_t ch)
{
PIT_TCTRL_REG(PIT,ch) |= PIT_TCTRL_TEN_MASK;
}
/**
* @brief
* @param
*/
void pit_stop(pit_channel_t ch)
{
PIT_TCTRL_REG(PIT,ch) &= ~PIT_TCTRL_TEN_MASK;
}
/**
* @brief
* @param ch
* @param *task_irq
*/
bool pit_set_callback_irq(pit_channel_t ch, void (*task_irq)(void))
{
if(index_ch[ch] < PIT_NUMBER_INDEX)
{
pit_irq[ch][ (index_ch[ch]) ] = task_irq;
index_ch[ch]++;
return true;
}
else
{
return false;
}
}
/**
* @brief
*/
void PIT_IRQHandler(void)
{
uint8_t index = 0;
uint8_t i = 0;
for(index=0;index<PIT_NUMBER_CHANNEL;index++)
{
if( PIT_TFLG_REG(PIT,index) )
{
PIT_TFLG_REG(PIT,index) = PIT_TFLG_TIF_MASK;
for(i=0;i<index_ch[index];i++)
{
pit_irq[index][i]();
}
}
}
}