Use this forum to chat about hardware specific topics for the ESP8266 (peripherals, memory, clocks, JTAG, programming)

User avatar
By viktak
#19318 I have success in building some custom applications using LUA and now I move forward to programming this little device in C. I studied C a million years ago and now it's slowly coming back to me :)

I have an ESP-01 board and am trying to read the value of GPIO0. The SDK documentation says this:
Code: Select allGPIO_INPUT_GET(gpio_no)

but I don't understand what I should write in place of gpio_no. I have tried simply 0, GPIO0, BIT0, none of these work.
Also, what should I expect to receive? Do I need to mask the result?

This is my current code, the interrupt handler:
Code: Select all void gpio0_int_handler(uint32_t *args)
{
    //  Disable GPIO interrupts while we set them up
    ETS_GPIO_INTR_DISABLE();

    //  Interrupt on NO edge
    gpio_pin_intr_state_set(GPIO_ID_PIN(0), GPIO_PIN_INTR_DISABLE);
   
    //  Record status of button
    int status;
    status = GPIO_INPUT_GET(0);
   
    uint32 gpio_status;
    gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);

     MQTT_Client* client = (MQTT_Client*)args;
   
    //  Publish message to MQTT
    if (status==0)
        MQTT_Publish(client, "viktak/test", "Switch CLOSED.", 14, 2, 0);
    else
        MQTT_Publish(client, "viktak/test", "Switch OPEN.", 14, 2, 0);
   
    os_delay_us(5000);

    //clear interrupt status
    GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status);

    //  Interrupt on any edge
    gpio_pin_intr_state_set(GPIO_ID_PIN(0), GPIO_PIN_INTR_ANYEDGE);

    //  Enable GPIO interrupts
    ETS_GPIO_INTR_ENABLE();
}

The interrupt handler (apart from testing for the GPIO state) works perfectly.

Any directions greatly appreciated
User avatar
By viktak
#19430 Ok, finally I figured it out myself. The problem was that I checked for the status of the button BEFORE I did the debouncing and not AFTER. Once I changed the order of a few lines it worked like a charm:

Code: Select allvoid gpio0_int_handler(uint32_t *args)
{
    //  Disable GPIO interrupts while we set them up
    ETS_GPIO_INTR_DISABLE();

    //  Interrupt on NO edge
    gpio_pin_intr_state_set(GPIO_ID_PIN(0), GPIO_PIN_INTR_DISABLE);
   
    os_delay_us(5000);
 
    uint32 gpio_status;
    gpio_status = GPIO_REG_READ(GPIO_STATUS_ADDRESS);

    //  Record status of button
    int status;
    status = GPIO_INPUT_GET(0);

    MQTT_Client* client = (MQTT_Client*)args;
   
    //  Publish message to MQTT
    if (status==0)
        MQTT_Publish(client, "viktak/test", "Switch now CLOSED.", 18, 2, 0);
    else
        MQTT_Publish(client, "viktak/test", "Switch now OPEN.", 18, 2, 0);
   
    //clear interrupt status
    GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, gpio_status);

    //  Interrupt on any edge
    gpio_pin_intr_state_set(GPIO_ID_PIN(0), GPIO_PIN_INTR_ANYEDGE);

    //  Enable GPIO interrupts
    ETS_GPIO_INTR_ENABLE();
}



I leave the answer here in the hope it would come useful to some in the future.