drv_adc.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * 2023-03-29 rose_man first version
  9. */
  10. #include "board.h"
  11. #include "drv_adc.h"
  12. #if defined BSP_USING_ADC
  13. #define DBG_TAG "drv.adc"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #if defined(BSP_USING_BL70X) || defined(BSP_USING_BL60X)
  17. #define ADC_GPIP_BASE ((uint32_t)0x40002000)
  18. #elif defined(BSP_USING_BL61X) || defined(BSP_USING_BL808)
  19. #define ADC_GPIP_BASE ((uint32_t)0x20002000)
  20. #endif
  21. static struct bflb_adc_config_s adc_config =
  22. {
  23. ADC_CLK_DIV_32,
  24. false,
  25. false,
  26. false,
  27. ADC_RESOLUTION_16B,
  28. ADC_VREF_2P0V
  29. };
  30. struct bl_adc
  31. {
  32. struct rt_adc_device bl_adc_device;
  33. struct bflb_device_s *bflb_device;
  34. };
  35. static struct bl_adc bl_adc_obj;
  36. static rt_err_t bl_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  37. {
  38. struct bflb_device_s *bl_adc_handler;
  39. RT_ASSERT(device != RT_NULL);
  40. bl_adc_handler = device->parent.user_data;
  41. struct bflb_adc_channel_s chan;
  42. chan.pos_chan = channel;
  43. chan.neg_chan = ADC_CHANNEL_GND;
  44. bflb_adc_channel_config(bl_adc_handler, &chan, channel);
  45. bflb_adc_link_rxdma(bl_adc_handler, enabled);
  46. if (enabled)
  47. {
  48. bflb_adc_start_conversion(bl_adc_handler);
  49. }
  50. else
  51. {
  52. bflb_adc_stop_conversion(bl_adc_handler);
  53. }
  54. return RT_EOK;
  55. }
  56. static rt_err_t bl_adc_get_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  57. {
  58. struct bflb_device_s *bl_adc_handler;
  59. RT_ASSERT(device != RT_NULL);
  60. RT_ASSERT(value != RT_NULL);
  61. bl_adc_handler = device->parent.user_data;
  62. /* get ADC value */
  63. *value = (rt_uint32_t)bflb_adc_read_raw(bl_adc_handler);
  64. return RT_EOK;
  65. }
  66. static const struct rt_adc_ops bl_adc_ops =
  67. {
  68. .enabled = bl_adc_enabled,
  69. .convert = bl_adc_get_value,
  70. };
  71. int rt_hw_adc_init(void)
  72. {
  73. int result = RT_EOK;
  74. /* ADC init */
  75. bl_adc_obj.bflb_device = bflb_device_get_by_name("adc");
  76. bflb_adc_init(bl_adc_obj.bflb_device, &adc_config);
  77. /* register ADC device */
  78. if (rt_hw_adc_register(&bl_adc_obj.bl_adc_device, "adc", &bl_adc_ops, &bl_adc_obj.bflb_device) == RT_EOK)
  79. {
  80. LOG_D("adc init success");
  81. }
  82. else
  83. {
  84. LOG_E("adc register failed");
  85. result = -RT_ERROR;
  86. }
  87. return result;
  88. }
  89. INIT_BOARD_EXPORT(rt_hw_adc_init);
  90. #endif /* BSP_USING_ADC */