fb11e647
vrobic
reseau statique a...
|
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
|
/*
* Copyright (C) 2014-2016 Freie Universität Berlin
* 2015 Jan Wagner <mail@jwagner.eu>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup cpu_nrf5x_common
* @{
*
* @file
* @brief Implementation of the peripheral timer interface
*
* @author Christian Kühling <kuehling@zedat.fu-berlin.de>
* @author Timo Ziegler <timo.ziegler@fu-berlin.de>
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Jan Wagner <mail@jwagner.eu>
*
* @}
*/
#include "periph/timer.h"
#define F_TIMER (16000000U) /* the timer is clocked at 16MHz */
typedef struct {
timer_cb_t cb;
void *arg;
uint8_t flags;
} tim_ctx_t;
/**
* @brief timer state memory
*/
static tim_ctx_t ctx[TIMER_NUMOF];
static inline NRF_TIMER_Type *dev(tim_t tim)
{
return timer_config[tim].dev;
}
int timer_init(tim_t tim, unsigned long freq, timer_cb_t cb, void *arg)
{
/* make sure the given timer is valid */
if (tim >= TIMER_NUMOF) {
return -1;
}
/* save interrupt context */
ctx[tim].cb = cb;
ctx[tim].arg = arg;
/* power on timer */
#if CPU_FAM_NRF51
dev(tim)->POWER = 1;
#endif
/* reset and configure the timer */
dev(tim)->TASKS_STOP = 1;
dev(tim)->BITMODE = timer_config[tim].bitmode;
dev(tim)->MODE = TIMER_MODE_MODE_Timer;
dev(tim)->TASKS_CLEAR = 1;
/* figure out if desired frequency is available */
int i;
unsigned long cando = F_TIMER;
for (i = 0; i < 10; i++) {
if (freq == cando) {
dev(tim)->PRESCALER = i;
break;
}
cando /= 2;
}
if (i == 10) {
return -1;
}
/* reset compare state */
dev(tim)->EVENTS_COMPARE[0] = 0;
dev(tim)->EVENTS_COMPARE[1] = 0;
dev(tim)->EVENTS_COMPARE[2] = 0;
/* enable interrupts */
timer_irq_enable(tim);
/* start the timer */
dev(tim)->TASKS_START = 1;
return 0;
}
int timer_set(tim_t tim, int chan, unsigned int value)
{
uint32_t now = timer_read(tim);
return timer_set_absolute(tim, chan, (now + value));
}
int timer_set_absolute(tim_t tim, int chan, unsigned int value)
{
/* see if channel is valid */
if (chan >= timer_config[tim].channels) {
return -1;
}
ctx[tim].flags |= (1 << chan);
dev(tim)->CC[chan] = value;
dev(tim)->INTENSET = (TIMER_INTENSET_COMPARE0_Msk << chan);
return 1;
}
int timer_clear(tim_t tim, int chan)
{
/* see if channel is valid */
if (chan >= timer_config[tim].channels) {
return -1;
}
dev(tim)->INTENCLR = (TIMER_INTENSET_COMPARE0_Msk << chan);
ctx[tim].flags &= ~(1 << chan);
return 1;
}
unsigned int timer_read(tim_t tim)
{
dev(tim)->TASKS_CAPTURE[timer_config[tim].channels] = 1;
return dev(tim)->CC[timer_config[tim].channels];
}
void timer_start(tim_t tim)
{
dev(tim)->TASKS_START = 1;
}
void timer_stop(tim_t tim)
{
dev(tim)->TASKS_STOP = 1;
}
void timer_irq_enable(tim_t tim)
{
NVIC_EnableIRQ(timer_config[tim].irqn);
}
void timer_irq_disable(tim_t tim)
{
NVIC_DisableIRQ(timer_config[tim].irqn);
}
static inline void irq_handler(int num)
{
for (unsigned i = 0; i < timer_config[num].channels; i++) {
if (dev(num)->EVENTS_COMPARE[i] == 1) {
dev(num)->EVENTS_COMPARE[i] = 0;
if (ctx[num].flags & (1 << i)) {
ctx[num].flags &= ~(1 << i);
dev(num)->INTENCLR = (TIMER_INTENSET_COMPARE0_Msk << i);
ctx[num].cb(ctx[num].arg, i);
}
}
}
cortexm_isr_end();
}
#ifdef TIMER_0_ISR
void TIMER_0_ISR(void)
{
irq_handler(0);
}
#endif
#ifdef TIMER_1_ISR
void TIMER_1_ISR(void)
{
irq_handler(1);
}
#endif
#ifdef TIMER_2_ISR
void TIMER_2_ISR(void)
{
irq_handler(2);
}
#endif
|