[avr + fluksod] enable crc checking in both spi directions

This commit is contained in:
Bart Van Der Meerssche 2011-01-17 11:11:44 +01:00
parent e282a750de
commit d208545000
4 changed files with 40 additions and 11 deletions

View File

@ -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; 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)); crc = _crc_ibutton_update(crc, bufferGetAtIndex(buffer, i));
} }
return crc; 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) void ctrlDecode(void)
{ {
uint8_t cmd[2], crc; uint8_t cmd[2], crc;
ctrlFlushTxBuffer(); 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[0]);
ctrlAddToTxBuffer(cmd[1]); ctrlAddToTxBuffer(cmd[1]);
@ -238,12 +254,16 @@ void ctrlDecode(void)
if (cmd[1] == 't') ctrlCmdCommit(); if (cmd[1] == 't') ctrlCmdCommit();
break; break;
} }
}
else {
ctrlAddToTxBuffer('z');
ctrlAddToTxBuffer('y');
}
crc = ctrlCalcCrc8(&ctrlTxBuffer); crc = ctrlCalcCrc8(&ctrlTxBuffer, 0);
ctrlWriteCharToTxBuffer(crc); ctrlWriteCharToTxBuffer(crc);
ctrlAddToTxBuffer('.'); ctrlAddToTxBuffer('.');
}
ctrlFlushRxBuffer(); ctrlFlushRxBuffer();
} }

View File

@ -108,9 +108,19 @@ void ctrlRxToTxLoop(void);
* Calculate the CRC-8 checksum over the bytes in the buffer. * Calculate the CRC-8 checksum over the bytes in the buffer.
* *
* @param buffer pointer to the buffer containing the data * @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 * @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, * Decode the message in the ctrl Rx buffer and dispatch to either ctrlCmdGet,

View File

@ -122,7 +122,6 @@ ISR(SPI_STC_vect)
break; break;
case SPI_END_OF_MESSAGE: case SPI_END_OF_MESSAGE:
if (!(spi_status & SPI_TO_FROM_UART)) { if (!(spi_status & SPI_TO_FROM_UART)) {
ctrlAddToRxBuffer(spi_rx);
spi_status |= SPI_NEW_CTRL_MSG; spi_status |= SPI_NEW_CTRL_MSG;
} }
break; break;

View File

@ -100,7 +100,7 @@ function encode(msg)
end 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 end
function tx(msg, cdev) function tx(msg, cdev)