11 #ifdef USE_SERIAL_4WAY_BLHELI_INTERFACE 21 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER 25 #if defined(USE_SERIAL_4WAY_SK_BOOTLOADER) 32 #define RX_LED_OFF LED0_OFF 33 #define RX_LED_ON LED0_ON 35 #define TX_LED_OFF LED1_OFF 36 #define TX_LED_ON LED1_ON 38 #define TX_LED_OFF LED0_OFF 39 #define TX_LED_ON LED0_ON 48 #define SERIAL_4WAY_INTERFACE_NAME_STR "m4wFCIntf" 50 #define SERIAL_4WAY_VER_MAIN 20 51 #define SERIAL_4WAY_VER_SUB_1 (uint8_t) 0 52 #define SERIAL_4WAY_VER_SUB_2 (uint8_t) 02 54 #define SERIAL_4WAY_PROTOCOL_VER 107 57 #if (SERIAL_4WAY_VER_MAIN > 24) 58 #error "beware of SERIAL_4WAY_VER_SUB_1 is uint8_t" 61 #define SERIAL_4WAY_VERSION (uint16_t) ((SERIAL_4WAY_VER_MAIN * 1000) + (SERIAL_4WAY_VER_SUB_1 * 100) + SERIAL_4WAY_VER_SUB_2) 63 #define SERIAL_4WAY_VERSION_HI (uint8_t) (SERIAL_4WAY_VERSION / 100) 64 #define SERIAL_4WAY_VERSION_LO (uint8_t) (SERIAL_4WAY_VERSION % 100) 66 static uint8_t escCount;
73 #define DeviceInfoSize 4 77 return (DeviceInfo.bytes[0] > 0);
120 memset(&escHardware, 0,
sizeof(escHardware));
121 for (
volatile uint8_t i = 0; i < nm; i++) {
122 uint8_t
pin = output_channels[i];
136 void esc4wayRelease(
void)
138 while (escCount > 0) {
145 #define SET_DISCONNECTED DeviceInfo.words[0] = 0 147 #define INTF_MODE_IDX 3 // index for DeviceInfostate 157 #define cmd_Remote_Escape 0x2E // '.' 158 #define cmd_Local_Escape 0x2F // '/' 161 #define cmd_InterfaceTestAlive 0x30 // '0' alive 165 #define cmd_ProtocolGetVersion 0x31 // '1' version 169 #define cmd_InterfaceGetName 0x32 // '2' name 173 #define cmd_InterfaceGetVersion 0x33 // '3' version 178 #define cmd_InterfaceExit 0x34 // '4' exit 182 #define cmd_DeviceReset 0x35 // '5' reset 190 #define cmd_DeviceInitFlash 0x37 // '7' init flash access 194 #define cmd_DeviceEraseAll 0x38 // '8' erase all 198 #define cmd_DevicePageErase 0x39 // '9' page erase 204 #define cmd_DeviceRead 0x3A // ':' read Device 210 #define cmd_DeviceWrite 0x3B // ';' write 215 #define cmd_DeviceC2CK_LOW 0x3C // '<' 220 #define cmd_DeviceReadEEprom 0x3D // '=' read Device 226 #define cmd_DeviceWriteEEprom 0x3E // '>' write 231 #define cmd_InterfaceSetMode 0x3F // '?' 241 #define cmd_DeviceVerify 0x40 //'@' write 248 #define ACK_I_INVALID_CMD 0x02 249 #define ACK_I_INVALID_CRC 0x03 250 #define ACK_I_VERIFY_ERROR 0x04 255 #define ACK_I_INVALID_CHANNEL 0x08 256 #define ACK_I_INVALID_PARAM 0x09 257 #define ACK_D_GENERAL_ERROR 0x0F 294 crc = crc ^ ((uint16_t)data << 8);
295 for (i=0; i < 8; i++){
297 crc = (crc << 1) ^ 0x1021;
306 #define ATMEL_DEVICE_MATCH ((pDeviceInfo->words[0] == 0x9307) || (pDeviceInfo->words[0] == 0x930A) || \ 307 (pDeviceInfo->words[0] == 0x930F) || (pDeviceInfo->words[0] == 0x940B)) 309 #define SILABS_DEVICE_MATCH ((pDeviceInfo->words[0] == 0xF310)||(pDeviceInfo->words[0] ==0xF330) || \ 310 (pDeviceInfo->words[0] == 0xF410) || (pDeviceInfo->words[0] == 0xF390) || \ 311 (pDeviceInfo->words[0] == 0xF850) || (pDeviceInfo->words[0] == 0xE8B1) || \ 312 (pDeviceInfo->words[0] == 0xE8B2)) 314 #define ARM_DEVICE_MATCH ((pDeviceInfo->words[0] == 0x1F06) || \ 315 (pDeviceInfo->words[0] == 0x3306) || (pDeviceInfo->words[0] == 0x3406) || (pDeviceInfo->words[0] == 0x3506)) 317 static uint8_t CurrentInterfaceMode;
319 static uint8_t Connect(
uint8_32_u *pDeviceInfo)
321 for (uint8_t I = 0; I < 3; ++I) {
322 #if (defined(USE_SERIAL_4WAY_BLHELI_BOOTLOADER) && defined(USE_SERIAL_4WAY_SK_BOOTLOADER)) 324 CurrentInterfaceMode =
imSK;
328 if SILABS_DEVICE_MATCH {
331 }
else if ATMEL_DEVICE_MATCH {
334 }
else if ARM_DEVICE_MATCH {
340 #elif defined(USE_SERIAL_4WAY_BLHELI_BOOTLOADER) 342 if SILABS_DEVICE_MATCH {
345 }
else if ATMEL_DEVICE_MATCH {
348 }
else if ARM_DEVICE_MATCH {
353 #elif defined(USE_SERIAL_4WAY_SK_BOOTLOADER) 355 CurrentInterfaceMode =
imSK;
356 if ATMEL_DEVICE_MATCH
return 1;
365 static uint8_t ReadByte(
void)
375 static uint8_t ReadByteCrc(
void)
377 uint8_t b = ReadByte();
382 static void WriteByte(uint8_t b)
387 static void WriteByteCrc(uint8_t b)
393 static bool UartTxPending() {
400 uint8_t ParamBuf[256];
415 bool isExitScheduled =
false;
427 O_PARAM = &Dummy.bytes[0];
432 I_PARAM_LEN = ReadByteCrc();
435 uint8_t i = I_PARAM_LEN;
437 *InBuff = ReadByteCrc();
442 CRC_check.bytes[1] = ReadByte();
443 CRC_check.bytes[0] = ReadByte();
445 if(CRC_check.word == CRC_in.word) {
464 switch(CurrentInterfaceMode)
466 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER 477 #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER 489 if ( ACK_OUT !=
ACK_OK) SET_DISCONNECTED;
504 O_PARAM_LEN = strlen(SERIAL_4WAY_INTERFACE_NAME_STR);
505 O_PARAM = (uint8_t *)SERIAL_4WAY_INTERFACE_NAME_STR;
520 isExitScheduled =
true;
525 #if defined(USE_SERIAL_4WAY_BLHELI_BOOTLOADER) && defined(USE_SERIAL_4WAY_SK_BOOTLOADER) 527 #elif defined(USE_SERIAL_4WAY_BLHELI_BOOTLOADER) 529 #elif defined(USE_SERIAL_4WAY_SK_BOOTLOADER) 530 if (ParamBuf[0] ==
imSK) {
532 CurrentInterfaceMode = ParamBuf[0];
541 if (ParamBuf[0] < escCount) {
549 switch (CurrentInterfaceMode)
552 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER 560 #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER 573 if (ParamBuf[0] < escCount) {
581 O_PARAM_LEN = DeviceInfoSize;
582 O_PARAM = (uint8_t *)&DeviceInfo;
583 if(Connect(&DeviceInfo)) {
584 DeviceInfo.bytes[INTF_MODE_IDX] = CurrentInterfaceMode;
592 #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER 595 switch(CurrentInterfaceMode)
609 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER 612 switch (CurrentInterfaceMode)
617 Dummy.bytes[0] = ParamBuf[0];
645 switch(CurrentInterfaceMode)
647 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER 659 #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER 675 O_PARAM = (uint8_t *)&ParamBuf;
688 switch (CurrentInterfaceMode)
690 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER 701 #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER 717 O_PARAM = (uint8_t *)&ParamBuf;
731 switch (CurrentInterfaceMode)
733 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER 743 #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER 766 switch (CurrentInterfaceMode)
768 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER 783 #ifdef USE_SERIAL_4WAY_SK_BOOTLOADER 797 #ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER 800 switch (CurrentInterfaceMode)
850 WriteByteCrc(O_PARAM_LEN);
855 WriteByteCrc(*O_PARAM);
860 WriteByteCrc(ACK_OUT);
861 WriteByte(CRCout.bytes[1]);
862 WriteByte(CRCout.bytes[0]);
866 if (isExitScheduled) {
uint8_t BL_VerifyFlash(ioMem_t *pMem)
#define ACK_I_INVALID_CHANNEL
#define cmd_InterfaceGetName
#define ACK_I_INVALID_PARAM
virtual bool tx_pending()=0
#define ACK_D_GENERAL_ERROR
#define cmd_InterfaceSetMode
void setEscInput(uint8_t selEsc)
uint8_t Stk_WriteEEprom(ioMem_t *pMem)
uint8_t BL_ReadEEprom(ioMem_t *pMem)
#define cmd_DeviceWriteEEprom
void setEscHi(uint8_t selEsc)
const stm32_pin_info PIN_MAP[BOARD_NR_GPIO_PINS]
void BL_SendCMDRunRestartBootloader(uint8_32_u *pDeviceInfo)
uint8_t Stk_WriteFlash(ioMem_t *pMem)
#define SERIAL_4WAY_VERSION_HI
bool isEscLo(uint8_t selEsc)
#define ACK_I_INVALID_CMD
#define cmd_InterfaceTestAlive
void gpio_set_mode(const gpio_dev *const dev, uint8_t pin, gpio_pin_mode mode)
bool isMcuConnected(void)
uint8_t BL_WriteEEprom(ioMem_t *pMem)
uint8_t BL_SendCMDKeepAlive(void)
static const uint8_t * output_channels
uint8_t Stk_ConnectEx(uint8_32_u *pDeviceInfo)
#define cmd_ProtocolGetVersion
#define cmd_InterfaceGetVersion
#define cmd_DeviceReadEEprom
virtual size_t write(uint8_t)=0
uint8_t BL_PageErase(ioMem_t *pMem)
bool isEscHi(uint8_t selEsc)
uint8_t Stk_Chip_Erase(void)
uint8_t Stk_ReadFlash(ioMem_t *pMem)
#define SERIAL_4WAY_VERSION_LO
uint8_t BL_ReadFlash(uint8_t interface_mode, ioMem_t *pMem)
#define cmd_DeviceInitFlash
void setEscLo(uint8_t selEsc)
virtual uint32_t available()=0
const gpio_dev * gpio_device
static INLINE uint8_t gpio_read_bit(const gpio_dev *const dev, uint8_t pin)
void esc4wayProcess(AP_HAL::UARTDriver *uartPort)
uint8_t BL_WriteFlash(ioMem_t *pMem)
uint16_t _crc_xmodem_update(uint16_t crc, uint8_t data)
#define cmd_InterfaceExit
#define SERIAL_4WAY_PROTOCOL_VER
#define ACK_I_INVALID_CRC
#define cmd_DeviceEraseAll
uint8_t BL_ConnectEx(uint8_32_u *pDeviceInfo)
#define cmd_Remote_Escape
uint8_t Stk_ReadEEprom(ioMem_t *pMem)
Stores STM32-specific information related to a given Maple pin.
const gpio_dev *const gpio_device
#define ACK_I_VERIFY_ERROR
void hal_yield(uint16_t ttw)
void setEscOutput(uint8_t selEsc)
#define cmd_DevicePageErase
#define MAX_SUPPORTED_MOTORS
static INLINE void gpio_write_bit(const gpio_dev *const dev, uint8_t pin, uint8_t val)
uint8_t esc4wayInit(const uint8_t *output_channels, uint8_t nm)