1 /**
2  * \file
3  *
4  * \brief SAM Peripheral Analog-to-Digital Converter Driver
5  *
6  * Copyright (C) 2012-2015 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 
47 #ifndef ADC_CALLBACK_H_INCLUDED
48 #define ADC_CALLBACK_H_INCLUDED
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 /**
55  * \addtogroup asfdoc_sam0_adc_group
56  *
57  * @{
58  */
59 
60 #include <adc.h>
61 
62 /**
63  * Enum for the possible types of ADC asynchronous jobs that may be issued to
64  * the driver.
65  */
66 enum adc_job_type {
67 	/** Asynchronous ADC read into a user provided buffer */
68 	ADC_JOB_READ_BUFFER,
69 };
70 
71 /**
72  * \name Callback Management
73  * @{
74  */
75 void adc_register_callback(
76 		struct adc_module *const module,
77 		adc_callback_t callback_func,
78 		enum adc_callback callback_type);
79 
80 void adc_unregister_callback(
81 		struct adc_module *module,
82 		enum adc_callback callback_type);
83 
84 /**
85  * \brief Enables callback.
86  *
87  * Enables the callback function registered by \ref
88  * adc_register_callback. The callback function will be called from the
89  * interrupt handler when the conditions for the callback type are met.
90  *
91  * \param[in]     module Pointer to ADC software instance struct
92  * \param[in]     callback_type Callback type given by an enum
93  *
94  * \return    Status of the operation.
95  * \retval     STATUS_OK              If operation was completed
96  * \retval     STATUS_ERR_INVALID     If operation was not completed,
97  *                                    due to invalid callback_type
98  *
99  */
adc_enable_callback(struct adc_module * const module,enum adc_callback callback_type)100 static inline void adc_enable_callback(
101 		struct adc_module *const module,
102 		enum adc_callback callback_type)
103 {
104 	/* Sanity check arguments */
105 	Assert(module);
106 
107 	/* Enable callback */
108 	module->enabled_callback_mask |= (1 << callback_type);
109 
110 	/* Enable window interrupt if this is a window callback */
111 	if (callback_type == ADC_CALLBACK_WINDOW) {
112 		adc_enable_interrupt(module, ADC_INTERRUPT_WINDOW);
113 	}
114 	/* Enable overrun interrupt if error callback is registered */
115 	if (callback_type == ADC_CALLBACK_ERROR) {
116 		adc_enable_interrupt(module, ADC_INTERRUPT_OVERRUN);
117 	}
118 }
119 
120 /**
121  * \brief Disables callback.
122  *
123  * Disables the callback function registered by the \ref
124  * adc_register_callback.
125  *
126  * \param[in]     module Pointer to ADC software instance struct
127  * \param[in]     callback_type Callback type given by an enum
128  *
129  * \return    Status of the operation.
130  * \retval     STATUS_OK              If operation was completed
131  * \retval     STATUS_ERR_INVALID     If operation was not completed,
132  *                                    due to invalid callback_type
133  *
134  */
adc_disable_callback(struct adc_module * const module,enum adc_callback callback_type)135 static inline void adc_disable_callback(
136 		struct adc_module *const module,
137 		enum adc_callback callback_type)
138 {
139 	/* Sanity check arguments */
140 	Assert(module);
141 
142 	/* Disable callback */
143 	module->enabled_callback_mask &= ~(1 << callback_type);
144 
145 	/* Disable window interrupt if this is a window callback */
146 	if (callback_type == ADC_CALLBACK_WINDOW) {
147 		adc_disable_interrupt(module, ADC_INTERRUPT_WINDOW);
148 	}
149 	/* Disable overrun interrupt if this is the error callback */
150 	if (callback_type == ADC_CALLBACK_ERROR) {
151 		adc_disable_interrupt(module, ADC_INTERRUPT_OVERRUN);
152 	}
153 }
154 
155 /** @} */
156 
157 
158 /**
159  * \name Job Management
160  * @{
161  */
162 enum status_code adc_read_buffer_job(
163 		struct adc_module *const module_inst,
164 		uint16_t *buffer,
165 		uint16_t samples);
166 
167 enum status_code adc_get_job_status(
168 		struct adc_module *module_inst,
169 		enum adc_job_type type);
170 
171 void adc_abort_job(
172 		struct adc_module *module_inst,
173 		enum adc_job_type type);
174 /** @} */
175 
176 /** @} */
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 #endif /* ADC_CALLBACK_H_INCLUDED */
183