Article Index

Page 247

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "hal/adc_types.h"
#include "esp_adc/adc_oneshot.h"

void app_main(void)
{
    adc_oneshot_unit_handle_t adc1_handle;
    adc_oneshot_unit_init_cfg_t init_config1 = {
        .unit_id = ADC_UNIT_1,
        .ulp_mode = ADC_ULP_MODE_DISABLE,
    };
    adc_oneshot_new_unit(&init_config1, &adc1_handle);

    adc_oneshot_chan_cfg_t config = {
        .bitwidth = ADC_BITWIDTH_DEFAULT,
    };
    adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_6, &config);

    int data;
    while (true)
    {
        adc_oneshot_read(adc1_handle, ADC_CHANNEL_6, &data);
        printf("%d\n", data);
        fflush(stdout);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

 

Page 248

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "hal/adc_types.h"
#include "esp_adc/adc_oneshot.h"

#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"

void app_main(void)
{
    adc_cali_scheme_ver_t scheme_mask;
    adc_cali_check_scheme(&scheme_mask);
    if (scheme_mask & ADC_CALI_SCHEME_VER_CURVE_FITTING)
    {
        printf("Curve Fit %d\n", scheme_mask);
    }
    else
    {
        if (scheme_mask & ADC_CALI_SCHEME_VER_LINE_FITTING)
        {
            printf("Line Fit %d\n", scheme_mask);
        }
    }
}

 

 

Page 249

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "hal/adc_types.h"
#include "esp_adc/adc_oneshot.h"

#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"

void app_main(void)
{
    adc_oneshot_unit_handle_t adc1_handle;
    adc_oneshot_unit_init_cfg_t init_config1 = {
        .unit_id = ADC_UNIT_1,
        .ulp_mode = ADC_ULP_MODE_DISABLE,
    };
    adc_oneshot_new_unit(&init_config1, &adc1_handle);

    adc_oneshot_chan_cfg_t config = {
        .bitwidth = ADC_BITWIDTH_12,
        .atten = ADC_ATTEN_DB_0};
    adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_6, &config);

    /* adc_cali_line_fitting_config_t calfit = {
       .unit_id = ADC_UNIT_1,
        .atten = ADC_ATTEN_DB_0,
        .bitwidth = ADC_BITWIDTH_12
    }; */

    adc_cali_curve_fitting_config_t calfit = {
        .unit_id = ADC_UNIT_1,
        .atten = ADC_ATTEN_DB_0,
        .bitwidth = ADC_BITWIDTH_12};

    adc_cali_handle_t cali_handle;
    // adc_cali_create_scheme_line_fitting(&calfit, &cali_handle);
    adc_cali_create_scheme_curve_fitting(&calfit, &cali_handle);

    int data;
    while (true)
    {
        adc_oneshot_get_calibrated_result(adc1_handle, cali_handle,
                                          ADC_CHANNEL_6, &data);
        printf("data cal %d  mV\n", data);
        fflush(stdout);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

 

Page 251 From fragement 

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "hal/adc_types.h"
#include "esp_adc/adc_oneshot.h"

#include "esp_adc/adc_cali.h"
#include "esp_adc/adc_cali_scheme.h"
#include "driver/gptimer.h"

gptimer_handle_t tick_us_start(void)
{
    gptimer_config_t timer_config = {
        .clk_src = GPTIMER_CLK_SRC_DEFAULT,
        .direction = GPTIMER_COUNT_UP,
        .resolution_hz = 1000000,
    };
    gptimer_handle_t gptimer_us = NULL;
    gptimer_new_timer(&timer_config, &gptimer_us);
    gptimer_enable(gptimer_us);
    gptimer_start(gptimer_us);
    return gptimer_us;
}

int64_t tick_us(gptimer_handle_t gptimer_us, int64_t offset)
{
    uint64_t count;
    gptimer_get_raw_count(gptimer_us, &count);
    return count + offset;
}
void app_main(void)
{
    adc_oneshot_unit_handle_t adc1_handle;
    adc_oneshot_unit_init_cfg_t init_config1 = {
        .unit_id = ADC_UNIT_1,
        .ulp_mode = ADC_ULP_MODE_DISABLE,
    };
    adc_oneshot_new_unit(&init_config1, &adc1_handle);

    adc_oneshot_chan_cfg_t config = {
        .bitwidth = ADC_BITWIDTH_12,
        .atten = ADC_ATTEN_DB_0};
    adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_6, &config);

    /* adc_cali_line_fitting_config_t calfit = {
       .unit_id = ADC_UNIT_1,
        .atten = ADC_ATTEN_DB_0,
        .bitwidth = ADC_BITWIDTH_12
    }; */

    adc_cali_curve_fitting_config_t calfit = {
        .unit_id = ADC_UNIT_1,
        .atten = ADC_ATTEN_DB_0,
        .bitwidth = ADC_BITWIDTH_12};

    adc_cali_handle_t cali_handle;
    // adc_cali_create_scheme_line_fitting(&calfit, &cali_handle);
    adc_cali_create_scheme_curve_fitting(&calfit, &cali_handle);

    gptimer_handle_t timer = tick_us_start();

    int data;
    int64_t t = tick_us(timer, 0);
    for (int i = 0; i < 100000; i++)
    {
        adc_oneshot_read(adc1_handle, ADC_CHANNEL_6, &data);
    }
    t = tick_us(timer, 0) - t;
    printf("t=%lld \n", t / 100000);
}

 

Page 252 Note: Only works on an ESP32 or ESP32 S2

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/dac_oneshot.h"

void app_main(void)
{
  dac_oneshot_config_t oneshot_cfg = {
       .chan_id = DAC_CHAN_1
  };
  dac_oneshot_handle_t DAC_handle;
  dac_oneshot_new_channel(&oneshot_cfg, &DAC_handle);
 
  uint8_t value = 0;
  while (true) {
    dac_oneshot_output_voltage(DAC_handle, value);
    value++;
  }
}

 

Page 254 Note: Only works on an ESP32 or ESP32 S2

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/dac_oneshot.h"
#include "math.h"
uint8_t wave[256];

void app_main(void)
{
  for (int i = 0; i < 256; i++)
  {
    wave[i] = (uint8_t)((128.0 + 127 * sinf((float)i
                              * 2.0 * 3.14159 / 255.0)));
  }

  dac_oneshot_config_t oneshot_cfg = {
       .chan_id = DAC_CHAN_1
  };
  dac_oneshot_handle_t DAC_handle;
  dac_oneshot_new_channel(&oneshot_cfg, &DAC_handle);


  while (true) {
    for (int i = 0;i < 256;i++) {
      dac_oneshot_output_voltage(DAC_handle, wave[i]);
    }
  }
}

 

 

Page 255 Note: Only works on an ESP32 or ESP32 S2

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/dac_oneshot.h"
#include "math.h"
uint8_t wave[256];

void app_main(void)
{
  for (int i = 0; i < 256; i++)
  {
    wave[i] = (uint8_t)((128.0 + 127 * sinf((float)i
                              * 2.0 * 3.14159 / 255.0)));
  }

  dac_oneshot_config_t oneshot_cfg = {
       .chan_id = DAC_CHAN_1
  };
  dac_oneshot_handle_t DAC_handle;
  dac_oneshot_new_channel(&oneshot_cfg, &DAC_handle);


  while (true) {
    for (int i = 0;i < 256;i++) {
      dac_oneshot_output_voltage(DAC_handle, wave[i]);
    }
  }
}

 

Page 259 ESP32 only

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/touch_sensor.h"

void app_main(void)
{
  touch_pad_init();
  touch_pad_set_voltage(TOUCH_HVOLT_2V4,
                TOUCH_LVOLT_0V5, TOUCH_HVOLT_ATTEN_1V);
  touch_pad_config(TOUCH_PAD_NUM5, 0);
  uint16_t touch_value=0;
  while (true) {
       touch_pad_read(TOUCH_PAD_NUM5, &touch_value);
    printf("val_touch_gpio12 = %d \n", touch_value);
    vTaskDelay(1000 / portTICK_PERIOD_MS);
  }
}

 

Page 259 ESP32 S3 only

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/touch_sensor.h"

void app_main(void)
{
    touch_pad_init();
    touch_pad_config(TOUCH_PAD_NUM5);
    touch_pad_set_fsm_mode(TOUCH_FSM_MODE_SW);
    uint32_t touch_value = 0;
    while (true)
    {
        touch_pad_sw_start();
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        touch_pad_read_raw_data(TOUCH_PAD_NUM5, &touch_value);
        printf("val_touch_gpio5 = %ld \n", touch_value);
    }
}

 

Page 260 ESP32 only

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/touch_sensor.h"
#include "driver/gpio.h"

void ISR(void *arg)
{
    gpio_set_level(2, !gpio_get_level(2));
}

void app_main(void)
{
    gpio_reset_pin(2);
    gpio_set_direction(2, GPIO_MODE_INPUT_OUTPUT);
    touch_pad_init();
    touch_pad_set_voltage(TOUCH_HVOLT_2V4, TOUCH_LVOLT_0V5,
                          TOUCH_HVOLT_ATTEN_1V);

    touch_pad_config(TOUCH_PAD_NUM5, 800);
    touch_pad_set_trigger_mode(TOUCH_TRIGGER_BELOW);
    touch_pad_isr_register(ISR, NULL);
    touch_pad_intr_enable();
    uint16_t touch_value = 0;
    while (true)
    {
        uint32_t status = touch_pad_get_status();
        touch_pad_read(TOUCH_PAD_NUM5, &touch_value);
        printf("val_touch_gpio12 = %d \n", touch_value);
        printf("status = %ld\n", status);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

Page 262 ESP32 S3 only

 

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "driver/touch_sensor.h"
#include "driver/gpio.h"

void ISR(void *arg)
{
    gpio_set_level(2, !gpio_get_level(2));
    touch_pad_clear_status();
}

void app_main(void)
{
    gpio_reset_pin(2);
    gpio_set_direction(2, GPIO_MODE_INPUT_OUTPUT);

    touch_pad_init();
    touch_pad_config(TOUCH_PAD_NUM5);
    touch_pad_set_thresh(TOUCH_PAD_NUM5, 30000);
    touch_pad_set_channel_mask(1 << 5);
    touch_pad_isr_register(ISR, NULL, TOUCH_PAD_INTR_MASK_ALL);
    touch_pad_intr_enable(TOUCH_PAD_INTR_MASK_ACTIVE);
    touch_pad_set_fsm_mode(TOUCH_FSM_MODE_TIMER);
    touch_pad_fsm_start();
}