Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BufferedSerial mbed
Fork of ESPQuickFlash by
esp_command.h
00001 #ifndef ESP_COMMAND_H 00002 #define ESP_COMMAND_H 00003 00004 #include "slip.h" 00005 00006 00007 #define FLASH_BLOCK_SIZE 0x400 00008 #define RESET_WAIT_MS 250 00009 00010 enum esp_commands { 00011 ESP_FLASH_START = 0x02, 00012 ESP_FLASH_DATA = 0x03, 00013 ESP_FLASH_FINISH = 0x04, 00014 ESP_RAM_START = 0x05, 00015 ESP_RAM_DATA = 0x07, 00016 ESP_RAM_FINISH = 0x06, 00017 ESP_SYNC_FRAME = 0x08, 00018 ESP_WRITE_REG = 0x09, 00019 ESP_READ_REG = 0x0a, 00020 ESP_SPI_CONFIG = 0x0b, 00021 }; 00022 00023 00024 00025 00026 // Commands follow the following format 00027 // request: 00028 // [- 0x00 -|- command -|- size -|- value -|-- body --] 00029 // 1 1 2 4 size 00030 // 00031 // response: 00032 // [- 0x01 -|- command -|- size -|- value -|-- body --|- status -|- error -] 00033 // 1 1 2 4 size-error 1 1 00034 // 00035 template <typename SERIAL> 00036 class ESPCommand { 00037 private: 00038 SLIPPacket<SERIAL> _slip; 00039 DigitalIn _button; 00040 00041 public: 00042 ESPCommand(PinName tx, PinName rx, PinName button) : 00043 _slip(tx, rx), _button(button){} 00044 00045 private: 00046 bool command_start(char cmd, uint16_t len) { 00047 len -= 4; // First word not included in length 00048 00049 return _slip.req_start() && 00050 _slip.putc(0x00) && 00051 _slip.putc(cmd) && 00052 _slip.send(&len, 2); 00053 } 00054 00055 bool command_flush() { 00056 uint16_t len; 00057 00058 return _slip.resp_start() && 00059 _slip.getc() == 0x01 && 00060 _slip.getc() >= 0 && 00061 _slip.recv(&len, 2) && 00062 _slip.recv(0, 4+len-2) && 00063 _slip.getc() == 0x00 && 00064 _slip.getc() >= 0 && 00065 _slip.resp_finish(); 00066 } 00067 00068 bool command_finish() { 00069 return _slip.req_finish() && 00070 command_flush(); 00071 } 00072 00073 public: 00074 bool sync() { 00075 #ifdef LED_STATUS 00076 led_green = 0; // Show progress 00077 #endif 00078 while (true) { 00079 if (sync_frame()) { 00080 #ifdef LED_STATUS 00081 led_green = 1; // Show progress 00082 #endif 00083 return true; 00084 } 00085 } 00086 } 00087 00088 bool sync_frame() { 00089 00090 // Flush serial line 00091 _slip.flush(); 00092 00093 printf("\r\nPower cycle ESP, put into FW Update mode, push user button\r\n"); 00094 int temp_button = _button; 00095 while( temp_button == _button){;} // wait for button press 00096 printf("\r\nContinuing Now\r\n"); 00097 00098 // Send sync frame 00099 uint32_t x = 0; 00100 00101 if (!(command_start(ESP_SYNC_FRAME, 2*4 + 32) && 00102 _slip.send(&x, 4) && 00103 _slip.putc(0x07) && 00104 _slip.putc(0x07) && 00105 _slip.putc(0x12) && 00106 _slip.putc(0x20))) 00107 return false; 00108 00109 for (int i = 0; i < 32; i++) { 00110 if (!_slip.putc(0x55)) 00111 return false; 00112 } 00113 00114 if (!command_finish()) 00115 return false; 00116 00117 for (int i = 0; i < 7; i++) { 00118 if (!command_flush()) 00119 return false; 00120 } 00121 00122 return true; 00123 } 00124 00125 bool flash_start(uint32_t blocks, uint32_t block_size, uint32_t address) { 00126 uint32_t x = 0; 00127 uint32_t size = blocks * block_size; 00128 00129 return command_start(ESP_FLASH_START, 5*4) && 00130 _slip.send(&x, 4) && 00131 _slip.send(&size, 4) && 00132 _slip.send(&blocks, 4) && 00133 _slip.send(&block_size, 4) && 00134 _slip.send(&address, 4) && 00135 command_finish(); 00136 } 00137 00138 bool flash_data(const char *data, uint32_t len, uint32_t n) { 00139 uint32_t zero = 0; 00140 00141 uint32_t x = 0xef; 00142 for (int i = 0; i < len; i++) { 00143 x ^= data[i]; 00144 } 00145 00146 return command_start(ESP_FLASH_DATA, 5*4 + len) && 00147 _slip.send(&x, 4) && 00148 _slip.send(&len, 4) && 00149 _slip.send(&n, 4) && 00150 _slip.send(&zero, 4) && 00151 _slip.send(&zero, 4) && 00152 _slip.send(data, len) && 00153 command_finish(); 00154 } 00155 00156 bool flash_finish() { 00157 uint32_t x = 0; 00158 00159 return command_start(ESP_FLASH_FINISH, 2*4) && 00160 _slip.send(&x, 4) && 00161 _slip.send(&x, 4) && 00162 command_finish(); 00163 } 00164 00165 bool flash_write(uint32_t address, const char *data, uint32_t size) { 00166 uint32_t blocks = (size-1)/FLASH_BLOCK_SIZE + 1; 00167 00168 printf("Synching...\r\n"); 00169 if (!sync()) 00170 error("Sync error!"); 00171 00172 printf("Starting transfer 0x%05x - 0x%05x\r\n", address, address + blocks*FLASH_BLOCK_SIZE); 00173 if (!flash_start(blocks, FLASH_BLOCK_SIZE, address)) 00174 return false; 00175 00176 for (int i = 0; i < blocks; i++) { 00177 printf("Flashing address 0x%05x\r\n", address + i*FLASH_BLOCK_SIZE); 00178 if (!flash_data(&data[i*FLASH_BLOCK_SIZE], FLASH_BLOCK_SIZE, i)) 00179 return false; 00180 } 00181 00182 printf("Finishing transfer\r\n"); 00183 if (!flash_finish()) 00184 return false; 00185 00186 wait_ms(250); 00187 return true; 00188 } 00189 }; 00190 00191 #endif
Generated on Thu Jul 14 2022 01:55:18 by
1.7.2
