• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * Description: ADC driver header file.
16  */
17 
18 #ifndef __ADC_DRV_H__
19 #define __ADC_DRV_H__
20 
21 #include <hi_adc.h>
22 #include <hi_mdm_types.h>
23 #include <hi3861_platform_base.h>
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif  /* __cplusplus */
30 
31 #ifdef ADC_DEBUG
32 #define adc_print(fmt...)       \
33     do {                        \
34         printf(fmt);            \
35         printf("\n"); \
36     } while (0)
37 #else
38 #define adc_print(fmt...)
39 #endif
40 
41 #define LS_ADC_CLK_DIV1_REG     CLDO_CTL_CLK_DIV1_REG
42 #define LS_ADC_CLK_DIV1_OFFSET  9
43 
44 #define HI_LS_ADC_REG_BASE      0x40070000 /* LD ADC base address */
45 #define REG_ADC_CFG      (HI_LS_ADC_REG_BASE + 0x00)
46 #define REG_ADC_FIFO_CFG (HI_LS_ADC_REG_BASE + 0x04)
47 #define REG_ADC_IMSC     (HI_LS_ADC_REG_BASE + 0x08)
48 #define REG_ADC_CR       (HI_LS_ADC_REG_BASE + 0x0C)
49 #define REG_ADC_SR       (HI_LS_ADC_REG_BASE + 0x10)
50 #define REG_ADC_RIS      (HI_LS_ADC_REG_BASE + 0x14)
51 #define REG_ADC_MIS      (HI_LS_ADC_REG_BASE + 0x18)
52 #define REG_ADC_START    (HI_LS_ADC_REG_BASE + 0x1C)
53 #define REG_ADC_STOP     (HI_LS_ADC_REG_BASE + 0x20)
54 #define REG_ADC_DR       (HI_LS_ADC_REG_BASE + 0x24)
55 #define REG_ADC_CTRL     (HI_LS_ADC_REG_BASE + 0x28)
56 #define REG_ADC_EN       (HI_LS_ADC_REG_BASE + 0x2C)
57 
58 #define ADC_INT_FIFO_WATER_LINE (1 << 1)
59 #define ADC_INT_FIFO_OVER_FLOW  (1 << 0)
60 
61 #define ADC_SR_RNE (1 << 0) /* FIFO not empty flag: 0:empty 1:not empty */
62 #define ADC_SR_RFF (1 << 1) /* FIFO full flag 0:not full 1:full */
63 #define ADC_SR_BSY (1 << 2) /* ADC busy flag 0:idle 1:busy */
64 
65 #define ADC_SCAN_START  1
66 #define ADC_SCAN_STOP   1
67 #define ADC_POWER_ON    0
68 #define ADC_POWER_OFF   1
69 #define ADC_ISR_DISABLE 0
70 #define ADC_DATA_BIT_WIDTH 12
71 
72 /*
73  * The longest time to get 1 data is ((0xfff+(18*8)+3)*334)ns
74  * The unit of this cnt is about 5us
75  */
76 #define ADC_PER_DATA_TIMEOUT_CNT 500
77 #define ADC_LOOP_DELAY_US        5
78 
79 typedef void (*adc_clken_callback)(hi_void);
80 
81 /**
82 * @ingroup  iot_ls_adc
83 * @brief  Callback function of ADC read data done.
84 CNcomment:ADC读数完成后回调函数的类型。CNend
85 *
86 * @par 描述:
87 *           Callback function of ADC read data done.
88 CNcomment:ADC读数完成后回调函数的类型。CNend
89 *
90 * @attention Can NOT be called in the interruption.
91 CNcomment:在其它中断响应中无法被调用。CNend
92 * @param  data_buf [IN] type #hi_u16 *,received data.
93 CNcomment:回调入参,读到的数据。CNend
94 * @param  length   [IN] type #hi_u32,length of the received data.
95 CNcomment:读取数据的长度。CNend
96 *
97 * @retval None
98 * @par 依赖:
99 *            @li hi_adc.h:Describes ADC APIs.
100 CNcomment:文件用于描述ADC相关接口。CNend
101 * @see None
102 */
103 typedef hi_void(*adc_read_cb) (const hi_u16 *data_buf, hi_u32 length);
104 
105 typedef struct {
106     hi_u32 ch_vld : 8;
107     hi_u32 equ_model : 2;
108     hi_u32 reserved0 : 2;
109     hi_u32 delay_cnt : 12;
110     hi_u32 cur_bais : 2;
111     hi_u32 reserved1 : 6;
112 } adc_cfg_reg_s;
113 
114 typedef struct {
115     volatile hi_u32 buf_pos; /* offset of data buffer */
116     hi_u32 is_init;          /* init flag */
117     hi_u32 buf_length;       /* length of data buffer length */
118     hi_u16 *data_buf;        /* point of data buffer */
119     adc_read_cb adc_cb;      /* callback function after read data finish */
120 } adc_data;
121 
122 
123 /**
124  * @ingroup iot_ls_adc
125  *
126  * Settings: ADC RX threshold. CNcomment:设置参数:ADC接收水线。CNend
127  */
128 typedef enum {
129     HI_ADC_FIFO_WATER_LINE_127,
130     HI_ADC_FIFO_WATER_LINE_124,
131     HI_ADC_FIFO_WATER_LINE_64,
132     HI_ADC_FIFO_WATER_LINE_32,
133     HI_ADC_FIFO_WATER_LINE_16,
134     HI_ADC_FIFO_WATER_LINE_8,
135     HI_ADC_FIFO_WATER_LINE_4,
136     HI_ADC_FIFO_WATER_LINE_1,
137 } hi_adc_fifo_water_line;
138 
139 /**
140  * @ingroup iot_ls_adc
141  *
142  * ADC settings. CNcomment:ADC设置参数。CNend
143  */
144 typedef struct {
145     hi_u16 delay_cnt;                     /**< Countings from config to start collect, [0xF, 0xFF].
146                                            CNcomment:从配置采样到启动采样的延时时间计数,其值需在
147                                            0xF~0xFFF之间 CNend */
148     hi_adc_equ_model_sel equ_model_sel; /**< Average algorithm mode.
149                                            CNcomment:平均算法模式 CNend */
150     hi_adc_cur_bais cur_bais;           /**< ADC Analog power control.
151                                            CNcomment:模拟电源控制 CNend */
152     hi_adc_fifo_water_line fifo_water_line;  /**< FIFO threshold interruption settings. Not use in sync rx mode.
153                                                 CNcomment:FIFO水线中断设置,同步读取时无需设置 CNend */
154 } hi_adc_cfg;
155 
156 /**
157  * @ingroup iot_ls_adc
158  *
159  * Data format in 16bit, [0:11] are data bits, [0:2] are decimal fractions, [12:24] are channel number, [15] reserved.
160  CNcomment:读取到的ADC数据格式,[0:11]是数据位,其中[0:1]是小数位。[12:14]是通道编号,
161  [15]保留。CNend
162  */
163 typedef struct {
164     hi_u16 val : 12;          /**< Data bit, [0:1] are decimal fractions.
165                                CNcomment:数据位,其中[0:1]是小数位 CNend */
166     hi_u16 ch_id : 3;         /**< Channel number. CNcomment:通道编号 CNend */
167     hi_u16 reserved : 1;
168 } hi_adc_data_format;
169 
170 /**
171 * @ingroup  iot_ls_adc
172 * @brief  Initializes the data acquisition control module.
173 CNcomment:ADC模块初始化。CNend
174 *
175 * @par 描述:
176 *           Initializes the data acquisition control module, apply for interrupt, enable the module.
177 CNcomment:ADC模块初始化,对ADC模块初始化,申请中断,使能ADC模块。CNend
178 *
179 * @retval #HI_ERR_SUCCESS  Success.
180 * @retval #Other          Failure. See hi_errno.h for details.
181 * @see  hi_adc_shutdown。
182 */
183 hi_u32 hi_adc_init(hi_void);
184 
185 /**
186 * @ingroup  iot_ls_adc
187 * @brief   ADC settings. CNcomment:设置ADC参数。CNend
188 *
189 * @par 描述:
190 *          Set ADC parameters. CNcomment:设置ADC参数。CNend
191 *
192 * @attention None
193 * @param adc_cfg           [IN] type #hi_adc_cfg,ADC settings. CNcomment:ADC模块相关参数。CNend
194 *
195 * @retval #HI_ERR_SUCCESS  Success.
196 * @retval #Other          Failure. See hi_errno.h for details.
197 * @par 依赖:
198 *            @li hi_adc.h:Describes ADC APIs.
199 CNcomment:文件用于描述ADC相关接口。CNend
200 * @see  hi_adc_set_basic_info。
201 */
202 hi_u32 hi_adc_set_basic_info(const hi_adc_cfg *adc_cfg);
203 
204 /**
205 * @ingroup  iot_ls_adc
206 * @brief   Read ADC received data synchronously. CNcomment:同步读取ADC数据。CNend
207 *
208 * @par 描述:
209 *          Read ADC received data synchronously. CNcomment:同步读取ADC数据。CNend
210 *
211 * @attention None
212 * @param  channel          [IN] type #hi_u8 ,channel to be read. CNcomment:要使能的channel。CNend
213 * @param  data_buf         [IN] type #hi_u16 * ,data buffer to store the data.
214 CNcomment:读取的ADC数据保存buf地址。CNend
215 * @param  get_len          [IN] type #hi_u32 ,length to read, do NOT longer than the buffer size.
216 CNcomment:要读的数据长度,数据存入data_buf,不能大于data_buf长度。CNend
217 *
218 * @retval #HI_ERR_SUCCESS  Success.
219 * @retval #Other          Failure. See hi_errno.h for details.
220 * @par 依赖:
221 *            @li hi_adc.h:Describes ADC APIs.
222 CNcomment:文件用于描述ADC相关接口。CNend
223 * @see  hi_adc_get_data。
224 */
225 hi_u32 hi_adc_read_sync(hi_u8 channel, hi_u16 *data_buf, hi_u32 get_len);
226 
227 /**
228 * @ingroup  iot_ls_adc
229 * @brief   Asynchronously reading ADC data. CNcomment:异步读取ADC数据。CNend
230 *
231 * @par 描述:
232 *          Asynchronously reading ADC data. CNcomment:异步读取ADC数据。CNend
233 *
234 * @attention None
235 * @param  channel          [IN] type #hi_u8 ,channel to be read. CNcomment:要使能的channel。CNend
236 * @param  data_buf         [IN] type #hi_u16 * ,data buffer to store the data.
237 CNcomment:读取的ADC数据保存buf地址。CNend
238 * @param  get_len          [IN] type #hi_u32 ,length to read, do NOT longer than the buffer size.
239 CNcomment:要读的数据长度,数据存入data_buf,不能大于data_buf长度。CNend
240 * @param  cb               [IN] type #adc_read_cb ,Callback function when read done.
241 CNcomment:ADC读数完成后回调函数。CNend
242 *
243 * @retval #HI_ERR_SUCCESS  Success.
244 * @retval #Other          Failure. See hi_errno.h for details.
245 * @par 依赖:
246 *            @li hi_adc.h:Describes ADC APIs.
247 CNcomment:文件用于描述ADC相关接口。CNend
248 * @see  hi_adc_scan_stop。
249 */
250 hi_u32 hi_adc_read_async(hi_u8 channel, hi_u16 *data_buf, hi_u32 get_len, adc_read_cb cb);
251 
252 /**
253 * @ingroup  iot_ls_adc
254 * @brief  Deinitialize ADC module. CNcomment:关闭ADC模块。CNend
255 *
256 * @par 描述:
257 *           Deinitialize ADC module, clear up FIFO, disable interruption, shutdown power.
258 CNcomment:关闭ADC模块,清空FIFO,关闭中断,关闭电源。CNend
259 *
260 * @attention None
261 * @param None
262 * @retval None
263 * @par 依赖:
264 *            @li hi_adc.h:Describes ADC APIs.
265 CNcomment:文件用于描述ADC相关接口。CNend
266 * @see  hi_adc_init。
267 */
268 hi_void hi_adc_deinit(hi_void);
269 
270 #ifdef __cplusplus
271 #if __cplusplus
272 }
273 #endif
274 #endif  /* __cplusplus */
275 
276 #endif
277