1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include "aos/hal/adc.h"
6 #include "hal_trace.h"
7 #include "hal_gpadc.h"
8
__hal_adc_irqhandler(uint16_t irq_val,HAL_GPADC_MV_T volt)9 static void __hal_adc_irqhandler(uint16_t irq_val, HAL_GPADC_MV_T volt)
10 {
11 //printf("%s irq_val=%d, volt=%d\n", __FUNCTION__, irq_val, volt);
12 }
13
__hal_adc_port2chan(uint8_t port)14 static enum HAL_GPADC_CHAN_T __hal_adc_port2chan(uint8_t port)
15 {
16 return (port<3) ? port+HAL_GPADC_CHAN_2 : HAL_GPADC_CHAN_QTY;
17 }
18
19 /**
20 * Initialises an ADC interface, Prepares an ADC hardware interface for sampling
21 *
22 * @param[in] adc the interface which should be initialised
23 *
24 * @return 0 : on success, EIO : if an error occurred with any step
25 */
hal_adc_init(adc_dev_t * adc)26 int32_t hal_adc_init(adc_dev_t *adc)
27 {
28 //printf("%s port=%d, sampling_cycle=%d, priv=%p\n", __FUNCTION__, adc->port, adc->config.sampling_cycle, adc->priv);
29 if (adc->config.sampling_cycle) {
30 hal_gpadc_open(__hal_adc_port2chan(adc->port), HAL_GPADC_ATP_NULL, NULL);
31 } else {
32 hal_gpadc_open(__hal_adc_port2chan(adc->port), HAL_GPADC_ATP_ONESHOT, __hal_adc_irqhandler);
33 }
34 return 0;
35 }
36
37 /**
38 * Takes a single sample from an ADC interface
39 *
40 * @param[in] adc the interface which should be sampled
41 * @param[out] output pointer to a variable which will receive the sample
42 * @param[in] timeout ms timeout
43 *
44 * @return 0 : on success, EIO : if an error occurred with any step
45 */
hal_adc_value_get(adc_dev_t * adc,uint32_t * output,uint32_t timeout)46 int32_t hal_adc_value_get(adc_dev_t *adc, uint32_t *output, uint32_t timeout)
47 {
48 HAL_GPADC_MV_T volt = HAL_GPADC_BAD_VALUE;
49
50 hal_gpadc_open(__hal_adc_port2chan(adc->port), HAL_GPADC_ATP_ONESHOT, NULL);
51 osDelay(1);
52
53 if (output && hal_gpadc_get_volt(__hal_adc_port2chan(adc->port), &volt)) {
54 *output = volt;
55 /*printf("%s port=%d, sampling_cycle=%d, priv=%p => output=%d\r\n", __FUNCTION__, adc->port, adc->config.sampling_cycle, adc->priv, *output);*/
56 }
57 return 0;
58 }
59
60 /**
61 * De-initialises an ADC interface, Turns off an ADC hardware interface
62 *
63 * @param[in] adc the interface which should be de-initialised
64 *
65 * @return 0 : on success, EIO : if an error occurred with any step
66 */
hal_adc_finalize(adc_dev_t * adc)67 int32_t hal_adc_finalize(adc_dev_t *adc)
68 {
69 //printf("%s port=%d, sampling_cycle=%d, priv=%p\n", __FUNCTION__, adc->port, adc->config.sampling_cycle, adc->priv);
70 hal_gpadc_close(__hal_adc_port2chan(adc->port));
71 return 0;
72 }
73