Merge pull request #321 from 4712betaflight/4way-interface

4way interface
This commit is contained in:
borisbstyle 2016-04-04 21:09:42 +02:00
commit c694c67890
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 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;
ESC_SET_LO; // 0 = start bit
btime = BIT_TIME + micros();
while (micros() < btime);
for(uint8_t bit = 0; bit <8; bit++)
{
if(tx_b & 1)
{
// 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
{
} else {
ESC_SET_LO; // 0
}
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);
}
ESC_SET_HI; // 1 = stop bit
btime = btime + BIT_TIME;
while (micros() < btime);
}
static uint8_t suart_getc_(uint8_t *bt)
{
uint32_t btime;
uint32_t start_time;
uint32_t stop_time;
uint32_t wait_start;
*bt = 0;
wait_start = millis() + START_BIT_TIMEOUT_MS;
uint32_t wait_time = millis() + START_BIT_TIMEOUT_MS;
while (ESC_IS_HI) {
// check for start bit begin
if (millis() > wait_start) {
// check for startbit begin
if (millis() >= wait_time) {
return 0;
}
}
// start bit
start_time = micros();
btime = start_time + START_BIT_TIME;
stop_time = start_time + STOP_BIT_TIME;
uint16_t bitmask = 0;
uint8_t bit = 0;
while (micros() < btime);
if (ESC_IS_HI) return 0; // check start bit
for (uint8_t bit=0;bit<8;bit++)
{
btime = btime + BIT_TIME;
while (micros() < btime);
if (ESC_IS_HI)
{
*bt |= (1 << bit); // 1 else 0
while(1) {
if (ESC_IS_HI) {
bitmask |= (1 << bit);
}
btime = btime + BIT_TIME;
bit++;
if (bit == 10) break;
while (micros() < btime);
}
while (micros() < stop_time);
if (ESC_IS_LO) return 0; // check stop bit
return 1; // OK
// check start bit and stop bit
if ((bitmask & 1) || (!(bitmask & (1 << 9)))) {
return 0;
}
*bt = bitmask >> 1;
return 1;
}
#define USE_TXRX_LED
@ -224,7 +211,7 @@ void usb1WirePassthroughVcp(mspPort_t *mspPort, bufWriter_t *bufwriter, uint8_t
TX_LED_ON;
do {
bt = serialRead(mspPort->port);
suart_putc_(bt);
suart_putc_(&bt);
} while(serialRxBytesWaiting(mspPort->port));
ESC_INPUT;
TX_LED_OFF;

View File

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

View File

@ -57,136 +57,110 @@
#define brERRORCRC 0xC2
#define brNONE 0xFF
static union uint8_16u CRC_16;
static uint8_t cb;
static uint8_t suart_timeout;
#define START_BIT_TIMEOUT_MS 2
#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
#define BitHalfTime (BitTime >> 1) //26uS
#define StartBitTime (BitHalfTime + 1)
#define StopBitTime ((BitTime * 9) + BitHalfTime)
static uint8_t suart_getc_(void)
static uint8_t suart_getc_(uint8_t *bt)
{
uint8_t bt=0;
uint32_t btime;
uint32_t bstop;
uint32_t start_time;
suart_timeout = 1;
uint32_t wait_time = millis() + WaitStartBitTimeoutms;
uint32_t wait_time = millis() + START_BIT_TIMEOUT_MS;
while (ESC_IS_HI) {
// check for Startbit begin
// check for startbit begin
if (millis() >= wait_time) {
return 0;
}
}
// Startbit
// start bit
start_time = micros();
btime = start_time + StartBitTime;
bstop= start_time + StopBitTime;
btime = start_time + START_BIT_TIME;
uint16_t bitmask = 0;
uint8_t bit = 0;
while (micros() < btime);
if (ESC_IS_HI) {
return 0;
}
for (uint8_t bit = 0; bit < 8; bit++)
{
btime = btime + BitTime;
while (micros() < btime);
while(1) {
if (ESC_IS_HI)
{
bt |= (1 << bit);
bitmask |= (1 << bit);
}
}
while (micros() < bstop);
// check Stoppbit
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);
btime = btime + BIT_TIME;
bit++;
if (bit == 10) break;
while (micros() < btime);
}
ESC_SET_HI; //Set high = Stoppbit
btime = btime + BitTime;
while (micros() < btime);
// check start bit and stop bit
if ((bitmask & 1) || (!(bitmask & (1 << 9)))) {
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 void ByteCrc(void)
static void ByteCrc(uint8_t *bt)
{
uint8_t xb = *bt;
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 ^ 0xA001;
}
else
{
} else {
CRC_16.word = CRC_16.word >> 1;
}
cb = cb >> 1;
xb = xb >> 1;
}
}
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;
LastCRC_16.word = 0;
uint8_t LastACK = brNONE;
do {
cb = suart_getc_();
if(suart_timeout) goto timeout;
*pstring = cb;
ByteCrc();
if(!suart_getc_(pstring)) goto timeout;
ByteCrc(pstring);
pstring++;
len--;
} while(len > 0);
if(IsMcuConnected) {
//With CRC read 3 more
LastCRC_16.bytes[0] = suart_getc_();
if(suart_timeout) goto timeout;
LastCRC_16.bytes[1] = suart_getc_();
if(suart_timeout) goto timeout;
LastACK = suart_getc_();
if(!suart_getc_(&LastCRC_16.bytes[0])) goto timeout;
if(!suart_getc_(&LastCRC_16.bytes[1])) goto timeout;
if(!suart_getc_(&LastACK)) goto timeout;
if (CRC_16.word != LastCRC_16.word) {
LastACK = brERRORCRC;
}
} else {
//TODO check here LastACK
LastACK = suart_getc_();
if(!suart_getc_(&LastACK)) goto timeout;
}
timeout:
return (LastACK == brSUCCESS);
@ -195,20 +169,17 @@ timeout:
static void BL_SendBuf(uint8_t *pstring, uint8_t len)
{
ESC_OUTPUT;
// wait some us
delayMicroseconds(50);
CRC_16.word=0;
do {
cb = *pstring;
suart_putc_(pstring);
ByteCrc(pstring);
pstring++;
suart_putc_(cb);
ByteCrc();
len--;
} while (len > 0);
if (IsMcuConnected) {
suart_putc_(CRC_16.bytes[0]);
suart_putc_(CRC_16.bytes[1]);
suart_putc_(&CRC_16.bytes[0]);
suart_putc_(&CRC_16.bytes[1]);
}
ESC_INPUT;
}
@ -250,19 +221,13 @@ uint8_t BL_ConnectEx(void)
static uint8_t BL_GetACK(uint32_t Timeout)
{
uint8_t LastACK;
do {
LastACK = suart_getc_();
uint8_t LastACK = brNONE;
while (!(suart_getc_(&LastACK)) && (Timeout)) {
Timeout--;
} while ((suart_timeout) && (Timeout));
if(suart_timeout) {
LastACK = brNONE;
}
} ;
return (LastACK);
}
uint8_t BL_SendCMDKeepAlive(void)
{
uint8_t sCMD[] = {CMD_KEEP_ALIVE, 0};
@ -345,19 +310,19 @@ uint8_t BL_PageErase(void)
if (BL_SendCMDSetAddress()) {
uint8_t sCMD[] = {CMD_ERASE_FLASH, 0x01};
BL_SendBuf(sCMD, 2);
return (BL_GetACK((40 / WaitStartBitTimeoutms)) == brSUCCESS);
return (BL_GetACK((40 / START_BIT_TIMEOUT_MS)) == brSUCCESS);
}
return 0;
}
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)
{
return BL_WriteA(CMD_PROG_FLASH, (40 / WaitStartBitTimeoutms));
return BL_WriteA(CMD_PROG_FLASH, (40 / START_BIT_TIMEOUT_MS));
}
#endif