mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-29 14:52:12 +01:00
OP-21/Bootloader CLI- New version to be used with the new BL FW. Linux and MacOs Devs please test it, I'm having probs using serial port in unbuffered mode under Linux VM. I'm hoping its just a VM problem.
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2212 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
cc5a4c2bb9
commit
eb01c55478
@ -15,39 +15,61 @@ int main(int argc, char *argv[])
|
||||
|
||||
port * info;
|
||||
PortSettings settings;
|
||||
settings.BaudRate=BAUD19200;
|
||||
settings.BaudRate=BAUD57600;
|
||||
settings.DataBits=DATA_8;
|
||||
settings.FlowControl=FLOW_OFF;
|
||||
settings.Parity=PAR_NONE;
|
||||
settings.StopBits=STOP_2;
|
||||
settings.Timeout_Millisec=500;
|
||||
settings.StopBits=STOP_1;
|
||||
settings.Timeout_Millisec=5000;
|
||||
|
||||
info=new port(settings,"COM2");
|
||||
info=new port(settings,"COM3");
|
||||
info->rxBuf = sspRxBuf;
|
||||
info->rxBufSize = MAX_PACKET_DATA_LEN;
|
||||
info->txBuf = sspTxBuf;
|
||||
info->txBufSize = 255;
|
||||
info->max_retry = 3;
|
||||
info->timeoutLen = 100;
|
||||
info->timeoutLen = 5000;
|
||||
//qssp b(info);
|
||||
qsspt bb(info);
|
||||
uint8_t buf[1000];
|
||||
QCoreApplication a(argc, argv);
|
||||
QByteArray arr;
|
||||
while(true)
|
||||
while(!bb.ssp_Synchronise())
|
||||
{
|
||||
qDebug()<<"trying sync";
|
||||
}
|
||||
bb.start();
|
||||
qDebug()<<"sync complete";
|
||||
buf[0]=0;
|
||||
buf[1]=1;
|
||||
buf[2]=2;
|
||||
while(true)
|
||||
{
|
||||
if(bb.sendData(buf,63))
|
||||
qDebug()<<"send OK";
|
||||
// else
|
||||
// qDebug()<<"send NOK";
|
||||
// //bb.ssp_SendData(buf,63);
|
||||
}
|
||||
while(true)
|
||||
{
|
||||
|
||||
|
||||
|
||||
if(bb.packets_Available()>0)
|
||||
{
|
||||
arr=bb.read_Packet();
|
||||
qDebug()<<"receive="<<(int)arr[0]<<(int)arr[1]<<(int)arr[2];
|
||||
}
|
||||
|
||||
// b.ssp_ReceiveProcess();
|
||||
// b.ssp_SendProcess();
|
||||
// if(b.isDataAvailable())
|
||||
// {
|
||||
// QByteArray bb=b.readData();
|
||||
// qDebug()<<bb[0]<<bb[1]<<bb[2];
|
||||
// }
|
||||
bb.read_Packet(buf);
|
||||
qDebug()<<"receive="<<(int)buf[0]<<(int)buf[1]<<(int)buf[2];
|
||||
++buf[0];
|
||||
++buf[1];
|
||||
++buf[2];
|
||||
//bb.ssp_SendData(buf,63);
|
||||
bb.sendData(buf,63);
|
||||
}
|
||||
//bb.ssp_ReceiveProcess();
|
||||
//bb.ssp_SendProcess();
|
||||
|
||||
|
||||
}
|
||||
return a.exec();
|
||||
}
|
||||
|
@ -1,29 +1,38 @@
|
||||
#include "port.h"
|
||||
|
||||
port::port(PortSettings settings,QString name)
|
||||
#include "..\delay.h"
|
||||
port::port(PortSettings settings,QString name):mstatus(port::closed)
|
||||
{
|
||||
timer.start();
|
||||
sport = new QextSerialPort(name,settings, QextSerialPort::Polling);
|
||||
sport->open(QIODevice::ReadWrite | QIODevice::Unbuffered);
|
||||
if(sport->open(QIODevice::ReadWrite|QIODevice::Unbuffered))
|
||||
{
|
||||
mstatus=port::open;
|
||||
// sport->setDtr();
|
||||
}
|
||||
else
|
||||
mstatus=port::error;
|
||||
}
|
||||
port::portstatus port::status()
|
||||
{
|
||||
return mstatus;
|
||||
}
|
||||
|
||||
int16_t port::pfSerialRead(void)
|
||||
{
|
||||
|
||||
char c[1];
|
||||
if(sport->bytesAvailable()>0)
|
||||
if(sport->bytesAvailable())
|
||||
{
|
||||
sport->read(c,1);
|
||||
}
|
||||
else return -1;
|
||||
//qDebug()<<"read="<<c[0];
|
||||
return c[0];
|
||||
return (uint8_t)c[0];
|
||||
}
|
||||
|
||||
void port::pfSerialWrite(uint8_t c)
|
||||
{
|
||||
char cc[1];
|
||||
cc[0]=c;
|
||||
while (sport->write(cc,1)==-1){};
|
||||
sport->write(cc,1);
|
||||
}
|
||||
|
||||
uint32_t port::pfGetTime(void)
|
||||
|
@ -9,7 +9,7 @@
|
||||
class port
|
||||
{
|
||||
public:
|
||||
|
||||
enum portstatus{open,closed,error};
|
||||
virtual int16_t pfSerialRead(void); // function to read a character from the serial input stream
|
||||
virtual void pfSerialWrite( uint8_t ); // function to write a byte to be sent out the serial port
|
||||
virtual uint32_t pfGetTime(void);
|
||||
@ -38,7 +38,9 @@ public:
|
||||
uint32_t TxError;
|
||||
uint16_t flags;
|
||||
port(PortSettings settings,QString name);
|
||||
portstatus status();
|
||||
private:
|
||||
portstatus mstatus;
|
||||
QTime timer;
|
||||
QextSerialPort *sport;
|
||||
};
|
||||
|
@ -11,7 +11,7 @@
|
||||
#define ESC 224 // ESC character used in Serial Protocol
|
||||
#define ESC_SYNC 1 // ESC_SYNC character used in Serial Protocol
|
||||
#define ACK_BIT 0x80 // Ack bit, bit 7 of sequence number, 1 = Acknowledge, 0 =
|
||||
// new packet
|
||||
// new packet
|
||||
// packet location definitions.
|
||||
#define LENGTH 0
|
||||
#define SEQNUM 1
|
||||
@ -84,7 +84,7 @@ static const uint16_t CRC_TABLE[] = {
|
||||
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
|
||||
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
|
||||
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
|
||||
};
|
||||
};
|
||||
|
||||
/** EXTERNAL DATA **/
|
||||
|
||||
@ -110,16 +110,22 @@ void qssp::ssp_Init(const PortConfig_t* const info)
|
||||
|
||||
|
||||
thisport->maxRetryCount = info->max_retry;
|
||||
thisport->timeoutLen = info->timeoutLen;
|
||||
thisport->txBufSize = info->txBufSize;
|
||||
thisport->rxBufSize = info->rxBufSize;
|
||||
thisport->txBuf = info->txBuf;
|
||||
thisport->rxBuf = info->rxBuf;
|
||||
thisport->retryCount = 0;
|
||||
thisport->sendSynch = FALSE; //TRUE;
|
||||
thisport->rxSeqNo = 255;
|
||||
thisport->txSeqNo = 255;
|
||||
thisport->SendState = SSP_IDLE;
|
||||
thisport->timeoutLen = info->timeoutLen;
|
||||
thisport->txBufSize = info->txBufSize;
|
||||
thisport->rxBufSize = info->rxBufSize;
|
||||
thisport->txBuf = info->txBuf;
|
||||
thisport->rxBuf = info->rxBuf;
|
||||
thisport->retryCount = 0;
|
||||
thisport->sendSynch = FALSE; //TRUE;
|
||||
thisport->rxSeqNo = 255;
|
||||
thisport->txSeqNo = 255;
|
||||
thisport->SendState = SSP_IDLE;
|
||||
thisport->InputState =(ReceiveState)0;
|
||||
thisport->DecodeState =(decodeState_)0;
|
||||
thisport->TxError =0;
|
||||
thisport->RxError =0;
|
||||
thisport->txSeqNo =0;
|
||||
thisport->rxSeqNo =0;
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -135,33 +141,35 @@ void qssp::ssp_Init(const PortConfig_t* const info)
|
||||
*/
|
||||
int16_t qssp::ssp_SendProcess( )
|
||||
{
|
||||
int16_t value = SSP_TX_WAITING;
|
||||
int16_t value = SSP_TX_WAITING;
|
||||
|
||||
if (thisport->SendState == SSP_AWAITING_ACK ) {
|
||||
if (sf_CheckTimeout() == TRUE) {
|
||||
if (thisport->retryCount < thisport->maxRetryCount) {
|
||||
// Try again
|
||||
sf_SendPacket();
|
||||
sf_SetSendTimeout();
|
||||
value = SSP_TX_WAITING;
|
||||
} else {
|
||||
// Give up, # of trys has exceded the limit
|
||||
value = SSP_TX_TIMEOUT;
|
||||
CLEARBIT( thisport->flags, ACK_RECEIVED);
|
||||
if (thisport->SendState == SSP_AWAITING_ACK ) {
|
||||
if (sf_CheckTimeout() == TRUE) {
|
||||
if (thisport->retryCount < thisport->maxRetryCount) {
|
||||
// Try again
|
||||
sf_SendPacket();
|
||||
sf_SetSendTimeout();
|
||||
value = SSP_TX_WAITING;
|
||||
} else {
|
||||
// Give up, # of trys has exceded the limit
|
||||
value = SSP_TX_TIMEOUT;
|
||||
CLEARBIT( thisport->flags, ACK_RECEIVED);
|
||||
thisport->SendState = SSP_IDLE;
|
||||
}
|
||||
} else {
|
||||
value = SSP_TX_WAITING;
|
||||
}
|
||||
if (debug)
|
||||
qDebug()<<"Send TimeOut!";
|
||||
}
|
||||
} else {
|
||||
value = SSP_TX_WAITING;
|
||||
}
|
||||
} else if( thisport->SendState == SSP_ACKED ) {
|
||||
SETBIT( thisport->flags, ACK_RECEIVED);
|
||||
value = SSP_TX_ACKED;
|
||||
thisport->SendState = SSP_IDLE;
|
||||
} else {
|
||||
thisport->SendState = SSP_IDLE;
|
||||
} else {
|
||||
thisport->SendState = SSP_IDLE;
|
||||
value = SSP_TX_IDLE;
|
||||
}
|
||||
return value;
|
||||
return value;
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -174,17 +182,18 @@ int16_t qssp::ssp_SendProcess( )
|
||||
*/
|
||||
int16_t qssp::ssp_ReceiveProcess()
|
||||
{
|
||||
int16_t b;
|
||||
int16_t packet_status = SSP_RX_IDLE;
|
||||
int16_t b;
|
||||
int16_t packet_status = SSP_RX_IDLE;
|
||||
|
||||
do {
|
||||
b = thisport->pfSerialRead(); // attempt to read a char from the serial buffer
|
||||
if (b != -1) {
|
||||
packet_status = sf_ReceiveState(b); // process the newly received byte in the receive state machine
|
||||
}
|
||||
// keep going until either we received a full packet or there are no more bytes to process
|
||||
} while (packet_status != SSP_RX_COMPLETE && b != -1);
|
||||
return packet_status;
|
||||
do {
|
||||
b = thisport->pfSerialRead(); // attempt to read a char from the serial buffer
|
||||
if (b != -1) {
|
||||
packet_status = sf_ReceiveState(b); // process the newly received byte in the receive state machine
|
||||
}
|
||||
// keep going until either we received a full packet or there are no more bytes to process
|
||||
} while (packet_status != SSP_RX_COMPLETE && b != -1);
|
||||
|
||||
return packet_status;
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -225,8 +234,8 @@ uint16_t qssp::ssp_SendDataBlock(uint8_t *data, uint16_t length )
|
||||
uint16_t retval = FALSE;
|
||||
|
||||
packet_status = ssp_SendData(data, length ); // send the data
|
||||
while( packet_status == SSP_TX_WAITING ) { // check the status
|
||||
(void)ssp_ReceiveProcess(); // process any bytes received.
|
||||
while( packet_status == SSP_TX_WAITING ) { // check the status
|
||||
(void)ssp_ReceiveProcess(); // process any bytes received.
|
||||
packet_status = ssp_SendProcess(); // check the send status
|
||||
}
|
||||
if( packet_status == SSP_TX_ACKED ) { // figure out what happened to the packet
|
||||
@ -278,7 +287,7 @@ int16_t qssp::ssp_SendData(const uint8_t *data, const uint16_t length )
|
||||
thisport->txSeqNo++; // update the sequence number.
|
||||
if( thisport->txSeqNo > 0x7F) { // check for sequence number rollover
|
||||
thisport->txSeqNo = 1; // if we do have rollover then reset to 1 not zero,
|
||||
// zero is reserviced for synchronization requests
|
||||
// zero is reserviced for synchronization requests
|
||||
}
|
||||
}
|
||||
|
||||
@ -287,19 +296,23 @@ int16_t qssp::ssp_SendData(const uint8_t *data, const uint16_t length )
|
||||
thisport->txSeqNo++; // update the sequence number.
|
||||
if( thisport->txSeqNo > 0x7F) { // check for sequence number rollover
|
||||
thisport->txSeqNo = 1; // if we do have rollover then reset to 1 not zero,
|
||||
// zero is reserved for synchronization requests
|
||||
// zero is reserved for synchronization requests
|
||||
}
|
||||
#endif
|
||||
CLEARBIT( thisport->flags, ACK_RECEIVED);
|
||||
thisport->SendState = SSP_AWAITING_ACK;
|
||||
value = SSP_TX_WAITING;
|
||||
thisport->retryCount = 0; // zero out the retry counter for this transmission
|
||||
thisport->retryCount = 0; // zero out the retry counter for this transmission
|
||||
sf_MakePacket( thisport->txBuf, data, length, thisport->txSeqNo );
|
||||
sf_SendPacket( ); // punch out the packet to the serial port
|
||||
sf_SendPacket( ); // punch out the packet to the serial port
|
||||
sf_SetSendTimeout( ); // do the timeout values
|
||||
if (debug)
|
||||
qDebug()<<"Sent DATA PACKET:"<<thisport->txSeqNo;
|
||||
} else {
|
||||
// error we are already sending a packet. Need to wait for the current packet to be acked or timeout.
|
||||
value = SSP_TX_BUSY;
|
||||
if (debug)
|
||||
qDebug()<<"Error sending TX was busy";
|
||||
}
|
||||
return value;
|
||||
}
|
||||
@ -335,8 +348,8 @@ uint16_t qssp::ssp_Synchronise( )
|
||||
#else
|
||||
packet_status = ssp_SendData( NULL, 0 );
|
||||
#endif
|
||||
while( packet_status == SSP_TX_WAITING ) { // we loop until we time out.
|
||||
(void)ssp_ReceiveProcess( ); // do the receive process
|
||||
while( packet_status == SSP_TX_WAITING ) { // we loop until we time out.
|
||||
(void)ssp_ReceiveProcess( ); // do the receive process
|
||||
packet_status = ssp_SendProcess( ); // do the send process
|
||||
}
|
||||
thisport->sendSynch = FALSE;
|
||||
@ -366,14 +379,14 @@ uint16_t qssp::ssp_Synchronise( )
|
||||
* Packet should be formed through the use of sf_MakePacket before calling this function.
|
||||
*/
|
||||
void qssp::sf_SendPacket()
|
||||
{
|
||||
// add 3 to packet data length for: 1 length + 2 CRC (packet overhead)
|
||||
uint8_t packetLen = thisport->txBuf[LENGTH] + 3;
|
||||
{
|
||||
// add 3 to packet data length for: 1 length + 2 CRC (packet overhead)
|
||||
uint8_t packetLen = thisport->txBuf[LENGTH] + 3;
|
||||
|
||||
// use the raw serial write function so the SYNC byte does not get 'escaped'
|
||||
thisport->pfSerialWrite(SYNC);
|
||||
thisport->pfSerialWrite(SYNC);
|
||||
for( uint8_t x = 0; x < packetLen; x++ ) {
|
||||
sf_write_byte(thisport->txBuf[x] );
|
||||
sf_write_byte(thisport->txBuf[x] );
|
||||
}
|
||||
thisport->retryCount++;
|
||||
}
|
||||
@ -434,6 +447,8 @@ void qssp::sf_SendAckPacket(uint8_t seqNumber)
|
||||
// create the packet, note we pass AckSequenceNumber directly
|
||||
sf_MakePacket( thisport->txBuf, NULL, 0, AckSeqNumber );
|
||||
sf_SendPacket( );
|
||||
if (debug)
|
||||
qDebug()<<"Sent ACK PACKET:"<<seqNumber;
|
||||
// we don't set the timeout for an ACK because we don't ACK our ACKs in this protocol
|
||||
}
|
||||
|
||||
@ -448,15 +463,15 @@ void qssp::sf_SendAckPacket(uint8_t seqNumber)
|
||||
*/
|
||||
void qssp::sf_write_byte(uint8_t c )
|
||||
{
|
||||
if( c == SYNC ) { // check for SYNC byte
|
||||
thisport->pfSerialWrite( ESC ); // since we are not starting a packet we must ESCAPE the SYNCH byte
|
||||
thisport->pfSerialWrite( ESC_SYNC ); // now send the escaped synch char
|
||||
} else if( c == ESC ) { // Check for ESC character
|
||||
thisport->pfSerialWrite( ESC ); // if it is, we need to send it twice
|
||||
thisport->pfSerialWrite( ESC );
|
||||
} else {
|
||||
thisport->pfSerialWrite( c ); // otherwise write the byte to serial port
|
||||
}
|
||||
if( c == SYNC ) { // check for SYNC byte
|
||||
thisport->pfSerialWrite( ESC ); // since we are not starting a packet we must ESCAPE the SYNCH byte
|
||||
thisport->pfSerialWrite( ESC_SYNC ); // now send the escaped synch char
|
||||
} else if( c == ESC ) { // Check for ESC character
|
||||
thisport->pfSerialWrite( ESC ); // if it is, we need to send it twice
|
||||
thisport->pfSerialWrite( ESC );
|
||||
} else {
|
||||
thisport->pfSerialWrite( c ); // otherwise write the byte to serial port
|
||||
}
|
||||
}
|
||||
|
||||
/************************************************************************************************************
|
||||
@ -482,7 +497,7 @@ void qssp::sf_write_byte(uint8_t c )
|
||||
|
||||
uint16_t qssp::sf_crc16( uint16_t crc, uint8_t data )
|
||||
{
|
||||
return (crc >> 8) ^ CRC_TABLE[( crc ^ data ) & 0x00FF ];
|
||||
return (crc >> 8) ^ CRC_TABLE[( crc ^ data ) & 0x00FF ];
|
||||
}
|
||||
|
||||
|
||||
@ -499,7 +514,7 @@ void qssp::sf_SetSendTimeout()
|
||||
{
|
||||
uint32_t timeout;
|
||||
timeout = thisport->pfGetTime() + thisport->timeoutLen;
|
||||
thisport->timeout = timeout;
|
||||
thisport->timeout = timeout;
|
||||
}
|
||||
|
||||
/*!
|
||||
@ -520,6 +535,9 @@ uint16_t qssp::sf_CheckTimeout()
|
||||
if( current_time > thisport->timeout ) {
|
||||
retval = TRUE;
|
||||
}
|
||||
if(retval)
|
||||
if (debug)
|
||||
qDebug()<<"timeout"<<current_time<<thisport->timeout;
|
||||
return retval;
|
||||
}
|
||||
|
||||
@ -545,32 +563,32 @@ uint16_t qssp::sf_CheckTimeout()
|
||||
*/
|
||||
int16_t qssp::sf_ReceiveState(uint8_t c )
|
||||
{
|
||||
int16_t retval = SSP_RX_RECEIVING;
|
||||
int16_t retval = SSP_RX_RECEIVING;
|
||||
|
||||
switch( thisport->InputState ) {
|
||||
case state_unescaped_e:
|
||||
if( c == SYNC ) {
|
||||
thisport->DecodeState = decode_len1_e;
|
||||
} else if ( c == ESC ) {
|
||||
thisport->InputState = state_escaped_e;
|
||||
} else {
|
||||
retval = sf_DecodeState(c);
|
||||
}
|
||||
break; // end of unescaped state.
|
||||
case state_escaped_e:
|
||||
thisport->InputState = state_unescaped_e;
|
||||
if( c == SYNC ) {
|
||||
thisport->DecodeState = decode_len1_e;
|
||||
} else if (c == ESC_SYNC ) {
|
||||
retval = sf_DecodeState(SYNC);
|
||||
} else {
|
||||
retval = sf_DecodeState(c);
|
||||
}
|
||||
break; // end of the escaped state.
|
||||
default:
|
||||
break;
|
||||
switch( thisport->InputState ) {
|
||||
case state_unescaped_e:
|
||||
if( c == SYNC ) {
|
||||
thisport->DecodeState = decode_len1_e;
|
||||
} else if ( c == ESC ) {
|
||||
thisport->InputState = state_escaped_e;
|
||||
} else {
|
||||
retval = sf_DecodeState(c);
|
||||
}
|
||||
return retval;
|
||||
break; // end of unescaped state.
|
||||
case state_escaped_e:
|
||||
thisport->InputState = state_unescaped_e;
|
||||
if( c == SYNC ) {
|
||||
thisport->DecodeState = decode_len1_e;
|
||||
} else if (c == ESC_SYNC ) {
|
||||
retval = sf_DecodeState(SYNC);
|
||||
} else {
|
||||
retval = sf_DecodeState(c);
|
||||
}
|
||||
break; // end of the escaped state.
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -595,69 +613,69 @@ int16_t qssp::sf_ReceiveState(uint8_t c )
|
||||
*/
|
||||
int16_t qssp::sf_DecodeState( uint8_t c )
|
||||
{
|
||||
int16_t retval;
|
||||
switch( thisport->DecodeState ) {
|
||||
case decode_idle_e:
|
||||
int16_t retval;
|
||||
switch( thisport->DecodeState ) {
|
||||
case decode_idle_e:
|
||||
// 'c' is ignored in this state as the only way to leave the idle state is
|
||||
// recognition of the SYNC byte in the sf_ReceiveState function.
|
||||
retval = SSP_RX_IDLE;
|
||||
break;
|
||||
case decode_len1_e:
|
||||
thisport->rxBuf[LENGTH]= c;
|
||||
thisport->rxBufLen = c;
|
||||
break;
|
||||
case decode_len1_e:
|
||||
thisport->rxBuf[LENGTH]= c;
|
||||
thisport->rxBufLen = c;
|
||||
if( thisport->rxBufLen <= thisport->rxBufSize ) {
|
||||
thisport->DecodeState = decode_seqNo_e;
|
||||
retval = SSP_RX_RECEIVING;
|
||||
} else {
|
||||
thisport->DecodeState = decode_idle_e;
|
||||
retval = SSP_RX_IDLE;
|
||||
}
|
||||
break;
|
||||
case decode_seqNo_e:
|
||||
thisport->rxBuf[SEQNUM] = c;
|
||||
thisport->crc = 0xffff;
|
||||
thisport->rxBufLen--; // subtract 1 for the seq. no.
|
||||
thisport->rxBufPos = 2;
|
||||
|
||||
thisport->crc = sf_crc16( thisport->crc, c );
|
||||
if( thisport->rxBufLen > 0 ) {
|
||||
thisport->DecodeState = decode_data_e;
|
||||
} else {
|
||||
thisport->DecodeState = decode_crc1_e;
|
||||
}
|
||||
retval = SSP_RX_RECEIVING;
|
||||
break;
|
||||
case decode_data_e:
|
||||
thisport->rxBuf[ (thisport->rxBufPos)++] = c;
|
||||
thisport->crc = sf_crc16( thisport->crc, c );
|
||||
if( thisport->rxBufPos == (thisport->rxBufLen+2) ) {
|
||||
thisport->DecodeState = decode_crc1_e;
|
||||
}
|
||||
retval = SSP_RX_RECEIVING;
|
||||
break;
|
||||
case decode_crc1_e:
|
||||
thisport->crc = sf_crc16( thisport->crc, c );
|
||||
thisport->DecodeState = decode_crc2_e;
|
||||
retval = SSP_RX_RECEIVING;
|
||||
break;
|
||||
case decode_crc2_e:
|
||||
thisport->DecodeState = decode_idle_e;
|
||||
// verify the CRC value for the packet
|
||||
if( sf_crc16( thisport->crc, c) == 0) {
|
||||
// TODO shouldn't the return value of sf_ReceivePacket() be checked?
|
||||
sf_ReceivePacket();
|
||||
retval = SSP_RX_COMPLETE;
|
||||
} else {
|
||||
thisport->RxError++;
|
||||
retval = SSP_RX_IDLE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
thisport->DecodeState = decode_idle_e; // unknown state so reset to idle state and wait for the next start of a packet.
|
||||
retval = SSP_RX_IDLE;
|
||||
break;
|
||||
thisport->DecodeState = decode_seqNo_e;
|
||||
retval = SSP_RX_RECEIVING;
|
||||
} else {
|
||||
thisport->DecodeState = decode_idle_e;
|
||||
retval = SSP_RX_IDLE;
|
||||
}
|
||||
return retval;
|
||||
break;
|
||||
case decode_seqNo_e:
|
||||
thisport->rxBuf[SEQNUM] = c;
|
||||
thisport->crc = 0xffff;
|
||||
thisport->rxBufLen--; // subtract 1 for the seq. no.
|
||||
thisport->rxBufPos = 2;
|
||||
|
||||
thisport->crc = sf_crc16( thisport->crc, c );
|
||||
if( thisport->rxBufLen > 0 ) {
|
||||
thisport->DecodeState = decode_data_e;
|
||||
} else {
|
||||
thisport->DecodeState = decode_crc1_e;
|
||||
}
|
||||
retval = SSP_RX_RECEIVING;
|
||||
break;
|
||||
case decode_data_e:
|
||||
thisport->rxBuf[ (thisport->rxBufPos)++] = c;
|
||||
thisport->crc = sf_crc16( thisport->crc, c );
|
||||
if( thisport->rxBufPos == (thisport->rxBufLen+2) ) {
|
||||
thisport->DecodeState = decode_crc1_e;
|
||||
}
|
||||
retval = SSP_RX_RECEIVING;
|
||||
break;
|
||||
case decode_crc1_e:
|
||||
thisport->crc = sf_crc16( thisport->crc, c );
|
||||
thisport->DecodeState = decode_crc2_e;
|
||||
retval = SSP_RX_RECEIVING;
|
||||
break;
|
||||
case decode_crc2_e:
|
||||
thisport->DecodeState = decode_idle_e;
|
||||
// verify the CRC value for the packet
|
||||
if( sf_crc16( thisport->crc, c) == 0) {
|
||||
// TODO shouldn't the return value of sf_ReceivePacket() be checked?
|
||||
sf_ReceivePacket();
|
||||
retval = SSP_RX_COMPLETE;
|
||||
} else {
|
||||
thisport->RxError++;
|
||||
retval = SSP_RX_IDLE;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
thisport->DecodeState = decode_idle_e; // unknown state so reset to idle state and wait for the next start of a packet.
|
||||
retval = SSP_RX_IDLE;
|
||||
break;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
/************************************************************************************************************
|
||||
@ -686,58 +704,78 @@ int16_t qssp::sf_DecodeState( uint8_t c )
|
||||
|
||||
int16_t qssp::sf_ReceivePacket()
|
||||
{
|
||||
int16_t value = FALSE;
|
||||
int16_t value = FALSE;
|
||||
|
||||
if( ISBITSET(thisport->rxBuf[SEQNUM], ACK_BIT ) ) {
|
||||
// Received an ACK packet, need to check if it matches the previous sent packet
|
||||
if( ( thisport->rxBuf[SEQNUM] & 0x7F) == (thisport->txSeqNo & 0x7f)) {
|
||||
// It matches the last packet sent by us
|
||||
SETBIT( thisport->txSeqNo, ACK_BIT );
|
||||
thisport->SendState = SSP_ACKED;
|
||||
value = FALSE;
|
||||
}
|
||||
if( ISBITSET(thisport->rxBuf[SEQNUM], ACK_BIT ) ) {
|
||||
// Received an ACK packet, need to check if it matches the previous sent packet
|
||||
if( ( thisport->rxBuf[SEQNUM] & 0x7F) == (thisport->txSeqNo & 0x7f)) {
|
||||
// It matches the last packet sent by us
|
||||
SETBIT( thisport->txSeqNo, ACK_BIT );
|
||||
thisport->SendState = SSP_ACKED;
|
||||
value = FALSE;
|
||||
if (debug)
|
||||
qDebug()<<"Received ACK:"<<(thisport->txSeqNo & 0x7F);
|
||||
}
|
||||
// else ignore the ACK packet
|
||||
} else {
|
||||
// Received a 'data' packet, figure out what type of packet we received...
|
||||
if( thisport->rxBuf[SEQNUM] == 0 ) {
|
||||
// Synchronize sequence number with host
|
||||
} else {
|
||||
// Received a 'data' packet, figure out what type of packet we received...
|
||||
if( thisport->rxBuf[SEQNUM] == 0 ) {
|
||||
if (debug)
|
||||
qDebug()<<"Received SYNC Request";
|
||||
// Synchronize sequence number with host
|
||||
#ifdef ACTIVE_SYNCH
|
||||
thisport->sendSynch = TRUE;
|
||||
#endif
|
||||
sf_SendAckPacket(thisport->rxBuf[SEQNUM] );
|
||||
thisport->rxSeqNo = 0;
|
||||
value = FALSE;
|
||||
} else if( thisport->rxBuf[SEQNUM] == thisport->rxSeqNo ) {
|
||||
// Already seen this packet, just ack it, don't act on the packet.
|
||||
sf_SendAckPacket(thisport->rxBuf[SEQNUM] );
|
||||
value = FALSE;
|
||||
} else {
|
||||
//New Packet
|
||||
thisport->rxSeqNo = thisport->rxBuf[SEQNUM];
|
||||
// Let the application do something with the data/packet.
|
||||
sf_SendAckPacket(thisport->rxBuf[SEQNUM] );
|
||||
thisport->rxSeqNo = 0;
|
||||
value = FALSE;
|
||||
} else if( thisport->rxBuf[SEQNUM] == thisport->rxSeqNo ) {
|
||||
// Already seen this packet, just ack it, don't act on the packet.
|
||||
sf_SendAckPacket(thisport->rxBuf[SEQNUM] );
|
||||
value = FALSE;
|
||||
} else {
|
||||
//New Packet
|
||||
thisport->rxSeqNo = thisport->rxBuf[SEQNUM];
|
||||
// Let the application do something with the data/packet.
|
||||
|
||||
// skip the first two bytes (length and seq. no.) in the buffer.
|
||||
pfCallBack( &(thisport->rxBuf[2]), thisport->rxBufLen);
|
||||
// skip the first two bytes (length and seq. no.) in the buffer.
|
||||
if (debug)
|
||||
qDebug()<<"Received DATA PACKET seq="<<thisport->rxSeqNo<<"Data="<<(uint8_t)thisport->rxBuf[2]<<(uint8_t)thisport->rxBuf[3]<<(uint8_t)thisport->rxBuf[4];
|
||||
pfCallBack( &(thisport->rxBuf[2]), thisport->rxBufLen);
|
||||
|
||||
// after we send the ACK, it is possible for the host to send a new packet.
|
||||
// Thus the application needs to copy the data and reset the receive buffer
|
||||
// inside of thisport->pfCallBack()
|
||||
sf_SendAckPacket(thisport->rxBuf[SEQNUM] );
|
||||
value = TRUE;
|
||||
}
|
||||
// after we send the ACK, it is possible for the host to send a new packet.
|
||||
// Thus the application needs to copy the data and reset the receive buffer
|
||||
// inside of thisport->pfCallBack()
|
||||
sf_SendAckPacket(thisport->rxBuf[SEQNUM] );
|
||||
value = TRUE;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
qssp::qssp(port * info)
|
||||
qssp::qssp(port * info,bool debug):debug(debug)
|
||||
{
|
||||
thisport=info;
|
||||
thisport->retryCount = 0;
|
||||
thisport->sendSynch = FALSE; //TRUE;
|
||||
thisport->rxSeqNo = 255;
|
||||
thisport->txSeqNo = 255;
|
||||
thisport->SendState = SSP_IDLE;
|
||||
|
||||
thisport=info;
|
||||
thisport->maxRetryCount = info->max_retry;
|
||||
thisport->timeoutLen = info->timeoutLen;
|
||||
thisport->txBufSize = info->txBufSize;
|
||||
thisport->rxBufSize = info->rxBufSize;
|
||||
thisport->txBuf = info->txBuf;
|
||||
thisport->rxBuf = info->rxBuf;
|
||||
thisport->retryCount = 0;
|
||||
thisport->sendSynch = FALSE; //TRUE;
|
||||
thisport->rxSeqNo = 255;
|
||||
thisport->txSeqNo = 255;
|
||||
thisport->SendState = SSP_IDLE;
|
||||
thisport->InputState =(ReceiveState)0;
|
||||
thisport->DecodeState =(decodeState_)0;
|
||||
thisport->TxError =0;
|
||||
thisport->RxError =0;
|
||||
thisport->txSeqNo =0;
|
||||
thisport->rxSeqNo =0;
|
||||
}
|
||||
void qssp::pfCallBack( uint8_t * buf, uint16_t size)
|
||||
{
|
||||
qDebug()<<"receive callback"<<buf[0]<<buf[1]<<buf[2]<<buf[3]<<buf[4];
|
||||
if (debug)
|
||||
qDebug()<<"receive callback"<<buf[0]<<buf[1]<<buf[2]<<buf[3]<<buf[4];
|
||||
}
|
||||
|
@ -79,6 +79,7 @@ private:
|
||||
void sf_MakePacket( uint8_t *buf, const uint8_t * pdata, uint16_t length, uint8_t seqNo );
|
||||
int16_t sf_ReceivePacket();
|
||||
uint16_t ssp_SendDataBlock(uint8_t *data, uint16_t length );
|
||||
bool debug;
|
||||
public:
|
||||
/** PUBLIC FUNCTIONS **/
|
||||
virtual void pfCallBack( uint8_t *, uint16_t); // call back function that is called when a full packet has been received
|
||||
@ -89,7 +90,7 @@ public:
|
||||
void ssp_Init( const PortConfig_t* const info);
|
||||
int16_t ssp_ReceiveByte( );
|
||||
uint16_t ssp_Synchronise( );
|
||||
qssp(port * info);
|
||||
qssp(port * info,bool debug);
|
||||
};
|
||||
|
||||
#endif // QSSP_H
|
||||
|
@ -1,40 +1,72 @@
|
||||
#include "qsspt.h"
|
||||
|
||||
qsspt::qsspt(port * info):qssp(info),endthread(false)
|
||||
qsspt::qsspt(port * info,bool debug):qssp(info,debug),endthread(false),datapending(false),debug(debug)
|
||||
{
|
||||
this->start();
|
||||
}
|
||||
|
||||
void qsspt::run()
|
||||
{
|
||||
while(!endthread)
|
||||
{
|
||||
this->ssp_ReceiveProcess();
|
||||
this->ssp_SendProcess();
|
||||
receivestatus=this->ssp_ReceiveProcess();
|
||||
sendstatus=this->ssp_SendProcess();
|
||||
sendbufmutex.lock();
|
||||
if(datapending && receivestatus==SSP_TX_IDLE)
|
||||
{
|
||||
this->ssp_SendData(mbuf,msize);
|
||||
datapending=false;
|
||||
}
|
||||
sendbufmutex.unlock();
|
||||
if(sendstatus==SSP_TX_ACKED)
|
||||
sendwait.wakeAll();
|
||||
}
|
||||
|
||||
}
|
||||
bool qsspt::sendData(uint8_t * buf,uint16_t size)
|
||||
{
|
||||
if(datapending)
|
||||
return false;
|
||||
sendbufmutex.lock();
|
||||
datapending=true;
|
||||
mbuf=buf;
|
||||
msize=size;
|
||||
sendbufmutex.unlock();
|
||||
msendwait.lock();
|
||||
sendwait.wait(&msendwait,100000);
|
||||
msendwait.unlock();
|
||||
return true;
|
||||
}
|
||||
|
||||
void qsspt::pfCallBack( uint8_t * buf, uint16_t size)
|
||||
{
|
||||
//qDebug()<<"receive callback"<<buf[0]<<buf[1]<<buf[2]<<buf[3]<<buf[4]<<"array size="<<queue.count();
|
||||
if (debug)
|
||||
qDebug()<<"receive callback"<<buf[0]<<buf[1]<<buf[2]<<buf[3]<<buf[4]<<"array size="<<queue.count();
|
||||
QByteArray array;
|
||||
for(int x=0;x<size;x++)
|
||||
{
|
||||
array.append((char)buf[x]);
|
||||
array.append(buf[x]);
|
||||
}
|
||||
mutex.lock();
|
||||
queue.enqueue(array);
|
||||
//queue.enqueue((const char *)&buf);
|
||||
// queue.enqueue((const char *)&buf);
|
||||
mutex.unlock();
|
||||
}
|
||||
int qsspt::packets_Available()
|
||||
{
|
||||
return queue.count();
|
||||
return queue.count();
|
||||
}
|
||||
QByteArray qsspt::read_Packet()
|
||||
int qsspt::read_Packet(void * data)
|
||||
{
|
||||
mutex.lock();
|
||||
if(queue.size()==0)
|
||||
{
|
||||
mutex.unlock();
|
||||
return -1;
|
||||
}
|
||||
QByteArray arr=queue.dequeue();
|
||||
memcpy(data,(uint8_t*)arr.data(),arr.length());
|
||||
mutex.unlock();
|
||||
return arr;
|
||||
return arr.length();
|
||||
}
|
||||
qsspt::~qsspt()
|
||||
{
|
||||
|
@ -4,20 +4,30 @@
|
||||
#include "qssp.h"
|
||||
#include <QThread>
|
||||
#include <QQueue>
|
||||
|
||||
class qsspt:private qssp, public QThread
|
||||
#include <QWaitCondition>
|
||||
class qsspt:public qssp, public QThread
|
||||
{
|
||||
public:
|
||||
qsspt(port * info);
|
||||
qsspt(port * info,bool debug);
|
||||
void run();
|
||||
int packets_Available();
|
||||
QByteArray read_Packet();
|
||||
int read_Packet(void *);
|
||||
~qsspt();
|
||||
bool sendData(uint8_t * buf,uint16_t size);
|
||||
private:
|
||||
virtual void pfCallBack( uint8_t *, uint16_t);
|
||||
uint8_t * mbuf;
|
||||
uint16_t msize;
|
||||
QQueue<QByteArray> queue;
|
||||
QMutex mutex;
|
||||
QMutex sendbufmutex;
|
||||
bool datapending;
|
||||
bool endthread;
|
||||
uint16_t sendstatus;
|
||||
uint16_t receivestatus;
|
||||
QWaitCondition sendwait;
|
||||
QMutex msendwait;
|
||||
bool debug;
|
||||
};
|
||||
|
||||
#endif // QSSPT_H
|
||||
|
@ -8,15 +8,26 @@
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
///SSP/////////////////////////////////
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////
|
||||
// OP_DFU dfu(false);
|
||||
// dfu.test();
|
||||
QCoreApplication a(argc, argv);
|
||||
// argc=4;
|
||||
// argv[1]="-ls";
|
||||
// argv[2]="-t";
|
||||
// argv[3]="COM3";
|
||||
if(argc>1||!PRIVATE)
|
||||
{
|
||||
bool use_serial=false;
|
||||
bool verify;
|
||||
bool debug=false;
|
||||
OP_DFU::Actions action;
|
||||
QString file;
|
||||
QString serialport;
|
||||
QString description;
|
||||
int device=-1;
|
||||
QStringList args;
|
||||
@ -44,14 +55,17 @@ int main(int argc, char *argv[])
|
||||
cout<<"| -r : resets the device |\n";
|
||||
cout<<"| -j : exits bootloader and jumps to user FW |\n";
|
||||
cout<<"| -debug : prints debug information |\n";
|
||||
cout<<"| -t <port> : uses serial port* |\n";
|
||||
cout<<"| |\n";
|
||||
cout<<"| examples: |\n";
|
||||
cout<<"| |\n";
|
||||
cout<<"| program and verify device #0 |\n";
|
||||
cout<<"| OPUploadTool -p c:/OpenPilot.bin -w \"Openpilot Firmware\" -v -d 0 |\n";
|
||||
cout<<"| OPUploadTool -p c:/OpenPilot.bin -w \"Openpilot Firmware\" -v -d 0 |\n";
|
||||
cout<<"| |\n";
|
||||
cout<<"| Perform a quick compare of FW in file with FW in device #1 |\n";
|
||||
cout<<"| OPUploadTool -ch c:/OpenPilot2.bin -d 2 |\n";
|
||||
cout<<"| |\n";
|
||||
cout<<"| *requires valid user space firmwares already running |\n";
|
||||
cout<<"|________________________________________________________________________|\n";
|
||||
return 0;
|
||||
|
||||
@ -168,6 +182,14 @@ int main(int argc, char *argv[])
|
||||
cout<<"wrong parameters";
|
||||
return -1;
|
||||
}
|
||||
if(args.contains(USE_SERIAL))
|
||||
{
|
||||
if(args.indexOf(USE_SERIAL)+1<args.length())
|
||||
{
|
||||
serialport=(args[args.indexOf(USE_SERIAL)+1]);
|
||||
use_serial=true;
|
||||
}
|
||||
}
|
||||
if(debug)
|
||||
{
|
||||
qDebug()<<"Action="<<(int)action;
|
||||
@ -175,10 +197,16 @@ int main(int argc, char *argv[])
|
||||
qDebug()<<"Device="<<device;
|
||||
qDebug()<<"Action="<<action;
|
||||
qDebug()<<"Desctription"<<description;
|
||||
qDebug()<<"Use Serial port"<<use_serial;
|
||||
if(use_serial)
|
||||
qDebug()<<"Port Name"<<serialport;
|
||||
}
|
||||
|
||||
///////////////////////////////////ACTIONS START///////////////////////////////////////////////////
|
||||
OP_DFU dfu(debug);
|
||||
OP_DFU dfu(debug,use_serial,serialport);
|
||||
if(!dfu.ready())
|
||||
return -1;
|
||||
|
||||
dfu.AbortOperation();
|
||||
if(!dfu.enterDFU(0))
|
||||
{
|
||||
@ -294,12 +322,12 @@ int main(int argc, char *argv[])
|
||||
|
||||
return 0;
|
||||
}
|
||||
OP_DFU dfu(true);
|
||||
//dfu.findDevices();
|
||||
dfu.enterDFU(1);
|
||||
dfu.UploadFirmware("c:/ahrs.bin",true,1);
|
||||
// dfu.UploadDescription("josemanuel");
|
||||
// QString str=dfu.DownloadDescription(12);
|
||||
// OP_DFU dfu(true);
|
||||
// //dfu.findDevices();
|
||||
// dfu.enterDFU(1);
|
||||
// dfu.UploadFirmware("c:/ahrs.bin",true,1);
|
||||
// // dfu.UploadDescription("josemanuel");
|
||||
// // QString str=dfu.DownloadDescription(12);
|
||||
// dfu.JumpToApp();
|
||||
// qDebug()<<"Description="<<str;
|
||||
return a.exec();
|
||||
|
@ -2,41 +2,82 @@
|
||||
#include <cmath>
|
||||
#include <qwaitcondition.h>
|
||||
#include <QMutex>
|
||||
#include <conio.h>
|
||||
|
||||
OP_DFU::OP_DFU(bool _debug): debug(_debug)
|
||||
OP_DFU::OP_DFU(bool _debug,bool _use_serial,QString portname): debug(_debug),use_serial(_use_serial),mready(true)
|
||||
{
|
||||
send_delay=10;
|
||||
use_delay=true;
|
||||
int numDevices=0;
|
||||
cout<<"Please connect device now\n";
|
||||
int count=0;
|
||||
while(numDevices==0)
|
||||
if(use_serial)
|
||||
{
|
||||
cout<<".";
|
||||
delay::msleep(500);
|
||||
numDevices = hidHandle.open(1,0x20a0,0x4117,0,0); //0xff9c,0x0001);
|
||||
if(++count==10)
|
||||
cout<<"Connect hw now and press any key";
|
||||
// getch();
|
||||
// delay::msleep(2000);
|
||||
PortSettings settings;
|
||||
settings.BaudRate=BAUD57600;
|
||||
settings.DataBits=DATA_8;
|
||||
settings.FlowControl=FLOW_OFF;
|
||||
settings.Parity=PAR_NONE;
|
||||
settings.StopBits=STOP_1;
|
||||
settings.Timeout_Millisec=1000;
|
||||
info=new port(settings,portname);
|
||||
info->rxBuf = sspRxBuf;
|
||||
info->rxBufSize = MAX_PACKET_DATA_LEN;
|
||||
info->txBuf = sspTxBuf;
|
||||
info->txBufSize = MAX_PACKET_DATA_LEN;
|
||||
info->max_retry = 10;
|
||||
info->timeoutLen = 1000;
|
||||
if(info->status()!=port::open)
|
||||
{
|
||||
cout<<"\r";
|
||||
cout<<" ";
|
||||
cout<<"\r";
|
||||
count=0;
|
||||
cout<<"Could not open serial port\n";
|
||||
mready=false;
|
||||
return;
|
||||
}
|
||||
serialhandle=new qsspt(info,debug);
|
||||
|
||||
while(serialhandle->ssp_Synchronise()==false)
|
||||
{
|
||||
if (debug)
|
||||
qDebug()<<"SYNC failed, resending";
|
||||
}
|
||||
qDebug()<<"SYNC Succeded";
|
||||
serialhandle->start();
|
||||
// serialhandle->start();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
send_delay=10;
|
||||
use_delay=true;
|
||||
int numDevices=0;
|
||||
cout<<"Please connect device now\n";
|
||||
int count=0;
|
||||
while(numDevices==0)
|
||||
{
|
||||
cout<<".";
|
||||
delay::msleep(500);
|
||||
numDevices = hidHandle.open(1,0x20a0,0x4117,0,0); //0xff9c,0x0001);
|
||||
if(++count==10)
|
||||
{
|
||||
cout<<"\r";
|
||||
cout<<" ";
|
||||
cout<<"\r";
|
||||
count=0;
|
||||
}
|
||||
}
|
||||
if(debug)
|
||||
qDebug() << numDevices << " device(s) opened";
|
||||
//sendReset();
|
||||
}
|
||||
if(debug)
|
||||
qDebug() << numDevices << " device(s) opened";
|
||||
//sendReset();
|
||||
}
|
||||
void OP_DFU::sendReset(void)
|
||||
{
|
||||
char b[64]={0x02,0x24,0x3C,0x20,0x17,0x00,0x30,0x76,0x1F,0x40,0x62,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6C,0x40,0x2E,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
|
||||
hidHandle.send(0,b,64,1000);
|
||||
sendData(b,64);
|
||||
delay::msleep(600);
|
||||
char bb[64]={0x02,0x24,0x3C,0x20,0x17,0x00,0x30,0x76,0x1F,0x40,0xB9,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x44,0xE8,0x30,0x00,0x00,0x13,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
|
||||
hidHandle.send(0,bb,64,1000);
|
||||
sendData(bb,64);
|
||||
delay::msleep(600);
|
||||
char bbb[64]={0x02,0x24,0x3C,0x20,0x17,0x00,0x30,0x76,0x1F,0x40,0x10,0x0D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF9,0x61,0x34,0x00,0x00,0x13,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
|
||||
hidHandle.send(0,bbb,64,1000);
|
||||
sendData(bbb,64);
|
||||
}
|
||||
|
||||
bool OP_DFU::SaveByteArrayToFile(QString const & sfile, const QByteArray &array)
|
||||
@ -68,7 +109,7 @@ bool OP_DFU::enterDFU(int const &devNumber)
|
||||
buf[8] = 1;//DFU Data2
|
||||
buf[9] = 1;//DFU Data3
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 500);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
if(result<1)
|
||||
return false;
|
||||
if(debug)
|
||||
@ -104,7 +145,8 @@ bool OP_DFU::StartUpload(qint32 const & numberOfBytes, TransferTypes const & typ
|
||||
buf[11] = crc;
|
||||
if(debug)
|
||||
qDebug()<<"Number of packets:"<<numberOfPackets<<" Size of last packet:"<<lastPacketCount;
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
delay::msleep(1000);
|
||||
if(debug)
|
||||
qDebug() << result << " bytes sent";
|
||||
if(result>0)
|
||||
@ -145,7 +187,7 @@ bool OP_DFU::UploadData(qint32 const & numberOfBytes, QByteArray & data)
|
||||
packetsize=lastPacketCount;
|
||||
else
|
||||
packetsize=14;
|
||||
// qDebug()<<packetcount;
|
||||
// qDebug()<<packetcount;
|
||||
buf[2] = packetcount>>24;//DFU Count
|
||||
buf[3] = packetcount>>16;//DFU Count
|
||||
buf[4] = packetcount>>8;//DFU Count
|
||||
@ -164,8 +206,8 @@ bool OP_DFU::UploadData(qint32 const & numberOfBytes, QByteArray & data)
|
||||
// qDebug()<<" Data0="<<(int)data[0]<<" Data0="<<(int)data[1]<<" Data0="<<(int)data[2]<<" Data0="<<(int)data[3]<<" buf6="<<(int)buf[6]<<" buf7="<<(int)buf[7]<<" buf8="<<(int)buf[8]<<" buf9="<<(int)buf[9];
|
||||
//delay::msleep(send_delay);
|
||||
if(int ret=StatusRequest()!=OP_DFU::uploading) return false;
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
// qDebug()<<"sent:"<<result;
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
// qDebug()<<"sent:"<<result;
|
||||
if(result<1)
|
||||
{
|
||||
return false;
|
||||
@ -180,7 +222,7 @@ bool OP_DFU::UploadData(qint32 const & numberOfBytes, QByteArray & data)
|
||||
}
|
||||
OP_DFU::Status OP_DFU::UploadDescription(QString & description)
|
||||
{
|
||||
cout<<"Starting uploading description\n";
|
||||
cout<<"Starting uploading description\n";
|
||||
if(description.length()%4!=0)
|
||||
{
|
||||
int pad=description.length()/4;
|
||||
@ -229,8 +271,8 @@ void OP_DFU::test()
|
||||
++buf[1];
|
||||
++buf[2];
|
||||
++buf[63];
|
||||
hidHandle.send(0,buf,BUF_LEN,5000);
|
||||
result = hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
sendData(buf,BUF_LEN);
|
||||
result = receiveData(buf,BUF_LEN);
|
||||
|
||||
qDebug()<<"Result="<<result;
|
||||
qDebug()<<(int)buf[0]<<":"<<(int)buf[1]<<":"<<(int)buf[2]<<":"<<(int)buf[63];
|
||||
@ -263,7 +305,7 @@ QByteArray OP_DFU::StartDownload(qint32 const & numberOfBytes, TransferTypes con
|
||||
buf[8] = 1;//DFU Data2
|
||||
buf[9] = 1;//DFU Data3
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 500);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
if(debug)
|
||||
qDebug() << "StartDownload:"<<numberOfPackets<<"packets"<<" Last Packet Size="<<lastPacketCount<<" "<<result << " bytes sent";
|
||||
float percentage;
|
||||
@ -277,7 +319,7 @@ QByteArray OP_DFU::StartDownload(qint32 const & numberOfBytes, TransferTypes con
|
||||
|
||||
|
||||
// qDebug()<<"Status="<<StatusToString(StatusRequest());
|
||||
result = hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
result = receiveData(buf,BUF_LEN);
|
||||
if(debug)
|
||||
qDebug() << result << " bytes received"<<" Count="<<x<<"-"<<(int)buf[2]<<";"<<(int)buf[3]<<";"<<(int)buf[4]<<";"<<(int)buf[5]<<" Data="<<(int)buf[6]<<";"<<(int)buf[7]<<";"<<(int)buf[8]<<";"<<(int)buf[9];
|
||||
int size;
|
||||
@ -304,7 +346,7 @@ void OP_DFU::ResetDevice(void)
|
||||
buf[7] = 0;
|
||||
buf[8] = 0;
|
||||
buf[9] = 0;
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 500);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
}
|
||||
void OP_DFU::AbortOperation(void)
|
||||
{
|
||||
@ -319,7 +361,7 @@ void OP_DFU::AbortOperation(void)
|
||||
buf[7] = 0;
|
||||
buf[8] = 0;
|
||||
buf[9] = 0;
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 500);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
}
|
||||
void OP_DFU::JumpToApp()
|
||||
{
|
||||
@ -335,7 +377,7 @@ void OP_DFU::JumpToApp()
|
||||
buf[8] = 0;
|
||||
buf[9] = 0;
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 500);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
}
|
||||
OP_DFU::Status OP_DFU::StatusRequest()
|
||||
{
|
||||
@ -351,10 +393,10 @@ OP_DFU::Status OP_DFU::StatusRequest()
|
||||
buf[8] = 0;
|
||||
buf[9] = 0;
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 10000);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
if(debug)
|
||||
qDebug() << result << " bytes sent";
|
||||
result = hidHandle.receive(0,buf,BUF_LEN,10000);
|
||||
result = receiveData(buf,BUF_LEN);
|
||||
if(debug)
|
||||
qDebug() << result << " bytes received";
|
||||
if(buf[1]==OP_DFU::Status_Rep)
|
||||
@ -380,12 +422,12 @@ bool OP_DFU::findDevices()
|
||||
buf[7] = 0;
|
||||
buf[8] = 0;
|
||||
buf[9] = 0;
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
if(result<1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
result = hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
result = receiveData(buf,BUF_LEN);
|
||||
if(result<1)
|
||||
{
|
||||
return false;
|
||||
@ -413,8 +455,8 @@ bool OP_DFU::findDevices()
|
||||
buf[7] = 0;
|
||||
buf[8] = 0;
|
||||
buf[9] = 0;
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
result = hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
result = receiveData(buf,BUF_LEN);
|
||||
devices[x].ID=buf[9];
|
||||
devices[x].BL_Version=buf[7];
|
||||
devices[x].SizeOfDesc=buf[8];
|
||||
@ -466,7 +508,7 @@ bool OP_DFU::EndOperation()
|
||||
buf[8] = 0;
|
||||
buf[9] = 0;
|
||||
|
||||
int result = hidHandle.send(0,buf, BUF_LEN, 5000);
|
||||
int result = sendData(buf, BUF_LEN);
|
||||
// hidHandle.receive(0,buf,BUF_LEN,5000);
|
||||
if(debug)
|
||||
qDebug() << result << " bytes sent";
|
||||
@ -600,17 +642,17 @@ OP_DFU::Status OP_DFU::CompareFirmware(const QString &sfile, const CompareType &
|
||||
}
|
||||
if(type==OP_DFU::crccompare)
|
||||
{
|
||||
quint32 crc=CRCFromQBArray(arr,devices[device].SizeOfCode);
|
||||
if(crc==devices[device].FW_CRC)
|
||||
{
|
||||
cout<<"Compare Successfull CRC MATCH!\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"Compare failed CRC DONT MATCH!\n";
|
||||
}
|
||||
return StatusRequest();
|
||||
}
|
||||
quint32 crc=CRCFromQBArray(arr,devices[device].SizeOfCode);
|
||||
if(crc==devices[device].FW_CRC)
|
||||
{
|
||||
cout<<"Compare Successfull CRC MATCH!\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
cout<<"Compare failed CRC DONT MATCH!\n";
|
||||
}
|
||||
return StatusRequest();
|
||||
}
|
||||
else
|
||||
{
|
||||
if(arr==StartDownload(arr.length(),OP_DFU::FW))
|
||||
@ -736,3 +778,43 @@ quint32 OP_DFU::CRCFromQBArray(QByteArray array, quint32 Size)
|
||||
}
|
||||
return CRC32WideFast(0xFFFFFFFF,Size/4,(quint32*)t);
|
||||
}
|
||||
int OP_DFU::sendData(void * data,int size)
|
||||
{
|
||||
if(!use_serial)
|
||||
{
|
||||
return hidHandle.send(0,data, size, 5000);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(serialhandle->sendData((uint8_t*)data+1,size-1))
|
||||
{
|
||||
if (debug)
|
||||
qDebug()<<"packet sent"<<"data0"<<((uint8_t*)data+1)[0];
|
||||
return size;
|
||||
}
|
||||
if(debug)
|
||||
qDebug()<<"Serial send OVERRUN";
|
||||
}
|
||||
}
|
||||
int OP_DFU::receiveData(void * data,int size)
|
||||
{
|
||||
if(!use_serial)
|
||||
{
|
||||
return hidHandle.receive(0,data, size, 10000);
|
||||
}
|
||||
else
|
||||
{
|
||||
int x;
|
||||
QTime time;
|
||||
time.start();
|
||||
while(true)
|
||||
{
|
||||
if(x=serialhandle->read_Packet(data+1)!=-1||time.elapsed()>10000)
|
||||
{
|
||||
if(time.elapsed()>10000)
|
||||
qDebug()<<"____timeout";
|
||||
return x;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,10 +5,15 @@
|
||||
#include <../../plugins/rawhid/pjrc_rawhid.h>
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QCryptographicHash>
|
||||
#include <QCryptographicHash>
|
||||
#include <QList>
|
||||
#include <iostream>
|
||||
#include "delay.h"
|
||||
#include "../../../libs/qextserialport/src/qextserialport.h"
|
||||
#include <QTime>
|
||||
#include ".\SSP\qssp.h"
|
||||
#include ".\SSP\port.h"
|
||||
#include ".\SSP\qsspt.h"
|
||||
using namespace std;
|
||||
#define BUF_LEN 64
|
||||
|
||||
@ -25,6 +30,11 @@ using namespace std;
|
||||
#define LISTDEVICES "-ls" //done
|
||||
#define RESET "-r"
|
||||
#define JUMP "-j"
|
||||
#define USE_SERIAL "-t"
|
||||
|
||||
#define MAX_PACKET_DATA_LEN 255
|
||||
#define MAX_PACKET_BUF_SIZE (1+1+MAX_PACKET_DATA_LEN+2)
|
||||
|
||||
|
||||
class OP_DFU
|
||||
{
|
||||
@ -73,32 +83,31 @@ public:
|
||||
|
||||
enum Commands
|
||||
{
|
||||
Reserved,
|
||||
Req_Capabilities,
|
||||
Rep_Capabilities,
|
||||
EnterDFU,
|
||||
JumpFW,
|
||||
Reset,
|
||||
Abort_Operation,
|
||||
Upload,
|
||||
Op_END,
|
||||
Download_Req,
|
||||
Download,
|
||||
Status_Request,
|
||||
Status_Rep,
|
||||
Reserved,//0
|
||||
Req_Capabilities,//1
|
||||
Rep_Capabilities,//2
|
||||
EnterDFU,//3
|
||||
JumpFW,//4
|
||||
Reset,//5
|
||||
Abort_Operation,//6
|
||||
Upload,//7
|
||||
Op_END,//8
|
||||
Download_Req,//9
|
||||
Download,//10
|
||||
Status_Request,//11
|
||||
Status_Rep,//12
|
||||
|
||||
};
|
||||
struct device
|
||||
{
|
||||
int ID;
|
||||
quint32 FW_CRC;
|
||||
int BL_Version;
|
||||
int SizeOfDesc;
|
||||
quint32 SizeOfCode;
|
||||
bool Readable;
|
||||
bool Writable;
|
||||
int ID;
|
||||
quint32 FW_CRC;
|
||||
int BL_Version;
|
||||
int SizeOfDesc;
|
||||
quint32 SizeOfCode;
|
||||
bool Readable;
|
||||
bool Writable;
|
||||
};
|
||||
|
||||
void JumpToApp();
|
||||
void ResetDevice(void);
|
||||
bool enterDFU(int const &devNumber);
|
||||
@ -113,8 +122,8 @@ public:
|
||||
QByteArray StartDownload(qint32 const & numberOfBytes, TransferTypes const & type);
|
||||
bool SaveByteArrayToFile(QString const & file,QByteArray const &array);
|
||||
void CopyWords(char * source, char* destination, int count);
|
||||
// QByteArray DownloadData(int devNumber,int numberOfPackets);
|
||||
OP_DFU(bool debug);
|
||||
// QByteArray DownloadData(int devNumber,int numberOfPackets);
|
||||
OP_DFU(bool debug,bool use_serial,QString port);
|
||||
void sendReset(void);
|
||||
bool findDevices();
|
||||
QList<device> devices;
|
||||
@ -126,13 +135,22 @@ public:
|
||||
void test();
|
||||
int send_delay;
|
||||
bool use_delay;
|
||||
bool ready(){return mready;}
|
||||
void AbortOperation(void);
|
||||
private:
|
||||
bool mready;
|
||||
bool debug;
|
||||
int RWFlags;
|
||||
|
||||
bool use_serial;
|
||||
qsspt * serialhandle;
|
||||
int sendData(void*,int);
|
||||
int receiveData(void * data,int size);
|
||||
pjrc_rawhid hidHandle;
|
||||
int setStartBit(int command){return command|0x20;}
|
||||
uint8_t sspTxBuf[MAX_PACKET_BUF_SIZE];
|
||||
uint8_t sspRxBuf[MAX_PACKET_BUF_SIZE];
|
||||
|
||||
port * info;
|
||||
};
|
||||
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
QT += core
|
||||
|
||||
QT -= gui
|
||||
|
||||
DEFINES += QEXTSERIALPORT_LIBRARY
|
||||
TARGET = OPUploadTool
|
||||
CONFIG += console
|
||||
CONFIG -= app_bundle
|
||||
@ -16,14 +16,26 @@ TEMPLATE = app
|
||||
DEFINES += RAWHID_LIBRARY
|
||||
SOURCES += main.cpp \
|
||||
op_dfu.cpp \
|
||||
delay.cpp
|
||||
delay.cpp \
|
||||
./SSP/qssp.cpp \
|
||||
./SSP/port.cpp \
|
||||
./SSP/qsspt.cpp \
|
||||
../../libs/qextserialport/src/qextserialport.cpp
|
||||
HEADERS += ../../plugins/rawhid/pjrc_rawhid.h \
|
||||
../../plugins/rawhid/rawhid_global.h \
|
||||
op_dfu.h \
|
||||
delay.h
|
||||
delay.h \
|
||||
../../libs/qextserialport/src/qextserialport.h \
|
||||
../../libs/qextserialport/src/qextserialenumerator.h \
|
||||
../../libs/qextserialport/src/qextserialport_global.h \
|
||||
./SSP/qssp.h \
|
||||
./SSP/port.h \
|
||||
./SSP/common.h \
|
||||
./SSP/qsspt.h
|
||||
|
||||
win32 {
|
||||
SOURCES += ../../plugins/rawhid/pjrc_rawhid_win.cpp
|
||||
|
||||
LIBS += -lhid \
|
||||
-lsetupapi
|
||||
}
|
||||
@ -51,3 +63,16 @@ linux-g++-64 {
|
||||
SOURCES += ../../plugins/rawhid/pjrc_rawhid_unix.cpp
|
||||
LIBS += -lusb
|
||||
}
|
||||
unix:SOURCES += ../../libs/qextserialport/src/posix_qextserialport.cpp
|
||||
unix:!macx:SOURCES += ../../libs/qextserialport/src/qextserialenumerator_unix.cpp
|
||||
macx {
|
||||
SOURCES += ../../libs/qextserialport/src/qextserialenumerator_osx.cpp
|
||||
LIBS += -framework IOKit -framework CoreFoundation
|
||||
}
|
||||
|
||||
win32 {
|
||||
SOURCES += ../../libs/qextserialport/src/win_qextserialport.cpp \
|
||||
../../libs/qextserialport/src/qextserialenumerator_win.cpp
|
||||
DEFINES += WINVER=0x0501 # needed for mingw to pull in appropriate dbt business...probably a better way to do this
|
||||
LIBS += -lsetupapi
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user