adc_hal.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright 2019-2020 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "hal/adc_hal.h"
  15. #include "hal/adc_hal_conf.h"
  16. #if CONFIG_IDF_TARGET_ESP32C3
  17. #include "soc/soc.h"
  18. #include "esp_rom_sys.h"
  19. #endif
  20. void adc_hal_init(void)
  21. {
  22. // Set internal FSM wait time, fixed value.
  23. adc_ll_digi_set_fsm_time(SOC_ADC_FSM_RSTB_WAIT_DEFAULT, SOC_ADC_FSM_START_WAIT_DEFAULT,
  24. SOC_ADC_FSM_STANDBY_WAIT_DEFAULT);
  25. adc_ll_set_sample_cycle(ADC_FSM_SAMPLE_CYCLE_DEFAULT);
  26. adc_hal_pwdet_set_cct(SOC_ADC_PWDET_CCT_DEFAULT);
  27. adc_ll_digi_output_invert(ADC_NUM_1, SOC_ADC_DIGI_DATA_INVERT_DEFAULT(ADC_NUM_1));
  28. adc_ll_digi_output_invert(ADC_NUM_2, SOC_ADC_DIGI_DATA_INVERT_DEFAULT(ADC_NUM_2));
  29. adc_ll_digi_set_clk_div(SOC_ADC_DIGI_SAR_CLK_DIV_DEFAULT);
  30. }
  31. void adc_hal_deinit(void)
  32. {
  33. adc_ll_set_power_manage(ADC_POWER_SW_OFF);
  34. }
  35. int adc_hal_convert(adc_ll_num_t adc_n, int channel, int *value)
  36. {
  37. adc_ll_rtc_enable_channel(adc_n, channel);
  38. adc_ll_rtc_start_convert(adc_n, channel);
  39. while (adc_ll_rtc_convert_is_done(adc_n) != true);
  40. *value = adc_ll_rtc_get_convert_value(adc_n);
  41. return (int)adc_ll_rtc_analysis_raw_data(adc_n, (uint16_t)(*value));
  42. }
  43. #if CONFIG_IDF_TARGET_ESP32C3
  44. //This feature is currently supported on ESP32C3, will be supported on other chips soon
  45. /*---------------------------------------------------------------
  46. DMA setting
  47. ---------------------------------------------------------------*/
  48. void adc_hal_digi_dma_multi_descriptor(adc_dma_hal_config_t *dma_config, uint8_t *data_buf, uint32_t size, uint32_t num)
  49. {
  50. assert(((uint32_t)data_buf % 4) == 0);
  51. assert((size % 4) == 0);
  52. dma_descriptor_t *desc = dma_config->rx_desc;
  53. uint32_t n = 0;
  54. while (num--) {
  55. desc[n].dw0.size = size;
  56. desc[n].dw0.suc_eof = 0;
  57. desc[n].dw0.owner = 1;
  58. desc[n].buffer = data_buf;
  59. desc[n].next = &desc[n+1];
  60. data_buf += size;
  61. n++;
  62. }
  63. desc[n-1].next = NULL;
  64. }
  65. void adc_hal_digi_rxdma_start(adc_dma_hal_context_t *adc_dma_ctx, adc_dma_hal_config_t *dma_config)
  66. {
  67. gdma_ll_rx_reset_channel(adc_dma_ctx->dev, dma_config->dma_chan);
  68. gdma_ll_rx_set_desc_addr(adc_dma_ctx->dev, dma_config->dma_chan, (uint32_t)dma_config->rx_desc);
  69. gdma_ll_rx_start(adc_dma_ctx->dev, dma_config->dma_chan);
  70. }
  71. void adc_hal_digi_rxdma_stop(adc_dma_hal_context_t *adc_dma_ctx, adc_dma_hal_config_t *dma_config)
  72. {
  73. gdma_ll_rx_stop(adc_dma_ctx->dev, dma_config->dma_chan);
  74. }
  75. void adc_hal_digi_ena_intr(adc_dma_hal_context_t *adc_dma_ctx, adc_dma_hal_config_t *dma_config, uint32_t mask)
  76. {
  77. gdma_ll_enable_interrupt(adc_dma_ctx->dev, dma_config->dma_chan, mask, true);
  78. }
  79. void adc_hal_digi_clr_intr(adc_dma_hal_context_t *adc_dma_ctx, adc_dma_hal_config_t *dma_config, uint32_t mask)
  80. {
  81. gdma_ll_clear_interrupt_status(adc_dma_ctx->dev, dma_config->dma_chan, mask);
  82. }
  83. void adc_hal_digi_dis_intr(adc_dma_hal_context_t *adc_dma_ctx, adc_dma_hal_config_t *dma_config, uint32_t mask)
  84. {
  85. gdma_ll_enable_interrupt(adc_dma_ctx->dev, dma_config->dma_chan, mask, false);
  86. }
  87. void adc_hal_digi_set_eof_num(adc_dma_hal_context_t *adc_dma_ctx, adc_dma_hal_config_t *dma_config, uint32_t num)
  88. {
  89. adc_ll_digi_dma_set_eof_num(num);
  90. }
  91. void adc_hal_digi_start(adc_dma_hal_context_t *adc_dma_ctx, adc_dma_hal_config_t *dma_config)
  92. {
  93. //Set to 1: the ADC data will be sent to the DMA
  94. adc_ll_digi_dma_enable();
  95. //enable sar adc timer
  96. adc_ll_digi_trigger_enable();
  97. }
  98. void adc_hal_digi_stop(adc_dma_hal_context_t *adc_dma_ctx, adc_dma_hal_config_t *dma_config)
  99. {
  100. //Set to 0: the ADC data won't be sent to the DMA
  101. adc_ll_digi_dma_disable();
  102. //disable sar adc timer
  103. adc_ll_digi_trigger_disable();
  104. }
  105. void adc_hal_digi_init(adc_dma_hal_context_t *adc_dma_ctx, adc_dma_hal_config_t *dma_config)
  106. {
  107. adc_dma_ctx->dev = &GDMA;
  108. gdma_ll_enable_clock(adc_dma_ctx->dev, true);
  109. gdma_ll_clear_interrupt_status(adc_dma_ctx->dev, dma_config->dma_chan, UINT32_MAX);
  110. gdma_ll_rx_connect_to_periph(adc_dma_ctx->dev, dma_config->dma_chan, GDMA_LL_TRIG_SRC_ADC_DAC);
  111. }
  112. /*---------------------------------------------------------------
  113. Single Read
  114. ---------------------------------------------------------------*/
  115. void adc_hal_onetime_start(adc_digi_config_t *adc_digi_config)
  116. {
  117. /**
  118. * There is a hardware limitation. If the APB clock frequency is high, the step of this reg signal: ``onetime_start`` may not be captured by the
  119. * ADC digital controller (when its clock frequency is too slow). A rough estimate for this step should be at least 3 ADC digital controller
  120. * clock cycle.
  121. *
  122. * This limitation will be removed in hardware future versions.
  123. *
  124. */
  125. uint32_t digi_clk = APB_CLK_FREQ / (adc_digi_config->dig_clk.div_num + adc_digi_config->dig_clk.div_a / adc_digi_config->dig_clk.div_b + 1);
  126. //Convert frequency to time (us). Since decimals are removed by this division operation. Add 1 here in case of the fact that delay is not enough.
  127. uint32_t delay = (1000 * 1000) / digi_clk + 1;
  128. //3 ADC digital controller clock cycle
  129. delay = delay * 3;
  130. //This coefficient (8) is got from test. When digi_clk is not smaller than ``APB_CLK_FREQ/8``, no delay is needed.
  131. if (digi_clk >= APB_CLK_FREQ/8) {
  132. delay = 0;
  133. }
  134. adc_ll_onetime_start(false);
  135. esp_rom_delay_us(delay);
  136. adc_ll_onetime_start(true);
  137. //No need to delay here. Becuase if the start signal is not seen, there won't be a done intr.
  138. }
  139. void adc_hal_adc1_onetime_sample_enable(bool enable)
  140. {
  141. if (enable) {
  142. adc_ll_adc1_onetime_sample_ena();
  143. } else {
  144. adc_ll_adc1_onetime_sample_dis();
  145. }
  146. }
  147. void adc_hal_adc2_onetime_sample_enable(bool enable)
  148. {
  149. if (enable) {
  150. adc_ll_adc2_onetime_sample_ena();
  151. } else {
  152. adc_ll_adc2_onetime_sample_dis();
  153. }
  154. }
  155. void adc_hal_onetime_channel(adc_ll_num_t unit, adc_channel_t channel)
  156. {
  157. adc_ll_onetime_set_channel(unit, channel);
  158. }
  159. void adc_hal_set_onetime_atten(adc_atten_t atten)
  160. {
  161. adc_ll_onetime_set_atten(atten);
  162. }
  163. uint32_t adc_hal_adc1_read(void)
  164. {
  165. return adc_ll_adc1_read();
  166. }
  167. uint32_t adc_hal_adc2_read(void)
  168. {
  169. return adc_ll_adc2_read();
  170. }
  171. //--------------------INTR-------------------------------
  172. static adc_ll_intr_t get_event_intr(adc_event_t event)
  173. {
  174. adc_ll_intr_t intr_mask = 0;
  175. if (event & ADC_EVENT_ADC1_DONE) {
  176. intr_mask |= ADC_LL_INTR_ADC1_DONE;
  177. }
  178. if (event & ADC_EVENT_ADC2_DONE) {
  179. intr_mask |= ADC_LL_INTR_ADC2_DONE;
  180. }
  181. return intr_mask;
  182. }
  183. void adc_hal_intr_enable(adc_event_t event)
  184. {
  185. adc_ll_intr_enable(get_event_intr(event));
  186. }
  187. void adc_hal_intr_disable(adc_event_t event)
  188. {
  189. adc_ll_intr_disable(get_event_intr(event));
  190. }
  191. void adc_hal_intr_clear(adc_event_t event)
  192. {
  193. adc_ll_intr_clear(get_event_intr(event));
  194. }
  195. bool adc_hal_intr_get_raw(adc_event_t event)
  196. {
  197. return adc_ll_intr_get_raw(get_event_intr(event));
  198. }
  199. bool adc_hal_intr_get_status(adc_event_t event)
  200. {
  201. return adc_ll_intr_get_status(get_event_intr(event));
  202. }
  203. #endif