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