removed unused timeout
This commit is contained in:
parent
8d00bb0600
commit
63d81c6e7e
4 changed files with 45 additions and 407 deletions
|
@ -229,7 +229,7 @@ uint16_t B15F::analogRead(uint8_t channel)
|
|||
channel
|
||||
};
|
||||
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq), 1000);
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
|
||||
if(n_sent != sizeof(rq))
|
||||
abort("Sent failed");
|
||||
|
||||
|
@ -302,7 +302,7 @@ uint8_t B15F::pwmSetFrequency(uint32_t freq)
|
|||
static_cast<uint8_t>((freq >> 24) & 0xFF)
|
||||
};
|
||||
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq), 1000);
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
|
||||
if(n_sent != sizeof(rq))
|
||||
abort("Sent failed");
|
||||
|
||||
|
@ -321,7 +321,7 @@ bool B15F::pwmSetValue(uint8_t value)
|
|||
value
|
||||
};
|
||||
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq), 1000);
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
|
||||
if(n_sent != sizeof(rq))
|
||||
abort("Sent failed");
|
||||
|
||||
|
@ -341,7 +341,7 @@ bool B15F::setRegister(uint8_t adr, uint8_t val)
|
|||
val
|
||||
};
|
||||
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq), 1000);
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
|
||||
if(n_sent != sizeof(rq))
|
||||
abort("Sent failed");
|
||||
|
||||
|
@ -360,7 +360,7 @@ uint8_t B15F::getRegister(uint8_t adr)
|
|||
adr
|
||||
};
|
||||
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq), 1000);
|
||||
int n_sent = usart.receive(&rq[0], 0, sizeof(rq));
|
||||
if(n_sent != sizeof(rq))
|
||||
abort("Sent failed");
|
||||
|
||||
|
|
|
@ -57,13 +57,6 @@ void USART::flushOutputBuffer()
|
|||
throw USARTException("Fehler beim Versenden des Ausgangspuffers");
|
||||
}
|
||||
|
||||
void USART::printStatistics()
|
||||
{
|
||||
double pz = 1e2 * n_blocks_failed / n_blocks_total;
|
||||
pz = std::round(pz * 1e2) / 1e2;
|
||||
std::cout << "blocks total: " << n_blocks_total << " ok: " << (n_blocks_total - n_blocks_failed) << " failed: " << n_blocks_failed << " (" << pz << "%)" << std::endl;
|
||||
}
|
||||
|
||||
void USART::writeByte(uint8_t b)
|
||||
{
|
||||
int sent = write(file_desc, &b, 1);
|
||||
|
@ -92,7 +85,7 @@ void USART::writeU32(uint32_t w)
|
|||
throw USARTException("Fehler beim Senden: writeU32()");
|
||||
}
|
||||
|
||||
int USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len, uint32_t timeout)
|
||||
int USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len)
|
||||
{
|
||||
uint32_t elapsed = 0;
|
||||
int n_read = -1;
|
||||
|
@ -111,7 +104,7 @@ int USART::transmit(uint8_t *buffer, uint16_t offset, uint8_t len, uint32_t time
|
|||
return 0;
|
||||
}
|
||||
|
||||
int USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len, uint32_t timeout)
|
||||
int USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len)
|
||||
{
|
||||
uint32_t elapsed = 0;
|
||||
int n_sent = -1;
|
||||
|
@ -130,89 +123,6 @@ int USART::receive(uint8_t *buffer, uint16_t offset, uint8_t len, uint32_t timeo
|
|||
|
||||
return n_sent;
|
||||
}
|
||||
|
||||
void USART::writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len)
|
||||
{
|
||||
uint8_t crc;
|
||||
uint8_t aw;
|
||||
const uint16_t us_per_bit = (1000000 / baudrate) * 16;
|
||||
const uint16_t n_total = len + 3;
|
||||
|
||||
n_blocks_total++;
|
||||
bool failed = false;
|
||||
|
||||
do
|
||||
{
|
||||
// calc crc
|
||||
crc = 0;
|
||||
for(uint8_t i = 0; i < len; i++)
|
||||
{
|
||||
crc ^= buffer[i];
|
||||
for (uint8_t k = 0; k < 8; k++)
|
||||
{
|
||||
if (crc & 1)
|
||||
crc ^= CRC7_POLY;
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
// construct block
|
||||
block_buffer[0] = len;
|
||||
std::memcpy(&block_buffer[1], buffer + offset, len);
|
||||
block_buffer[len + 1] = crc;
|
||||
block_buffer[len + 2] = BLOCK_END;
|
||||
|
||||
// send block
|
||||
clearOutputBuffer();
|
||||
clearInputBuffer();
|
||||
int n_sent = receive(&block_buffer[0], 0, len + 3, us_per_bit * n_total);
|
||||
if(n_sent != n_total)
|
||||
throw std::runtime_error("fatal (send): " + std::to_string(n_sent));
|
||||
|
||||
/*for(uint8_t i = 0; i < len + 3; i++)
|
||||
{
|
||||
receive(&block_buffer[i], 0, 1, us_per_bit * n_total);
|
||||
//tcdrain(file_desc);
|
||||
//usleep(1000);
|
||||
}*/
|
||||
|
||||
// flush output data
|
||||
tcdrain(file_desc);
|
||||
|
||||
//usleep(us_per_bit * n_total * 10);
|
||||
|
||||
// check response
|
||||
int n_read = transmit(&aw, 0, 1, us_per_bit * n_blocks_total * 10);
|
||||
for(uint16_t i = 0; i < 255 && n_read != 1; i++)
|
||||
{
|
||||
writeByte(0x80); // Stoppzeichen für Block
|
||||
if(tcdrain(file_desc))
|
||||
{
|
||||
std::cout << "drain failed" << std::endl;
|
||||
}
|
||||
std::cout << "WARNING: read error (" << n_read << "), retry #" << (int) i << std::endl;
|
||||
usleep(us_per_bit*100);
|
||||
n_read = transmit(&aw, 0, 1, us_per_bit);
|
||||
}
|
||||
|
||||
if(n_read != 1)
|
||||
throw std::runtime_error("fatal: " + std::to_string(n_read));
|
||||
|
||||
//clearInputBuffer();
|
||||
|
||||
if(aw != 0xFF)
|
||||
{
|
||||
if(!failed)
|
||||
n_blocks_failed++;
|
||||
failed = true;
|
||||
std::cout << "block failed, retry" << std::endl;
|
||||
}
|
||||
}
|
||||
while(aw != 0xFF);
|
||||
|
||||
//std::cout << "OK" << std::endl;
|
||||
}
|
||||
|
||||
uint8_t USART::readByte(void)
|
||||
{
|
||||
char b;
|
||||
|
@ -237,78 +147,6 @@ uint16_t USART::readInt(void)
|
|||
return readByte() | readByte() << 8;
|
||||
}
|
||||
|
||||
bool USART::readBlock(uint8_t* buffer, uint16_t offset)
|
||||
{
|
||||
uint8_t len = readByte();
|
||||
uint8_t crc = 0;
|
||||
buffer += offset;
|
||||
|
||||
uint32_t block_timeout = timeout / 10;
|
||||
|
||||
// wait for block
|
||||
int n_ready;
|
||||
uint16_t elapsed = 0;
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
auto end = start;
|
||||
while(elapsed < block_timeout)
|
||||
{
|
||||
int code = ioctl(file_desc, FIONREAD, &n_ready);
|
||||
if(code != 0)
|
||||
{
|
||||
std::cout << "n_ready code: " << code << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if(n_ready >= len + 1)
|
||||
break;
|
||||
|
||||
end = std::chrono::steady_clock::now();
|
||||
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
||||
}
|
||||
if(elapsed >= timeout)
|
||||
{
|
||||
std::cout << "block timeout: " << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
while(len--)
|
||||
{
|
||||
*buffer = readByte();
|
||||
|
||||
crc ^= *buffer++;
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (crc & 1)
|
||||
crc ^= CRC7_POLY;
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
crc ^= readByte();
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
if (crc & 1)
|
||||
crc ^= CRC7_POLY;
|
||||
crc >>= 1;
|
||||
}
|
||||
|
||||
if(TEST == 1)
|
||||
crc = 1;
|
||||
if(TEST > 100)
|
||||
TEST = 0;
|
||||
|
||||
if (crc == 0)
|
||||
{
|
||||
writeByte(0xFF);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
writeByte(0xFE);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t USART::getBaudrate()
|
||||
{
|
||||
return baudrate;
|
||||
|
|
|
@ -54,11 +54,6 @@ public:
|
|||
*/
|
||||
void flushOutputBuffer(void);
|
||||
|
||||
/**
|
||||
* Gibt Anzahl an erfolgreichen und fehlgeschlagenen Block-Übertragungen an
|
||||
*/
|
||||
void printStatistics(void);
|
||||
|
||||
/*************************************************/
|
||||
|
||||
|
||||
|
@ -100,11 +95,8 @@ public:
|
|||
*/
|
||||
uint16_t readInt(void);
|
||||
|
||||
int transmit(uint8_t *buffer, uint16_t offset, uint8_t len, uint32_t timeout);
|
||||
int receive(uint8_t *buffer, uint16_t offset, uint8_t len, uint32_t timeout);
|
||||
|
||||
void writeBlock(uint8_t* buffer, uint16_t offset, uint8_t len);
|
||||
bool readBlock(uint8_t* buffer, uint16_t offset);
|
||||
int transmit(uint8_t *buffer, uint16_t offset, uint8_t len);
|
||||
int receive(uint8_t *buffer, uint16_t offset, uint8_t len);
|
||||
|
||||
/*************************************/
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue