continuous_read_main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_ADC_UNIT ADC_UNIT_1
  15. #define _EXAMPLE_ADC_UNIT_STR(unit) #unit
  16. #define EXAMPLE_ADC_UNIT_STR(unit) _EXAMPLE_ADC_UNIT_STR(unit)
  17. #define EXAMPLE_ADC_CONV_MODE ADC_CONV_SINGLE_UNIT_1
  18. #define EXAMPLE_ADC_ATTEN ADC_ATTEN_DB_0
  19. #define EXAMPLE_ADC_BIT_WIDTH SOC_ADC_DIGI_MAX_BITWIDTH
  20. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  21. #define EXAMPLE_ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE1
  22. #define EXAMPLE_ADC_GET_CHANNEL(p_data) ((p_data)->type1.channel)
  23. #define EXAMPLE_ADC_GET_DATA(p_data) ((p_data)->type1.data)
  24. #else
  25. #define EXAMPLE_ADC_OUTPUT_TYPE ADC_DIGI_OUTPUT_FORMAT_TYPE2
  26. #define EXAMPLE_ADC_GET_CHANNEL(p_data) ((p_data)->type2.channel)
  27. #define EXAMPLE_ADC_GET_DATA(p_data) ((p_data)->type2.data)
  28. #endif
  29. #define EXAMPLE_READ_LEN 256
  30. #if CONFIG_IDF_TARGET_ESP32
  31. static adc_channel_t channel[2] = {ADC_CHANNEL_6, ADC_CHANNEL_7};
  32. #else
  33. static adc_channel_t channel[2] = {ADC_CHANNEL_2, ADC_CHANNEL_3};
  34. #endif
  35. static TaskHandle_t s_task_handle;
  36. static const char *TAG = "EXAMPLE";
  37. static bool IRAM_ATTR s_conv_done_cb(adc_continuous_handle_t handle, const adc_continuous_evt_data_t *edata, void *user_data)
  38. {
  39. BaseType_t mustYield = pdFALSE;
  40. //Notify that ADC continuous driver has done enough number of conversions
  41. vTaskNotifyGiveFromISR(s_task_handle, &mustYield);
  42. return (mustYield == pdTRUE);
  43. }
  44. static void continuous_adc_init(adc_channel_t *channel, uint8_t channel_num, adc_continuous_handle_t *out_handle)
  45. {
  46. adc_continuous_handle_t handle = NULL;
  47. adc_continuous_handle_cfg_t adc_config = {
  48. .max_store_buf_size = 1024,
  49. .conv_frame_size = EXAMPLE_READ_LEN,
  50. };
  51. ESP_ERROR_CHECK(adc_continuous_new_handle(&adc_config, &handle));
  52. adc_continuous_config_t dig_cfg = {
  53. .sample_freq_hz = 20 * 1000,
  54. .conv_mode = EXAMPLE_ADC_CONV_MODE,
  55. .format = EXAMPLE_ADC_OUTPUT_TYPE,
  56. };
  57. adc_digi_pattern_config_t adc_pattern[SOC_ADC_PATT_LEN_MAX] = {0};
  58. dig_cfg.pattern_num = channel_num;
  59. for (int i = 0; i < channel_num; i++) {
  60. adc_pattern[i].atten = EXAMPLE_ADC_ATTEN;
  61. adc_pattern[i].channel = channel[i] & 0x7;
  62. adc_pattern[i].unit = EXAMPLE_ADC_UNIT;
  63. adc_pattern[i].bit_width = EXAMPLE_ADC_BIT_WIDTH;
  64. ESP_LOGI(TAG, "adc_pattern[%d].atten is :%"PRIx8, i, adc_pattern[i].atten);
  65. ESP_LOGI(TAG, "adc_pattern[%d].channel is :%"PRIx8, i, adc_pattern[i].channel);
  66. ESP_LOGI(TAG, "adc_pattern[%d].unit is :%"PRIx8, i, adc_pattern[i].unit);
  67. }
  68. dig_cfg.adc_pattern = adc_pattern;
  69. ESP_ERROR_CHECK(adc_continuous_config(handle, &dig_cfg));
  70. *out_handle = handle;
  71. }
  72. void app_main(void)
  73. {
  74. esp_err_t ret;
  75. uint32_t ret_num = 0;
  76. uint8_t result[EXAMPLE_READ_LEN] = {0};
  77. memset(result, 0xcc, EXAMPLE_READ_LEN);
  78. s_task_handle = xTaskGetCurrentTaskHandle();
  79. adc_continuous_handle_t handle = NULL;
  80. continuous_adc_init(channel, sizeof(channel) / sizeof(adc_channel_t), &handle);
  81. adc_continuous_evt_cbs_t cbs = {
  82. .on_conv_done = s_conv_done_cb,
  83. };
  84. ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(handle, &cbs, NULL));
  85. ESP_ERROR_CHECK(adc_continuous_start(handle));
  86. while (1) {
  87. /**
  88. * This is to show you the way to use the ADC continuous mode driver event callback.
  89. * This `ulTaskNotifyTake` will block when the data processing in the task is fast.
  90. * However in this example, the data processing (print) is slow, so you barely block here.
  91. *
  92. * Without using this event callback (to notify this task), you can still just call
  93. * `adc_continuous_read()` here in a loop, with/without a certain block timeout.
  94. */
  95. ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
  96. char unit[] = EXAMPLE_ADC_UNIT_STR(EXAMPLE_ADC_UNIT);
  97. while (1) {
  98. ret = adc_continuous_read(handle, result, EXAMPLE_READ_LEN, &ret_num, 0);
  99. if (ret == ESP_OK) {
  100. ESP_LOGI("TASK", "ret is %x, ret_num is %"PRIu32" bytes", ret, ret_num);
  101. for (int i = 0; i < ret_num; i += SOC_ADC_DIGI_RESULT_BYTES) {
  102. adc_digi_output_data_t *p = (adc_digi_output_data_t*)&result[i];
  103. uint32_t chan_num = EXAMPLE_ADC_GET_CHANNEL(p);
  104. uint32_t data = EXAMPLE_ADC_GET_DATA(p);
  105. /* Check the channel number validation, the data is invalid if the channel num exceed the maximum channel */
  106. if (chan_num < SOC_ADC_CHANNEL_NUM(EXAMPLE_ADC_UNIT)) {
  107. ESP_LOGI(TAG, "Unit: %s, Channel: %"PRIu32", Value: %"PRIx32, unit, chan_num, data);
  108. } else {
  109. ESP_LOGW(TAG, "Invalid data [%s_%"PRIu32"_%"PRIx32"]", unit, chan_num, data);
  110. }
  111. }
  112. /**
  113. * Because printing is slow, so every time you call `ulTaskNotifyTake`, it will immediately return.
  114. * To avoid a task watchdog timeout, add a delay here. When you replace the way you process the data,
  115. * usually you don't need this delay (as this task will block for a while).
  116. */
  117. vTaskDelay(1);
  118. } else if (ret == ESP_ERR_TIMEOUT) {
  119. //We try to read `EXAMPLE_READ_LEN` until API returns timeout, which means there's no available data
  120. break;
  121. }
  122. }
  123. }
  124. ESP_ERROR_CHECK(adc_continuous_stop(handle));
  125. ESP_ERROR_CHECK(adc_continuous_deinit(handle));
  126. }