serial_comm_prv.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. typedef enum __attribute__((packed))
  26. {
  27. FLASH_BEGIN = 0x02,
  28. FLASH_DATA = 0x03,
  29. FLASH_END = 0x04,
  30. MEM_BEGIN = 0x05,
  31. MEM_END = 0x06,
  32. MEM_DATA = 0x07,
  33. SYNC = 0x08,
  34. WRITE_REG = 0x09,
  35. READ_REG = 0x0a,
  36. SPI_SET_PARAMS = 0x0b,
  37. SPI_ATTACH = 0x0d,
  38. CHANGE_BAUDRATE = 0x0f,
  39. FLASH_DEFL_BEGIN = 0x10,
  40. FLASH_DEFL_DATA = 0x11,
  41. FLASH_DEFL_END = 0x12,
  42. SPI_FLASH_MD5 = 0x13,
  43. } command_t;
  44. typedef enum __attribute__((packed))
  45. {
  46. RESPONSE_OK = 0x00,
  47. INVALID_COMMAND = 0x05, // parameters or length field is invalid
  48. COMMAND_FAILED = 0x06, // Failed to act on received message
  49. INVALID_CRC = 0x07, // Invalid CRC in message
  50. 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.
  51. FLASH_READ_ERR = 0x09, // SPI read failed
  52. READ_LENGTH_ERR = 0x0a, // SPI read request length is too long
  53. DEFLATE_ERROR = 0x0b, // ESP32 compressed uploads only
  54. } error_code_t;
  55. typedef struct __attribute__((packed))
  56. {
  57. uint8_t direction;
  58. uint8_t command; // One of command_t
  59. uint16_t size;
  60. uint32_t checksum;
  61. } command_common_t;
  62. typedef struct __attribute__((packed))
  63. {
  64. command_common_t common;
  65. uint32_t erase_size;
  66. uint32_t packet_count;
  67. uint32_t packet_size;
  68. uint32_t offset;
  69. } begin_command_t;
  70. typedef struct __attribute__((packed))
  71. {
  72. command_common_t common;
  73. uint32_t data_size;
  74. uint32_t sequence_number;
  75. uint32_t zero_0;
  76. uint32_t zero_1;
  77. } data_command_t;
  78. typedef struct __attribute__((packed))
  79. {
  80. command_common_t common;
  81. uint32_t stay_in_loader;
  82. } flash_end_command_t;
  83. typedef struct __attribute__((packed))
  84. {
  85. command_common_t common;
  86. uint32_t stay_in_loader;
  87. uint32_t entry_point_address;
  88. } mem_end_command_t;
  89. typedef struct __attribute__((packed))
  90. {
  91. command_common_t common;
  92. uint8_t sync_sequence[36];
  93. } sync_command_t;
  94. typedef struct __attribute__((packed))
  95. {
  96. command_common_t common;
  97. uint32_t address;
  98. uint32_t value;
  99. uint32_t mask;
  100. uint32_t delay_us;
  101. } write_reg_command_t;
  102. typedef struct __attribute__((packed))
  103. {
  104. command_common_t common;
  105. uint32_t address;
  106. } read_reg_command_t;
  107. typedef struct __attribute__((packed))
  108. {
  109. command_common_t common;
  110. uint32_t configuration;
  111. uint32_t zero; // ESP32 ROM only
  112. } spi_attach_command_t;
  113. typedef struct __attribute__((packed))
  114. {
  115. uint8_t direction;
  116. uint8_t command; // One of command_t
  117. uint16_t size;
  118. uint32_t value;
  119. uint8_t status;
  120. uint8_t error;
  121. uint8_t reserved_0; // ESP32 ROM only
  122. uint8_t reserved_1; // ESP32 ROM only
  123. } response_t;
  124. #ifdef __cplusplus
  125. }
  126. #endif