drv_adc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-04 Rbb666 first version
  9. */
  10. #include "drv_common.h"
  11. #include "drv_adc.h"
  12. #include "cyhal.h"
  13. #include "cybsp.h"
  14. #if defined(BSP_USING_ADC1) || defined(BSP_USING_ADC2)
  15. //#define DRV_DEBUG
  16. #define LOG_TAG "drv.adc"
  17. #include <drv_log.h>
  18. #define VPLUS_CHANNEL_0 (P10_0)
  19. struct ifx_adc
  20. {
  21. struct rt_adc_device ifx_adc_device;
  22. cyhal_adc_channel_t *adc_ch;
  23. char *name;
  24. };
  25. static struct ifx_adc ifx_adc_obj[] =
  26. {
  27. #ifdef BSP_USING_ADC1
  28. ADC1_CONFIG,
  29. #endif
  30. };
  31. static rt_err_t ifx_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  32. {
  33. cyhal_adc_channel_t *adc_ch;
  34. cy_rslt_t result;
  35. RT_ASSERT(device != RT_NULL);
  36. adc_ch = device->parent.user_data;
  37. const cyhal_adc_channel_config_t channel_config =
  38. {
  39. .enable_averaging = false, // Disable averaging for channel
  40. .min_acquisition_ns = 1000, // Minimum acquisition time set to 1us
  41. .enabled = enabled // Sample this channel when ADC performs a scan
  42. };
  43. if (enabled)
  44. {
  45. /* Initialize ADC. The ADC block which can connect to pin 10[0] is selected */
  46. result = cyhal_adc_init(&adc_obj, VPLUS_CHANNEL_0, NULL);
  47. if (result != RT_EOK)
  48. {
  49. LOG_E("ADC initialization failed. Error: %ld\n", (long unsigned int)result);
  50. return -RT_ENOSYS;
  51. }
  52. /* Initialize a channel 0 and configure it to scan P10_0 in single ended mode. */
  53. result = cyhal_adc_channel_init_diff(adc_ch, &adc_obj, VPLUS_CHANNEL_0,
  54. CYHAL_ADC_VNEG, &channel_config);
  55. if (result != RT_EOK)
  56. {
  57. LOG_E("ADC single ended channel initialization failed. Error: %ld\n", (long unsigned int)result);
  58. return -RT_ENOSYS;
  59. }
  60. /* Update ADC configuration */
  61. result = cyhal_adc_configure(&adc_obj, &adc_config);
  62. if (result != RT_EOK)
  63. {
  64. printf("ADC configuration update failed. Error: %ld\n", (long unsigned int)result);
  65. return -RT_ENOSYS;
  66. }
  67. }
  68. else
  69. {
  70. cyhal_adc_free(&adc_obj);
  71. cyhal_adc_channel_free(adc_ch);
  72. }
  73. return RT_EOK;
  74. }
  75. static rt_err_t ifx_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  76. {
  77. cyhal_adc_channel_t *adc_ch;
  78. RT_ASSERT(device != RT_NULL);
  79. adc_ch = device->parent.user_data;
  80. channel = adc_ch->channel_idx;
  81. *value = cyhal_adc_read(adc_ch);
  82. return RT_EOK;
  83. }
  84. static const struct rt_adc_ops at_adc_ops =
  85. {
  86. .enabled = ifx_adc_enabled,
  87. .convert = ifx_get_adc_value,
  88. };
  89. static int rt_hw_adc_init(void)
  90. {
  91. int result = RT_EOK;
  92. int i = 0;
  93. for (i = 0; i < sizeof(ifx_adc_obj) / sizeof(ifx_adc_obj[0]); i++)
  94. {
  95. /* register ADC device */
  96. if (rt_hw_adc_register(&ifx_adc_obj[i].ifx_adc_device, ifx_adc_obj[i].name, &at_adc_ops, ifx_adc_obj[i].adc_ch) == RT_EOK)
  97. {
  98. LOG_D("%s register success", at32_adc_obj[i].name);
  99. }
  100. else
  101. {
  102. LOG_E("%s register failed", ifx_adc_obj[i].name);
  103. result = -RT_ERROR;
  104. }
  105. }
  106. return result;
  107. }
  108. INIT_BOARD_EXPORT(rt_hw_adc_init);
  109. #endif /* BSP_USING_ADC */