drv_eadc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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-2-11 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 = EADC0,
  39. .eadc_max_ch_num = 19,
  40. },
  41. #endif
  42. #if defined(BSP_USING_EADC1)
  43. {
  44. .name = "eadc1",
  45. .eadc_base = EADC1,
  46. .eadc_max_ch_num = 19,
  47. },
  48. #endif
  49. {0}
  50. };
  51. static const struct rt_adc_ops nu_adc_ops =
  52. {
  53. nu_eadc_enabled,
  54. nu_get_eadc_value,
  55. };
  56. typedef struct rt_adc_ops *rt_adc_ops_t;
  57. /* nu_adc_enabled - Enable ADC clock and wait for ready */
  58. static rt_err_t nu_eadc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  59. {
  60. EADC_T *eadc_base = ((nu_eadc_t)device)->eadc_base;
  61. int *peadc_reg_tab = &((nu_eadc_t)device)->eadc_reg_tab;
  62. RT_ASSERT(device != RT_NULL);
  63. if (channel >= ((nu_eadc_t)device)->eadc_max_ch_num)
  64. return -(RT_EINVAL);
  65. if (enabled)
  66. {
  67. if (*peadc_reg_tab == 0)
  68. {
  69. EADC_Open(eadc_base, EADC_CTL_DIFFEN_SINGLE_END);
  70. }
  71. *peadc_reg_tab |= (0x1 << channel);
  72. }
  73. else
  74. {
  75. *peadc_reg_tab &= ~(0x1 << channel);
  76. if (*peadc_reg_tab == 0)
  77. {
  78. EADC_Close(eadc_base);
  79. }
  80. }
  81. return RT_EOK;
  82. }
  83. static rt_err_t nu_get_eadc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  84. {
  85. RT_ASSERT(device != RT_NULL);
  86. RT_ASSERT(value != RT_NULL);
  87. EADC_T *eadc_base = ((nu_eadc_t)device)->eadc_base;
  88. int *peadc_reg_tab = &((nu_eadc_t)device)->eadc_reg_tab;
  89. if (channel >= ((nu_eadc_t)device)->eadc_max_ch_num)
  90. {
  91. *value = 0xFFFFFFFF;
  92. return -(RT_EINVAL);
  93. }
  94. if ((*peadc_reg_tab & (1 << channel)) == 0)
  95. {
  96. *value = 0xFFFFFFFF;
  97. return -(RT_EBUSY);
  98. }
  99. EADC_ConfigSampleModule(eadc_base, 0, EADC_SOFTWARE_TRIGGER, channel);
  100. EADC_CLR_INT_FLAG(eadc_base, EADC_STATUS2_ADIF0_Msk);
  101. EADC_ENABLE_INT(eadc_base, BIT0);
  102. EADC_ENABLE_SAMPLE_MODULE_INT(eadc_base, 0, BIT0);
  103. EADC_START_CONV(eadc_base, BIT0);
  104. while (EADC_GET_INT_FLAG(eadc_base, BIT0) == 0);
  105. *value = EADC_GET_CONV_DATA(eadc_base, 0);
  106. return RT_EOK;
  107. }
  108. int rt_hw_eadc_init(void)
  109. {
  110. rt_err_t result = RT_ERROR;
  111. int nu_sel = 0;
  112. while (nu_eadc_arr[nu_sel].name != 0)
  113. {
  114. nu_eadc_arr[nu_sel].eadc_reg_tab = 0;
  115. result = rt_hw_adc_register(&nu_eadc_arr[nu_sel].dev, nu_eadc_arr[nu_sel].name, &nu_adc_ops, NULL);
  116. RT_ASSERT(result == RT_EOK);
  117. nu_sel++;
  118. }
  119. return result;
  120. }
  121. INIT_BOARD_EXPORT(rt_hw_eadc_init);
  122. #endif //#if defined(BSP_USING_EADC)