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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
/**
* \file
*
* \brief SAM DUALTIMER Driver for SAMB11
*
* Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
*
* \asf_license_start
*
* \page License
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel microcontroller product.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* \asf_license_stop
*
*/
/*
* Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
*/
#include "dualtimer.h"
static dualtimer_callback_t dualtimer_callback_timer1 = NULL;
static dualtimer_callback_t dualtimer_callback_timer2 = NULL;
/**
* \brief Initializes config with predefined default values.
*
* This function will initialize a given Dualtimer configuration structure to
* a set of known default values. This function should be called on
* any new instance of the configuration structures before being
* modified by the user application.
*
* \param[out] config Pointer to a DUALTIMER module configuration structure to set
*/
void dualtimer_get_config_defaults(struct dualtimer_config *config)
{
config->timer1.timer_enable = true;
config->timer2.timer_enable = true;
config->timer1.counter_mode = DUALTIMER_PERIODIC_MODE;
config->timer2.counter_mode = DUALTIMER_PERIODIC_MODE;
config->timer1.counter_size = DUALTIMER_COUNTER_SIZE_32BIT;
config->timer2.counter_size = DUALTIMER_COUNTER_SIZE_32BIT;
config->timer1.clock_prescaler = DUALTIMER_CLOCK_PRESCALER_DIV1;
config->timer2.clock_prescaler = DUALTIMER_CLOCK_PRESCALER_DIV1;
config->timer1.interrup_enable = true;
config->timer2.interrup_enable = true;
config->timer1.load_value = 0;
config->timer2.load_value = 0;
config->clock_source = DUALTIMER_CLK_INPUT_0;
}
/**
* \brief Get Dualtimer module timer1/timer2 current value.
*
* \param[in] timer Timer1/Timer2
*
* \retval Timer1/Timer2 current value
*/
uint32_t dualtimer_get_value(enum dualtimer_timer timer)
{
if (timer == DUALTIMER_TIMER1) {
return DUALTIMER0->TIMER1VALUE.reg;
} else {
return DUALTIMER0->TIMER2VALUE.reg;
}
}
/**
* \brief Set Dualtimer module timer1/timer2 load value.
*
* \param[in] timer Timer1/Timer2
* \param[in] cur_bg Current/Background
* \param[in] value Load value
*/
void dualtimer_set_counter(enum dualtimer_timer timer,
enum dualtimer_set_register cur_bg, uint32_t value)
{
if (timer == DUALTIMER_TIMER1) {
if (cur_bg == DUALTIMER_SET_CURRUNT_REG) {
DUALTIMER0->TIMER1LOAD.reg = value;
} else {
DUALTIMER0->TIMER1BGLOAD.reg = value;
}
} else {
if (cur_bg == DUALTIMER_SET_CURRUNT_REG) {
DUALTIMER0->TIMER2LOAD.reg = value;
} else {
DUALTIMER0->TIMER2BGLOAD.reg = value;
}
}
}
/**
* \brief Get Dualtimer module timer1/timer2 raw interrupt status
*
* \param[in] timer Timer1/Timer2
*
* \retval The raw interrupt status of timer1/timer2
*/
uint8_t dualtimer_get_status(enum dualtimer_timer timer)
{
if (timer == DUALTIMER_TIMER1) {
return DUALTIMER0->TIMER1RIS.reg;
} else {
return DUALTIMER0->TIMER2RIS.reg;
}
}
/**
* \brief Get Dualtimer module timer1/timer2 interrupt status
*
* \param[in] timer Timer1/Timer2
*
* \retval The interrupt status of timer1/timer2
*/
uint8_t dualtimer_get_interrupt_status(enum dualtimer_timer timer)
{
if (timer == DUALTIMER_TIMER1) {
return DUALTIMER0->TIMER1MIS.reg;
} else {
return DUALTIMER0->TIMER2MIS.reg;
}
}
/**
* \brief Clear Dualtimer module timer1/timer2 interrupt status
*
* Clear the Dualtimer module timer1/timer2 interrupt status
*
* \param[in] timer Timer1/Timer2
*/
void dualtimer_clear_interrupt_status(enum dualtimer_timer timer)
{
if (timer == DUALTIMER_TIMER1) {
DUALTIMER0->TIMER1INTCLR.reg = 1;
} else {
DUALTIMER0->TIMER2INTCLR.reg = 1;
}
}
/**
* \brief Set Dualtimer module timer1/timer2 enable
*
* Enable the Dualtimer module timer1/timer2
*
* \param[in] timer Timer1/Timer2
*/
void dualtimer_enable(enum dualtimer_timer timer)
{
if (timer == DUALTIMER_TIMER1) {
DUALTIMER0->TIMER1CONTROL.reg |= DUALTIMER_TIMER1CONTROL_TIMER_ENABLE;
} else {
DUALTIMER0->TIMER2CONTROL.reg |= DUALTIMER_TIMER2CONTROL_TIMER_ENABLE;
}
}
/**
* \brief Set Dualtimer module timer1/timer2 disable
*
* Disable the Dualtimer module timer1/timer2
*
* \param[in] timer Timer1/Timer2
*/
void dualtimer_disable(enum dualtimer_timer timer)
{
if (timer == DUALTIMER_TIMER1) {
DUALTIMER0->TIMER1CONTROL.reg &= ~DUALTIMER_TIMER1CONTROL_TIMER_ENABLE;
} else {
DUALTIMER0->TIMER2CONTROL.reg &= ~DUALTIMER_TIMER2CONTROL_TIMER_ENABLE;
}
}
/**
* \brief Dualtimer ISR handler.
*
* Dualtimer ISR handler.
*
*/
static void dualtimer_isr_handler(void)
{
if (dualtimer_get_interrupt_status(DUALTIMER_TIMER1)) {
dualtimer_clear_interrupt_status(DUALTIMER_TIMER1);
if (dualtimer_callback_timer1)
dualtimer_callback_timer1();
}
if (dualtimer_get_interrupt_status(DUALTIMER_TIMER2)) {
dualtimer_clear_interrupt_status(DUALTIMER_TIMER2);
if (dualtimer_callback_timer2)
dualtimer_callback_timer2();
}
}
/**
* \brief Initializes Dualtimer module instance.
*
* Initializes the Dualtimer module, based on the given
* configuration values.
*
* \param[in] config Pointer to the Dualtimer configuration options struct
*
* \return Status of the initialization procedure.
*/
void dualtimer_init(const struct dualtimer_config *config)
{
uint8_t regval = 0;
/* Global reset */
system_peripheral_reset(PERIPHERAL_DUALT_TIMER);
/* Common config */
if (config->timer1.timer_enable || config->timer2.timer_enable) {
LPMCU_MISC_REGS0->LPMCU_CLOCK_ENABLES_0.reg |=
LPMCU_MISC_REGS_LPMCU_CLOCK_ENABLES_0_DUALTIMER0_CLK_EN;
LPMCU_MISC_REGS0->LPMCU_CTRL.bit.DUALTIMER0_CLK_SEL = config->clock_source;
}
/* Timer1 config */
if (config->timer1.timer_enable) {
if (config->timer1.counter_mode == DUALTIMER_ONE_SHOT_MODE) {
regval = DUALTIMER_TIMER1CONTROL_ONE_SHOT_COUNT_1;
} else if (config->timer1.counter_mode == DUALTIMER_FREE_RUNNING_MODE) {
regval = DUALTIMER_TIMER1CONTROL_TIMER_MODE_0;
} else if (config->timer1.counter_mode == DUALTIMER_PERIODIC_MODE) {
regval = DUALTIMER_TIMER1CONTROL_TIMER_MODE_1;
}
regval |= (DUALTIMER_TIMER1CONTROL_TIMER_SIZE &
((config->timer1.counter_size) << DUALTIMER_TIMER1CONTROL_TIMER_SIZE_Pos)) |
DUALTIMER_TIMER1CONTROL_TIMERPRE(config->timer1.clock_prescaler);
if (config->timer1.interrup_enable) {
regval |= DUALTIMER_TIMER1CONTROL_INTERRUPT_ENABLE;
}
DUALTIMER0->TIMER1LOAD.reg = config->timer1.load_value;
DUALTIMER0->TIMER1CONTROL.reg = regval;
LPMCU_MISC_REGS0->DUALTIMER0_CTRL.reg |= LPMCU_MISC_REGS_DUALTIMER0_CTRL_CNTR_1_ENABLE;
dualtimer_enable(DUALTIMER_TIMER1);
}
/* Timer2 config */
if (config->timer2.timer_enable) {
if (config->timer2.counter_mode == DUALTIMER_ONE_SHOT_MODE) {
regval = DUALTIMER_TIMER2CONTROL_ONE_SHOT_COUNT_1;
} else if (config->timer2.counter_mode == DUALTIMER_FREE_RUNNING_MODE) {
regval = DUALTIMER_TIMER2CONTROL_TIMER_MODE_0;
} else if (config->timer2.counter_mode == DUALTIMER_PERIODIC_MODE) {
regval = DUALTIMER_TIMER2CONTROL_TIMER_MODE_1;
}
regval |= (DUALTIMER_TIMER2CONTROL_TIMER_SIZE &
((config->timer2.counter_size) << DUALTIMER_TIMER2CONTROL_TIMER_SIZE_Pos)) |
DUALTIMER_TIMER2CONTROL_TIMERPRE(config->timer2.clock_prescaler);
if (config->timer2.interrup_enable) {
regval |= DUALTIMER_TIMER2CONTROL_INTERRUPT_ENABLE;
}
DUALTIMER0->TIMER2LOAD.reg = config->timer2.load_value;
DUALTIMER0->TIMER2CONTROL.reg = regval;
LPMCU_MISC_REGS0->DUALTIMER0_CTRL.reg |= LPMCU_MISC_REGS_DUALTIMER0_CTRL_CNTR_2_ENABLE;
dualtimer_enable(DUALTIMER_TIMER2);
}
system_register_isr(RAM_ISR_TABLE_DUALTIMER_INDEX, (uint32_t)dualtimer_isr_handler);
}
/**
* \brief Registers a callback.
*
* Registers and enable a callback function which is implemented by the user.
*
* \param[in] callback_func Pointer to callback function
*/
void dualtimer_register_callback(enum dualtimer_timer timer, dualtimer_callback_t fun)
{
if (timer == DUALTIMER_TIMER1) {
dualtimer_callback_timer1 = fun;
} else {
dualtimer_callback_timer2 = fun;
}
}
/**
* \brief Unregisters a callback.
*
* Unregisters and disable a callback function implemented by the user.
*
*/
void dualtimer_unregister_callback(enum dualtimer_timer timer)
{
if (timer == DUALTIMER_TIMER1) {
dualtimer_callback_timer1 = NULL;
} else {
dualtimer_callback_timer2 = NULL;
}
}
|