blheli_4way_protocol.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. blheli 4way protocol. Based on serial_4way.c from betaflight
  3. */
  4. /*
  5. * Cleanflight is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Cleanflight is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
  17. * Author: 4712
  18. */
  19. // Interface related only
  20. // establish and test connection to the Interface
  21. // Send Structure
  22. // ESC + CMD PARAM_LEN [PARAM (if len > 0)] CRC16_Hi CRC16_Lo
  23. // Return
  24. // ESC CMD PARAM_LEN [PARAM (if len > 0)] + ACK (uint8_t OK or ERR) + CRC16_Hi CRC16_Lo
  25. #define cmd_Remote_Escape 0x2E // '.'
  26. #define cmd_Local_Escape 0x2F // '/'
  27. // Test Interface still present
  28. #define cmd_InterfaceTestAlive 0x30 // '0' alive
  29. // RETURN: ACK
  30. // get Protocol Version Number 01..255
  31. #define cmd_ProtocolGetVersion 0x31 // '1' version
  32. // RETURN: uint8_t VersionNumber + ACK
  33. // get Version String
  34. #define cmd_InterfaceGetName 0x32 // '2' name
  35. // RETURN: String + ACK
  36. //get Version Number 01..255
  37. #define cmd_InterfaceGetVersion 0x33 // '3' version
  38. // RETURN: uint8_t AVersionNumber + ACK
  39. // Exit / Restart Interface - can be used to switch to Box Mode
  40. #define cmd_InterfaceExit 0x34 // '4' exit
  41. // RETURN: ACK
  42. // Reset the Device connected to the Interface
  43. #define cmd_DeviceReset 0x35 // '5' reset
  44. // RETURN: ACK
  45. // Get the Device ID connected
  46. // #define cmd_DeviceGetID 0x36 //'6' device id removed since 06/106
  47. // RETURN: uint8_t DeviceID + ACK
  48. // Initialize Flash Access for Device connected
  49. #define cmd_DeviceInitFlash 0x37 // '7' init flash access
  50. // RETURN: ACK
  51. // Erase the whole Device Memory of connected Device
  52. #define cmd_DeviceEraseAll 0x38 // '8' erase all
  53. // RETURN: ACK
  54. // Erase one Page of Device Memory of connected Device
  55. #define cmd_DevicePageErase 0x39 // '9' page erase
  56. // PARAM: uint8_t APageNumber
  57. // RETURN: ACK
  58. // Read to Buffer from Device Memory of connected Device // Buffer Len is Max 256 Bytes
  59. // BuffLen = 0 means 256 Bytes
  60. #define cmd_DeviceRead 0x3A // ':' read Device
  61. // PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BuffLen[0..255]
  62. // RETURN: PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255] ACK
  63. // Write to Buffer for Device Memory of connected Device // Buffer Len is Max 256 Bytes
  64. // BuffLen = 0 means 256 Bytes
  65. #define cmd_DeviceWrite 0x3B // ';' write
  66. // PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255]
  67. // RETURN: ACK
  68. // Set C2CK low infinite ) permanent Reset state
  69. #define cmd_DeviceC2CK_LOW 0x3C // '<'
  70. // RETURN: ACK
  71. // Read to Buffer from Device Memory of connected Device //Buffer Len is Max 256 Bytes
  72. // BuffLen = 0 means 256 Bytes
  73. #define cmd_DeviceReadEEprom 0x3D // '=' read Device
  74. // PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BuffLen[0..255]
  75. // RETURN: PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255] ACK
  76. // Write to Buffer for Device Memory of connected Device // Buffer Len is Max 256 Bytes
  77. // BuffLen = 0 means 256 Bytes
  78. #define cmd_DeviceWriteEEprom 0x3E // '>' write
  79. // PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255]
  80. // RETURN: ACK
  81. // Set Interface Mode
  82. #define cmd_InterfaceSetMode 0x3F // '?'
  83. // #define imC2 0
  84. // #define imSIL_BLB 1
  85. // #define imATM_BLB 2
  86. // #define imSK 3
  87. // PARAM: uint8_t Mode
  88. // RETURN: ACK or ACK_I_INVALID_CHANNEL
  89. //Write to Buffer for Verify Device Memory of connected Device //Buffer Len is Max 256 Bytes
  90. //BuffLen = 0 means 256 Bytes
  91. #define cmd_DeviceVerify 0x40 //'@' write
  92. //PARAM: uint8_t ADRESS_Hi + ADRESS_Lo + BUffLen + Buffer[0..255]
  93. //RETURN: ACK
  94. /*
  95. local defines
  96. */
  97. #define SERIAL_4WAY_VER_MAIN 20
  98. #define SERIAL_4WAY_VER_SUB_1 (uint8_t) 0
  99. #define SERIAL_4WAY_VER_SUB_2 (uint8_t) 03
  100. #define SERIAL_4WAY_PROTOCOL_VER 107
  101. // *** end
  102. #if (SERIAL_4WAY_VER_MAIN > 24)
  103. #error "beware of SERIAL_4WAY_VER_SUB_1 is uint8_t"
  104. #endif
  105. #define SERIAL_4WAY_VERSION (uint16_t) ((SERIAL_4WAY_VER_MAIN * 1000) + (SERIAL_4WAY_VER_SUB_1 * 100) + SERIAL_4WAY_VER_SUB_2)
  106. #define SERIAL_4WAY_VERSION_HI (uint8_t) (SERIAL_4WAY_VERSION / 100)
  107. #define SERIAL_4WAY_VERSION_LO (uint8_t) (SERIAL_4WAY_VERSION % 100)
  108. #define brSUCCESS 0x30
  109. #define brERRORVERIFY 0xC0
  110. #define brERRORCOMMAND 0xC1
  111. #define brERRORCRC 0xC2
  112. #define brNONE 0xFF
  113. #define CMD_RUN 0x00
  114. #define CMD_PROG_FLASH 0x01
  115. #define CMD_ERASE_FLASH 0x02
  116. #define CMD_READ_FLASH_SIL 0x03
  117. #define CMD_VERIFY_FLASH 0x03
  118. #define CMD_VERIFY_FLASH_ARM 0x04
  119. #define CMD_READ_EEPROM 0x04
  120. #define CMD_PROG_EEPROM 0x05
  121. #define CMD_READ_SRAM 0x06
  122. #define CMD_READ_FLASH_ATM 0x07
  123. #define CMD_KEEP_ALIVE 0xFD
  124. #define CMD_SET_ADDRESS 0xFF
  125. #define CMD_SET_BUFFER 0xFE
  126. #define RestartBootloader 0
  127. #define ExitBootloader 1
  128. #define ACK_OK 0x00
  129. #define ACK_I_INVALID_CMD 0x02
  130. #define ACK_I_INVALID_CRC 0x03
  131. #define ACK_I_VERIFY_ERROR 0x04
  132. #define ACK_I_INVALID_CHANNEL 0x08
  133. #define ACK_I_INVALID_PARAM 0x09
  134. #define ACK_D_GENERAL_ERROR 0x0F
  135. // interface modes
  136. #define imC2 0
  137. #define imSIL_BLB 1
  138. #define imATM_BLB 2
  139. #define imSK 3
  140. #define imARM_BLB 4