no success setting a GPIO

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

no success setting a GPIO

Postby mzimmers » Wed May 30, 2018 11:00 pm

Hi all -

Working on the ESP32-DevKitC and trying to toggle a GPIO pin.

Here's the initialization code:

Code: Select all

#define BUZZER_NUM (GPIO_NUM_5)
#define BUZZER_PIN (GPIO_SEL_5)
...
Buzzer::Buzzer()
{
    // create the queue for receiving button push notifications.
    m_buzzerQueue = xQueueCreate(10, sizeof(BuzzerQueueEntry));
    // configure the io_conf structure.
    io_conf.pin_bit_mask = BUZZER_PIN;
    io_conf.intr_type = static_cast<gpio_int_type_t>(GPIO_PIN_INTR_DISABLE);
    io_conf.mode = GPIO_MODE_OUTPUT;
    io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
    io_conf.pull_down_en = GPIO_PULLDOWN_ENABLE;
    //configure GPIO with the given settings
    esp_err_t err = gpio_config(&io_conf);
    if (err != ESP_OK)
    {
        ESP_LOGE(TAG, "error %d configuring GPIO.", err);
    }
}
Here's the code that does the work:

Code: Select all

void Buzzer::buzzer_task()
{
    BuzzerQueueEntry entry;
    esp_err_t err;
    uint32_t level;
    while (1)
    {
        entry.value = 0; // reset between queue reads.

        // look for a change request from our queue. Process if present.
        if (xQueueReceive(m_buzzerQueue, &entry, portMAX_DELAY) == pdTRUE)
        {
            if (entry.value > 0)
            {
                // turn buzzer on
                ESP_LOGI(TAG, "turning buzzer on.");
                level = 1;
                err = gpio_set_level(BUZZER_NUM, level);
                if (err != ESP_OK)
                {
                    ESP_LOGE(TAG, "error enabling GPIO.");
                }
                ESP_LOGI(TAG, "GPIO level is %d", gpio_get_level(BUZZER_NUM));
            }
            else
            {
                // turn buzzer off
                ESP_LOGI(TAG, "turning buzzer off.");
                level = 0;
                err = gpio_set_level(BUZZER_NUM, level);
                if (err != ESP_OK)
                {
                    ESP_LOGE(TAG, "error disabling GPIO.");
                }
                ESP_LOGI(TAG, "GPIO level is %d", gpio_get_level(BUZZER_NUM));
            }
        }
    }
}
and here's the call:

Code: Select all

    BuzzerQueueEntry buzz;
    buzz.value = 1;
    xQueueSendToBack(buzzQueue, &buzz, 0);
    vTaskDelay(20);
    buzz.value = 0;
    xQueueSendToBack(buzzQueue, &buzz, 0);
It produces this output:
I (386) Buzzer: turning buzzer on.
I (386) Buzzer: GPIO level is 0
I (586) Buzzer: turning buzzer off.
I (586) Buzzer: GPIO level is 0
(No error messages indicating that my get/set calls are failing.)
Any ideas what I'm doing wrong?

Thanks...

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: no success setting a GPIO

Postby kolban » Wed May 30, 2018 11:37 pm

Its likely that the semantic for reading from a GPIO pin defined as output is undefined or simply false (0). I think I saw a post in the last few weeks asking if it were possible to read the value of a previously written GPIO pin and the answer was yes but it used some mysterious (to me) settings. Another post suggested that since you set the GPIO value previously, you already know the signal on it and could save that value in some variable and just reference it.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

User avatar
mzimmers
Posts: 643
Joined: Wed Mar 07, 2018 11:54 pm
Location: USA

Re: no success setting a GPIO

Postby mzimmers » Thu May 31, 2018 12:53 am

Bingo. I changed it to input and output, and it reads properly on the eval board. (I'll change it back on the target board.) Thanks, Neil.

Who is online

Users browsing this forum: Google [Bot] and 116 guests