Skip to content
modRT edited this page Oct 18, 2015 · 14 revisions

#The modRT Wiki

Serial communication

Header J1 of the Xmega Xplained board is reserved for modbus communication. Modbus status LEDs are connected to J1 as well. Pins 1 and 2 are left open so users can still use the I2C interface for their purposes.

Serial communication hardware: RS485

Schematic of RS485 interface

There are two 9pin subd connectors so setting up a RS485 bus is easy. All devices are connected in a daisy chain. The first and the last device need to terminate and bias the bus. For details refer to wikipedia article RS485. Please note that RS485 hardware was not tested.

Serial communication hardware: RS232

RS232 interface for

With RS232 communication is limited to one slave and there is only one 9 pin subd connector. For details on RS232 refer to Wikipedia RS232 and Wikipedia serial port.

Packet driver (module to send and receive messages)

Great attention was paid to the modbus timing specifications. To guarantee that the slave messages are sent without gaps I use the DMA controller of the Xmega. After the USART buffer was loaded (by means of the DMA controller) with last character to be transmitted the DMA controller triggers an interrupt. The interrupt service routine sets timer TCC1 to trigger after 4.5 character times (one character time for the last character to be sent and 3.5 to indicate the message end (as required by the modbus specifications). Only when TCC1 triggers the transmission driver is disabled to make sure that the bus is at a well defined logic level until the master detects the message end. Note that a gap (within a message) with a time of 1.5 times the time needed to send one character (I refer to this as character time) or more triggers an error or terminates the message prematurely. Reception is more complex. Timer (TCD0) is set to expire after 3.5 character times when the first character of a message was received. Subsequently every time the USART receives a character timer TCD0 is reset to 0 after checking if timer TCD0 counted to a value corresponding to a gap of 1.5 characters or more indicating a reception error. When timer TCD0 expires no character was received for 3.5 character times and the end of the message was reached.

The USART hardware needs a clock which determines USART timing. I use the internal 32MHz clock, however this clock by itself is not precise enough. The calibration feature of the Xmega is used to correct the 32MHz clock using the highly precise, quartz controlled, 32768Hz oscillator as a reference. If this calibration is omitted occasional transmission errors will be observed.

There are separate buffers for transmission and reception. the first byte of the buffer indicates the buffer length. Both buffers are declared as global variables (no dynamic memory allocation).

Xmega processors have a hardware to calculate CRC16 but they have the wrong polynomial representation and CRC16 has to be computed by software.

Modbus

This implementation is based on Modbus specification 1.1b3

During initialization the modbus slave address is set to 1 if button SW1 is pressed. Use this feature if the unit is connected for the first time or the modbus slave address is not known. If SW1 is not pushed the modbus slave address is read from EEPROM address 0x0000. To change the modbus slave address write to modbus register 0x0000. The new slave address only goes into effect after the ACK was sent to the master.

Not implemented

  1. These Modbus functions return NACK
    • modbusReadInputRegister (0x04)
    • modbusWriteSingleRegister (0x06)
    • modbusReadExceptionStatus (0x07)
    • modbusGetComEventCounter (0x0b)
    • modbusGetComEventLog (0x0c)
    • modbusReportServerID (0x11)
    • modbusReadFileRecord (0x14)
    • modbusWriteFileRecord (0x15)
    • modbusMaskWriteRegister (0x16)
    • modbusReadWriteMultipleRegisters (0x17)
    • modbusReadFIFOqueue (0x18)

Licenses

FreeRTOS is governed by a modified GPL. See file license.txt for details. modRT may be modified and distributed under the terms of the MIT license. Refer to the file license.MIT for details. To identify if a source file belongs to modRT check the first lines of the source file.