1 #ifndef _BFLB_ADC_H
2 #define _BFLB_ADC_H
3 
4 #include "bflb_core.h"
5 
6 /** @addtogroup LHAL
7   * @{
8   */
9 
10 /** @addtogroup ADC
11   * @{
12   */
13 
14 /** @defgroup ADC_CHANNEL adc channel definition
15   * @{
16   */
17 #define ADC_CHANNEL_0         0
18 #define ADC_CHANNEL_1         1
19 #define ADC_CHANNEL_2         2
20 #define ADC_CHANNEL_3         3
21 #define ADC_CHANNEL_4         4
22 #define ADC_CHANNEL_5         5
23 #define ADC_CHANNEL_6         6
24 #define ADC_CHANNEL_7         7
25 #define ADC_CHANNEL_8         8
26 #define ADC_CHANNEL_9         9
27 #define ADC_CHANNEL_10        10
28 #define ADC_CHANNEL_11        11
29 #define ADC_CHANNEL_DACA      12
30 #define ADC_CHANNEL_DACB      13
31 #define ADC_CHANNEL_TSEN_P    14
32 #define ADC_CHANNEL_TSEN_N    15
33 #define ADC_CHANNEL_VREF      16
34 #define ADC_CHANNEL_VABT_HALF 18
35 #define ADC_CHANNEL_GND       23
36 /**
37   * @}
38   */
39 
40 /** @defgroup ADC_CLK_DIV adc clock divison definition
41   * @{
42   */
43 #define ADC_CLK_DIV_4  1
44 #define ADC_CLK_DIV_8  2
45 #define ADC_CLK_DIV_12 3
46 #define ADC_CLK_DIV_16 4
47 #define ADC_CLK_DIV_20 5
48 #define ADC_CLK_DIV_24 6
49 #define ADC_CLK_DIV_32 7
50 /**
51   * @}
52   */
53 
54 /** @defgroup ADC_RESOLUTION adc resolution definition
55   * @{
56   */
57 #define ADC_RESOLUTION_12B 0
58 #define ADC_RESOLUTION_14B 2
59 #define ADC_RESOLUTION_16B 4
60 /**
61   * @}
62   */
63 
64 /** @defgroup ADC_VREF adc reference select definition
65   * @{
66   */
67 #define ADC_VREF_3P2V 0
68 #define ADC_VREF_2P0V 1
69 /**
70   * @}
71   */
72 
73 /** @defgroup ADC_TSEN_MOD adc tsen mode definition
74   * @{
75   */
76 #define ADC_TSEN_MOD_INTERNAL_DIODE 0
77 #define ADC_TSEN_MOD_EXTERNAL_DIODE 1
78 /**
79   * @}
80   */
81 
82 /** @defgroup ADC_INTSTS adc interrupt status definition
83   * @{
84   */
85 #define ADC_INTSTS_NEG_SATURATION (1 << 0)
86 #define ADC_INTSTS_POS_SATURATION (1 << 1)
87 #define ADC_INTSTS_FIFO_UNDERRUN  (1 << 2)
88 #define ADC_INTSTS_FIFO_OVERRUN   (1 << 3)
89 #define ADC_INTSTS_ADC_READY      (1 << 4)
90 /**
91   * @}
92   */
93 
94 /** @defgroup ADC_INTCLR adc interrupt clear definition
95   * @{
96   */
97 #define ADC_INTCLR_NEG_SATURATION (1 << 0)
98 #define ADC_INTCLR_POS_SATURATION (1 << 1)
99 #define ADC_INTCLR_FIFO_UNDERRUN  (1 << 2)
100 #define ADC_INTCLR_FIFO_OVERRUN   (1 << 3)
101 #define ADC_INTCLR_ADC_READY      (1 << 4)
102 /**
103   * @}
104   */
105 
106 /**
107  * @brief ADC configuration structure
108  *
109  * @param clk_div                ADC clock divison, use @ref ADC_CLK_DIV
110  * @param scan_conv_mode         ADC scan mode enable
111  * @param continuous_conv_mode   ADC continuous conversion enable
112  * @param differential_mode      ADC differential mode enable
113  * @param resolution             ADC resolution, use @ref ADC_RESOLUTION
114  * @param vref                   ADC reference select, use @ref ADC_VREF
115  */
116 struct bflb_adc_config_s {
117     uint8_t clk_div;
118     uint8_t scan_conv_mode;
119     uint8_t continuous_conv_mode;
120     uint8_t differential_mode;
121     uint8_t resolution;
122     uint8_t vref;
123 };
124 
125 /**
126  * @brief ADC channel select
127  *
128  * @param pos_chan         Select ADC positive input in none-scan mode
129  * @param neg_chan         Select ADC negative input in none-scan mode
130  */
131 struct bflb_adc_channel_s {
132     uint8_t pos_chan;
133     uint8_t neg_chan;
134 };
135 
136 /**
137  * @brief ADC result select
138  *
139  * @param pos_chan         ADC positive channel
140  * @param neg_chan         ADC negative channel
141  * @param value            ADC value
142  * @param millivolt        ADC voltage result
143  */
144 struct bflb_adc_result_s {
145     int8_t pos_chan;
146     int8_t neg_chan;
147     int32_t value;
148     int32_t millivolt;
149 };
150 
151 #ifdef __cplusplus
152 extern "C" {
153 #endif
154 
155 /**
156  * @brief Initialize adc.
157  *
158  * @param [in] dev device handle
159  * @param [in] config pointer to save adc configuration
160  */
161 void bflb_adc_init(struct bflb_device_s *dev, const struct bflb_adc_config_s *config);
162 
163 /**
164  * @brief Deinitialize adc.
165  *
166  * @param [in] dev device handle
167  */
168 void bflb_adc_deinit(struct bflb_device_s *dev);
169 
170 /**
171  * @brief Enable adc rx dma.
172  *
173  * @param [in] dev device handle
174  * @param [in] enable true means enable, otherwise disable.
175  */
176 void bflb_adc_link_rxdma(struct bflb_device_s *dev, bool enable);
177 
178 /**
179  * @brief Config adc channels to sample.
180  *
181  * @param [in] dev device handle
182  * @param [in] chan pointer to the channel configurations.
183  * @param [in] channels pair number of channels
184  * @return Zero on success; a negated errno value on failure
185  */
186 int bflb_adc_channel_config(struct bflb_device_s *dev, struct bflb_adc_channel_s *chan, uint8_t channels);
187 
188 /**
189  * @brief Start adc conversion
190  *
191  * @param [in] dev device handle
192  */
193 void bflb_adc_start_conversion(struct bflb_device_s *dev);
194 
195 /**
196  * @brief Stop adc conversion
197  *
198  * @param [in] dev device handle
199  */
200 void bflb_adc_stop_conversion(struct bflb_device_s *dev);
201 
202 /**
203  * @brief Get adc number of completed conversions
204  *
205  * @param [in] dev device handle
206  * @return number of completed conversions
207  */
208 uint8_t bflb_adc_get_count(struct bflb_device_s *dev);
209 
210 /**
211  * @brief Read adc conversion value
212  *
213  * @param [in] dev device handle
214  * @return conversion value
215  */
216 uint32_t bflb_adc_read_raw(struct bflb_device_s *dev);
217 
218 /**
219  * @brief Enable or disable adc conversion completion interrupt.
220  * Triggerring when a channel conversion is completed.
221  *
222  * @param [in] dev device handle
223  * @param [in] mask true means disable, false means enable
224  */
225 void bflb_adc_rxint_mask(struct bflb_device_s *dev, bool mask);
226 
227 /**
228  * @brief Enable or disable adc error interrupt.
229  *
230  * @param [in] dev device handle
231  * @param [in] mask true means disable, false means enable
232  */
233 void bflb_adc_errint_mask(struct bflb_device_s *dev, bool mask);
234 
235 /**
236  * @brief Get adc interrupt instatus.
237  *
238  * @param [in] dev device handle
239  * @return interrupt instatus value, use @ref ADC_INTSTS
240  */
241 uint32_t bflb_adc_get_intstatus(struct bflb_device_s *dev);
242 
243 /**
244  * @brief Clear adc interrupt instatus.
245  *
246  * @param [in] dev device handle
247  * @param [in] int_clear interrupt clear value, use @ref ADC_INTCLR
248  */
249 void bflb_adc_int_clear(struct bflb_device_s *dev, uint32_t int_clear);
250 
251 /**
252  * @brief Parse adc conversion value into millivolt and actual numerical value.
253  *
254  * @param [in] dev device handle
255  * @param [in] buffer pointer to adc original value from bflb_adc_read_raw api
256  * @param [out] result pointer to save parse result
257  * @param [in] count count to parse
258  */
259 void bflb_adc_parse_result(struct bflb_device_s *dev, uint32_t *buffer, struct bflb_adc_result_s *result, uint16_t count);
260 
261 /**
262  * @brief Initialize adc temperature sensor
263  *
264  * @param [in] dev device handle
265  * @param [in] tsen_mod temperature sensor mode, use @ref ADC_TSEN_MOD
266  */
267 void bflb_adc_tsen_init(struct bflb_device_s *dev, uint8_t tsen_mod);
268 
269 /**
270  * @brief Get adc temperature
271  *
272  * @param [in] dev device handle
273  * @return temperature
274  */
275 float bflb_adc_tsen_get_temp(struct bflb_device_s *dev);
276 
277 /**
278  * @brief Enable adc vbat power.
279  *
280  * @param [in] dev device handle
281  */
282 void bflb_adc_vbat_enable(struct bflb_device_s *dev);
283 
284 /**
285  * @brief Disable adc vbat power.
286  *
287  * @param [in] dev device handle
288  */
289 void bflb_adc_vbat_disable(struct bflb_device_s *dev);
290 
291 #ifdef __cplusplus
292 }
293 #endif
294 
295 /**
296   * @}
297   */
298 
299 /**
300   * @}
301   */
302 
303 #endif