where : ibrtses embedded
the ibrt short message protocol
This simple and compact protocol is especially suited for general purpose bus interaction with embedded devices. The protocol is a binary master-slave with a CRC protection for 2^16 bits, or 8kbytes. The single byte length field restricts the message length to 255 bytes though.
the fields
The message is composed of fields :
[SYN+] STX Len SRC DST Cmd [Data] CRC
where
- [SYN+] is at least 1 SYN character 0x16
- STX is start of message (1 byte) = 0x02
- Len is 1 bytes counteing the number of bytes not including the SYN
- SRC is the source ID (1 byte)
- DST is the destination ID (1 byte)
- Cmd is a predefined command code (1 byte)
- [Data] is optional of any length depending on the command code
- CRC is a 16 bit CRC, high byte first
the protocol bounding rules
- communication is half-duplex, even though the medium would allow
simultaneous transmission and reception
- a slave device only answers to a command, doesn't transmit just by itself
- a device only replies if the ID matches and if the CRC is correct
- there is only one command out at a time. Each command has a reply
unless sent to the broadcase adress
- the bus master waits until the reply comes or until the timeout expires.
- a reply has the same command code as the issued command, but the
data field can be composed differently.
- a reply has the SRC and DST fields swapped.
- the broadcast ID to which all devices answer is 0xFF
Additional to the bounding rules
- the command processing is stateless
- commands are quick to be processed
the CRC
the 16bit CRC allows to detect single bit errors withing 2^16bits,
or 8KBytes. The CRC is calculated as
PROCEDURE calcCRCbyte(data:BYTE;VAR crc:WORD);
VAR i:BYTE;
BEGIN
FOR i:=0 TO 7 DO BEGIN
IF ((data and $01)XOR(crc AND $0001)<>0) THEN BEGIN
crc:=crc shr 1;
crc:= crc XOR $A001;
END
ELSE BEGIN
crc:=crc shr 1;
END;
data:=data shr 1;
END;
END;
The command to be sent is assumed zero aligned, eg STX is on address 0,
and SYN is not included in the CRC
FUNCTION CRC(VAR DATA;size:byte):WORD;
VAR i:byte;
BEGIN
CRC:=0;
FOR i:=0 TO size-1 DO BEGIN
calcCRCbyte(DATA[i],CRC);
END;
END;
the receiving state machine
The state machine operates with the following states
- 0 : idle - wait for SYN
- 1 : wait for STX
- 2 : read LenLo
- 3 : read LenHi
- 4 : count down
- 0x0E : Buffer full
- 0x0F : Message received
VAR
RxBuffer:Array[0..RxBufLen-1]of byte;
RxState:byte; // idle :=0;
RxLen:byte;
RxPtr:Byte;
CRC:word;
The interrupt procedure is moreless identical on the
PC and on the embedded device. On the PC, it is called
event. and is some levels above the actual hardware
interrupt, but the action is the same.
interrupt rx_interrupt;
var u:byte;
begin
u:=ReadUART;
if (RxPtr=RxBufLen) then RxState:=0x0E;
else begin
case RxState of
0:if u=SYN then RxState :=1; | // wait for SYN
1:case u of // wait for STX
SYN:RxState :=1; |
STX:RxBuf[0]:=STX; RxState :=2; |
else state:=0;
end; |
2:RxBuf[1]:=u; RxLen:=u; // read Len
RxState :=3; RxPtr:=2; |
3:RxBuf[RxPtr]:=u; // counting
inc(RxPtr);
If RxPtr=RxLen then RxState :=0x0F; |
end; // case
end; // buffer not full
end; // interrupt
This procedure is in the main loop of the embedded controller
and does the slave part.
procedure ComLoop;
begin
if RxState=0x0E then begin // discard content
RxState:=0;
end;
if RxState=0x0F then begin
RxCRCMessage(rxptr-2); // CRC is updated
if (CRChigh=RxBuf[rxptr-2])and
(CRCLow=RxBuf[rxptr-1]) then begin// crc ok.
if (RxBuf[3]=DeviceID)or(RxBuf[3]=$FF) then begin
src:=RxBuf[2];
dst:=RxBuf[3];
cmd:=RxBuf[4];
case cmd of
0x00:// Echo
CopyRxTx;
AddCrc;
SetupTx;|
..
0x44://UserString read
txlen:=ord(UserString[0])+9;
SetupTxFrame;
CopyBlock(@UserString,@TxBuf+6,ord(UserString[0])+1);
AddCrc;
SetupTx;|
end; //case
end; // ID ok
end; // crc ok
RxState:=0;
RxPtr:=0;
end; // 0x0F
end;
independent implementation details
ibrt devices that implement the "ibrt short message protocol" have the following details defined as
- baudrate 4800
- other serial parameters 8,n,1
- buffer length 40..50 bytes
- the underlying hardware is RS232 or RS422
predefined commands
the following commands are predefined and implemented in all devices
command code |
meaning |
0x00 |
Echo |
0x30 |
write user string |
0x40 |
read device string |
0x41 |
read serial number |
0x42 |
read copyright message |
0x43 |
read URL reference |
0x44 |
read user string |
further
With the current hardware (PC) having a response time of several hundred ms,
this protocol is not optimally suited to download huge amounts of data.
As first measure, the message length has to be increased, as second
measure, the baudrate to be increased.
For longer messages have a look at the ibrt long message protocol
embedded
home
last updated: 22.dec.06 or perhaps later
Questions ?
Suggestions?
Feedback ?
sponsored links
Copyright (99,2006) Ing.Büro R.Tschaggelar