Timeout resynchronization
If during packet reception next byte wait time exceeds timeout value, then partially received command is ignored and receive buffer is
cleared. Controller timeout should be less than PC timeout, taking into account time it takes to transmit the data.
Zero byte resynchronization
There are no command codes that start with a zero byte ('\0'). This allows for a following synchronization procedure: controller always
answers with a zero byte if the first command byte is zero, PC ignores first response byte if it is a zero byte. Then, if synchronization is
disrupted on either side the following algorithm is used:
In case PC receives "errc", "errd" or a wrong command answer code, then PC sends 4 to 250 zeroes to the controller (250 byte limit is
caused by input buffer length and usage of I2C protocol, less than 4 zeroes do not guarantee successful resynchronization). During this
time PC continuously reads incoming bytes from the controller until the first zero is received and stops sending and receiving right after
that.
Received zero byte is likely not a part of a response to a previous command because on error PC receives "errc"/"errd" response. It is
possible in rare cases, then synchronization procedure will start again. Therefore first zero byte received by the PC means that
controller input buffer is already empty and will remain so until any command is sent. Right after receiving first zero byte from the
controller PC is ready to transmit next command code. The rest of zero bytes in transit will be ignored because they will be received
before controller response.
This completes the zero byte synchronization procedure.
Library-side error processing
Nearly every library function has a return status of type result_t.
After sending command to the controller library reads incoming bytes until a non-zero byte is received. All zero bytes are ignored.
Library reads first 4 bytes and compares them to the command code. It then waits for data section and CRC, if needed. If first 4
received bytes do not match the sent command identifier, then zero byte synchronization procedure is launched, command is
considered to be sent unsuccessfully. If first 4 received bytes match the sent command identifier and command has data section, but
the received CRC doesn't match CRC calculated from the received data, then zero byte synchronization procedure is launched,
command is considered to be sent unsuccessfully. If a timeout is reached while the library is waiting for the controller response, then
zero byte synchronization procedure is launched, command is considered to be sent unsuccessfully.
If no errors were detected, then command is considered to be successfully completed and result_ok is returned.
Library return codes
result_ok. No errors detected.
result_error. Generic error. Can happen because of hardware problems, empty port buffer, timeout or successfull
synchronization after an error. Another common reason for this error is protocol version mismatch between controller firmware