Thursday, March 12, 2015

Arduino function for ESP8266

        If programming the GPIO is hard for you as a beginner but you are familiar with Arduino functions, the following functions can be used as have the same name as in the Arduino IDE.

First define some data in an .h file.

#define HIGH 0x1
#define LOW 0x0
#define INPUT 0x1
#define OUTPUT 0x0
                                                   /*   GPIO | SDK board    |       */
                                                   /*   --------------------|       */
int gpio_pin_register[16] = {PERIPHS_IO_MUX_GPIO0_U,    // 0  gpio 0        | 
                             PERIPHS_IO_MUX_U0TXD_U,    // 1  gpio 1        | 
                             PERIPHS_IO_MUX_GPIO2_U,    // 2  gpio 2        | 
                             PERIPHS_IO_MUX_U0RXD_U,    // 3                |
                             PERIPHS_IO_MUX_GPIO4_U,    // 4  gpio 4        | 
                             PERIPHS_IO_MUX_GPIO5_U,    // 5  gpio 5 buzzer | 
                             PERIPHS_IO_MUX_SD_CLK_U,   // 6                |
                             PERIPHS_IO_MUX_SD_DATA0_U, // 7                |
                             PERIPHS_IO_MUX_SD_DATA1_U, // 8                |
                             PERIPHS_IO_MUX_SD_DATA2_U, // 9  gpio 9        | 
                             PERIPHS_IO_MUX_SD_DATA3_U, // 10 gpio 10       | 
                             PERIPHS_IO_MUX_SD_CMD_U,   // 11               |
                             PERIPHS_IO_MUX_MTDI_U,     // 12 gpio12  blue  | 
                             PERIPHS_IO_MUX_MTCK_U,     // 13 gpio13  green | 
                             PERIPHS_IO_MUX_MTMS_U,     // 14 gpio14  white | 
                             PERIPHS_IO_MUX_MTDO_U};    // 15 gpio15  red   | 
                             

#define GPIO_PIN_ADDR(i)    (GPIO_PIN0_ADDRESS + i*4)


Now the Arduino like functions can be added:

/********************************************************************
* FunctionName: pinMode
* Description : set up a pin mode.
* Parameters  : pin: pin mumber
*             : mode 0 for OUTPUT or 1 for INPUT
*             : pullup 0 for pullup DISABLED and 1 for pullup ENABLED
*             : pulldown not yet supported
* Returns     : None                                                     
*********************************************************************/

void ICACHE_FLASH_ATTR pinMode(uint8_t pin, uint8_t mode, uint8_t pullup) {
    if ((0x1 << pin) & 0b110101) {
        PIN_FUNC_SELECT(gpio_pin_register[pin], 0); //0,2,4,5
    } else {
        PIN_FUNC_SELECT(gpio_pin_register[pin], 3);
    }
    PIN_PULLDWN_DIS(gpio_pin_register[pin]);
    if(pullup)
      PIN_PULLUP_EN(gpio_pin_register[pin]);
    else  
      PIN_PULLUP_DIS(gpio_pin_register[pin]);
    if (mode) {
        GPIO_REG_WRITE(GPIO_ENABLE_W1TC_ADDRESS, 1<<pin); // GPIO input
    } else {
        GPIO_REG_WRITE(GPIO_ENABLE_W1TS_ADDRESS, 1<<pin); // GPIO output
    }
}


Example:
pinMode(0, OUTPUT, 0); //is setting the GPIO 0 as an OUTPUT with pullup disabled

If you want to control also the pulldown add an extra parameter and call the 
PIN_PULLDWN_DIS(gpio_pin_register[pin])for disable and PIN_PULLDWN_EN(gpio_pin_register[pin])for pulldown enable.


Next function are the well known Arduino function for read and write:

/********************************************************************
* FunctionName: digitalWrite
* Description : use to set up HIGH or LOW on a GPIO pin
* Parameters  : pin: pin mumber
*             : state HIGH or LOW
* Returns     : None                                                     
*********************************************************************/

void ICACHE_FLASH_ATTR 
digitalWrite(uint8_t pin, uint8_t state) {
    if (state) {
        GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1<<pin); // set GPIO pin high
    } else {
        GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1<<pin); // set GPIO pin low
    }
}
/********************************************************************
* FunctionName: digitalRead
* Description : use to set up HIGH or LOW on a GPIO pin
* Parameters  : pin: pin mumber
* Returns     : 1 if pin is HIGH or 0 if pin is LOW                                                     
*********************************************************************/

int ICACHE_FLASH_ATTR 
digitalRead(uint8_t pin) {
    return (GPIO_REG_READ(GPIO_OUT_ADDRESS)>>pin) & 1;
}



Example:

digitalWrite(0, HIGH); //set HIGH the GPIO 0 pin

int gpio_state = digitalRead(0); will return 1 if the GPIO 0 pin's is HIGH or 0 if is LOW

If you save your .h file in the include directory, just use #include "your_file_name.h" in your user_main.c.


For controlling GPIO16 see this post.








No comments:

Post a Comment