Skip to content

Commit

Permalink
Add timeout to serial four way interface for ESC programming (#13277)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveCEvans committed Jan 7, 2024
1 parent 70e70c5 commit 024224d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 59 deletions.
14 changes: 7 additions & 7 deletions src/main/drivers/at32/serial_usb_vcp_at32f4.c
Expand Up @@ -362,15 +362,14 @@ static uint32_t usbVcpAvailable(const serialPort_t *instance)
{
UNUSED(instance);

uint32_t available=0;
cdc_struct_type *pcdc = (cdc_struct_type *)otg_core_struct.dev.class_handler->pdata;
uint32_t available = APP_Rx_ptr_in-APP_Rx_ptr_out;

available=APP_Rx_ptr_in-APP_Rx_ptr_out;
if(available == 0){
cdc_struct_type *pcdc = (cdc_struct_type *)otg_core_struct.dev.class_handler->pdata;
if(pcdc->g_rx_completed == 1){
available=pcdc->g_rxlen;
}
// Return the sum of the bytes in the APP_Rx_Buffer buffer and those received by the VCP driver
if (pcdc->g_rx_completed == 1) {
available += pcdc->g_rxlen;
}

return available;
}

Expand All @@ -382,6 +381,7 @@ static uint8_t usbVcpRead(serialPort_t *instance)
APP_Rx_ptr_out = 0;
APP_Rx_ptr_in = usb_vcp_get_rxdata(&otg_core_struct.dev, APP_Rx_Buffer);
if(APP_Rx_ptr_in == 0) {
// We've drained the input buffer
return 0;
}
}
Expand Down
131 changes: 79 additions & 52 deletions src/main/io/serial_4way.c
Expand Up @@ -92,6 +92,12 @@
#define SERIAL_4WAY_VERSION_HI (uint8_t) (SERIAL_4WAY_VERSION / 100)
#define SERIAL_4WAY_VERSION_LO (uint8_t) (SERIAL_4WAY_VERSION % 100)

#define ESC_TIMEOUT_US 3000000
#define CMD_TIMEOUT_US 50000
#define ARG_TIMEOUT_US 25000
#define DAT_TIMEOUT_US 10000
#define CRC_TIMEOUT_US 10000

static uint8_t escCount;

escHardware_t escHardware[MAX_SUPPORTED_MOTORS];
Expand Down Expand Up @@ -389,19 +395,31 @@ static uint8_t Connect(uint8_32_u *pDeviceInfo)

static serialPort_t *port;

static uint8_t ReadByte(void)
static bool ReadByte(uint8_t *data, timeDelta_t timeoutUs)
{
// need timeout?
while (!serialRxBytesWaiting(port));
return serialRead(port);
// need timedOut?
timeUs_t startTime = micros();
while (!serialRxBytesWaiting(port)) {
if (cmpTimeUs(micros(), startTime) > timeoutUs) {
return true;
}
}

*data = serialRead(port);

return false;
}

static uint8_16_u CRC_in;
static uint8_t ReadByteCrc(void)
static bool ReadByteCrc(uint8_t *data, timeDelta_t timeoutUs)
{
uint8_t b = ReadByte();
CRC_in.word = _crc_xmodem_update(CRC_in.word, b);
return b;
bool timedOut = ReadByte(data, timeoutUs);

if (!timedOut) {
CRC_in.word = _crc_xmodem_update(CRC_in.word, *data);
}

return timedOut;
}

static void WriteByte(uint8_t b)
Expand All @@ -418,7 +436,6 @@ static void WriteByteCrc(uint8_t b)

void esc4wayProcess(serialPort_t *mspPort)
{

uint8_t ParamBuf[256];
uint8_t ESC;
uint8_t I_PARAM_LEN;
Expand All @@ -442,34 +459,42 @@ void esc4wayProcess(serialPort_t *mspPort)
bool isExitScheduled = false;

while (1) {
bool timedOut = false;

// restart looking for new sequence from host
do {
CRC_in.word = 0;
ESC = ReadByteCrc();
} while (ESC != cmd_Local_Escape);
timedOut = ReadByteCrc(&ESC, ESC_TIMEOUT_US);
} while ((ESC != cmd_Local_Escape) && !timedOut);

RX_LED_ON;

Dummy.word = 0;
O_PARAM = &Dummy.bytes[0];
O_PARAM_LEN = 1;
CMD = ReadByteCrc();
ioMem.D_FLASH_ADDR_H = ReadByteCrc();
ioMem.D_FLASH_ADDR_L = ReadByteCrc();
I_PARAM_LEN = ReadByteCrc();

InBuff = ParamBuf;
uint8_t i = I_PARAM_LEN;
do {
*InBuff = ReadByteCrc();
InBuff++;
i--;
} while (i != 0);

CRC_check.bytes[1] = ReadByte();
CRC_check.bytes[0] = ReadByte();

if (CRC_check.word == CRC_in.word) {
if (!timedOut) {
timedOut = ReadByteCrc(&CMD, CMD_TIMEOUT_US);
timedOut |= ReadByteCrc(&ioMem.D_FLASH_ADDR_H, ARG_TIMEOUT_US);
timedOut |= ReadByteCrc(&ioMem.D_FLASH_ADDR_L, ARG_TIMEOUT_US);
timedOut |= ReadByteCrc(&I_PARAM_LEN, ARG_TIMEOUT_US);

if (!timedOut) {
InBuff = ParamBuf;
uint8_t i = I_PARAM_LEN;
do {
timedOut |= ReadByteCrc(InBuff, DAT_TIMEOUT_US);
InBuff++;
i--;
} while ((i > 0) && !timedOut);

if (!timedOut) {
timedOut |= ReadByte(&CRC_check.bytes[1], CRC_TIMEOUT_US);
timedOut |= ReadByte(&CRC_check.bytes[0], CRC_TIMEOUT_US);
}
}
}
if ((CRC_check.word == CRC_in.word) && !timedOut) {
ACK_OUT = ACK_OK;
} else {
ACK_OUT = ACK_I_INVALID_CRC;
Expand All @@ -491,7 +516,7 @@ void esc4wayProcess(serialPort_t *mspPort)
if (isMcuConnected()) {
switch (CurrentInterfaceMode)
{
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
case imATM_BLB:
case imSIL_BLB:
case imARM_BLB:
Expand All @@ -501,16 +526,16 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
case imSK:
{
if (!Stk_SignOn()) { // SetStateDisconnected();
ACK_OUT = ACK_D_GENERAL_ERROR;
}
break;
}
#endif
#endif
default:
ACK_OUT = ACK_D_GENERAL_ERROR;
}
Expand Down Expand Up @@ -581,7 +606,7 @@ void esc4wayProcess(serialPort_t *mspPort)
switch (CurrentInterfaceMode)
{
case imSIL_BLB:
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
case imATM_BLB:
case imARM_BLB:
{
Expand All @@ -596,13 +621,13 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
case imSK:
{
break;
}
#endif
#endif
}
SET_DISCONNECTED;
break;
Expand Down Expand Up @@ -684,7 +709,7 @@ void esc4wayProcess(serialPort_t *mspPort)
*/
switch (CurrentInterfaceMode)
{
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
case imSIL_BLB:
case imATM_BLB:
case imARM_BLB:
Expand All @@ -695,8 +720,8 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
case imSK:
{
if (!Stk_ReadFlash(&ioMem))
Expand All @@ -705,7 +730,7 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#endif
default:
ACK_OUT = ACK_I_INVALID_CMD;
}
Expand All @@ -727,7 +752,7 @@ void esc4wayProcess(serialPort_t *mspPort)
*/
switch (CurrentInterfaceMode)
{
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
case imATM_BLB:
{
if (!BL_ReadEEprom(&ioMem))
Expand All @@ -736,8 +761,8 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
case imSK:
{
if (!Stk_ReadEEprom(&ioMem))
Expand All @@ -746,7 +771,7 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#endif
default:
ACK_OUT = ACK_I_INVALID_CMD;
}
Expand All @@ -769,7 +794,7 @@ void esc4wayProcess(serialPort_t *mspPort)
*/
switch (CurrentInterfaceMode)
{
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
case imSIL_BLB:
case imATM_BLB:
case imARM_BLB:
Expand All @@ -779,8 +804,8 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
case imSK:
{
if (!Stk_WriteFlash(&ioMem))
Expand All @@ -789,7 +814,7 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#endif
}
break;
}
Expand Down Expand Up @@ -820,7 +845,7 @@ void esc4wayProcess(serialPort_t *mspPort)
break;
}
#endif
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
#ifdef USE_SERIAL_4WAY_SK_BOOTLOADER
case imSK:
{
if (Stk_WriteEEprom(&ioMem))
Expand All @@ -829,12 +854,12 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#endif
}
break;
}
//*** Device Memory Verify Ops ***
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
#ifdef USE_SERIAL_4WAY_BLHELI_BOOTLOADER
case cmd_DeviceVerify:
{
switch (CurrentInterfaceMode)
Expand Down Expand Up @@ -870,7 +895,7 @@ void esc4wayProcess(serialPort_t *mspPort)
}
break;
}
#endif
#endif
default:
{
ACK_OUT = ACK_I_INVALID_CMD;
Expand All @@ -889,7 +914,7 @@ void esc4wayProcess(serialPort_t *mspPort)
WriteByteCrc(ioMem.D_FLASH_ADDR_L);
WriteByteCrc(O_PARAM_LEN);

i=O_PARAM_LEN;
uint8_t i = O_PARAM_LEN;
do {
while (!serialTxBytesFree(port));

Expand All @@ -904,11 +929,13 @@ void esc4wayProcess(serialPort_t *mspPort)
serialEndWrite(port);

TX_LED_OFF;

if (isExitScheduled) {
esc4wayRelease();
return;
}
};

}


Expand Down

0 comments on commit 024224d

Please sign in to comment.