adc.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-05-07 aozima the first version
  9. * 2018-11-16 Ernest Chen add finsh command and update adc function
  10. * 2022-05-11 Stanley Lwin add finsh voltage conversion command
  11. */
  12. #ifndef __ADC_H__
  13. #define __ADC_H__
  14. #include <rtthread.h>
  15. #define RT_ADC_INTERN_CH_TEMPER (-1)
  16. #define RT_ADC_INTERN_CH_VREF (-2)
  17. #define RT_ADC_INTERN_CH_VBAT (-3)
  18. struct rt_adc_device;
  19. struct rt_adc_ops
  20. {
  21. rt_err_t (*enabled)(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled);
  22. rt_err_t (*convert)(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value);
  23. rt_uint8_t (*get_resolution)(struct rt_adc_device *device);
  24. rt_int16_t (*get_vref) (struct rt_adc_device *device);
  25. };
  26. struct rt_adc_device
  27. {
  28. struct rt_device parent;
  29. const struct rt_adc_ops *ops;
  30. };
  31. typedef struct rt_adc_device *rt_adc_device_t;
  32. typedef enum
  33. {
  34. RT_ADC_CMD_ENABLE = RT_DEVICE_CTRL_BASE(ADC) + 1,
  35. RT_ADC_CMD_DISABLE = RT_DEVICE_CTRL_BASE(ADC) + 2,
  36. RT_ADC_CMD_GET_RESOLUTION = RT_DEVICE_CTRL_BASE(ADC) + 3, /* get the resolution in bits */
  37. RT_ADC_CMD_GET_VREF = RT_DEVICE_CTRL_BASE(ADC) + 4, /* get reference voltage */
  38. } rt_adc_cmd_t;
  39. rt_err_t rt_hw_adc_register(rt_adc_device_t adc,const char *name, const struct rt_adc_ops *ops, const void *user_data);
  40. rt_uint32_t rt_adc_read(rt_adc_device_t dev, rt_int8_t channel);
  41. rt_err_t rt_adc_enable(rt_adc_device_t dev, rt_int8_t channel);
  42. rt_err_t rt_adc_disable(rt_adc_device_t dev, rt_int8_t channel);
  43. rt_int16_t rt_adc_voltage(rt_adc_device_t dev, rt_int8_t channel);
  44. #endif /* __ADC_H__ */