summaryrefslogtreecommitdiff
path: root/bsp/samd21/sam_d2x_asflib/sam0/drivers/timer/timer.c
blob: 707371de59343b418e9b935cff9f09c2b2bf0f79 (plain)
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
/**
 * \file
 *
 * \brief SAM TIMER 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 "timer.h"

static timer_callback_t timer_callback;
/**
 * \brief Initializes config with predefined default values.
 *
 * This function will initialize a given TIMER 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.
 *
 * The default configuration is as follows:
 *  \li Timer interrupt set as disable
 *  \li Set relaod value as 0
 *
 * \param[out]  config  Pointer to a TIMER module configuration structure to set
 */
void timer_get_config_defaults(struct timer_config *config)
{
	config->reload_value = 0;
	config->interrupt_enable = true;
}


/**
 * \brief Get TIMER0 module current value.
 *
 * \retval Current value
 */
uint32_t timer_get_value(void)
{
	return TIMER0->VALUE.reg;
}

/**
 * \brief Set TIMER0 module value.
 *
 * \param[in]     value        Reload value
 */
void timer_set_value(uint32_t value)
{
	TIMER0->RELOAD.reg = value;
}

/**
 * \brief Get TIMER0 module interrupt status
 *
 * \retval The status of module
 */
uint32_t timer_get_interrupt_status(void)
{
	return TIMER0->INTSTATUSCLEAR.reg;
}

/**
 * \brief Clear TIMER0 module interrupt status
 *
 * Clear the TIMER0 module interrupt status
 */
void timer_clear_interrupt_status(void)
{
	TIMER0->INTSTATUSCLEAR.reg = 1;
	/* Wait for operation finish */
	while (TIMER0->INTSTATUSCLEAR.reg);
}

/**
 * \brief Set TIMER0 module enable
 *
 * Enable the TIMER0 module
 */
void timer_enable(void)
{
	TIMER0->CTRL.reg |= TIMER_CTRL_ENABLE;
}

/**
 * \brief Set TIMER0 disable
 *
 * Disable the TIMER0 module
 */
void timer_disable(void)
{
	TIMER0->CTRL.reg &= (~TIMER_CTRL_ENABLE);
}

/**
 * \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 timer_register_callback(timer_callback_t fun)
{
	timer_callback = fun; 
}

/**
 * \brief Unregisters a callback.
 *
 * Unregisters and disable a callback function implemented by the user.
 *
 */
void timer_unregister_callback(void)
{
	timer_callback = NULL; 
}

/**
 * \brief Timer ISR handler.
 *
 * Timer ISR handler.
 *
 */
static void timer_isr_handler(void)
{
	if (timer_get_interrupt_status()) {
		timer_clear_interrupt_status();
		
		if (timer_callback) {
			timer_callback();
		}
	}
}

/**
 * \brief Initializes TIMER0 module instance.
 *
 * Initializes the TIMER0 module, based on the given
 * configuration values.
 *
 * \param[in]     config       Pointer to the TIMER configuration options struct
 *
 * \return Status of the initialization procedure.
 */
void timer_init(const struct timer_config *config)
{
	/* Global reset */
	system_peripheral_reset(PERIPHERAL_TIMER);

	TIMER0->CTRL.reg = config->interrupt_enable << TIMER_CTRL_INTERRUPT_ENABLE_Pos;
	TIMER0->RELOAD.reg = config->reload_value;
	
	timer_callback = NULL;
	system_register_isr(RAM_ISR_TABLE_TIMER0_INDEX, (uint32_t)timer_isr_handler);
}