Why GPIO data types must be defined again in Arduino environment?

insanoff
Posts: 15
Joined: Sat Aug 10, 2019 9:10 am

Why GPIO data types must be defined again in Arduino environment?

Postby insanoff » Fri May 22, 2020 9:21 pm

Hi Everyone,
I tried to execute the following example code from the Arduino environment, but it threw a data type error ESP32_freeRTOS_test:50:21: error: cannot convert 'GPIO_INT_TYPE' to 'gpio_int_type_t' in assignment for all GPIO configuration lines. I fixed them by adding the appropriate data types. Another problem was the definition of the pins, as I had to change them to GPIO_NUM_xx type.
But why it doesn't work for me? What difference in Arduino causes the problem?

Thanks!
Adam

Original code: https://github.com/espressif/esp-idf/bl ... ple_main.c

My adapted working code:

Code: Select all

/* GPIO Example
   This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.

   URL: https://github.com/espressif/esp-idf/blob/master/examples/peripherals/gpio/main/gpio_example_main.c
*/

#define GPIO_OUTPUT_IO_0				GPIO_NUM_2
#define GPIO_OUTPUT_PIN_SEL				(1ULL<<GPIO_OUTPUT_IO_0)
#define GPIO_INPUT_IO_0					GPIO_NUM_34
#define GPIO_INPUT_PIN_SEL				(1ULL<<GPIO_INPUT_IO_0)
#define ESP_INTR_FLAG_DEFAULT			0

static xQueueHandle gpio_evt_queue = NULL;

static void IRAM_ATTR gpio_isr_handler(void* arg)
{
  uint32_t gpio_num = (uint32_t) arg;
  xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
}

static void gpio_task_example(void* arg)
{
  uint32_t io_num;
  for (;;) {
    if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
      printf("GPIO[%d] intr, val: %d\n", io_num, gpio_get_level((gpio_num_t)io_num));
    }
  }
}

void setup() {
  gpio_config_t io_conf;
  //disable interrupt
  io_conf.intr_type = (gpio_int_type_t)GPIO_PIN_INTR_DISABLE;
  //set as output mode
  io_conf.mode = GPIO_MODE_OUTPUT;
  //bit mask of the pins that you want to set,e.g.GPIO18/19
  io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
  //disable pull-down mode
  io_conf.pull_down_en = (gpio_pulldown_t)0;
  //disable pull-up mode
  io_conf.pull_up_en = (gpio_pullup_t)0;
  //configure GPIO with the given settings
  gpio_config(&io_conf);

  //interrupt of rising edge
  io_conf.intr_type = (gpio_int_type_t)GPIO_PIN_INTR_POSEDGE;
  //bit mask of the pins, use GPIO4/5 here
  io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
  //set as input mode
  io_conf.mode = GPIO_MODE_INPUT;
  //enable pull-up mode
  io_conf.pull_up_en = (gpio_pullup_t)1;
  gpio_config(&io_conf);

  //change gpio intrrupt type for one pin
  gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);

  //create a queue to handle gpio event from isr
  gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
  //start gpio task
  xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);

  //install gpio isr service
  gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
  //hook isr handler for specific gpio pin
  gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);

  //remove isr handler for gpio number.
  gpio_isr_handler_remove(GPIO_INPUT_IO_0);
  //hook isr handler for specific gpio pin again
  gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void*) GPIO_INPUT_IO_0);

  int cnt = 0;
  while (1) {
    printf("cnt: %d\n", cnt++);
    vTaskDelay(1000 / portTICK_RATE_MS);
    gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
  }
  //  gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
}

void loop() {}

insanoff
Posts: 15
Joined: Sat Aug 10, 2019 9:10 am

Re: Why GPIO data types must be defined again in Arduino environment?

Postby insanoff » Sat May 23, 2020 12:16 am

OK, I got it. That's because Arduino uses C++, but ESP32 code is written in C.

Who is online

Users browsing this forum: Bing [Bot], MicroController and 255 guests