Robotino3 IO protocol

From RobotinoWiki
Revision as of 14:52, 6 July 2012 by Verbeek (talk | contribs)

Description of the data exchange protocol between LPC2378 and COM-Express module over USB

Most of the I/Os comming with Robotino 3 are still connected to a NXP LPC2378 microcontroller (MC). In contrast to previous versions of Robotino the interface between the MC and Robotino's internal industral PC is no longer RS232 but USB. This page describes the protocol used for communication between MC and PC over USB.

Package assembly

All data send from PC to MC and vice versa has the form

Field name Bytes Description
Head 1 Start of package
Length 2 Length of package payload
Payload Payload data
Checksum 2 Calculated from length and payload bytes

Head

The package header is the unique byte 0xAA.

Length

The number of bytes of the payload data.

package[1] = length & 0xFF;
package[2] = 0xFF & ( length >> 8 );

Payload

The package payload data. The payload is composed of one or more commands.

Checksum

Complement 2 of the sum of Length and Payload bytes before escaping.

uint16 calculate_checksum( const uint8* data, int dataSize )
{
  uint16 out = 0;
  for( int i=0; i<dataSize; ++i )
  {
    out += *p;
    ++data;
  }
  return 0xffff & ( (1<<16) - out );
}

Data escaping

The Head byte 0xAA must be unique through out all data send. By this the receiver knows that whenever it reads 0xAA that a new package begins. This is achieved by going through all the Length, Payload and Checksum bytes and whenever 0xAA or the escape byte 0x55 is found this byte is prefixed with 0x55 and the byte is xored with 0x20.

uint8* data; //the complete package data (Head, Length, Payload, Checksum)
uint8* escapedData; //buffer big enough to hold the escaped package data
for( int i=1; i<numBytesInData; ++i ) //not that we are starting at 1, we do not escape the Head
{
  if( 0xAA == *data || 0x55 == *data )
  {
    escapedData+= 0x55;
    escapedData+= ( *data ^ 0x20 );
  }
  else
  {
    escapedData+= *data;
  }
  ++data;
}

When reading an escaped package lock for the escape byte 0x55. When you found 0x55 in the stream read the next byte and xor it with 0x20 to get the unescaped value.

Commands

Field name Bytes Description
Tag 1 The command tag
Length 1 Number of bytes of command data
Data The command data. If Length=0 there is no Data field at all.

Tag

Name Value Command data Description
GET_DISTANCE_SENSOR_READINGS 1 none Send from PC to MC requesting the readings of all infrared distance sensors