[avr + fluksod] enable crc checking in both spi directions
This commit is contained in:
parent
e282a750de
commit
d208545000
|
@ -205,23 +205,39 @@ void ctrlRxToTxLoop(void)
|
|||
}
|
||||
}
|
||||
|
||||
uint8_t ctrlCalcCrc8(cBuffer* buffer)
|
||||
uint8_t ctrlCalcCrc8(cBuffer* buffer, uint8_t chop)
|
||||
{
|
||||
uint8_t i, crc = 0;
|
||||
|
||||
for (i = 0; i < buffer->datalength; i++) {
|
||||
for (i = 0; i < buffer->datalength - chop; i++) {
|
||||
crc = _crc_ibutton_update(crc, bufferGetAtIndex(buffer, i));
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
uint8_t ctrlExtractCrc8fromMessage(cBuffer* buffer)
|
||||
{
|
||||
uint8_t crc, high_hex, low_hex;
|
||||
|
||||
high_hex = bufferGetAtIndex(buffer, buffer->datalength - 2);
|
||||
low_hex = bufferGetAtIndex(buffer, buffer->datalength - 1);
|
||||
|
||||
htob(high_hex, low_hex, &crc);
|
||||
return crc;
|
||||
}
|
||||
|
||||
void ctrlDecode(void)
|
||||
{
|
||||
uint8_t cmd[2], crc;
|
||||
|
||||
ctrlFlushTxBuffer();
|
||||
|
||||
if (ctrlGetFromRxBuffer(cmd) && ctrlGetFromRxBuffer(cmd+1)) {
|
||||
crc = ctrlExtractCrc8fromMessage(&ctrlRxBuffer);
|
||||
if (ctrlCalcCrc8(&ctrlRxBuffer, 2) != crc) {
|
||||
ctrlAddToTxBuffer('z');
|
||||
ctrlAddToTxBuffer('z');
|
||||
}
|
||||
else if (ctrlGetFromRxBuffer(cmd) && ctrlGetFromRxBuffer(cmd+1)) {
|
||||
ctrlAddToTxBuffer(cmd[0]);
|
||||
ctrlAddToTxBuffer(cmd[1]);
|
||||
|
||||
|
@ -238,12 +254,16 @@ void ctrlDecode(void)
|
|||
if (cmd[1] == 't') ctrlCmdCommit();
|
||||
break;
|
||||
}
|
||||
|
||||
crc = ctrlCalcCrc8(&ctrlTxBuffer);
|
||||
ctrlWriteCharToTxBuffer(crc);
|
||||
|
||||
ctrlAddToTxBuffer('.');
|
||||
}
|
||||
else {
|
||||
ctrlAddToTxBuffer('z');
|
||||
ctrlAddToTxBuffer('y');
|
||||
}
|
||||
|
||||
crc = ctrlCalcCrc8(&ctrlTxBuffer, 0);
|
||||
ctrlWriteCharToTxBuffer(crc);
|
||||
|
||||
ctrlAddToTxBuffer('.');
|
||||
|
||||
ctrlFlushRxBuffer();
|
||||
}
|
||||
|
|
|
@ -108,9 +108,19 @@ void ctrlRxToTxLoop(void);
|
|||
* Calculate the CRC-8 checksum over the bytes in the buffer.
|
||||
*
|
||||
* @param buffer pointer to the buffer containing the data
|
||||
* @param chop chop number of bytes from end of buffer for crc calc
|
||||
* @return CRC-8 checksum
|
||||
*/
|
||||
uint8_t ctrlCalcCrc8(cBuffer* buffer);
|
||||
uint8_t ctrlCalcCrc8(cBuffer* buffer, uint8_t chop);
|
||||
|
||||
|
||||
/**
|
||||
* Extract the CRC-8 checksum out of the message in the buffer.
|
||||
*
|
||||
* @param buffer pointer to the buffer containing the message
|
||||
* @return CRC-8 checksum
|
||||
*/
|
||||
uint8_t ctrlExtractCrc8fromMessage(cBuffer* buffer);
|
||||
|
||||
/**
|
||||
* Decode the message in the ctrl Rx buffer and dispatch to either ctrlCmdGet,
|
||||
|
|
|
@ -122,7 +122,6 @@ ISR(SPI_STC_vect)
|
|||
break;
|
||||
case SPI_END_OF_MESSAGE:
|
||||
if (!(spi_status & SPI_TO_FROM_UART)) {
|
||||
ctrlAddToRxBuffer(spi_rx);
|
||||
spi_status |= SPI_NEW_CTRL_MSG;
|
||||
}
|
||||
break;
|
||||
|
|
|
@ -100,7 +100,7 @@ function encode(msg)
|
|||
|
||||
end
|
||||
|
||||
--> TODO msg.encoded = msg.encoded .. dow_crc(msg.encoded)
|
||||
msg.encoded = msg.encoded .. nixio.bin.numtohex(nixio.bin.dow_crc(msg.encoded), 1)
|
||||
end
|
||||
|
||||
function tx(msg, cdev)
|
||||
|
|
Loading…
Reference in New Issue