-
Notifications
You must be signed in to change notification settings - Fork 271
/
periodic.c
287 lines (223 loc) · 5.65 KB
/
periodic.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* Copyright (C) 2017-2019 by Jacob Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
// ----- Includes -----
#include <kll_defs.h>
#include "mcu_compat.h"
#if defined(_kinetis_)
#include "kinetis.h"
#elif defined(_sam_)
#include "sam.h"
#elif defined(_nrf_)
#include "nrf5.h"
#elif defined(_host_)
#include <stdint.h>
#endif
#include "sysview.h"
// ----- Variables -----
static void (*periodic_func)(void);
#if defined(_host_)
uint32_t Periodic_cycles_store = 0;
#endif
// ----- Functions -----
#if defined(_kinetis_k_)
// Must set function pointer first!!
void Periodic_init( uint32_t cycles )
{
// Setup PIT (Programmable Interrupt Timer)
SIM_SCGC6 |= SIM_SCGC6_PIT;;
PIT_TCTRL0 = 0x00; // Make sure timer is disabled first
PIT_MCR = 0x00; // Enable module, do not freeze timers in debug mode
// Timer Count-down value
// Number of cycles to count from CPU clock before calling interrupt
PIT_LDVAL0 = cycles;
// Enable Timer, Enable interrupt
PIT_TCTRL0 = PIT_TCTRL_TIE | PIT_TCTRL_TEN;
// Enable PIT Ch0 interrupt
NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
// Set PIT0 interrupt to a low priority
NVIC_SET_PRIORITY( IRQ_PIT_CH0, Periodic_Priority_define );
}
void Periodic_enable()
{
// Used to re-enable IRQ
NVIC_ENABLE_IRQ( IRQ_PIT_CH0 );
}
void Periodic_disable()
{
// Used to disable IRQ
NVIC_DISABLE_IRQ( IRQ_PIT_CH0 );
}
void Periodic_function( void *func )
{
// Set function pointer
periodic_func = func;
}
uint32_t Periodic_cycles()
{
return PIT_LDVAL0;
}
void pit0_isr()
{
// Call specified function
(*periodic_func)();
// Clear the interrupt
PIT_TFLG0 = PIT_TFLG_TIF;
}
#elif defined(_sam4s_a_) || defined(_sam4s_b_)
void Periodic_init( uint32_t cycles )
{
// Enable clock for timer
// Using Timer Module 1, Channel 3 (TC5)
PMC->PMC_PCER0 |= (1 << ID_TC2);
// Setup Timer Counter to MCK/32
TC0->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK3 | TC_CMR_CPCTRG;
// Timer Count-down value
// Number of cycles to count from CPU clock before calling interrupt
TC0->TC_CHANNEL[2].TC_RC = TC_RA_RA(cycles/2);
// Enable Timer, Enable interrupt
TC0->TC_CHANNEL[2].TC_IER = TC_IER_CPCS;
TC0->TC_CHANNEL[2].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
// Enable TC0 interrupt
NVIC_EnableIRQ( TC2_IRQn );
// Set TC0 interrupt to a low priority
NVIC_SetPriority( TC2_IRQn, Periodic_Priority_define );
}
void Periodic_function( void *func )
{
// Set function pointer
periodic_func = func;
}
void Periodic_enable()
{
// Used to re-enable IRQ
NVIC_EnableIRQ( TC2_IRQn );
}
void Periodic_disable()
{
// Used to disable IRQ
NVIC_DisableIRQ( TC2_IRQn );
}
uint32_t Periodic_cycles()
{
return TC0->TC_CHANNEL[2].TC_CV;
}
void TC2_Handler()
{
SEGGER_SYSVIEW_RecordEnterISR();
uint32_t status = TC0->TC_CHANNEL[2].TC_SR;
if ( status & TC_SR_CPCS )
{
(*periodic_func)();
}
SEGGER_SYSVIEW_RecordExitISRToScheduler();
}
#elif defined(_sam_)
void Periodic_init( uint32_t cycles )
{
// Enable clock for timer
// Using Timer Module 1, Channel 3 (TC5)
PMC->PMC_PCER0 |= (1 << ID_TC5);
// Setup Timer Counter to MCK/32
TC1->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK3 | TC_CMR_CPCTRG;
// Timer Count-down value
// Number of cycles to count from CPU clock before calling interrupt
TC1->TC_CHANNEL[2].TC_RC = TC_RA_RA(cycles/2);
// Enable Timer, Enable interrupt
TC1->TC_CHANNEL[2].TC_IER = TC_IER_CPCS;
TC1->TC_CHANNEL[2].TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
// Enable TC0 interrupt
NVIC_EnableIRQ( TC5_IRQn );
// Set TC0 interrupt to a low priority
NVIC_SetPriority( TC5_IRQn, Periodic_Priority_define );
}
void Periodic_function( void *func )
{
// Set function pointer
periodic_func = func;
}
void Periodic_enable()
{
// Used to re-enable IRQ
NVIC_EnableIRQ( TC5_IRQn );
}
void Periodic_disable()
{
// Used to disable IRQ
NVIC_DisableIRQ( TC5_IRQn );
}
uint32_t Periodic_cycles()
{
return TC1->TC_CHANNEL[2].TC_CV;
}
void TC5_Handler()
{
SEGGER_SYSVIEW_RecordEnterISR();
uint32_t status = TC1->TC_CHANNEL[2].TC_SR;
if ( status & TC_SR_CPCS )
{
(*periodic_func)();
}
SEGGER_SYSVIEW_RecordExitISRToScheduler();
}
#elif defined(_nrf_)
void Periodic_init( uint32_t cycles )
{
// NRF5 TODO
}
void Periodic_function( void *func )
{
// Set function pointer
periodic_func = func;
}
void Periodic_enable()
{
// NRF5 TODO
}
void Periodic_disable()
{
// NRF5 TODO
}
uint32_t Periodic_cycles()
{
// NRF5 TODO
return 0;
}
#elif defined(_host_)
void Periodic_init( uint32_t cycles )
{
Periodic_cycles_store = cycles;
}
void Periodic_function( void *func )
{
// Set function pointer
periodic_func = func;
}
void Periodic_enable()
{
}
void Periodic_disable()
{
}
uint32_t Periodic_cycles()
{
return Periodic_cycles_store;
}
#endif