serial_comm_prv.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #pragma once
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include "loader_config.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define STATUS_FAILURE 1
  23. #define STATUS_SUCCESS 0
  24. #define READ_DIRECTION 1
  25. #define WRITE_DIRECTION 0
  26. #define MD5_SIZE 32
  27. typedef enum __attribute__((packed))
  28. {
  29. FLASH_BEGIN = 0x02,
  30. FLASH_DATA = 0x03,
  31. FLASH_END = 0x04,
  32. MEM_BEGIN = 0x05,
  33. MEM_END = 0x06,
  34. MEM_DATA = 0x07,
  35. SYNC = 0x08,
  36. WRITE_REG = 0x09,
  37. READ_REG = 0x0a,
  38. SPI_SET_PARAMS = 0x0b,
  39. SPI_ATTACH = 0x0d,
  40. CHANGE_BAUDRATE = 0x0f,
  41. FLASH_DEFL_BEGIN = 0x10,
  42. FLASH_DEFL_DATA = 0x11,
  43. FLASH_DEFL_END = 0x12,
  44. SPI_FLASH_MD5 = 0x13,
  45. } command_t;
  46. typedef enum __attribute__((packed))
  47. {
  48. RESPONSE_OK = 0x00,
  49. INVALID_COMMAND = 0x05, // parameters or length field is invalid
  50. COMMAND_FAILED = 0x06, // Failed to act on received message
  51. INVALID_CRC = 0x07, // Invalid CRC in message
  52. FLASH_WRITE_ERR = 0x08, // After writing a block of data to flash, the ROM loader reads the value back and the 8-bit CRC is compared to the data read from flash. If they don't match, this error is returned.
  53. FLASH_READ_ERR = 0x09, // SPI read failed
  54. READ_LENGTH_ERR = 0x0a, // SPI read request length is too long
  55. DEFLATE_ERROR = 0x0b, // ESP32 compressed uploads only
  56. } error_code_t;
  57. typedef struct __attribute__((packed))
  58. {
  59. uint8_t direction;
  60. uint8_t command; // One of command_t
  61. uint16_t size;
  62. uint32_t checksum;
  63. } command_common_t;
  64. typedef struct __attribute__((packed))
  65. {
  66. command_common_t common;
  67. uint32_t erase_size;
  68. uint32_t packet_count;
  69. uint32_t packet_size;
  70. uint32_t offset;
  71. #ifdef TARGET_ESP32_S2
  72. uint32_t encrypted;
  73. #endif
  74. } begin_command_t;
  75. typedef struct __attribute__((packed))
  76. {
  77. command_common_t common;
  78. uint32_t data_size;
  79. uint32_t sequence_number;
  80. uint32_t zero_0;
  81. uint32_t zero_1;
  82. } data_command_t;
  83. typedef struct __attribute__((packed))
  84. {
  85. command_common_t common;
  86. uint32_t stay_in_loader;
  87. } flash_end_command_t;
  88. typedef struct __attribute__((packed))
  89. {
  90. command_common_t common;
  91. uint32_t stay_in_loader;
  92. uint32_t entry_point_address;
  93. } mem_end_command_t;
  94. typedef struct __attribute__((packed))
  95. {
  96. command_common_t common;
  97. uint8_t sync_sequence[36];
  98. } sync_command_t;
  99. typedef struct __attribute__((packed))
  100. {
  101. command_common_t common;
  102. uint32_t address;
  103. uint32_t value;
  104. uint32_t mask;
  105. uint32_t delay_us;
  106. } write_reg_command_t;
  107. typedef struct __attribute__((packed))
  108. {
  109. command_common_t common;
  110. uint32_t address;
  111. } read_reg_command_t;
  112. typedef struct __attribute__((packed))
  113. {
  114. command_common_t common;
  115. uint32_t configuration;
  116. uint32_t zero; // ESP32 ROM only
  117. } spi_attach_command_t;
  118. typedef struct __attribute__((packed))
  119. {
  120. command_common_t common;
  121. uint32_t new_baudrate;
  122. uint32_t old_baudrate;
  123. } change_baudrate_command_t;
  124. typedef struct __attribute__((packed))
  125. {
  126. command_common_t common;
  127. uint32_t address;
  128. uint32_t size;
  129. uint32_t reserved_0;
  130. uint32_t reserved_1;
  131. } spi_flash_md5_command_t;
  132. typedef struct __attribute__((packed))
  133. {
  134. uint8_t direction;
  135. uint8_t command; // One of command_t
  136. uint16_t size;
  137. uint32_t value;
  138. } common_response_t;
  139. typedef struct __attribute__((packed))
  140. {
  141. uint8_t failed;
  142. uint8_t error;
  143. #ifndef TARGET_ESP8266
  144. uint8_t reserved_0;
  145. uint8_t reserved_1;
  146. #endif
  147. } response_status_t;
  148. typedef struct __attribute__((packed))
  149. {
  150. common_response_t common;
  151. response_status_t status;
  152. } response_t;
  153. typedef struct __attribute__((packed))
  154. {
  155. common_response_t common;
  156. uint8_t md5[MD5_SIZE]; // ROM only
  157. response_status_t status;
  158. } rom_md5_response_t;
  159. typedef struct __attribute__((packed))
  160. {
  161. command_common_t common;
  162. uint32_t id;
  163. uint32_t total_size;
  164. uint32_t block_size;
  165. uint32_t sector_size;
  166. uint32_t page_size;
  167. uint32_t status_mask;
  168. } write_spi_command_t;
  169. #ifdef __cplusplus
  170. }
  171. #endif