drv_eadc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**************************************************************************//**
  2. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-7-10 FYChou First version
  9. *
  10. ******************************************************************************/
  11. #include <rtconfig.h>
  12. #include <rtdevice.h>
  13. #include "NuMicro.h"
  14. #ifdef BSP_USING_EADC
  15. /* Private define ---------------------------------------------------------------*/
  16. /* Private Typedef --------------------------------------------------------------*/
  17. struct nu_eadc
  18. {
  19. struct rt_adc_device dev;
  20. char *name;
  21. EADC_T *eadc_base;
  22. int eadc_reg_tab;
  23. int eadc_max_ch_num;
  24. };
  25. typedef struct nu_eadc *nu_eadc_t;
  26. /* Private functions ------------------------------------------------------------*/
  27. static rt_err_t nu_eadc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled);
  28. static rt_err_t nu_get_eadc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value);
  29. static rt_err_t nu_get_eadc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value);
  30. /* Public functions ------------------------------------------------------------*/
  31. int rt_hw_eadc_init(void);
  32. /* Private variables ------------------------------------------------------------*/
  33. static struct nu_eadc nu_eadc_arr [] =
  34. {
  35. #if defined(BSP_USING_EADC0)
  36. {
  37. .name = "eadc0",
  38. .eadc_base = EADC,
  39. .eadc_max_ch_num = 16,
  40. },
  41. #endif
  42. {0}
  43. };
  44. static const struct rt_adc_ops nu_adc_ops =
  45. {
  46. nu_eadc_enabled,
  47. nu_get_eadc_value,
  48. };
  49. typedef struct rt_adc_ops *rt_adc_ops_t;
  50. /* nu_adc_enabled - Enable ADC clock and wait for ready */
  51. static rt_err_t nu_eadc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  52. {
  53. EADC_T *eadc_base = ((nu_eadc_t)device)->eadc_base;
  54. int *peadc_reg_tab = &((nu_eadc_t)device)->eadc_reg_tab;
  55. RT_ASSERT(device != RT_NULL);
  56. if (channel >= ((nu_eadc_t)device)->eadc_max_ch_num)
  57. return -(RT_EINVAL);
  58. if (enabled)
  59. {
  60. if (*peadc_reg_tab == 0)
  61. {
  62. EADC_Open(eadc_base, EADC_CTL_DIFFEN_SINGLE_END);
  63. }
  64. *peadc_reg_tab |= (0x1 << channel);
  65. }
  66. else
  67. {
  68. *peadc_reg_tab &= ~(0x1 << channel);
  69. if (*peadc_reg_tab == 0)
  70. {
  71. EADC_Close(eadc_base);
  72. }
  73. }
  74. return RT_EOK;
  75. }
  76. static rt_err_t nu_get_eadc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  77. {
  78. RT_ASSERT(device != RT_NULL);
  79. RT_ASSERT(value != RT_NULL);
  80. EADC_T *eadc_base = ((nu_eadc_t)device)->eadc_base;
  81. int *peadc_reg_tab = &((nu_eadc_t)device)->eadc_reg_tab;
  82. if (channel >= ((nu_eadc_t)device)->eadc_max_ch_num)
  83. {
  84. *value = 0xFFFFFFFF;
  85. return -(RT_EINVAL);
  86. }
  87. if ((*peadc_reg_tab & (1 << channel)) == 0)
  88. {
  89. *value = 0xFFFFFFFF;
  90. return -(RT_EBUSY);
  91. }
  92. EADC_ConfigSampleModule(eadc_base, 0, EADC_SOFTWARE_TRIGGER, channel);
  93. EADC_CLR_INT_FLAG(eadc_base, EADC_STATUS2_ADIF0_Msk);
  94. EADC_ENABLE_INT(eadc_base, BIT0);
  95. EADC_ENABLE_SAMPLE_MODULE_INT(eadc_base, 0, BIT0);
  96. EADC_START_CONV(eadc_base, BIT0);
  97. while (EADC_GET_INT_FLAG(eadc_base, BIT0) == 0);
  98. *value = EADC_GET_CONV_DATA(eadc_base, 0);
  99. return RT_EOK;
  100. }
  101. int rt_hw_eadc_init(void)
  102. {
  103. rt_err_t result = RT_ERROR;
  104. int nu_sel = 0;
  105. while (nu_eadc_arr[nu_sel].name != 0)
  106. {
  107. nu_eadc_arr[nu_sel].eadc_reg_tab = 0;
  108. result = rt_hw_adc_register(&nu_eadc_arr[nu_sel].dev, nu_eadc_arr[nu_sel].name, &nu_adc_ops, NULL);
  109. RT_ASSERT(result == RT_EOK);
  110. nu_sel++;
  111. }
  112. return (int)result;
  113. }
  114. INIT_BOARD_EXPORT(rt_hw_eadc_init);
  115. #endif //#if defined(BSP_USING_EADC)