drv_adc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2019 Winner Microelectronics Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-11-23 fanwenl 1st version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "wm_io.h"
  13. #include "wm_adc.h"
  14. #include "wm_gpio_afsel.h"
  15. #include "drv_adc.h"
  16. #ifdef BSP_USING_ADC
  17. #if defined(USING_ADC_CH1) || defined(USING_ADC_CH2) || defined(USING_ADC_CH3) || defined(USING_ADC_CH4) || \
  18. defined(USING_ADC_CH5) || defined(USING_ADC_CH6) || defined(USING_ADC_CH7) || defined(USING_ADC_CH8)
  19. static rt_err_t wm_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  20. {
  21. if (channel < 1 || channel > 8)
  22. return RT_ERROR;
  23. if (enabled == RT_TRUE)
  24. {
  25. tls_adc_start_with_cpu(channel - 1);
  26. }
  27. else
  28. {
  29. tls_adc_stop(0);
  30. }
  31. return RT_EOK;
  32. }
  33. static rt_err_t wm_adc_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  34. {
  35. if (channel < 1 || channel > 8)
  36. return RT_ERROR;
  37. *value = adc_get_inputVolt(channel - 1) ;
  38. return RT_EOK;
  39. }
  40. static struct rt_adc_ops wm_adc_ops =
  41. {
  42. wm_adc_enabled,
  43. wm_adc_convert,
  44. };
  45. static struct rt_adc_device wm_adc;
  46. #endif
  47. #ifdef USING_CPU_TEMP
  48. static rt_err_t wm_cpu_temp_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  49. {
  50. if (enabled == RT_FALSE)
  51. {
  52. tls_adc_stop(0);
  53. }
  54. return RT_EOK;
  55. }
  56. static rt_err_t wm_cpu_temp_convert(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  57. {
  58. *value = (rt_uint32_t)adc_temp();
  59. /**
  60. sprintf(temperature, "%d.%d", *value/1000, (*value%1000)/100);
  61. printf("tem: %s", temperature);
  62. */
  63. return RT_EOK;
  64. }
  65. static struct rt_adc_ops wm_cpu_temp_ops =
  66. {
  67. wm_cpu_temp_enabled,
  68. wm_cpu_temp_convert,
  69. };
  70. static struct rt_adc_device wm_cpu_temp;
  71. #endif
  72. int wm_hw_adc_init(void)
  73. {
  74. /*adc io config*/
  75. #ifdef USING_ADC_CH1
  76. wm_adc_config(0);
  77. #endif
  78. #ifdef USING_ADC_CH2
  79. wm_adc_config(1);
  80. #endif
  81. #ifdef USING_ADC_CH3
  82. wm_adc_config(2);
  83. #endif
  84. #ifdef USING_ADC_CH4
  85. wm_adc_config(3);
  86. #endif
  87. #ifdef USING_ADC_CH5
  88. wm_adc_config(4);
  89. #endif
  90. #ifdef USING_ADC_CH6
  91. wm_adc_config(5);
  92. #endif
  93. #ifdef USING_ADC_CH7
  94. wm_adc_config(6);
  95. #endif
  96. #ifdef USING_ADC_CH8
  97. wm_adc_config(7);
  98. #endif
  99. #if defined(USING_ADC_CH1) || defined(USING_ADC_CH2) || defined(USING_ADC_CH3) || defined(USING_ADC_CH4) || \
  100. defined(USING_ADC_CH5) || defined(USING_ADC_CH6) || defined(USING_ADC_CH7) || defined(USING_ADC_CH8)
  101. rt_hw_adc_register(&wm_adc, "adc", &wm_adc_ops, 0);
  102. #endif
  103. #ifdef USING_CPU_TEMP
  104. rt_hw_adc_register(&wm_cpu_temp, "cputemp", &wm_cpu_temp_ops, 0);
  105. #endif
  106. return RT_EOK;
  107. }
  108. INIT_DEVICE_EXPORT(wm_hw_adc_init);
  109. #endif /* BSP_USING_ADC */