1#include "main-header.h"
2#include "espressif/esp8266_flash.h"
3#include "espressif/esp8266_flash.h"
6static int read_timeout = 12;
9static int esp_flash_baud = 115200;
10static void writebyte(
byte b);
11static void writebuf_escaped(
const byte *buf,
int len);
12static void writebyte_escaped(
const byte b);
15static void init_timer();
16static byte is_timer_expired();
24static void send_packet(
const byte *buf) {
27 while (buf[len] != 0xC0) {
31 writebuf_escaped(buf + 1, len - 2);
36static void writebyte(
byte b) {
37 write_serial_char(USART_ESP8266, b);
41static void writebuf_escaped(
const byte *buf,
int len) {
44 for (i = 0; i < len; i++) {
45 writebyte_escaped(buf[i]);
48static void writebyte_escaped(
const byte b) {
52 }
else if (b == 0xDB) {
60static void write16bit_escaped(
int num) {
61 writebyte_escaped(num & 0xFF);
62 writebyte_escaped((num >> 8) & 0xFF);
65static void write32bit_escaped(
long num) {
66 writebyte_escaped(num & 0xFF);
67 writebyte_escaped((num >> 8) & 0xFF);
68 writebyte_escaped((num >> 16) & 0xFF);
69 writebyte_escaped((num >> 24) & 0xFF);
76static void clear_serial_buf() {
77 ringbuffer_init(&esp_ringbuffer);
79void espflash_settimeout(
const int timeo) {
89 const byte syncbytes[] = { 0xC0, 0, ESP_SYNC, 0x24, 0x07, 0x07, 0x12, 0x20,
90 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
91 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
92 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
93 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
100 printf(
"Esp8266 off...\r\n");
101 mculib_pin_set(ESP8266_MCULIBHANDLE, PIN_ESP8266_CHPD, 0);
102 mculib_pin_set(ESP8266_MCULIBHANDLE, PIN_ESP8266_GPIO0, 0);
105 printf(
"Esp8266 on...\r\n");
106 mculib_pin_set(ESP8266_MCULIBHANDLE, PIN_ESP8266_CHPD, 1);
108 set_esp8266_usart_mode(ESP8266_USART_PASSTHROUGH);
109 int baud = esp_flash_baud;
112 if ((i = mculib_serialport_enable(ESP8266_MCULIBHANDLE, USART_ESP8266, baud))) {
113 printf(
"Failed to enable serialport #%i: %i\r\n", USART_ESP8266, i);
116 printf(
"ESP8266 syncing @%i baud\r\n", baud);
117 espflash_settimeout(2);
128 if (started != mculib_get_seconds_since_boot()) {
129 send_packet(syncbytes);
131 started = mculib_get_seconds_since_boot();
132 printf(
"Sent sync packet #%i\r\n", res);
134 i = ringbuffer_get_byte(&esp_ringbuffer);
136 printf(
"Received: %i (0x%x)\r\n", i, i);
142 espflash_settimeout(1);
143 for (i = 0; i < max_attempts; i++) {
144 printf(
"sync %i of %i)\r\n", (i + 1), max_attempts);
146 send_packet(syncbytes);
147 res = read_response(&response);
149 printf(
"Failed to read response - error %i\r\n", res);
151 if (response.status != 0) {
152 printf(
"Sync Response Status: %i\r\n", response.status);
158 espflash_settimeout(10);
159 printf(
"ESP8266 (or ESP32) synced @ %i baud\r\n", baud);
165 espflash_settimeout(10);
182static int readbyte() {
186 while (((esp = ringbuffer_get_byte(&esp_ringbuffer)) >= 256) && (!is_timer_expired())) {
202 response->status = 172;
204 long srr = mculib_get_seconds_since_boot();
207 while (((b = readbyte()) != -1) && (b != 0xc0)) {
208 if ((mculib_get_seconds_since_boot() - srr) > 10) {
224 if ((mculib_get_seconds_since_boot() - srr) > (read_timeout * 5)) {
229 printf(
"ESP8266-Flash: Whilst reading packet we got a timeout\r\n");
230 printf(
"ESP8266-Flash: Read %i bytes so far. skipC0=%i\r\n", pos, skipC0);
236 if ((skipC0) && (b == 0xC0)) {
243 if (!is_valid_response(response)) {
250 response->command = b;
251 }
else if (pos == 2) {
252 response->size = (b & 0xFF);
253 }
else if (pos == 3) {
254 response->size = response->size | ((b & 0xFF) << 8);
255 }
else if ((pos >= 4) && (pos <= 7)) {
256 vb = (
byte *)&response->value;
257 vb[pos - 4] = (
byte)(b & 0xff);
258 }
else if (pos == 8) {
259 response->status = b;
260 }
else if (pos == 9) {
277 int res = read_response(response);
278 if ((res == 0) && (response->command ==
command)) {
291 if (response == NULL) {
294 CNWDEBUG(
"Command: %i\n", response->command);
295 if (response->command == ESP_SYNC) {
296 response->status = 0;
310static byte is_timer_expired() {
311 if ((mculib_get_seconds_since_boot() - started) >= read_timeout) {
319static void init_timer() {
320 started = mculib_get_seconds_since_boot();
328int espflash_read_register(
long regnum,
long *result) {
334 writebyte(ESP_READ_REG);
335 write16bit_escaped(4);
336 write32bit_escaped(0);
337 write32bit_escaped(regnum);
339 int res = wait_for_response(&response, ESP_READ_REG);
341 if ((res == 0) && (response.status != 0)) {
342 printf(
"ESP8266 replied %i to read_reg()!\r\n", response.status);
347 x = (
void *)response.value;
348 printf(
"Read register value: %p\r\n", x);
349 *result = response.value;
353int esp8266_flash_from_serialport() {
357void espflash_set_baud(
int baud) {
358 esp_flash_baud = baud;
359 printf(
"Flash baudrate set to %i\r\n", esp_flash_baud);
361int espflash_finish() {
363 const byte finish[] = { 0xC0, 0, ESP_FLASH_END, 4, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0xC0 };
367 int res = read_response(&response);
369 if ((res == 0) && (response.command != ESP_FLASH_END)) {
370 printf(
"We're confused. esp8266 replied with command %i to ESP_FLASH_END\r\n", response.command);
373 if ((res == 0) && (response.status != 0)) {
374 printf(
"ESP8266 replied %i to flash finish()!\r\n", response.status);