1 /*
2  * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include "hal/adc_types.h"
10 #include "hal/adc_hal_common.h"
11 #include "soc/soc_caps.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 
18 typedef struct sens_dev_t *adc_oneshot_soc_handle_t;
19 
20 typedef struct adc_oneshot_hal_cfg_t {
21     adc_unit_t unit;                 ///< ADC unit
22     adc_hal_work_mode_t work_mode;   ///< ADC work mode
23     adc_oneshot_clk_src_t clk_src;   ///< Clock source
24     uint32_t clk_src_freq_hz;        ///< Clock source frequency in hz
25 } adc_oneshot_hal_cfg_t;
26 
27 /**
28  * ADC channel configuration
29  */
30 typedef struct adc_oneshot_hal_chan_cfg_t {
31     adc_atten_t atten;               ///< ADC attenuation
32     adc_bitwidth_t bitwidth;         ///< ADC conversion result bits
33 } adc_oneshot_hal_chan_cfg_t;
34 
35 /**
36  * Context of the ADC unit, should be maintained by both the driver and the HAL.
37  */
38 typedef struct adc_oneshot_hal_ctx_t {
39     /* These should be configured by driver, dou't modify these directly in hal*/
40     adc_oneshot_soc_handle_t dev;    ///< ADC SoC layer handle
41     adc_unit_t unit;                 ///< ADC unit
42     adc_hal_work_mode_t work_mode;   ///< ADC work mode
43     adc_oneshot_hal_chan_cfg_t chan_configs[SOC_ADC_MAX_CHANNEL_NUM];    ///< ADC configurations per channel
44     adc_oneshot_clk_src_t clk_src;   ///< Clock source
45     uint32_t clk_src_freq_hz;        ///< Clock source frequency in hz
46 } adc_oneshot_hal_ctx_t;
47 
48 /**
49  * Initialise the context
50  *
51  * @param hal    ADC Oneshot Hal context
52  * @param config ADC Oneshot Hal init config
53  */
54 void adc_oneshot_hal_init(adc_oneshot_hal_ctx_t *hal, const adc_oneshot_hal_cfg_t *config);
55 
56 /**
57  * Prepare ADC Oneshot hal context
58  *
59  * @param hal    ADC Oneshot Hal context
60  * @param config ADC Oneshot Hal configuration
61  * @param chan   ADC Channel
62  */
63 void adc_oneshot_hal_channel_config(adc_oneshot_hal_ctx_t *hal, const adc_oneshot_hal_chan_cfg_t *config, adc_channel_t chan);
64 
65 /**
66  * Set ADC Oneshot mode required registers
67  *
68  * @param hal     ADC Oneshot Hal context
69  * @param channel ADC Channel
70  */
71 void adc_oneshot_hal_setup(adc_oneshot_hal_ctx_t *hal, adc_channel_t channel);
72 
73 /**
74  * Start ADC conversion in Oneshot mode and get the raw result
75  *
76  * @param hal          ADC Oneshot Hal context
77  * @param[out] out_raw ADC oneshot conversion raw result
78  *
79  * @return
80  *        - true: ADC raw result is valid
81  *        - false: ADC raw result is invalid
82  */
83 bool adc_oneshot_hal_convert(adc_oneshot_hal_ctx_t *hal, int *out_raw);
84 
85 
86 #ifdef __cplusplus
87 }
88 #endif
89