continuous_read_main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "sdkconfig.h"
  9. #include "esp_log.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "freertos/semphr.h"
  13. #include "esp_adc/adc_continuous.h"
  14. #define EXAMPLE_READ_LEN 256
  15. #define GET_UNIT(x) ((x>>3) & 0x1)
  16. #if CONFIG_IDF_TARGET_ESP32
  17. #define ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1 //ESP32 only supports ADC1 DMA mode
  18. #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE1
  19. #elif CONFIG_IDF_TARGET_ESP32S2
  20. #define ADC_CONV_MODE ADC_CONV_BOTH_UNIT
  21. #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
  22. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  23. #define ADC_CONV_MODE ADC_CONV_ALTER_UNIT //ESP32C3 only supports alter mode
  24. #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
  25. #elif CONFIG_IDF_TARGET_ESP32S3
  26. #define ADC_CONV_MODE ADC_CONV_BOTH_UNIT
  27. #define ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
  28. #endif
  29. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  30. static adc_channel_t channel[3] = {ADC_CHANNEL_2, ADC_CHANNEL_3, (ADC_CHANNEL_0 | 1 << 3)};
  31. #endif
  32. #if CONFIG_IDF_TARGET_ESP32S2
  33. static adc_channel_t channel[3] = {ADC_CHANNEL_2, ADC_CHANNEL_3, (ADC_CHANNEL_0 | 1 << 3)};
  34. #endif
  35. #if CONFIG_IDF_TARGET_ESP32
  36. static adc_channel_t channel[1] = {ADC_CHANNEL_7};
  37. #endif
  38. static TaskHandle_t s_task_handle;
  39. static const char *TAG = "EXAMPLE";
  40. static bool IRAM_ATTR s_conv_done_cb(adc_continuous_handle_t handle, const adc_continuous_evt_data_t *edata, void *user_data)
  41. {
  42. BaseType_t mustYield = pdFALSE;
  43. //Notify that ADC continuous driver has done enough number of conversions
  44. vTaskNotifyGiveFromISR(s_task_handle, &mustYield);
  45. return (mustYield == pdTRUE);
  46. }
  47. static void continuous_adc_init(adc_channel_t *channel, uint8_t channel_num, adc_continuous_handle_t *out_handle)
  48. {
  49. adc_continuous_handle_t handle = NULL;
  50. adc_continuous_handle_cfg_t adc_config = {
  51. .max_store_buf_size = 1024,
  52. .conv_frame_size = EXAMPLE_READ_LEN,
  53. };
  54. ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_config, &handle));
  55. adc_continuous_config_t dig_cfg = {
  56. .sample_freq_hz = 20 * 1000,
  57. .conv_mode = ADC_CONV_MODE,
  58. .format = ADC_OUTPUT_TYPE,
  59. };
  60. adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0};
  61. dig_cfg.pattern_num = channel_num;
  62. for (int i = 0; i < channel_num; i++) {
  63. uint8_t unit = GET_UNIT(channel[i]);
  64. uint8_t ch = channel[i] & 0x7;
  65. adc_pattern[i].atten = ADC_ATTEN_DB_0;
  66. adc_pattern[i].channel = ch;
  67. adc_pattern[i].unit = unit;
  68. adc_pattern[i].bit_width = SOC_ADC_DIGI_MAX_BITWIDTH;
  69. ESP_LOGI(TAG, "adc_pattern[%d].atten is :%x", i, adc_pattern[i].atten);
  70. ESP_LOGI(TAG, "adc_pattern[%d].channel is :%x", i, adc_pattern[i].channel);
  71. ESP_LOGI(TAG, "adc_pattern[%d].unit is :%x", i, adc_pattern[i].unit);
  72. }
  73. dig_cfg.adc_pattern = adc_pattern;
  74. ESP_ERROR_CHECK(adc_continuous_config(handle, &dig_cfg));
  75. *out_handle = handle;
  76. }
  77. #if !CONFIG_IDF_TARGET_ESP32
  78. static bool check_valid_data(const adc_digi_output_data_t *data)
  79. {
  80. const unsigned int unit = data->type2.unit;
  81. if (unit > 2) return false;
  82. if (data->type2.channel >= SOC_ADC_CHANNEL_NUM(unit)) return false;
  83. return true;
  84. }
  85. #endif
  86. void app_main(void)
  87. {
  88. esp_err_t ret;
  89. uint32_t ret_num = 0;
  90. uint8_t result[EXAMPLE_READ_LEN] = {0};
  91. memset(result, 0xcc, EXAMPLE_READ_LEN);
  92. s_task_handle = xTaskGetCurrentTaskHandle();
  93. adc_continuous_handle_t handle = NULL;
  94. continuous_adc_init(channel, sizeof(channel) / sizeof(adc_channel_t), &handle);
  95. adc_continuous_evt_cbs_t cbs = {
  96. .on_conv_done = s_conv_done_cb,
  97. };
  98. ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle, &cbs, NULL));
  99. ESP_ERROR_CHECK(adc_continuous_start(handle));
  100. while(1) {
  101. /**
  102. * This is to show you the way to use the ADC continuous mode driver event callback.
  103. * This `ulTaskNotifyTake` will block when the data processing in the task is fast.
  104. * However in this example, the data processing (print) is slow, so you barely block here.
  105. *
  106. * Without using this event callback (to notify this task), you can still just call
  107. * `adc_continuous_read()` here in a loop, with/without a certain block timeout.
  108. */
  109. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  110. while (1) {
  111. ret = adc_continuous_read(handle, result, EXAMPLE_READ_LEN, &ret_num, 0);
  112. if (ret == ESP_OK) {
  113. ESP_LOGI("TASK", "ret is %x, ret_num is %"PRIu32, ret, ret_num);
  114. for (int i = 0; i < ret_num; i += SOC_ADC_DIGI_RESULT_BYTES) {
  115. adc_digi_output_data_t *p = (void*)&result[i];
  116. #if CONFIG_IDF_TARGET_ESP32
  117. ESP_LOGI(TAG, "Unit: %d, Channel: %d, Value: %x", 1, p->type1.channel, p->type1.data);
  118. #else
  119. if (ADC_CONV_MODE == ADC_CONV_BOTH_UNIT || ADC_CONV_MODE == ADC_CONV_ALTER_UNIT) {
  120. if (check_valid_data(p)) {
  121. ESP_LOGI(TAG, "Unit: %d,_Channel: %d, Value: %x", p->type2.unit+1, p->type2.channel, p->type2.data);
  122. } else {
  123. ESP_LOGI(TAG, "Invalid data [%d_%d_%x]", p->type2.unit+1, p->type2.channel, p->type2.data);
  124. }
  125. }
  126. #if CONFIG_IDF_TARGET_ESP32S2
  127. else if (ADC_CONV_MODE == ADC_CONV_SINGLE_UNIT_2) {
  128. ESP_LOGI(TAG, "Unit: %d, Channel: %d, Value: %x", 2, p->type1.channel, p->type1.data);
  129. } else if (ADC_CONV_MODE == ADC_CONV_SINGLE_UNIT_1) {
  130. ESP_LOGI(TAG, "Unit: %d, Channel: %d, Value: %x", 1, p->type1.channel, p->type1.data);
  131. }
  132. #endif //#if CONFIG_IDF_TARGET_ESP32S2
  133. #endif
  134. }
  135. /**
  136. * Because printing is slow, so every time you call `ulTaskNotifyTake`, it will immediately return.
  137. * To avoid a task watchdog timeout, add a delay here. When you replace the way you process the data,
  138. * usually you don't need this delay (as this task will block for a while).
  139. */
  140. vTaskDelay(1);
  141. } else if (ret == ESP_ERR_TIMEOUT) {
  142. //We try to read `EXAMPLE_READ_LEN` until API returns timeout, which means there's no available data
  143. break;
  144. }
  145. }
  146. }
  147. ESP_ERROR_CHECK(adc_continuous_stop(handle));
  148. ESP_ERROR_CHECK(adc_continuous_deinit(handle));
  149. }