drv_adc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-06-27 AHTYDHD the first version
  9. */
  10. #include "drv_adc.h"
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include "inc/hw_memmap.h"
  14. #include "driverlib/adc.h"
  15. #include "driverlib/sysctl.h"
  16. #ifdef RT_USING_ADC
  17. #include "adc_config.h"
  18. #include "tm4c123_config.h"
  19. #include <string.h>
  20. #define LOG_TAG "drv.adc"
  21. #include <drv_log.h>
  22. static struct tm4c123_adc_config adc_config[] =
  23. {
  24. #ifdef BSP_USING_ADC0
  25. ADC0_CONFIG,
  26. #endif
  27. #ifdef BSP_USING_ADC1
  28. ADC1_CONFIG,
  29. #endif
  30. };
  31. struct tm4c123_adc
  32. {
  33. struct tm4c123_adc_config *config;
  34. struct rt_adc_device adc_device;
  35. };
  36. static struct tm4c123_adc adc_obj[sizeof(adc_config) / sizeof(adc_config[0])] = {0};
  37. static rt_err_t tm4c123_adc_enabled(struct rt_adc_device *device, rt_uint32_t channel, rt_bool_t enabled)
  38. {
  39. RT_ASSERT(device != RT_NULL);
  40. struct tm4c123_adc_config *config = (struct tm4c123_adc_config *)device->parent.user_data;
  41. if (enabled)
  42. {
  43. ADCSequenceEnable(config->adcbase, config->sequence);
  44. ADCIntClear(config->adcbase, config->sequence);
  45. }
  46. else
  47. {
  48. ADCSequenceDisable(config->adcbase, config->sequence);
  49. }
  50. return RT_EOK;
  51. }
  52. static rt_err_t tm4c123_get_adc_value(struct rt_adc_device *device, rt_uint32_t channel, rt_uint32_t *value)
  53. {
  54. RT_ASSERT(device != RT_NULL);
  55. RT_ASSERT(value != RT_NULL);
  56. uint32_t pui32ADC0Value[4] = {0};
  57. struct tm4c123_adc_config *config = (struct tm4c123_adc_config *)device->parent.user_data;
  58. /* Trigger the ADC conversion. */
  59. ADCProcessorTrigger(config->adcbase, config->sequence);
  60. while (!ADCIntStatus(config->adcbase, config->sequence, false))
  61. {
  62. }
  63. /* Clear the ADC interrupt flag. */
  64. ADCIntClear(config->adcbase, config->sequence);
  65. /* Read ADC Value. */
  66. ADCSequenceDataGet(config->adcbase, config->sequence, pui32ADC0Value);
  67. /* get ADC value */
  68. *value = (rt_uint32_t)pui32ADC0Value[channel];
  69. return RT_EOK;
  70. }
  71. static const struct rt_adc_ops tm4c123_adc_ops =
  72. {
  73. .enabled = tm4c123_adc_enabled,
  74. .convert = tm4c123_get_adc_value,
  75. };
  76. static rt_err_t tm4c123_hw_adc_init(struct tm4c123_adc *device)
  77. {
  78. uint32_t adcbase = device->config->adcbase;
  79. uint32_t sequencenum = device->config->sequence;
  80. ADCSequenceConfigure(adcbase, sequencenum,
  81. device->config->trigermode, 0);
  82. ADCSequenceStepConfigure(adcbase, sequencenum, 0, ADC_CTL_CH7);
  83. ADCSequenceStepConfigure(adcbase, sequencenum, 1, ADC_CTL_CH6 | ADC_CTL_IE);
  84. ADCSequenceStepConfigure(adcbase, sequencenum, 2, ADC_CTL_CH5);
  85. /*Tell the ADC logic
  86. that this is the last conversion on sequence 3 (ADC_CTL_END). */
  87. ADCSequenceStepConfigure(adcbase, sequencenum, 3, ADC_CTL_CH4 | ADC_CTL_IE |
  88. ADC_CTL_END);
  89. return RT_EOK;
  90. }
  91. static int tm4c123_adc_init(void)
  92. {
  93. int i = 0;
  94. rt_size_t obj_num = sizeof(adc_obj) / sizeof(struct tm4c123_adc);
  95. rt_err_t result = RT_EOK;
  96. adc_hw_config();
  97. for (i = 0; i < obj_num; i++)
  98. {
  99. /* ADC init */
  100. adc_obj[i].config = &adc_config[i];
  101. if (tm4c123_hw_adc_init(&adc_obj[i]) != RT_EOK)
  102. {
  103. LOG_E("%s init failed", adc_obj[i].config->name);
  104. result = -RT_ERROR;
  105. return result;
  106. }
  107. else
  108. {
  109. LOG_D("%s init success", adc_obj[i].config->name);
  110. /* register adc device */
  111. if (rt_hw_adc_register(&adc_obj[i].adc_device, adc_obj[i].config->name, &tm4c123_adc_ops, &adc_config[i]) == RT_EOK)
  112. {
  113. LOG_D("%s register success", adc_obj[i].config->name);
  114. }
  115. else
  116. {
  117. LOG_E("%s register failed", adc_obj[i].config->name);
  118. result = -RT_ERROR;
  119. }
  120. }
  121. }
  122. return result;
  123. }
  124. INIT_APP_EXPORT(tm4c123_adc_init);
  125. #endif /*RT_UING_ADC*/
  126. /************************** end of file ******************/