1 /**
2  * \file
3  *
4  * \brief SAM TIMER Driver for SAMB11
5  *
6  * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 #include "timer.h"
47 
48 static timer_callback_t timer_callback;
49 /**
50  * \brief Initializes config with predefined default values.
51  *
52  * This function will initialize a given TIMER configuration structure to
53  * a set of known default values. This function should be called on
54  * any new instance of the configuration structures before being
55  * modified by the user application.
56  *
57  * The default configuration is as follows:
58  *  \li Timer interrupt set as disable
59  *  \li Set relaod value as 0
60  *
61  * \param[out]  config  Pointer to a TIMER module configuration structure to set
62  */
timer_get_config_defaults(struct timer_config * config)63 void timer_get_config_defaults(struct timer_config *config)
64 {
65 	config->reload_value = 0;
66 	config->interrupt_enable = true;
67 }
68 
69 
70 /**
71  * \brief Get TIMER0 module current value.
72  *
73  * \retval Current value
74  */
timer_get_value(void)75 uint32_t timer_get_value(void)
76 {
77 	return TIMER0->VALUE.reg;
78 }
79 
80 /**
81  * \brief Set TIMER0 module value.
82  *
83  * \param[in]     value        Reload value
84  */
timer_set_value(uint32_t value)85 void timer_set_value(uint32_t value)
86 {
87 	TIMER0->RELOAD.reg = value;
88 }
89 
90 /**
91  * \brief Get TIMER0 module interrupt status
92  *
93  * \retval The status of module
94  */
timer_get_interrupt_status(void)95 uint32_t timer_get_interrupt_status(void)
96 {
97 	return TIMER0->INTSTATUSCLEAR.reg;
98 }
99 
100 /**
101  * \brief Clear TIMER0 module interrupt status
102  *
103  * Clear the TIMER0 module interrupt status
104  */
timer_clear_interrupt_status(void)105 void timer_clear_interrupt_status(void)
106 {
107 	TIMER0->INTSTATUSCLEAR.reg = 1;
108 	/* Wait for operation finish */
109 	while (TIMER0->INTSTATUSCLEAR.reg);
110 }
111 
112 /**
113  * \brief Set TIMER0 module enable
114  *
115  * Enable the TIMER0 module
116  */
timer_enable(void)117 void timer_enable(void)
118 {
119 	TIMER0->CTRL.reg |= TIMER_CTRL_ENABLE;
120 }
121 
122 /**
123  * \brief Set TIMER0 disable
124  *
125  * Disable the TIMER0 module
126  */
timer_disable(void)127 void timer_disable(void)
128 {
129 	TIMER0->CTRL.reg &= (~TIMER_CTRL_ENABLE);
130 }
131 
132 /**
133  * \brief Registers a callback.
134  *
135  * Registers and enable a callback function which is implemented by the user.
136  *
137  * \param[in]     callback_func Pointer to callback function
138  */
timer_register_callback(timer_callback_t fun)139 void timer_register_callback(timer_callback_t fun)
140 {
141 	timer_callback = fun;
142 }
143 
144 /**
145  * \brief Unregisters a callback.
146  *
147  * Unregisters and disable a callback function implemented by the user.
148  *
149  */
timer_unregister_callback(void)150 void timer_unregister_callback(void)
151 {
152 	timer_callback = NULL;
153 }
154 
155 /**
156  * \brief Timer ISR handler.
157  *
158  * Timer ISR handler.
159  *
160  */
timer_isr_handler(void)161 static void timer_isr_handler(void)
162 {
163 	if (timer_get_interrupt_status()) {
164 		timer_clear_interrupt_status();
165 
166 		if (timer_callback) {
167 			timer_callback();
168 		}
169 	}
170 }
171 
172 /**
173  * \brief Initializes TIMER0 module instance.
174  *
175  * Initializes the TIMER0 module, based on the given
176  * configuration values.
177  *
178  * \param[in]     config       Pointer to the TIMER configuration options struct
179  *
180  * \return Status of the initialization procedure.
181  */
timer_init(const struct timer_config * config)182 void timer_init(const struct timer_config *config)
183 {
184 	/* Global reset */
185 	system_peripheral_reset(PERIPHERAL_TIMER);
186 
187 	TIMER0->CTRL.reg = config->interrupt_enable << TIMER_CTRL_INTERRUPT_ENABLE_Pos;
188 	TIMER0->RELOAD.reg = config->reload_value;
189 
190 	timer_callback = NULL;
191 	system_register_isr(RAM_ISR_TABLE_TIMER0_INDEX, (uint32_t)timer_isr_handler);
192 }