Tuan PM
/
esptool
esptool
Revision 0:f6a51823dfa8, committed 2015-04-06
- Comitter:
- TuanPM
- Date:
- Mon Apr 06 06:43:53 2015 +0000
- Commit message:
- ok
Changed in this revision
diff -r 000000000000 -r f6a51823dfa8 esptool.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/esptool.cpp Mon Apr 06 06:43:53 2015 +0000 @@ -0,0 +1,81 @@ +#include "esptool.h" + +ESPTOOL::ESPTOOL(Serial& serial, PinName pinEnable, PinName pinProgram): _pinEn(pinEnable, _pinPro(pinProgram) { + _serial = serial; + _serial.baud(115200); +} +uint8_t ESPTOOL::read(){ + uint8_t data = _serial.getc(); + if(data == 0xDB){ + data = _serial.getc(); + if(data == 0xDC) + return 0xC0; + else if(data == 0xDD) + return 0xDB; + else + while(1); + } else + return data; +} +void ESPTOOL::read(uint8_t *data, uint32_t len){ + while(len --) + *data ++ = read(); +} + +void ESPTOOL:write(uint8_t *data, uint32_t len){ + uint8_t* buf_send = data, data_send; + + while(len--){ + data_send = *buf_send++; + if(data_send == 0xDB){ + _serial.putc(0xDB); + _serial.putc(0xDD); + } else if(data_send == 0xC0) { + _serial.putc(0xDB); + _serial.putc(0xDC); + } else { + _serial.putc(data_send); + } + } + +} +uint8_t ESPTOOL::command(uint8_t op, uint8_t *data, uint16_t data_len, uint32_t ck, uint8_t *recv_data, uint16_t *recv_len) { + uint8_t header[8]; + _serial.putc(0xC0); + _serial.putc(0x00); + write(&op, 1); + write((uint8_t*)&data_len, 2); + write((uint8_t*)&ck, 4); + _serial.putc(0xC0); + + if(_serial.getc() != 0xC0) + return 0; + read(header, 8); + + if(header[0] != 0x01 || header[1] != op) + return 0; + recv_len = (uint16_t*)&header[2]; + read(recv_data, *recv_len); + if(_serial.getc() != 0xC0) + return 0; + + return 1; +} + +uint8_t ESPTOOL::connect() { + _pinEn = 0; + _pinPro = 0; + wait(0.2); + _pinEn = 1; + while(_serial.available()) + _serial.getc(); + +} +uint8_t ESPTOOL::sync(){ + uint8_t i; + uint8_t sync_frame[5] = {0x07, 0x07, 0x12, 0x20, 0x55*32}; + uint8_t recv_frame[5]; + uint8_t recv_len; + for(i=0; i<7; i++) + command(ESP_SYNC, sync_frame, 5, 0, recv_frame, &faerecv_len); +} \ No newline at end of file
diff -r 000000000000 -r f6a51823dfa8 esptool.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/esptool.h Mon Apr 06 06:43:53 2015 +0000 @@ -0,0 +1,47 @@ +#ifndef _ESPTOOL_H_ +#define _ESPTOOL_H_ +#include "mbed.h" +#define ESP_FLASH_BEGIN 0x02 +#define ESP_FLASH_DATA 0x03 +#define ESP_FLASH_END 0x04 +#define ESP_MEM_BEGIN 0x05 +#define ESP_MEM_END 0x06 +#define ESP_MEM_DATA 0x07 +#define ESP_SYNC 0x08 +#define ESP_WRITE_REG 0x09 +#define ESP_READ_REG 0x0a + +//# Maximum block sized for RAM and Flash writes, respectively. +#define ESP_RAM_BLOCK 0x1800 +#define ESP_FLASH_BLOCK 0x400 + +//# Default baudrate. The ROM auto-bauds, so we can use more or less whatever we want. +#define ESP_ROM_BAUD 115200 + +//# First byte of the application image +#define ESP_IMAGE_MAGIC 0xe9 + +//# Initial state for the checksum routine +#define ESP_CHECKSUM_MAGIC 0xef + +//# OTP ROM addresses +#define ESP_OTP_MAC0 0x3ff00050 +#define ESP_OTP_MAC1 0x3ff00054 + +//# Sflash stub: an assembly routine to read from spi flash and send to host +#define SFLASH_STUB "\x80\x3c\x00\x40\x1c\x4b\x00\x40\x21\x11\x00\x40\x00\x80" \ + "\xfe\x3f\xc1\xfb\xff\xd1\xf8\xff\x2d\x0d\x31\xfd\xff\x41\xf7\xff\x4a" \ + "\xdd\x51\xf9\xff\xc0\x05\x00\x21\xf9\xff\x31\xf3\xff\x41\xf5\xff\xc0" \ + "\x04\x00\x0b\xcc\x56\xec\xfd\x06\xff\xff\x00\x00" +class ESPTOOL { + +public: + ESPTOOL(Serial& serial, PinName pinEnable, PinName pinProgram) +private: + Serial& _serial; + DigitalOut _pinEn, _pinPro; + uint8_t read(); + void read(uint8_t *data, uint32_t len); + void write(uint8_t *data, uint32_t len); +} +#endif \ No newline at end of file
diff -r 000000000000 -r f6a51823dfa8 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Apr 06 06:43:53 2015 +0000 @@ -0,0 +1,12 @@ +#include "mbed.h" + +DigitalOut myled(LED1); + +int main() { + while(1) { + myled = 1; // LED is ON + wait(0.2); // 200 ms + myled = 0; // LED is OFF + wait(1.0); // 1 sec + } +}
diff -r 000000000000 -r f6a51823dfa8 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Apr 06 06:43:53 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/487b796308b0 \ No newline at end of file