1 /*
2  * Copyright (C) 2017-2020 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     drv/adc.h
7  * @brief    Header File for ADC Driver
8  * @version  V1.0
9  * @date     08. Apr 2020
10  * @model    adc
11  ******************************************************************************/
12 
13 #ifndef _DRV_ADC_H_
14 #define _DRV_ADC_H_
15 
16 #include <stdint.h>
17 #include <stdbool.h>
18 
19 #include <drv/common.h>
20 #include <drv/dma.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /****** ADC Event *****/
27 typedef enum {
28     ADC_EVENT_CONVERT_COMPLETE = 0,      ///< All data convert completed
29     ADC_EVENT_CONVERT_HALF_DONE,         ///< Convert half done
30     ADC_EVENT_ERROR                      ///< All errors including but not limited to what converted data has not been read before the new conversion result is load to the data register
31 } csi_adc_event_t;
32 
33 typedef struct csi_adc csi_adc_t;
34 struct csi_adc {
35     csi_dev_t                dev;        ///< Hw-device info
36     void (*callback)(csi_adc_t *adc, csi_adc_event_t event, void *arg);  ///< User callback ,signaled by driver event
37     void                    *arg;        ///< User private param ,passed to user callback
38     uint32_t                *data;       ///< Data buf
39     uint32_t                 num;        ///< Data size by word
40     csi_dma_ch_t            *dma;        ///< Dma channel handle
41     csi_error_t             (*start)(csi_adc_t *adc);  ///< Start function
42     csi_error_t             (*stop)(csi_adc_t *adc);   ///< Stop function
43     csi_state_t             state;       ///< ADC current state
44     void                    *priv;
45 };
46 
47 /**
48   \brief       Initialize adc Interface. Initialize the resources needed for the adc interface
49   \param[in]   adc    ADC handle to operate
50   \param[in]   idx    ADC controller index
51   \return      Error code \ref csi_error_t
52 */
53 csi_error_t csi_adc_init(csi_adc_t *adc, uint32_t idx);
54 
55 /**
56   \brief       De-initialize adc Interface. stops operation and releases the software resources used by the interface
57   \param[in]   handle    ADC handle to operate
58   \return      None
59 */
60 void csi_adc_uninit(csi_adc_t *adc);
61 
62 /**
63   \brief       Set adc receive buffer
64   \param[in]   adc    ADC handle to operate
65   \param[in]   num    The receive data length by word.
66   \return     Error code \ref csi_error_t
67 */
68 csi_error_t csi_adc_set_buffer(csi_adc_t *adc, uint32_t *data, uint32_t num);
69 
70 /**
71   \brief       Start adc
72   \param[in]   handle    ADC handle to operate
73   \return      Error code \ref csi_error_t
74 */
75 csi_error_t csi_adc_start(csi_adc_t *adc);
76 
77 /**
78   \brief       Enable dma or interrupt, and start adc conversion
79   \param[in]   handle    ADC handle to operate
80   \return      Error code \ref csi_error_t
81 */
82 csi_error_t csi_adc_start_async(csi_adc_t *adc);
83 
84 /**
85   \brief       Stop adc
86   \param[in]   handle    ADC handle to operate
87   \return      Error code \ref csi_error_t
88 */
89 csi_error_t csi_adc_stop(csi_adc_t *adc);
90 
91 /**
92   \brief       Disable dma or interrupt, and stop adc conversion
93   \param[in]   handle    ADC handle to operate
94   \return      Error code \ref csi_error_t
95 */
96 csi_error_t csi_adc_stop_async(csi_adc_t *adc);
97 
98 /**
99   \brief       ADC channel enable
100   \param[in]   adc          ADC handle to operate
101   \param[in]   ch_id        ADC channel id
102   \param[in]   is_enable    true->enable, false->disable
103   \return      Error code \ref csi_error_t
104 */
105 csi_error_t csi_adc_channel_enable(csi_adc_t *adc, uint8_t ch_id, bool is_enable);
106 
107 /**
108   \brief       Set the ADC sampling time for the selected channel
109   \param[in]   adc          ADC handle to operate
110   \param[in]   ch_id        ADC channel id
111   \param[in]   clock_num    Channel sampling clock number
112   \return      Error code \ref csi_error_t
113 */
114 csi_error_t csi_adc_channel_sampling_time(csi_adc_t *adc, uint8_t ch_id, uint16_t clock_num);
115 
116 /**
117   \brief       Set the ADC controller sampling time
118   \param[in]   adc          ADC handle to operate
119   \param[in]   clock_num    ADC controller sampling clock number
120   \return      Error code \ref csi_error_t
121 */
122 csi_error_t csi_adc_sampling_time(csi_adc_t *adc, uint16_t clock_num);
123 
124 /**
125   \brief       Enable the continue mode of ADC
126   \param[in]   adc          ADC handle to operate
127   \param[in]   is_enable    true->enable, false->disable
128   \return      Error code \ref csi_error_t
129 */
130 csi_error_t csi_adc_continue_mode(csi_adc_t *adc, bool is_enable);
131 
132 /**
133   \brief       Set ADC frequence division
134   \param[in]   adc    ADC handle to operate
135   \param[in]   div    The division of frequence
136   \return      The actual config frequency
137 */
138 uint32_t csi_adc_freq_div(csi_adc_t *adc, uint32_t div);
139 
140 /**
141   \brief       Receiving data from ADC receiver
142   \param[in]   handle    ADC handle to operate
143   \return      If read successful, this function shall return the result of convert value
144                otherwise, the function shall return error code
145 */
146 int32_t csi_adc_read(csi_adc_t *adc);
147 
148 /**
149   \brief       Receiving converted voltage(unit:mV) from ADC receiver
150   \param[in]   handle    ADC handle to operate
151   \return      If read successful, this function shall return the voltage(unit:mV)
152                otherwise, the function shall return error code
153 */
154 int32_t csi_adc_read_voltage(csi_adc_t *adc);
155 
156 /**
157   \brief       Get ADC state
158   \param[in]   adc      ADC handle to operate
159   \param[in]   state    ADC state
160   \return      Error code \ref csi_error_t
161 */
162 csi_error_t csi_adc_get_state(csi_adc_t *adc, csi_state_t *state);
163 
164 /**
165   \brief       Get ADC range
166   \param[in]   adc      ADC handle to operate
167   \param[in]   ch_id    Channel id
168   \param[out]  range    ADC range
169   \return      Error code \ref csi_error_t
170 */
171 csi_error_t csi_adc_get_range(csi_adc_t *adc, uint8_t ch_id, uint32_t *range);
172 
173 /**
174   \brief       Attach the callback handler to adc
175   \param[in]   adc         Operate handle
176   \param[in]   callback    Callback function
177   \param[in]   arg         User can define it by himself as callback's param
178   \return      Error code \ref csi_error_t
179 */
180 csi_error_t csi_adc_attach_callback(csi_adc_t *adc, void *callback, void *arg);
181 
182 /**
183   \brief       Detach the callback handler
184   \param[in]   adc    Operate handle
185   \return      None
186 */
187 void        csi_adc_detach_callback(csi_adc_t *adc);
188 
189 /**
190   \brief       Link DMA channel to adc device
191   \param[in]   adc    ADC handle to operate
192   \param[in]   dma    The DMA channel handle for send, when it is NULL means to unlink the channel
193   \return      Error code \ref csi_error_t
194 */
195 csi_error_t csi_adc_link_dma(csi_adc_t *adc, csi_dma_ch_t *dma);
196 
197 /**
198   \brief       Enable adc low power mode
199   \param[in]   adc    ADC handle to operate
200   \return      Error code \ref csi_error_t
201 */
202 csi_error_t csi_adc_enable_pm(csi_adc_t *adc);
203 
204 /**
205   \brief       Disable adc low power mode
206   \param[in]   adc    ADC handle to operate
207   \return      None
208 */
209 void csi_adc_disable_pm(csi_adc_t *adc);
210 
211 #ifdef __cplusplus
212 }
213 #endif
214 
215 #endif /* _DRV_ADC_H_ */
216