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.
Esp8266 blog. Learn how to compile, how to work with the wireless chip esp8266. ESP-01 ESP-03, ESP-07, ESP-12, ESP201 all are here
Showing posts with label compiling. Show all posts
Showing posts with label compiling. Show all posts
Thursday, March 12, 2015
Thursday, March 5, 2015
Making your own environment for ESP8266.
If you don't want to use my setup described here, you can make your own setup using your own distribution.
You can make your own environment for compiling your code or the demo code for the ESP8266.
Step 1.
Download ova image ( lunbuntu 14.04) for VirtualBox or install your preferred Linux distribution.
User is esp8266 and password is espressif
sudo apt-get install git autoconf build-essential gperf bison flex texinfo libtool libncurses5-dev
wget gawk libc6-dev python-serial libexpat1-dev
Step 2.
sudo mkdir /opt/Espressif
Step 3.
sudo chown esp8266 /opt/Espressif/
Step 4.
cd /opt/Espressif
Step 5.
git clone -b lx106 git://github.com/jcmvbkbc/crosstool-NG.git
Step 6.
cd crosstool-NG/
./bootstrap && ./configure --prefix=`pwd` && make && make install
Step 7.
./ct-ng xtensa-lx106-elf
Step 8.
./ct-ng build
Note: It takes about 25 minutes !!!!!!!
Step 9.
Edit the .profile file from /home/esp8266 and add the line
PATH="$HOME/bin:/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/bin:$PATH"
Step 10.
cd /opt/Espressif
wget -O esp_iot_sdk_v0.9.5_15_01_23.zip https://github.com/esp8266/esp8266-
wiki/raw/master/sdk/esp_iot_sdk_v0.9.5_15_01_23.zip
Note: is the SDK from 23 Jan 2015. Verify if there is a new one.
Step 11.
unzip esp_iot_sdk_v0.9.5_15_01_23.zip
mv esp_iot_sdk_v0.9.5 ESP8266_SDK/
Step 12.
cd /opt/Espressif/ESP8266_SDK
wget -O lib/libc.a https://github.com/esp8266/esp8266-wiki/raw/master/libs/libc.a
wget -O lib/libhal.a https://github.com/esp8266/esp8266-wiki/raw/master/libs/libhal.a
wget -O include.tgz https://github.com/esp8266/esp8266-wiki/raw/master/include.tgz
tar -xvzf include.tgz
rm include.tgz
Step 13.
cd /opt/Espressif
git clone https://github.com/themadinventor/esptool esptool-py
chown -R esp8266 crosstool-NG/
sudo ln -s $PWD/esptool-py/esptool.py crosstool-NG/builds/xtensa-lx106-elf/bin/
Step 14.
The SDK esp_iot_sdk_v0.9.5_15_01_23.zip has a directory named Document with some pdf files about API, AT commands and release notes.
Step 15.
Copy the "at" directory from /opt/Espressif/ESP8266_SDK/examples and compile it
cp -R /opt/Espressif/ESP8266_SDK/examples/at /opt/Espressif/ESP8266_SDK/
cd /opt/Espressif/ESP8266_SDK/at
make clean
make
The result resides in the /opt/Espressif/ESP8266_SDK/bin directory
Flashing the code. Note that the above command is on long line. You can put the command into a file flashEsp, give that file executable rights and use it with ./flashEsp
sudo /opt/Espressif/esptool-py/esptool.py --port /dev/ttyUSB0 write_flash 0x3C000 /opt/Espressif/ESP8266_SDK/bin/blank.bin 0x00000 /opt/Espressif/ESP8266_SDK/bin/eagle.flash.bin 0x40000 /opt/Espressif/ESP8266_SDK/bin/eagle.irom0text.bin OR /opt/Espressif/esptool-py/esptool.py -p /dev/ttyUSB0 write_flash 0x40000 ../bin/eagle.irom0text.bin write_flash 0x00000 ../bin/eagle.flash.bin
I hope I didn't missed any step.
You can make your own environment for compiling your code or the demo code for the ESP8266.
Step 1.
Download ova image ( lunbuntu 14.04) for VirtualBox or install your preferred Linux distribution.
User is esp8266 and password is espressif
sudo apt-get install git autoconf build-essential gperf bison flex texinfo libtool libncurses5-dev
wget gawk libc6-dev python-serial libexpat1-dev
Step 2.
sudo mkdir /opt/Espressif
Step 3.
sudo chown esp8266 /opt/Espressif/
Step 4.
cd /opt/Espressif
Step 5.
git clone -b lx106 git://github.com/jcmvbkbc/crosstool-NG.git
Step 6.
cd crosstool-NG/
./bootstrap && ./configure --prefix=`pwd` && make && make install
Step 7.
./ct-ng xtensa-lx106-elf
Step 8.
./ct-ng build
Note: It takes about 25 minutes !!!!!!!
Step 9.
Edit the .profile file from /home/esp8266 and add the line
PATH="$HOME/bin:/opt/Espressif/crosstool-NG/builds/xtensa-lx106-elf/bin:$PATH"
Step 10.
cd /opt/Espressif
wget -O esp_iot_sdk_v0.9.5_15_01_23.zip https://github.com/esp8266/esp8266-
wiki/raw/master/sdk/esp_iot_sdk_v0.9.5_15_01_23.zip
Note: is the SDK from 23 Jan 2015. Verify if there is a new one.
Step 11.
unzip esp_iot_sdk_v0.9.5_15_01_23.zip
mv esp_iot_sdk_v0.9.5 ESP8266_SDK/
Step 12.
cd /opt/Espressif/ESP8266_SDK
wget -O lib/libc.a https://github.com/esp8266/esp8266-wiki/raw/master/libs/libc.a
wget -O lib/libhal.a https://github.com/esp8266/esp8266-wiki/raw/master/libs/libhal.a
wget -O include.tgz https://github.com/esp8266/esp8266-wiki/raw/master/include.tgz
tar -xvzf include.tgz
rm include.tgz
Step 13.
cd /opt/Espressif
git clone https://github.com/themadinventor/esptool esptool-py
chown -R esp8266 crosstool-NG/
sudo ln -s $PWD/esptool-py/esptool.py crosstool-NG/builds/xtensa-lx106-elf/bin/
Step 14.
The SDK esp_iot_sdk_v0.9.5_15_01_23.zip has a directory named Document with some pdf files about API, AT commands and release notes.
Step 15.
Copy the "at" directory from /opt/Espressif/ESP8266_SDK/examples and compile it
cp -R /opt/Espressif/ESP8266_SDK/examples/at /opt/Espressif/ESP8266_SDK/
cd /opt/Espressif/ESP8266_SDK/at
make clean
make
The result resides in the /opt/Espressif/ESP8266_SDK/bin directory
Flashing the code. Note that the above command is on long line. You can put the command into a file flashEsp, give that file executable rights and use it with ./flashEsp
sudo /opt/Espressif/esptool-py/esptool.py --port /dev/ttyUSB0 write_flash 0x3C000 /opt/Espressif/ESP8266_SDK/bin/blank.bin 0x00000 /opt/Espressif/ESP8266_SDK/bin/eagle.flash.bin 0x40000 /opt/Espressif/ESP8266_SDK/bin/eagle.irom0text.bin OR /opt/Espressif/esptool-py/esptool.py -p /dev/ttyUSB0 write_flash 0x40000 ../bin/eagle.irom0text.bin write_flash 0x00000 ../bin/eagle.flash.bin
I hope I didn't missed any step.
Use my ESP8266 environment !!!
User ESP8266
password espressif
Let's see how you can compile our first example of code.
To do this download the virtual box ova from my drive.
Apply this ova file to a Virtual Box.
Start the virtual machine
go to /opt/Espressif/ESP8266_SDK/ where there are two directories with examples:
IoTDemo - a web server controlling some GPIO's
at - build a custom AT command along with all command that are supportded by the at library
go to /opt/Espressif/ESP8266_SDK/IoTDemo/
make clean
make
two files are generated into the /opt/Espressif/ESP8266_SDK/bin directory.
!!!
No boot needed.
Generate eagle.flash.bin and eagle.irom0text.bin successully in folder bin.
eagle.flash.bin-------->0x00000
eagle.irom0text.bin---->0x40000
!!!
Use those files and the optional blank image blank.bin or you can use this command
to write the files.
sudo /opt/Espressif/esptool-py/esptool.py --port /dev/ttyUSB0 write_flash 0x3C000 /opt/Espressif/ESP8266_SDK/bin/blank.bin 0x00000 /opt/Espressif/ESP8266_SDK/bin/eagle.flash.bin 0x40000 /opt/Espressif/ESP8266_SDK/bin/
eagle.irom0text.bin
Write the files on ESP-01.
1. Power off the ESP-01
2. Connect GPIO0 to GND
3. Power on the ESP-01
4. Burn firmware
Write the files on ESP201 Dev Board.
1. Power off the ESP-201
2. Keep pressed S2 and power the board
3. Release the S2 switch
4. Burn firmware
Notes:
password espressif
Let's see how you can compile our first example of code.
To do this download the virtual box ova from my drive.
Apply this ova file to a Virtual Box.
Start the virtual machine
go to /opt/Espressif/ESP8266_SDK/ where there are two directories with examples:
IoTDemo - a web server controlling some GPIO's
at - build a custom AT command along with all command that are supportded by the at library
go to /opt/Espressif/ESP8266_SDK/IoTDemo/
make clean
make
two files are generated into the /opt/Espressif/ESP8266_SDK/bin directory.
!!!
No boot needed.
Generate eagle.flash.bin and eagle.irom0text.bin successully in folder bin.
eagle.flash.bin-------->0x00000
eagle.irom0text.bin---->0x40000
!!!
to write the files.
sudo /opt/Espressif/esptool-py/esptool.py --port /dev/ttyUSB0 write_flash 0x3C000 /opt/Espressif/ESP8266_SDK/bin/blank.bin 0x00000 /opt/Espressif/ESP8266_SDK/bin/eagle.flash.bin 0x40000 /opt/Espressif/ESP8266_SDK/bin/
eagle.irom0text.bin
Write the files on ESP-01.
1. Power off the ESP-01
2. Connect GPIO0 to GND
3. Power on the ESP-01
4. Burn firmware
Write the files on ESP201 Dev Board.
1. Power off the ESP-201
2. Keep pressed S2 and power the board
3. Release the S2 switch
4. Burn firmware
Notes:
- In /opt/Espressif/ESP8266_SDK/document/ there are tow directories English an Chinese with document about the SDK fucntion, AT command. Spend some time looking into them.
- In the Virtual Box image I've added the moserial application that can be used to see the debug information from ESP.
- Launch moserial with: sudo moserial &
- If Software Updater is bugging you, do not update. You are just fine.
Subscribe to:
Posts (Atom)