1 /**
2  * \file
3  *
4  * \brief ADC related functionality declaration.
5  *
6  * Copyright (c) 2016-2018 Microchip Technology Inc. and its subsidiaries.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Subject to your compliance with these terms, you may use Microchip
13  * software and any derivatives exclusively with Microchip products.
14  * It is your responsibility to comply with third party license terms applicable
15  * to your use of third party software (including open source software) that
16  * may accompany Microchip software.
17  *
18  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19  * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20  * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22  * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23  * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24  * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25  * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
26  * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27  * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28  * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29  *
30  * \asf_license_stop
31  *
32  */
33 
34 #ifndef _HPL_ADC_DMA_H_INCLUDED
35 #define _HPL_ADC_DMA_H_INCLUDED
36 
37 /**
38  * \addtogroup HPL ADC
39  *
40  * \section hpl_dma_adc_rev Revision History
41  * - v1.0.0 Initial Release
42  *
43  *@{
44  */
45 
46 #include <hpl_adc_sync.h>
47 #include <hpl_irq.h>
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /**
54  * \brief ADC device structure
55  *
56  * The ADC device structure forward declaration.
57  */
58 struct _adc_dma_device;
59 
60 /**
61  * \brief ADC callback types
62  */
63 enum _adc_dma_callback_type { ADC_DMA_DEVICE_COMPLETE_CB, ADC_DMA_DEVICE_ERROR_CB };
64 
65 /**
66  * \brief ADC interrupt callbacks
67  */
68 struct _adc_dma_callbacks {
69 	void (*complete)(struct _adc_dma_device *device, const uint16_t data);
70 	void (*error)(struct _adc_dma_device *device);
71 };
72 
73 /**
74  * \brief ADC descriptor device structure
75  */
76 struct _adc_dma_device {
77 	struct _adc_dma_callbacks adc_dma_cb;
78 	struct _irq_descriptor    irq;
79 	void *                    hw;
80 };
81 
82 /**
83  * \name HPL functions
84  */
85 //@{
86 /**
87  * \brief Initialize synchronous ADC
88  *
89  * This function does low level ADC configuration.
90  *
91  * param[in] device The pointer to ADC device instance
92  * param[in] hw The pointer to hardware instance
93  *
94  * \return Initialization status
95  */
96 int32_t _adc_dma_init(struct _adc_dma_device *const device, void *const hw);
97 
98 /**
99  * \brief Deinitialize ADC
100  *
101  * \param[in] device The pointer to ADC device instance
102  */
103 void _adc_dma_deinit(struct _adc_dma_device *const device);
104 
105 /**
106  * \brief Enable ADC peripheral
107  *
108  * \param[in] device   The pointer to ADC device instance
109  * \param[in] channel  Channel number
110  */
111 void _adc_dma_enable_channel(struct _adc_dma_device *const device, const uint8_t channel);
112 
113 /**
114  * \brief Disable ADC peripheral
115  *
116  * \param[in] device   The pointer to ADC device instance
117  * \param[in] channel  Channel number
118  */
119 void _adc_dma_disable_channel(struct _adc_dma_device *const device, const uint8_t channel);
120 
121 /**
122  * \brief Return address of ADC DMA source
123  *
124  * \param[in] device The pointer to ADC device instance
125  *
126  * \return ADC DMA source address
127  */
128 uint32_t _adc_get_source_for_dma(struct _adc_dma_device *const device);
129 
130 /**
131  * \brief Retrieve ADC conversion data size
132  *
133  * \param[in] device The pointer to ADC device instance
134  *
135  * \return The data size in bytes
136  */
137 uint8_t _adc_dma_get_data_size(const struct _adc_dma_device *const device);
138 
139 /**
140  * \brief Check if conversion is done
141  *
142  * \param[in] device   The pointer to ADC device instance
143  *
144  * \return The status of conversion
145  * \retval true The conversion is done
146  * \retval false The conversion is not done
147  */
148 bool _adc_dma_is_conversion_done(const struct _adc_dma_device *const device);
149 
150 /**
151  * \brief Make conversion
152  *
153  * \param[in] device The pointer to ADC device instance
154  */
155 void _adc_dma_convert(struct _adc_dma_device *const device);
156 
157 /**
158  * \brief Set reference source
159  *
160  * \param[in] device The pointer to ADC device instance
161  * \param[in] reference A reference source to set
162  */
163 void _adc_dma_set_reference_source(struct _adc_dma_device *const device, const adc_reference_t reference);
164 
165 /**
166  * \brief Set resolution
167  *
168  * \param[in] device The pointer to ADC device instance
169  * \param[in] resolution A resolution to set
170  */
171 void _adc_dma_set_resolution(struct _adc_dma_device *const device, const adc_resolution_t resolution);
172 
173 /**
174  * \brief Set ADC input source of a channel
175  *
176  * \param[in] device    The pointer to ADC device instance
177  * \param[in] pos_input A positive input source to set
178  * \param[in] neg_input A negative input source to set
179  * \param[in] channel   Channel number
180  */
181 void _adc_dma_set_inputs(struct _adc_dma_device *const device, const adc_pos_input_t pos_input,
182                          const adc_neg_input_t neg_input, const uint8_t channel);
183 
184 /**
185  * \brief Set conversion mode
186  *
187  * \param[in] device The pointer to ADC device instance
188  * \param[in] mode A conversion mode to set
189  */
190 void _adc_dma_set_conversion_mode(struct _adc_dma_device *const device, const enum adc_conversion_mode mode);
191 
192 /**
193  * \brief Set differential mode
194  *
195  * \param[in] device  The pointer to ADC device instance
196  * \param[in] channel Channel number
197  * \param[in] mode    A differential mode to set
198  */
199 void _adc_dma_set_channel_differential_mode(struct _adc_dma_device *const device, const uint8_t channel,
200                                             const enum adc_differential_mode mode);
201 
202 /**
203  * \brief Set gain
204  *
205  * \param[in] device  The pointer to ADC device instance
206  * \param[in] channel Channel number
207  * \param[in] gain    A gain to set
208  */
209 void _adc_dma_set_channel_gain(struct _adc_dma_device *const device, const uint8_t channel, const adc_gain_t gain);
210 
211 /**
212  * \brief Set window mode
213  *
214  * \param[in] device The pointer to ADC device instance
215  * \param[in] mode A mode to set
216  */
217 void _adc_dma_set_window_mode(struct _adc_dma_device *const device, const adc_window_mode_t mode);
218 
219 /**
220  * \brief Set thresholds
221  *
222  * \param[in] device        The pointer to ADC device instance
223  * \param[in] low_threshold A lower thresholds to set
224  * \param[in] up_threshold  An upper thresholds to set
225  */
226 void _adc_dma_set_thresholds(struct _adc_dma_device *const device, const adc_threshold_t low_threshold,
227                              const adc_threshold_t up_threshold);
228 
229 /**
230  * \brief Retrieve threshold state
231  *
232  * \param[in] device The pointer to ADC device instance
233  * \param[out] state The threshold state
234  */
235 void _adc_dma_get_threshold_state(const struct _adc_dma_device *const device, adc_threshold_status_t *const state);
236 
237 //@}
238 
239 #ifdef __cplusplus
240 }
241 #endif
242 /**@}*/
243 #endif /* _HPL_ADC_DMA_H_INCLUDED */
244