19 #ifdef USE_SERIAL_4WAY_BLHELI_INTERFACE 25 #if defined(USE_SERIAL_4WAY_BLHELI_BOOTLOADER) 29 #define RestartBootloader 0 30 #define ExitBootloader 1 33 #define CMD_PROG_FLASH 0x01 34 #define CMD_ERASE_FLASH 0x02 35 #define CMD_READ_FLASH_SIL 0x03 36 #define CMD_VERIFY_FLASH 0x03 37 #define CMD_VERIFY_FLASH_ARM 0x04 38 #define CMD_READ_EEPROM 0x04 39 #define CMD_PROG_EEPROM 0x05 40 #define CMD_READ_SRAM 0x06 41 #define CMD_READ_FLASH_ATM 0x07 42 #define CMD_KEEP_ALIVE 0xFD 43 #define CMD_SET_ADDRESS 0xFF 44 #define CMD_SET_BUFFER 0xFE 46 #define CMD_BOOTINIT 0x07 47 #define CMD_BOOTSIGN 0x08 50 #define START_BIT_TIMEOUT_MS 2 52 #define BIT_TIME (52) // 52uS 53 #define BIT_TIME_HALVE (BIT_TIME >> 1) // 26uS 54 #define BIT_TIME_3_4 (BIT_TIME_HALVE + (BIT_TIME_HALVE >> 1)) // 39uS 55 #define START_BIT_TIME (BIT_TIME_3_4) 58 static uint8_t suart_getc_(uint8_t *bt)
63 uint32_t wait_time =
millis() + START_BIT_TIMEOUT_MS;
66 if (
millis() >= wait_time) {
72 btime = start_time + START_BIT_TIME;
79 bitmask |= (1 << bit);
81 btime = btime + BIT_TIME;
87 if ((bitmask & 1) || (!(bitmask & (1 << 9)))) {
94 static void suart_putc_(
const uint8_t *tx_b)
97 uint16_t bitmask = (*tx_b << 2) | 1 | (1 << 10);
106 btime = btime + BIT_TIME;
107 bitmask = (bitmask >> 1);
108 if (bitmask == 0)
break;
116 static void ByteCrc(
const uint8_t *bt)
119 for (uint8_t i = 0; i < 8; i++)
121 if (((xb & 0x01) ^ (CRC_16.word & 0x0001)) !=0 ) {
122 CRC_16.word = CRC_16.word >> 1;
123 CRC_16.word = CRC_16.word ^ 0xA001;
125 CRC_16.word = CRC_16.word >> 1;
131 static uint8_t BL_ReadBuf(uint8_t *pstring, uint8_t len)
138 if(!suart_getc_(pstring))
goto timeout;
146 if(!suart_getc_(&LastCRC_16.bytes[0]))
goto timeout;
147 if(!suart_getc_(&LastCRC_16.bytes[1]))
goto timeout;
148 if(!suart_getc_(&LastACK))
goto timeout;
149 if (CRC_16.word != LastCRC_16.word) {
153 if(!suart_getc_(&LastACK))
goto timeout;
159 static void BL_SendBuf(
const uint8_t *pstring, uint8_t len)
164 suart_putc_(pstring);
171 suart_putc_(&CRC_16.bytes[0]);
172 suart_putc_(&CRC_16.bytes[1]);
180 #define DevSignHi (BootMsgLen) 181 #define DevSignLo (BootMsgLen+1) 185 uint8_t BootMsg[BootMsgLen+1] =
"471";
187 #if defined(USE_SERIAL_4WAY_SK_BOOTLOADER) 188 static const uint8_t BootInit[] = {0,0,0,0,0,0,0,0,0,0,0,0,0x0D,
'B',
'L',
'H',
'e',
'l',
'i',0xF4,0x7D};
189 BL_SendBuf(BootInit, 21);
191 static const uint8_t BootInit[] = {0,0,0,0,0,0,0,0,0x0D,
'B',
'L',
'H',
'e',
'l',
'i',0xF4,0x7D};
192 BL_SendBuf(BootInit, 17);
194 if (!BL_ReadBuf(BootInfo, BootMsgLen + 4)) {
199 for (uint8_t i = 0; i < (BootMsgLen - 1); i++) {
200 if (BootInfo[i] != BootMsg[i]) {
206 pDeviceInfo->bytes[2] = BootInfo[BootMsgLen - 1];
207 pDeviceInfo->bytes[1] = BootInfo[DevSignHi];
208 pDeviceInfo->bytes[0] = BootInfo[DevSignLo];
212 static uint8_t BL_GetACK(uint32_t Timeout)
215 while (!(suart_getc_(&LastACK)) && (Timeout)) {
234 pDeviceInfo->bytes[0] = 1;
239 static uint8_t BL_SendCMDSetAddress(
ioMem_t *pMem)
248 static uint8_t BL_SendCMDSetBuffer(
ioMem_t *pMem)
256 if (BL_GetACK(2) !=
brNONE)
return 0;
261 static uint8_t BL_ReadA(uint8_t cmd,
ioMem_t *pMem)
263 if (BL_SendCMDSetAddress(pMem)) {
271 static uint8_t BL_WriteA(uint8_t cmd,
ioMem_t *pMem, uint32_t timeout)
273 if (BL_SendCMDSetAddress(pMem)) {
274 if (!BL_SendCMDSetBuffer(pMem))
return 0;
275 uint8_t sCMD[] = {cmd, 0x01};
277 return (BL_GetACK(timeout) ==
brSUCCESS);
298 if (BL_SendCMDSetAddress(pMem)) {
301 return (BL_GetACK((1400 / START_BIT_TIMEOUT_MS)) ==
brSUCCESS);
308 return BL_WriteA(
CMD_PROG_EEPROM, pMem, (3000 / START_BIT_TIMEOUT_MS));
313 return BL_WriteA(
CMD_PROG_FLASH, pMem, (400 / START_BIT_TIMEOUT_MS));
318 if (BL_SendCMDSetAddress(pMem)) {
319 if (!BL_SendCMDSetBuffer(pMem))
return 0;
322 return (BL_GetACK(40 / START_BIT_TIMEOUT_MS));
struct timespec start_time
uint8_t BL_VerifyFlash(ioMem_t *pMem)
uint8_t BL_ReadEEprom(ioMem_t *pMem)
void BL_SendCMDRunRestartBootloader(uint8_32_u *pDeviceInfo)
bool isMcuConnected(void)
uint8_t BL_WriteEEprom(ioMem_t *pMem)
uint8_t BL_SendCMDKeepAlive(void)
uint8_t BL_PageErase(ioMem_t *pMem)
#define CMD_VERIFY_FLASH_ARM
#define CMD_READ_FLASH_ATM
uint8_t BL_ReadFlash(uint8_t interface_mode, ioMem_t *pMem)
#define CMD_READ_FLASH_SIL
uint8_t BL_WriteFlash(ioMem_t *pMem)
uint8_t BL_ConnectEx(uint8_32_u *pDeviceInfo)
#define RestartBootloader