For my project I am using an arduino DUE so the memory requirements were not as big a deal for me!
I did actually implement the CRC-16 by algorithm at first, but I swapped that implementation out for the speed of the tabled CRC version. When I get a chance I will post a third arduino version designed for low memory applications that implements the algorithm version.
If memory is a concern then the framed data output buffer should be customized. It is currently set to be 'framed_data' in "void Framing::sendFramedData(byte* data, int length)". This can conservatively be set to a value which is equal to (2*length_outbound_data + 8 ). The value (2*length_outbound_data + 8 ) is chosen for the worst case where all data to be sent equals DLE and both CRC values equal DLE.
I do not have a design document but basically the packet is structured as follows: [DLE] [STX] [Stuffed Data] [Stuffed CRC16] [DLE] [ETX]
Stuffing refers to adding an additional DLE character before any DLE that appears in the data to be sent or in the calculated CRC.
The CRC is calculated before byte stuffing occurs, or on the receiving end it is calculated after the data has been unstuffed.
The basic while loop structure is as follows:
-While a read timeout has NOT occurred
-wait for an available byte to be received
Once an available byte has been received:
-While a timeout has NOT occured
-Check for start flag [DLE] [STX]
When the start flag has been received:
-While a timeout has NOT occured AND end flag [DLE] [ETX] has not been received
-Receive data and Unstuff data (replace all instances of [DLE] [DLE] with [DLE])
When the end flag has been received:
-Use last two bytes of data as CRC16 and check data validity against this CRC value
-IF data valid, return data. IF data invalid return -1
IF a timeout occurs before any data is received return 0. IF timeout occurs during data read return -1.
If I were to make a design doc, what would you like to see in it?