# Arduino style programming for ESP8266
## Description
This is simple code library that uses Espressif drivers without any modification, so you can use it with fresh copy of the SDK. The point of this library is to ease the use of the gpio, i2c, spi, uart and flash memory. It's written with **esp_iot_sdk_v0.9.5**. We don't guarantee compatibility with older SDK.
## Installation
In this file we will not discuss how to install working toolchain. If you don't know how, we recommend using [this](http://www.esp8266.com/wiki/doku.php?id=toolchain).
Clone this repository:
``` bash
# mkdir -p /home/foo/checkout
# cd /home/foo/checkout
# svn checkout https://github.com/OLIMEX/ESP8266/trunk/arduino_style
```
Let's say that you've previously copied sdk to **/home/foo/sdk/**. You should see the this tree:
``` bash
# cd /home/foo/sdk/
# tree -d
.
├── app
├── bin
│ ├── at
│ └── upgrade
├── document
│ ├── Chinese
│ └── English
├── examples
│ ├── at
│ │ ├── include
│ │ └── user
│ ├── IoT_Demo
│ │ ├── driver
│ │ ├── include
│ │ │ ├── driver
│ │ │ └── ssl
│ │ └── user
│ └── smart_config
│ ├── include
│ └── user
├── include
│ └── json
├── ld
├── lib
└── tools
```
Let's build IoT_Demo. Copy example folder to the root folder of the SDK:
``` bash
# cp -r examples/IoT_Demo .
```
If you installed the toolchain correctly the build should work perfect:
``` bash
# make COMPILE=gcc
!!!
No boot needed.
Generate eagle.flash.bin and eagle.irom0text.bin successfully in folder bin.
eagle.flash.bin-------->0x00000
eagle.irom0text.bin---->0x40000
!!!
```
Now to use arduino_style library copy **arduino** folder and **arduinostyle.h** from this repository into **user** directory:
``` bash
# cp -r /home/foo/checkout/arduino /home/foo/sdk/IoT_Demo/user/
# cp /home/foo/checkout/arduinostyle.h /home/foo/sdk/IoT_Demo/user/
```
Now what's left is to make a little modification to the **Makefile**:
``` bash
# nano Makefile
```
Find **COMPONENTS_eagle.app.v6** and edit it to looks like this:
``` c
COMPONENTS_eagle.app.v6 = \
user/libuser.a \
user/arduino/libarduino.a \
driver/libdriver.a
```
Type again *make* and everything should be fine:
``` bash
# make COMPILE=gcc
```
Now to use this library simply include *arduinostyle.h* in your project
``` c
#include "arduinostyle.h
```
## Available functions
### GPIO
The following functions are available for using gpio:
#### Set direction
``` c
pinMode(uint8 pin, pinmode_t mode)
```
* pin - Can be between 0 and 5 and between 12 and 15. The other pins are used for the spi flash.
* mode - Can be *INPUT*, *OUTPUT*, *INPUT_PULLUP*. You can also use 0 for input, 1 - output and 2 - input with pull-up resistor.
#### Set output level
```c
digitalWrite(uint8 pin, pinvalue_t value)
```
* pin - Can be between 0 and 5 and between 12 and 15. The other pins are used for the spi flash.
* value - Can be *OUTPUT*, *INPUT* or 0 and 1. In pin is set as input it enables/disables pull-up resistor.
#### Read input level
``` c
digitalRead(uint8 pin)
```
* pin - Can be between 0 and 5 and between 12 and 15. The other pins are used for the spi flash.
Function return the input level, if pin is configured as input. Otherwise it returns output level.
### UART
For printing messages on the serial interface we recommends to use the build-in function *os_printf* because is much more flexible than *Serial.println*.
#### Setup communication
``` c
Serial.begin(buad)
```
* baud - Communication speed. Available speeds are: **9600**, **19200**, **38400**, **57600**, **74880**, **115200**, **230400**, **460800**, **921600**.
This will only initialize *UART1* (the one on the UEXT connector).
#### Print line with CR/LF
``` c
Serial.println(char *buffer)
```
* buffer - Message to be printed. Note that if you want to print number you should previously create buffer with *sprintf*.
#### Print line without CR/LF
``` c
Serial.print(char *buffer)
```
#### Print single character
``` c
Serial.write(char c)
```
#### Reads incoming serial data.
``` c
i = Serial.read()
```
* Function returns the first byte of incoming serial data available (or -1 if no data is available).
### I2C
I2C pins are pins 5 and 6 in the UEXT connector. The following functions are available.
#### Setup communication
``` c
Wire.begin()
```
Setup SDA and SCL pins.
#### Send slave address with W flag
``` c
Wire.beginTransmission(uint8 address)
```
* address - The address of the slave device **without** W/R flag at the end.
#### Send one byte of data
``` c
Wire.write(uint8 data)
```
* data - One byte of data to be sent to the slave device.
#### Send Stop condition and check for status of previously commands
``` c
Wire.endTransmission()
```
The function check for address NACK or data NACK and returns:
* I2C_SUCCESS - 0
* I2C_DATA_TOO_LONG - 1
* I2C_ADDRESS_NACK - 2
* I2C_DATA_NACK - 3
* I2C_OTHER_ERROR - 4
#### Read bytes from slave device
``` c
Wire.requestFrom(uint8 address, uint8 bytes)
```
* address - The address of the slave device **without** W/R flag at the end.
* bytes - Number of bytes to read.
#### Check if there are any available data for reading
``` c
Wire.available()
```
Check for available data in the buffer and returns their count.
#### Read single byte of data
``` c
Wire.read()
```
Return byte of data from the data buffer.
### Flash
You could use the spi flash memory for storing user data. **Make sure that the place where you are writing is not used!** The memory is divided to sectors by 4096 bytes. This is the minimum erase size.
#### Erase sectors in range
``` c
flash.erase(uint32 startAddress, uint32 endAddress)
```
* startAddress - Start address of the segment
* endAddress - End address of the segment
Note that this function will erase the sectors where start and end addresses are. If start and end address are the same, it will erase single sector.
#### Read data from address
``` c
flash.read(uint32 address)
```
* address - The address of the data field.
This function returns 4bytes of data.
#### Write data to address
``` c
flash.write(uint32 address, uint32 data)
```
* address - The address of the data field.
* data - 4 bytes of data to be written.
### SPI
There is hardware SPI, but in this library we make it bit-banging. This way you can have more control.
If you want to increase speed change the delay value in *arduino/arduino_spi.c*:
``` c
spi_config.delay = 10;
```
#### Setup SPI
``` c
spi.begin(uint8 pin)
```
* pin - Chip select pin to be used. For UEXT connector it should be **15**.
#### Set data order
``` c
spi.setBitOrder(spiOrder_t order)
```
* order - can be MSBFIRST (default) or LSBFIRST.
#### Set communication mode
``` c
spi.setDataMode(spiMode_t mode)
```
* mode - Can be *SPI_MODE0*, *SPI_MODE1*, *SPI_MODE2* or *SPI_MODE3*.
#### Transfer data with slave device
``` c
spi.transfer(uint8 pin, uint8 val, spiTransferMode_t transferMode)
```
* pin - Chip-select pin.
* val - Data to be transmitted to the slave device
* transferMode - Can be *SPI_CONTINUE* or *SPI_LAST*. The first one will hold chip-select active.
The function return one byte of data from slave to master.
## Contact
*[email protected]*
## Revision
5 MAR 2015 - Initial release
没有合适的资源?快使用搜索试试~ 我知道了~
ESP8266-master.zip
共232个文件
h:25个
c:23个
pdf:16个
需积分: 5 0 下载量 48 浏览量
2021-12-02
18:50:49
上传
评论
收藏 3.67MB ZIP 举报
温馨提示
ESP8266-master.zip
资源详情
资源评论
资源推荐
收起资源包目录
ESP8266-master.zip (232个子文件)
httpd_app.a 130KB
app_app.a 8KB
ESP8266-EVB-BAT_Rev_B.bcr 435B
MOD-WiFi-ESP8266_Rev_B.bcr 92B
MOD-WiFi-ESP8266-DEV_Rev_B1.bcr 92B
ESP8266-EVB_Rev_A1.bcr 92B
0x40000.bin 141KB
0x40000.bin 133KB
0x00000.bin 32KB
0x00000.bin 29KB
MOD-WiFi-ESP8266-DEV_Rev_B1.bom 7KB
ESP8266-EVB-BAT_Rev_B.bom 6KB
ESP8266-EVB_Rev_A1.bom 4KB
MOD-WiFi-ESP8266-DEV_Rev_A.brd 473KB
MOD-WiFi-ESP8266_Rev_A.brd 422KB
ESP8266-EVB-BAT_Rev_B.brd 172KB
ESP8266-EVB-BAT_Rev_B.brd 172KB
ESP8266-EVB-BAT_Rev_B.brd 172KB
ESP8266-EVB-BAT_Rev_A.brd 171KB
MOD-WiFi-ESP8266-DEV_Rev_B1.brd 155KB
MOD-WiFi-ESP8266-DEV_Rev_B1.brd 155KB
MOD-WiFi-ESP8266-DEV_Rev_B2.brd 155KB
MOD-WiFi-ESP8266-DEV_Rev_B.brd 155KB
ESP8266-EVB_Rev_A.brd 151KB
ESP8266-EVB_Rev_A1.brd 151KB
ESP8266-EVB_Rev_A1.brd 151KB
MOD-WiFi-ESP8266_Rev_B1.brd 137KB
MOD-WiFi-ESP8266_Rev_B.brd 137KB
spi.c 18KB
arduino_i2c.c 12KB
httpd.c 11KB
espfs.c 6KB
arduino_serial.c 6KB
arduino_spi.c 5KB
main.c 5KB
i2c.c 5KB
httpdespfs.c 4KB
arduino_gpio.c 4KB
arduino_flash.c 2KB
stdout.c 2KB
relay.c 1KB
user_main.c 1KB
button.c 1KB
main.c 1KB
user_main.c 1KB
cgii2c.c 933B
io.c 744B
heatshrink_decoder.c 509B
heatshrink_decoder.c 199B
heatshrink_encoder.c 137B
espfs.c 48B
ESP8266-EVB-BAT_Rev_B.cmp 82KB
ESP8266-EVB_Rev_A1.cmp 66KB
MOD-WiFi-ESP8266-DEV_Rev_B1.cmp 40KB
MOD-WiFi-ESP8266_Rev_B.cmp 32KB
style.css 1KB
ESP8266-EVB-BAT_Rev_B.dim 10KB
MOD-WiFi-ESP8266_Rev_B.dim 2KB
MOD-WiFi-ESP8266-DEV_Rev_B1.dim 2KB
ESP8266-EVB_Rev_A1.dim 209B
ESP8266-EVB-BAT_Rev_B.drd 3KB
ESP8266-EVB_Rev_A1.drd 2KB
MOD-WiFi-ESP8266-DEV_Rev_B1.drd 2KB
MOD-WiFi-ESP8266_Rev_B.drd 975B
ESP8266-EVB-BAT_Rev_B.dri 1KB
ESP8266-EVB_Rev_A1.dri 957B
MOD-WiFi-ESP8266-DEV_Rev_B1.dri 919B
MOD-WiFi-ESP8266_Rev_B.dri 874B
ESP8266-EVB-BAT_Rev_B.drl 144B
ESP8266-EVB_Rev_A1.drl 108B
MOD-WiFi-ESP8266-DEV_Rev_B1.drl 84B
MOD-WiFi-ESP8266_Rev_B.drl 72B
webpages.espfs 37KB
.gitignore 12B
.gitmodules 115B
MOD-WiFi-ESP8266-DEV_Rev_B1.gpi 951B
ESP8266-EVB-BAT_Rev_B.gpi 937B
MOD-WiFi-ESP8266_Rev_B.gpi 931B
ESP8266-EVB_Rev_A1.gpi 917B
ESP8266-EVB-BAT_Rev_B.gtd 235KB
ESP8266-EVB_Rev_A1.gtd 181KB
MOD-WiFi-ESP8266-DEV_Rev_B1.gtd 150KB
MOD-WiFi-ESP8266_Rev_B.gtd 96KB
spi_register.h 6KB
uart_hw.h 6KB
arduino_i2c.h 3KB
arduino_gpio.h 2KB
arduino_spi.h 2KB
espmissingincludes.h 2KB
i2c.h 2KB
httpd.h 1KB
arduino_serial.h 1KB
arduino_flash.h 1KB
spi.h 1KB
espfsformat.h 900B
heatshrink_config_httpd.h 770B
arduinostyle.h 347B
stdint.h 245B
espfs.h 201B
httpdespfs.h 197B
共 232 条
- 1
- 2
- 3
水木年华TEL⁷²⁷⁸⁰⁹
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0