serial_comm_prv.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. } begin_command_t;
  71. typedef struct __attribute__((packed))
  72. {
  73. command_common_t common;
  74. uint32_t data_size;
  75. uint32_t sequence_number;
  76. uint32_t zero_0;
  77. uint32_t zero_1;
  78. } data_command_t;
  79. typedef struct __attribute__((packed))
  80. {
  81. command_common_t common;
  82. uint32_t stay_in_loader;
  83. } flash_end_command_t;
  84. typedef struct __attribute__((packed))
  85. {
  86. command_common_t common;
  87. uint32_t stay_in_loader;
  88. uint32_t entry_point_address;
  89. } mem_end_command_t;
  90. typedef struct __attribute__((packed))
  91. {
  92. command_common_t common;
  93. uint8_t sync_sequence[36];
  94. } sync_command_t;
  95. typedef struct __attribute__((packed))
  96. {
  97. command_common_t common;
  98. uint32_t address;
  99. uint32_t value;
  100. uint32_t mask;
  101. uint32_t delay_us;
  102. } write_reg_command_t;
  103. typedef struct __attribute__((packed))
  104. {
  105. command_common_t common;
  106. uint32_t address;
  107. } read_reg_command_t;
  108. typedef struct __attribute__((packed))
  109. {
  110. command_common_t common;
  111. uint32_t configuration;
  112. uint32_t zero; // ESP32 ROM only
  113. } spi_attach_command_t;
  114. typedef struct __attribute__((packed))
  115. {
  116. command_common_t common;
  117. uint32_t new_baudrate;
  118. uint32_t old_baudrate;
  119. } change_baudrate_command_t;
  120. typedef struct __attribute__((packed))
  121. {
  122. command_common_t common;
  123. uint32_t address;
  124. uint32_t size;
  125. uint32_t reserved_0;
  126. uint32_t reserved_1;
  127. } spi_flash_md5_command_t;
  128. typedef struct __attribute__((packed))
  129. {
  130. uint8_t direction;
  131. uint8_t command; // One of command_t
  132. uint16_t size;
  133. uint32_t value;
  134. } common_response_t;
  135. typedef struct __attribute__((packed))
  136. {
  137. uint8_t failed;
  138. uint8_t error;
  139. uint8_t reserved_0; // ESP32 ROM only
  140. uint8_t reserved_1; // ESP32 ROM only
  141. } response_status_t;
  142. typedef struct __attribute__((packed))
  143. {
  144. common_response_t common;
  145. response_status_t status;
  146. } response_t;
  147. typedef struct __attribute__((packed))
  148. {
  149. common_response_t common;
  150. uint8_t md5[MD5_SIZE]; // ROM only
  151. response_status_t status;
  152. } rom_md5_response_t;
  153. #ifdef __cplusplus
  154. }
  155. #endif