Skip to content
phail edited this page Jul 1, 2019 · 4 revisions

Machine Protocol Definition

Each message is restricted to 255 bytes of data so that an umambiguous length byte can be used.

At the lowest level, every message has the structure (represented by PROTOCOL_MSG2 in protocol.h):

SOM (Start of Message)
CI (incrementing continuity indicator)
LEN (len of DATA in bytes: 0-255)
DATA (data bytes of message).
CS (checksum = 0 - (CI + LEN + sum of DATA))

This message structure is used to carry commands and data in the DATA portion.

SOM marks the beginning of a new message. There are two different SOMs:

  • 0x02: Message will be replied to with ACK by the other side. If no ACK is received, Message will be resend
  • 0x04: Message is not acknowledged, useful for time critical messages.

The first byte of the DATA portion is the CMD byte.

CS is such that the sum of all bytes from CI to CS is 00 when ANDed with 0xFF.

Low level protocol operation

At a low level, the protocol uses the commands:

#define PROTOCOL_CMD_ACK 'A'
#define PROTOCOL_CMD_NACK 'N'

along with timeout values timeout1 (~500ms) and timeout2 (~100ms) to control message flow and ensure message delivery in adverse conditions.

Sending:

  • When a message is sent, another will not be sent until an ACK has been received or all retries (2) sent.
  • If a NACK is received, the last message will be sent with the same CI, up to 2 retries.
  • If an ACK response is not received within timeout1, last message will be resent with the same CI, up to 2 retries.

Receiving:

  • if a message is received with the same CI as the last received message, ACK will be sent, but the message discarded.

  • if an ACK is received which contains the same CI as the last ACK, the ACK will be ignored.

  • if an ACK is received which contains a CI different to the last sent message, the ACK will be ignored. (?? implications??)

  • In receive, if in a message (SOM has been received) and the time since the last character exceeds timeout2, the incoming message should be discarded, and a NACK should be sent with the CI of the message in progress or zero if no CI received yet.

  • sending messages and receiving messages are independent - e.g. if expecting an ACK, and a new message is received, the ACK should still be expected.

  • normally, characters received BETWEEN messages which are not SOM should be treated as ASCII commands.

  • implement a mode where non SOM characters between messages cause (TIMEOUT2) to be started, resulting in a NACK with CI of last received message + 1.

Higher level protocol operation

Defined commamnds:

#define PROTOCOL_CMD_TEST 'T'
#define PROTOCOL_CMD_TESTRESPONSE 't'
#define PROTOCOL_CMD_REBOOT 'B'
#define PROTOCOL_CMD_UNKNOWN '?'
#define PROTOCOL_CMD_READVAL 'R'
#define PROTOCOL_CMD_WRITEVAL 'W'

PROTOCOL_CMD_TEST will result in a response containing the same DATA, except the CMD byte will have been chnaged to PROTOCOL_CMD_TESTRESPONSE (currently the firmware will actually send TWO responses in this form).

PROTOCOL_CMD_REBOOT will restart the firmware

PROTOCOL_CMD_UNKNOWN will be returned in the case that a message with an unrecognised command is received.

PROTOCOL_CMD_READVAL/PROTOCOL_CMD_WRITEVAL - read/write parameters, or perform functions on the firmware see following pages.