15 #ifdef USE_SERIAL_4WAY_BLHELI_INTERFACE 21 #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER 23 #define BIT_LO_US (32) //32uS 24 #define BIT_HI_US (2*BIT_LO_US) 26 static uint8_t StkInBuf[16];
28 #define STK_BIT_TIMEOUT 250 // micro seconds 29 #define STK_WAIT_TICKS (1000 / STK_BIT_TIMEOUT) // per ms 30 #define STK_WAITCYLCES (STK_WAIT_TICKS * 35) // 35ms 31 #define STK_WAITCYLCES_START (STK_WAIT_TICKS / 2) // 0.5 ms 32 #define STK_WAITCYLCES_EXT (STK_WAIT_TICKS * 5000) //5 s 34 #define WaitPinLo while (ESC_IS_HI) {if (micros() > timeout_timer) goto timeout;} 35 #define WaitPinHi while (ESC_IS_LO) {if (micros() > timeout_timer) goto timeout;} 37 static uint32_t LastBitTime;
38 static uint32_t HiLoTsh;
40 static uint8_t SeqNumber;
41 static uint8_t StkCmd;
42 static uint8_t ckSumIn;
43 static uint8_t ckSumOut;
46 #define MESSAGE_START 0x1B 49 #define CMD_SIGN_ON 0x01 50 #define CMD_LOAD_ADDRESS 0x06 51 #define CMD_CHIP_ERASE_ISP 0x12 52 #define CMD_PROGRAM_FLASH_ISP 0x13 53 #define CMD_READ_FLASH_ISP 0x14 54 #define CMD_PROGRAM_EEPROM_ISP 0x15 55 #define CMD_READ_EEPROM_ISP 0x16 56 #define CMD_READ_SIGNATURE_ISP 0x1B 57 #define CMD_SPI_MULTI 0x1D 59 #define STATUS_CMD_OK 0x00 61 #define CmdFlashEepromRead 0xA0 62 #define EnterIspCmd1 0xAC 63 #define EnterIspCmd2 0x53 64 #define signature_r 0x30 66 #define IRQ_OFF // dummy 67 #define IRQ_ON // dummy 69 static void StkSendByte(uint8_t dat)
72 for (uint8_t i = 0; i < 8; i++) {
94 static void StkSendPacketHeader(
void)
102 StkSendByte(MESSAGE_START);
103 StkSendByte(++SeqNumber);
106 static void StkSendPacketFooter(
void)
108 StkSendByte(ckSumOut);
117 static int8_t ReadBit(
void)
119 uint32_t btimer =
micros();
120 uint32_t timeout_timer = btimer + STK_BIT_TIMEOUT;
123 LastBitTime =
micros() - btimer;
124 if (LastBitTime <= HiLoTsh) {
125 timeout_timer = timeout_timer + STK_BIT_TIMEOUT;
137 static uint8_t ReadByte(uint8_t *bt)
140 for (uint8_t i = 0; i < 8; i++) {
141 int8_t bit = ReadBit();
142 if (bit == -1)
goto timeout;
153 static uint8_t StkReadLeader(
void)
157 HiLoTsh = BIT_HI_US + BIT_LO_US;
162 if((StkCmd == CMD_PROGRAM_EEPROM_ISP) || (StkCmd == CMD_CHIP_ERASE_ISP)) {
163 waitcycl = STK_WAITCYLCES_EXT;
164 }
else if(StkCmd == CMD_SIGN_ON) {
165 waitcycl = STK_WAITCYLCES_START;
167 waitcycl= STK_WAITCYLCES;
169 for ( ; waitcycl >0 ; waitcycl--) {
171 if (ReadBit() >- 1)
break;
179 for (uint8_t i = 0; i < 10; i++) {
180 if (ReadBit() == -1)
goto timeout;
184 HiLoTsh = (LastBitTime >> 1) + (LastBitTime >> 2);
190 if (bit == -1)
goto timeout;
197 static uint8_t StkRcvPacket(uint8_t *pstring)
203 if (!StkReadLeader())
goto Err;
205 if (!ReadByte(&bt) || (bt != MESSAGE_START))
goto Err;
206 if (!ReadByte(&bt) || (bt != SeqNumber))
goto Err;
207 ReadByte(&Len.bytes[1]);
208 if (Len.bytes[1] > 1)
goto Err;
209 ReadByte(&Len.bytes[0]);
210 if (Len.bytes[0] < 1)
goto Err;
211 if (!ReadByte(&bt) || (bt != TOKEN))
goto Err;
212 if (!ReadByte(&bt) || (bt != StkCmd))
goto Err;
213 if (!ReadByte(&bt) || (bt != STATUS_CMD_OK))
goto Err;
214 for (uint16_t i = 0; i < (Len.word - 2); i++)
216 if (!ReadByte(pstring))
goto Err;
220 if (ckSumIn != 0)
goto Err;
228 static uint8_t _CMD_SPI_MULTI_EX(
volatile uint8_t * ResByte,uint8_t Cmd,uint8_t AdrHi,uint8_t AdrLo)
230 StkCmd= CMD_SPI_MULTI;
231 StkSendPacketHeader();
235 StkSendByte(CMD_SPI_MULTI);
243 StkSendPacketFooter();
244 if (StkRcvPacket(StkInBuf)) {
245 if ((StkInBuf[0] == 0x00) && ((StkInBuf[1] == Cmd)||(StkInBuf[1] == 0x00)) &&(StkInBuf[2] == 0x00)) {
246 *ResByte = StkInBuf[3];
253 static uint8_t _CMD_LOAD_ADDRESS(
ioMem_t *pMem)
258 StkCmd = CMD_LOAD_ADDRESS;
259 StkSendPacketHeader();
263 StkSendByte(CMD_LOAD_ADDRESS);
268 StkSendPacketFooter();
269 return StkRcvPacket(StkInBuf);
272 static uint8_t _CMD_READ_MEM_ISP(
ioMem_t *pMem)
280 StkSendPacketHeader();
287 StkSendByte(CmdFlashEepromRead);
288 StkSendPacketFooter();
289 return (StkRcvPacket(pMem->
D_PTR_I));
292 static uint8_t _CMD_PROGRAM_MEM_ISP(
ioMem_t *pMem)
299 Len.word = LenLo + 10;
304 StkSendPacketHeader();
305 StkSendByte(Len.bytes[1]);
306 StkSendByte(Len.bytes[0]);
323 StkSendPacketFooter();
324 return StkRcvPacket(StkInBuf);
330 StkSendPacketHeader();
334 StkSendByte(CMD_SIGN_ON);
335 StkSendPacketFooter();
336 return StkRcvPacket(StkInBuf);
342 if (_CMD_SPI_MULTI_EX(&pDeviceInfo->bytes[1], signature_r,0,1)) {
343 if (_CMD_SPI_MULTI_EX(&pDeviceInfo->bytes[0], signature_r,0,2)) {
353 StkCmd = CMD_CHIP_ERASE_ISP;
354 StkSendPacketHeader();
358 StkSendByte(CMD_CHIP_ERASE_ISP);
365 StkSendPacketFooter();
366 return StkRcvPacket(StkInBuf);
371 if (_CMD_LOAD_ADDRESS(pMem)) {
372 StkCmd = CMD_READ_FLASH_ISP;
373 return (_CMD_READ_MEM_ISP(pMem));
381 if (_CMD_LOAD_ADDRESS(pMem)) {
382 StkCmd = CMD_READ_EEPROM_ISP;
383 return (_CMD_READ_MEM_ISP(pMem));
390 if (_CMD_LOAD_ADDRESS(pMem)) {
391 StkCmd = CMD_PROGRAM_FLASH_ISP;
392 return (_CMD_PROGRAM_MEM_ISP(pMem));
399 if (_CMD_LOAD_ADDRESS(pMem)) {
400 StkCmd = CMD_PROGRAM_EEPROM_ISP;
401 return (_CMD_PROGRAM_MEM_ISP(pMem));
uint8_t Stk_WriteEEprom(ioMem_t *pMem)
uint8_t Stk_WriteFlash(ioMem_t *pMem)
uint8_t Stk_ConnectEx(uint8_32_u *pDeviceInfo)
uint8_t Stk_Chip_Erase(void)
uint8_t Stk_ReadFlash(ioMem_t *pMem)
uint8_t Stk_ReadEEprom(ioMem_t *pMem)
static void delay_us(uint16_t t)