serial_comm_prv.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define STATUS_FAILURE 1
  22. #define STATUS_SUCCESS 0
  23. #define READ_DIRECTION 1
  24. #define WRITE_DIRECTION 0
  25. #define MD5_SIZE 32
  26. typedef enum __attribute__((packed))
  27. {
  28. FLASH_BEGIN = 0x02,
  29. FLASH_DATA = 0x03,
  30. FLASH_END = 0x04,
  31. MEM_BEGIN = 0x05,
  32. MEM_END = 0x06,
  33. MEM_DATA = 0x07,
  34. SYNC = 0x08,
  35. WRITE_REG = 0x09,
  36. READ_REG = 0x0a,
  37. SPI_SET_PARAMS = 0x0b,
  38. SPI_ATTACH = 0x0d,
  39. CHANGE_BAUDRATE = 0x0f,
  40. FLASH_DEFL_BEGIN = 0x10,
  41. FLASH_DEFL_DATA = 0x11,
  42. FLASH_DEFL_END = 0x12,
  43. SPI_FLASH_MD5 = 0x13,
  44. } command_t;
  45. typedef enum __attribute__((packed))
  46. {
  47. RESPONSE_OK = 0x00,
  48. INVALID_COMMAND = 0x05, // parameters or length field is invalid
  49. COMMAND_FAILED = 0x06, // Failed to act on received message
  50. INVALID_CRC = 0x07, // Invalid CRC in message
  51. 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.
  52. FLASH_READ_ERR = 0x09, // SPI read failed
  53. READ_LENGTH_ERR = 0x0a, // SPI read request length is too long
  54. DEFLATE_ERROR = 0x0b, // ESP32 compressed uploads only
  55. } error_code_t;
  56. typedef struct __attribute__((packed))
  57. {
  58. uint8_t direction;
  59. uint8_t command; // One of command_t
  60. uint16_t size;
  61. uint32_t checksum;
  62. } command_common_t;
  63. typedef struct __attribute__((packed))
  64. {
  65. command_common_t common;
  66. uint32_t erase_size;
  67. uint32_t packet_count;
  68. uint32_t packet_size;
  69. uint32_t offset;
  70. uint32_t encrypted;
  71. } flash_begin_command_t;
  72. typedef struct __attribute__((packed))
  73. {
  74. command_common_t common;
  75. uint32_t data_size;
  76. uint32_t sequence_number;
  77. uint32_t zero_0;
  78. uint32_t zero_1;
  79. } data_command_t;
  80. typedef struct __attribute__((packed))
  81. {
  82. command_common_t common;
  83. uint32_t stay_in_loader;
  84. } flash_end_command_t;
  85. typedef struct __attribute__((packed))
  86. {
  87. command_common_t common;
  88. uint32_t total_size;
  89. uint32_t blocks;
  90. uint32_t block_size;
  91. uint32_t offset;
  92. } mem_begin_command_t;
  93. typedef struct __attribute__((packed))
  94. {
  95. command_common_t common;
  96. uint32_t stay_in_loader;
  97. uint32_t entry_point_address;
  98. } mem_end_command_t;
  99. typedef struct __attribute__((packed))
  100. {
  101. command_common_t common;
  102. uint8_t sync_sequence[36];
  103. } sync_command_t;
  104. typedef struct __attribute__((packed))
  105. {
  106. command_common_t common;
  107. uint32_t address;
  108. uint32_t value;
  109. uint32_t mask;
  110. uint32_t delay_us;
  111. } write_reg_command_t;
  112. typedef struct __attribute__((packed))
  113. {
  114. command_common_t common;
  115. uint32_t address;
  116. } read_reg_command_t;
  117. typedef struct __attribute__((packed))
  118. {
  119. command_common_t common;
  120. uint32_t configuration;
  121. uint32_t zero; // ESP32 ROM only
  122. } spi_attach_command_t;
  123. typedef struct __attribute__((packed))
  124. {
  125. command_common_t common;
  126. uint32_t new_baudrate;
  127. uint32_t old_baudrate;
  128. } change_baudrate_command_t;
  129. typedef struct __attribute__((packed))
  130. {
  131. command_common_t common;
  132. uint32_t address;
  133. uint32_t size;
  134. uint32_t reserved_0;
  135. uint32_t reserved_1;
  136. } spi_flash_md5_command_t;
  137. typedef struct __attribute__((packed))
  138. {
  139. uint8_t direction;
  140. uint8_t command; // One of command_t
  141. uint16_t size;
  142. uint32_t value;
  143. } common_response_t;
  144. typedef struct __attribute__((packed))
  145. {
  146. uint8_t failed;
  147. uint8_t error;
  148. } response_status_t;
  149. typedef struct __attribute__((packed))
  150. {
  151. common_response_t common;
  152. response_status_t status;
  153. } response_t;
  154. typedef struct __attribute__((packed))
  155. {
  156. common_response_t common;
  157. uint8_t md5[MD5_SIZE]; // ROM only
  158. response_status_t status;
  159. } rom_md5_response_t;
  160. typedef struct __attribute__((packed))
  161. {
  162. command_common_t common;
  163. uint32_t id;
  164. uint32_t total_size;
  165. uint32_t block_size;
  166. uint32_t sector_size;
  167. uint32_t page_size;
  168. uint32_t status_mask;
  169. } write_spi_command_t;
  170. #ifdef __cplusplus
  171. }
  172. #endif