4way-interface cleanup

A few optimizations as well
This commit is contained in:
4712 2016-04-04 20:33:15 +02:00
parent f6c55c240c
commit eb9c8a22b0
4 changed files with 94 additions and 144 deletions

View File

@ -104,70 +104,57 @@ void usb1WireDeInitializeVcp(void){
#define START_BIT_TIME (BIT_TIME_HALVE + 1) #define START_BIT_TIME (BIT_TIME_HALVE + 1)
#define STOP_BIT_TIME ((BIT_TIME * 9) + BIT_TIME_HALVE) #define STOP_BIT_TIME ((BIT_TIME * 9) + BIT_TIME_HALVE)
static void suart_putc_(uint8_t tx_b) static void suart_putc_(uint8_t *tx_b)
{ {
uint32_t btime; // shift out stopbit first
ESC_SET_LO; // 0 = start bit uint16_t bitmask = (*tx_b << 2) | 1 | (1 << 10);
btime = BIT_TIME + micros(); uint32_t btime = micros();
while (micros() < btime); while(1) {
for(uint8_t bit = 0; bit <8; bit++) if(bitmask & 1) {
{
if(tx_b & 1)
{
ESC_SET_HI; // 1 ESC_SET_HI; // 1
} } else {
else
{
ESC_SET_LO; // 0 ESC_SET_LO; // 0
} }
btime = btime + BIT_TIME; btime = btime + BIT_TIME;
tx_b = (tx_b >> 1); bitmask = (bitmask >> 1);
if (bitmask == 0) break; // stopbit shifted out - but don't wait
while (micros() < btime); while (micros() < btime);
} }
ESC_SET_HI; // 1 = stop bit
btime = btime + BIT_TIME;
while (micros() < btime);
} }
static uint8_t suart_getc_(uint8_t *bt) static uint8_t suart_getc_(uint8_t *bt)
{ {
uint32_t btime; uint32_t btime;
uint32_t start_time; uint32_t start_time;
uint32_t stop_time;
uint32_t wait_start;
*bt = 0; uint32_t wait_time = millis() + START_BIT_TIMEOUT_MS;
wait_start = millis() + START_BIT_TIMEOUT_MS;
while (ESC_IS_HI) { while (ESC_IS_HI) {
// check for start bit begin // check for startbit begin
if (millis() > wait_start) { if (millis() >= wait_time) {
return 0; return 0;
} }
} }
// start bit // start bit
start_time = micros(); start_time = micros();
btime = start_time + START_BIT_TIME; btime = start_time + START_BIT_TIME;
stop_time = start_time + STOP_BIT_TIME; uint16_t bitmask = 0;
uint8_t bit = 0;
while (micros() < btime); while (micros() < btime);
while(1) {
if (ESC_IS_HI) return 0; // check start bit if (ESC_IS_HI) {
for (uint8_t bit=0;bit<8;bit++) bitmask |= (1 << bit);
{
btime = btime + BIT_TIME;
while (micros() < btime);
if (ESC_IS_HI)
{
*bt |= (1 << bit); // 1 else 0
} }
btime = btime + BIT_TIME;
bit++;
if (bit == 10) break;
while (micros() < btime);
} }
while (micros() < stop_time); // check start bit and stop bit
if ((bitmask & 1) || (!(bitmask & (1 << 9)))) {
if (ESC_IS_LO) return 0; // check stop bit return 0;
}
return 1; // OK *bt = bitmask >> 1;
return 1;
} }
#define USE_TXRX_LED #define USE_TXRX_LED
@ -224,7 +211,7 @@ void usb1WirePassthroughVcp(mspPort_t *mspPort, bufWriter_t *bufwriter, uint8_t
TX_LED_ON; TX_LED_ON;
do { do {
bt = serialRead(mspPort->port); bt = serialRead(mspPort->port);
suart_putc_(bt); suart_putc_(&bt);
} while(serialRxBytesWaiting(mspPort->port)); } while(serialRxBytesWaiting(mspPort->port));
ESC_INPUT; ESC_INPUT;
TX_LED_OFF; TX_LED_OFF;

View File

@ -358,7 +358,6 @@ void Process4WayInterface(mspPort_t *mspPort, bufWriter_t *bufwriter) {
#ifdef BEEPER #ifdef BEEPER
// fix for buzzer often starts beeping continuously when the ESCs are read // fix for buzzer often starts beeping continuously when the ESCs are read
// switch beeper silent here // switch beeper silent here
// TODO (4712) check: is beeperSilence useful here?
beeperSilence(); beeperSilence();
#endif #endif
bool isExitScheduled = false; bool isExitScheduled = false;
@ -409,7 +408,6 @@ void Process4WayInterface(mspPort_t *mspPort, bufWriter_t *bufwriter) {
// ******* Interface related stuff ******* // ******* Interface related stuff *******
case cmd_InterfaceTestAlive: case cmd_InterfaceTestAlive:
{ {
break;
if (IsMcuConnected){ if (IsMcuConnected){
switch(CurrentInterfaceMode) switch(CurrentInterfaceMode)
{ {

View File

@ -57,136 +57,110 @@
#define brERRORCRC 0xC2 #define brERRORCRC 0xC2
#define brNONE 0xFF #define brNONE 0xFF
static union uint8_16u CRC_16;
static uint8_t cb; #define START_BIT_TIMEOUT_MS 2
static uint8_t suart_timeout;
#define WaitStartBitTimeoutms 2 #define BIT_TIME (52) //52uS
#define BIT_TIME_HALVE (BIT_TIME >> 1) //26uS
#define START_BIT_TIME (BIT_TIME_HALVE)// + 1)
//#define STOP_BIT_TIME ((BIT_TIME * 9) + BIT_TIME_HALVE)
#define BitTime (52) //52uS static uint8_t suart_getc_(uint8_t *bt)
#define BitHalfTime (BitTime >> 1) //26uS
#define StartBitTime (BitHalfTime + 1)
#define StopBitTime ((BitTime * 9) + BitHalfTime)
static uint8_t suart_getc_(void)
{ {
uint8_t bt=0;
uint32_t btime; uint32_t btime;
uint32_t bstop;
uint32_t start_time; uint32_t start_time;
suart_timeout = 1; uint32_t wait_time = millis() + START_BIT_TIMEOUT_MS;
uint32_t wait_time = millis() + WaitStartBitTimeoutms;
while (ESC_IS_HI) { while (ESC_IS_HI) {
// check for Startbit begin // check for startbit begin
if (millis() >= wait_time) { if (millis() >= wait_time) {
return 0; return 0;
} }
} }
// Startbit // start bit
start_time = micros(); start_time = micros();
btime = start_time + START_BIT_TIME;
btime = start_time + StartBitTime; uint16_t bitmask = 0;
bstop= start_time + StopBitTime; uint8_t bit = 0;
while (micros() < btime); while (micros() < btime);
if (ESC_IS_HI) { while(1) {
return 0;
}
for (uint8_t bit = 0; bit < 8; bit++)
{
btime = btime + BitTime;
while (micros() < btime);
if (ESC_IS_HI) if (ESC_IS_HI)
{ {
bt |= (1 << bit); bitmask |= (1 << bit);
} }
} btime = btime + BIT_TIME;
while (micros() < bstop); bit++;
// check Stoppbit if (bit == 10) break;
if (ESC_IS_LO) {
return 0;
}
suart_timeout = 0;
return (bt);
}
static void suart_putc_(uint8_t TXbyte)
{
uint32_t btime;
ESC_SET_LO; // Set low = StartBit
btime = BitTime + micros();
while (micros() < btime);
for(uint8_t bit = 0; bit < 8; bit++)
{
if(TXbyte & 1)
{
ESC_SET_HI; // 1
}
else
{
ESC_SET_LO; // 0
}
btime = btime + BitTime;
TXbyte = (TXbyte >> 1);
while (micros() < btime); while (micros() < btime);
} }
ESC_SET_HI; //Set high = Stoppbit // check start bit and stop bit
btime = btime + BitTime; if ((bitmask & 1) || (!(bitmask & (1 << 9)))) {
while (micros() < btime); return 0;
}
*bt = bitmask >> 1;
return 1;
} }
static void suart_putc_(uint8_t *tx_b)
{
// shift out stopbit first
uint16_t bitmask = (*tx_b << 2) | 1 | (1 << 10);
uint32_t btime = micros();
while(1) {
if(bitmask & 1) {
ESC_SET_HI; // 1
}
else {
ESC_SET_LO; // 0
}
btime = btime + BIT_TIME;
bitmask = (bitmask >> 1);
if (bitmask == 0) break; // stopbit shifted out - but don't wait
while (micros() < btime);
}
}
static union uint8_16u CRC_16;
static union uint8_16u LastCRC_16; static union uint8_16u LastCRC_16;
static void ByteCrc(void) static void ByteCrc(uint8_t *bt)
{ {
uint8_t xb = *bt;
for (uint8_t i = 0; i < 8; i++) for (uint8_t i = 0; i < 8; i++)
{ {
if (((cb & 0x01) ^ (CRC_16.word & 0x0001)) !=0 ) if (((xb & 0x01) ^ (CRC_16.word & 0x0001)) !=0 ) {
{
CRC_16.word = CRC_16.word >> 1; CRC_16.word = CRC_16.word >> 1;
CRC_16.word = CRC_16.word ^ 0xA001; CRC_16.word = CRC_16.word ^ 0xA001;
} } else {
else
{
CRC_16.word = CRC_16.word >> 1; CRC_16.word = CRC_16.word >> 1;
} }
cb = cb >> 1; xb = xb >> 1;
} }
} }
static uint8_t BL_ReadBuf(uint8_t *pstring, uint8_t len) static uint8_t BL_ReadBuf(uint8_t *pstring, uint8_t len)
{ {
//Todo CRC in case of timeout? // len 0 means 256
//len 0 means 256
CRC_16.word = 0; CRC_16.word = 0;
LastCRC_16.word = 0; LastCRC_16.word = 0;
uint8_t LastACK = brNONE; uint8_t LastACK = brNONE;
do { do {
cb = suart_getc_(); if(!suart_getc_(pstring)) goto timeout;
if(suart_timeout) goto timeout; ByteCrc(pstring);
*pstring = cb;
ByteCrc();
pstring++; pstring++;
len--; len--;
} while(len > 0); } while(len > 0);
if(IsMcuConnected) { if(IsMcuConnected) {
//With CRC read 3 more //With CRC read 3 more
LastCRC_16.bytes[0] = suart_getc_(); if(!suart_getc_(&LastCRC_16.bytes[0])) goto timeout;
if(suart_timeout) goto timeout; if(!suart_getc_(&LastCRC_16.bytes[1])) goto timeout;
LastCRC_16.bytes[1] = suart_getc_(); if(!suart_getc_(&LastACK)) goto timeout;
if(suart_timeout) goto timeout;
LastACK = suart_getc_();
if (CRC_16.word != LastCRC_16.word) { if (CRC_16.word != LastCRC_16.word) {
LastACK = brERRORCRC; LastACK = brERRORCRC;
} }
} else { } else {
//TODO check here LastACK if(!suart_getc_(&LastACK)) goto timeout;
LastACK = suart_getc_();
} }
timeout: timeout:
return (LastACK == brSUCCESS); return (LastACK == brSUCCESS);
@ -195,20 +169,17 @@ timeout:
static void BL_SendBuf(uint8_t *pstring, uint8_t len) static void BL_SendBuf(uint8_t *pstring, uint8_t len)
{ {
ESC_OUTPUT; ESC_OUTPUT;
// wait some us
delayMicroseconds(50);
CRC_16.word=0; CRC_16.word=0;
do { do {
cb = *pstring; suart_putc_(pstring);
ByteCrc(pstring);
pstring++; pstring++;
suart_putc_(cb);
ByteCrc();
len--; len--;
} while (len > 0); } while (len > 0);
if (IsMcuConnected) { if (IsMcuConnected) {
suart_putc_(CRC_16.bytes[0]); suart_putc_(&CRC_16.bytes[0]);
suart_putc_(CRC_16.bytes[1]); suart_putc_(&CRC_16.bytes[1]);
} }
ESC_INPUT; ESC_INPUT;
} }
@ -250,19 +221,13 @@ uint8_t BL_ConnectEx(void)
static uint8_t BL_GetACK(uint32_t Timeout) static uint8_t BL_GetACK(uint32_t Timeout)
{ {
uint8_t LastACK; uint8_t LastACK = brNONE;
do { while (!(suart_getc_(&LastACK)) && (Timeout)) {
LastACK = suart_getc_();
Timeout--; Timeout--;
} while ((suart_timeout) && (Timeout)); } ;
if(suart_timeout) {
LastACK = brNONE;
}
return (LastACK); return (LastACK);
} }
uint8_t BL_SendCMDKeepAlive(void) uint8_t BL_SendCMDKeepAlive(void)
{ {
uint8_t sCMD[] = {CMD_KEEP_ALIVE, 0}; uint8_t sCMD[] = {CMD_KEEP_ALIVE, 0};
@ -345,19 +310,19 @@ uint8_t BL_PageErase(void)
if (BL_SendCMDSetAddress()) { if (BL_SendCMDSetAddress()) {
uint8_t sCMD[] = {CMD_ERASE_FLASH, 0x01}; uint8_t sCMD[] = {CMD_ERASE_FLASH, 0x01};
BL_SendBuf(sCMD, 2); BL_SendBuf(sCMD, 2);
return (BL_GetACK((40 / WaitStartBitTimeoutms)) == brSUCCESS); return (BL_GetACK((40 / START_BIT_TIMEOUT_MS)) == brSUCCESS);
} }
return 0; return 0;
} }
uint8_t BL_WriteEEprom(void) uint8_t BL_WriteEEprom(void)
{ {
return BL_WriteA(CMD_PROG_EEPROM, (3000 / WaitStartBitTimeoutms)); return BL_WriteA(CMD_PROG_EEPROM, (3000 / START_BIT_TIMEOUT_MS));
} }
uint8_t BL_WriteFlash(void) uint8_t BL_WriteFlash(void)
{ {
return BL_WriteA(CMD_PROG_FLASH, (40 / WaitStartBitTimeoutms)); return BL_WriteA(CMD_PROG_FLASH, (40 / START_BIT_TIMEOUT_MS));
} }
#endif #endif

View File

@ -24,7 +24,7 @@
void BL_SendBootInit(void); void BL_SendBootInit(void);
uint8_t BL_ConnectEx(void); uint8_t BL_ConnectEx(void);
uint8_t BL_SendCMDKeepAlive(void); uint8_t BL_SendCMDKeepAlive(void);
uint8_t BL_PageErase(void); uint8_t BL_PageErase(void);
uint8_t BL_ReadEEprom(void); uint8_t BL_ReadEEprom(void);
uint8_t BL_WriteEEprom(void); uint8_t BL_WriteEEprom(void);