1
0

adc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. * 2017-09-18 Haley the first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "am_mcu_apollo.h"
  13. #ifdef RT_USING_ADC
  14. /* messagequeue define */
  15. struct rt_messagequeue adcbat_mq;
  16. #define BATTERY_GPIO 35 /* Battery */
  17. #define BATTERY_ADC_PIN AM_HAL_PIN_35_ADCSE7
  18. #define BATTERY_ADC_CHANNEL AM_HAL_ADC_SLOT_CHSEL_SE7 /* BATTERY ADC采集通道 */
  19. #define BATTERY_ADC_CHANNELNUM 7 /* BATTERY ADC采集通道号 */
  20. #define ADC_CTIMER_NUM 3 /* ADC使用定时器 */
  21. #define ADC_CTIMER_COUNT (2048/512 - 1)
  22. #define ADC_CHANNEL_NUM 1 /* ADC采集通道个数 */
  23. #define ADC_SAMPLE_NUM 8 /* ADC采样个数 */
  24. rt_uint8_t bat_adc_cnt = 0;
  25. static rt_uint8_t am_adcbat_buffer_pool[256];
  26. static rt_int16_t am_adcbat_buffertemp[32];
  27. rt_uint8_t am_adc_data_get(rt_uint8_t channel, rt_int16_t *buff, rt_uint16_t size)
  28. {
  29. rt_uint8_t adc_bufftemp[32];
  30. if (channel == BATTERY_ADC_CHANNELNUM)
  31. {
  32. /* wait adc message forever */
  33. rt_mq_recv(&adcbat_mq, adc_bufftemp, 32, RT_WAITING_FOREVER);
  34. }
  35. /* copy the data */
  36. rt_memcpy(buff, adc_bufftemp, size*sizeof(rt_int16_t));
  37. return 0;
  38. }
  39. void am_adc_start(rt_uint8_t channel)
  40. {
  41. /* messagequeue init */
  42. rt_mq_init(&adcbat_mq, "mq_adcbat",
  43. &am_adcbat_buffer_pool[0],
  44. 32 - sizeof(void*),
  45. sizeof(am_adcbat_buffer_pool),
  46. RT_IPC_FLAG_FIFO);
  47. /* Start the ctimer */
  48. am_hal_ctimer_start(ADC_CTIMER_NUM, AM_HAL_CTIMER_TIMERA);
  49. /* Trigger the ADC once */
  50. am_hal_adc_trigger();
  51. }
  52. void am_adc_stop(rt_uint8_t channel)
  53. {
  54. /* Stop the ctimer */
  55. am_hal_ctimer_stop(ADC_CTIMER_NUM, AM_HAL_CTIMER_TIMERA);
  56. /* messagequeue delete */
  57. rt_mq_delete(&adceeg_mq);
  58. /* messagequeue delete */
  59. rt_mq_delete(&adcbat_mq);
  60. }
  61. /**
  62. * @brief Interrupt handler for the ADC
  63. *
  64. * This function is Interrupt handler for the ADC
  65. *
  66. * @return None.
  67. */
  68. void am_adc_isr(void)
  69. {
  70. uint32_t ui32Status, ui32FifoData;
  71. /* enter interrupt */
  72. rt_interrupt_enter();
  73. /* Read the interrupt status */
  74. ui32Status = am_hal_adc_int_status_get(true);
  75. /* Clear the ADC interrupt */
  76. am_hal_adc_int_clear(ui32Status);
  77. /* If we got a FIFO 75% full (which should be our only ADC interrupt), go ahead and read the data */
  78. if (ui32Status & AM_HAL_ADC_INT_FIFOOVR1)
  79. {
  80. do
  81. {
  82. /* Read the value from the FIFO into the circular buffer */
  83. ui32FifoData = am_hal_adc_fifo_pop();
  84. if (AM_HAL_ADC_FIFO_SLOT(ui32FifoData) == BATTERY_ADC_CHANNELNUM)
  85. {
  86. am_adcbat_buffertemp[bat_adc_cnt++] = AM_HAL_ADC_FIFO_SAMPLE(ui32FifoData);
  87. }
  88. if ((bat_adc_cnt > ADC_SAMPLE_NUM + 2 - 1))
  89. {
  90. bat_adc_cnt = 0;
  91. /* send the message */
  92. rt_mq_send(&adcbat_mq, am_adcbat_buffertemp, ADC_SAMPLE_NUM*sizeof(rt_int16_t));
  93. }
  94. } while (AM_HAL_ADC_FIFO_COUNT(ui32FifoData) > 0);
  95. }
  96. /* leave interrupt */
  97. rt_interrupt_leave();
  98. }
  99. static void timerA3_for_adc_init(void)
  100. {
  101. /* Start a timer to trigger the ADC periodically (1 second) */
  102. am_hal_ctimer_config_single(ADC_CTIMER_NUM, AM_HAL_CTIMER_TIMERA,
  103. AM_HAL_CTIMER_XT_2_048KHZ |
  104. AM_HAL_CTIMER_FN_REPEAT |
  105. AM_HAL_CTIMER_INT_ENABLE |
  106. AM_HAL_CTIMER_PIN_ENABLE);
  107. am_hal_ctimer_int_enable(AM_HAL_CTIMER_INT_TIMERA3);
  108. /* Set 512 sample rate */
  109. am_hal_ctimer_period_set(ADC_CTIMER_NUM, AM_HAL_CTIMER_TIMERA, ADC_CTIMER_COUNT, 1);
  110. /* Enable the timer A3 to trigger the ADC directly */
  111. am_hal_ctimer_adc_trigger_enable();
  112. /* Start the timer */
  113. //am_hal_ctimer_start(ADC_CTIMER_NUM, AM_HAL_CTIMER_TIMERA);
  114. }
  115. /**
  116. * @brief Initialize the ADC
  117. *
  118. * This function initialize the ADC
  119. *
  120. * @return None.
  121. */
  122. int rt_hw_adc_init(void)
  123. {
  124. am_hal_adc_config_t sADCConfig;
  125. /* timer for adc init*/
  126. timerA3_for_adc_init();
  127. /* Set a pin to act as our ADC input */
  128. am_hal_gpio_pin_config(BATTERY_GPIO, BATTERY_ADC_PIN);
  129. /* Enable interrupts */
  130. am_hal_interrupt_enable(AM_HAL_INTERRUPT_ADC);
  131. /* Enable the ADC power domain */
  132. am_hal_pwrctrl_periph_enable(AM_HAL_PWRCTRL_ADC);
  133. /* Set up the ADC configuration parameters. These settings are reasonable
  134. for accurate measurements at a low sample rate */
  135. sADCConfig.ui32Clock = AM_HAL_ADC_CLOCK_HFRC;
  136. sADCConfig.ui32TriggerConfig = AM_HAL_ADC_TRIGGER_SOFT;
  137. sADCConfig.ui32Reference = AM_HAL_ADC_REF_INT_2P0;
  138. sADCConfig.ui32ClockMode = AM_HAL_ADC_CK_LOW_POWER;
  139. sADCConfig.ui32PowerMode = AM_HAL_ADC_LPMODE_0;
  140. sADCConfig.ui32Repeat = AM_HAL_ADC_REPEAT;
  141. am_hal_adc_config(&sADCConfig);
  142. /* For this example, the samples will be coming in slowly. This means we
  143. can afford to wake up for every conversion */
  144. am_hal_adc_int_enable(AM_HAL_ADC_INT_FIFOOVR1);
  145. /* Set up an ADC slot */
  146. am_hal_adc_slot_config(BATTERY_ADC_CHANNELNUM, AM_HAL_ADC_SLOT_AVG_1 |
  147. AM_HAL_ADC_SLOT_14BIT |
  148. BATTERY_ADC_CHANNEL |
  149. AM_HAL_ADC_SLOT_ENABLE);
  150. /* Enable the ADC */
  151. am_hal_adc_enable();
  152. /* Trigger the ADC once */
  153. //am_hal_adc_trigger();
  154. //rt_kprintf("adc_init!\n");
  155. return 0;
  156. }
  157. #ifdef RT_USING_COMPONENTS_INIT
  158. INIT_BOARD_EXPORT(rt_hw_adc_init);
  159. #endif
  160. #endif
  161. /*@}*/