serial_comm_prv.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. } 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 stay_in_loader;
  89. uint32_t entry_point_address;
  90. } mem_end_command_t;
  91. typedef struct __attribute__((packed))
  92. {
  93. command_common_t common;
  94. uint8_t sync_sequence[36];
  95. } sync_command_t;
  96. typedef struct __attribute__((packed))
  97. {
  98. command_common_t common;
  99. uint32_t address;
  100. uint32_t value;
  101. uint32_t mask;
  102. uint32_t delay_us;
  103. } write_reg_command_t;
  104. typedef struct __attribute__((packed))
  105. {
  106. command_common_t common;
  107. uint32_t address;
  108. } read_reg_command_t;
  109. typedef struct __attribute__((packed))
  110. {
  111. command_common_t common;
  112. uint32_t configuration;
  113. uint32_t zero; // ESP32 ROM only
  114. } spi_attach_command_t;
  115. typedef struct __attribute__((packed))
  116. {
  117. command_common_t common;
  118. uint32_t new_baudrate;
  119. uint32_t old_baudrate;
  120. } change_baudrate_command_t;
  121. typedef struct __attribute__((packed))
  122. {
  123. command_common_t common;
  124. uint32_t address;
  125. uint32_t size;
  126. uint32_t reserved_0;
  127. uint32_t reserved_1;
  128. } spi_flash_md5_command_t;
  129. typedef struct __attribute__((packed))
  130. {
  131. uint8_t direction;
  132. uint8_t command; // One of command_t
  133. uint16_t size;
  134. uint32_t value;
  135. } common_response_t;
  136. typedef struct __attribute__((packed))
  137. {
  138. uint8_t failed;
  139. uint8_t error;
  140. } response_status_t;
  141. typedef struct __attribute__((packed))
  142. {
  143. common_response_t common;
  144. response_status_t status;
  145. } response_t;
  146. typedef struct __attribute__((packed))
  147. {
  148. common_response_t common;
  149. uint8_t md5[MD5_SIZE]; // ROM only
  150. response_status_t status;
  151. } rom_md5_response_t;
  152. typedef struct __attribute__((packed))
  153. {
  154. command_common_t common;
  155. uint32_t id;
  156. uint32_t total_size;
  157. uint32_t block_size;
  158. uint32_t sector_size;
  159. uint32_t page_size;
  160. uint32_t status_mask;
  161. } write_spi_command_t;
  162. #ifdef __cplusplus
  163. }
  164. #endif