High Speed Serial Port Design Pattern

Embedded processors now-a-days are connected with each other via high speed serial links. There links can range from 1 Mbps to 100 Mbps. At these high link speeds, the byte by byte transfers described in the serial port design pattern prove inadequate. High speed transfers require devices that can directly transfer data to and from memory without interrupting the processor.

Intent

This design pattern covers interfacing techniques with high speed serial communication devices. The main objective is to encapsulate the interface with the device and provide a hardware independent interface to the high speed serial port.

Also Known As

Motivation

The main motivation for development of this design pattern is to minimize dependency on hardware. Very often the hardware team decides to change the interface devices due to cost, end-of-life or functionality improvements. This involves a costly software porting exercise. High speed serial port design pattern encapsulates DMA configuration, register interfacing and interrupt handling specific to a device. Change in the device will just result in changes to just the classes involved in implementing this design pattern. 

Applicability

This pattern is applicable to all serial devices that involve DMA (direct memory access) transfers to and from the device. In such devices serial transmission and reception are completely handled by the serial device. The device operates as a bus master and transfers data to and from the memory without main processor intervention.

Structure

Serial Port is implemented with the SerialPort and SerialPortManager classes. The SerialPortManager maintains an array of SerialPort objects. Each SerialPort object manages the transmit and receive buffers. The SerialPortManager class also implements the interrupt service routine.

As mentioned earlier, the serial port is intelligent. The device needs to be programmed with the addresses of the transmit and receive message queues. Once these addresses have been programmed, the device automatically reads the buffer header to determine the current state of the buffer. For example, when the device finishes transmission of a buffer, it reads the buffer header for the next buffer to determine there is a new message ready for transmission. If a new message is found, the device initiates transmission immediately, without involving the processor.

Participants

The key participants in this pattern are:

Collaboration

The interactions between the participants are shown in the figure below:

UML Class Diagram for DMA Serial Port Pattern

Consequences

Implementing the Serial Port design pattern keeps the hardware dependent code confined to a few classes in the system. This simplifies the software port to new hardware.

Implementation

The implementation of this design pattern is explained in terms of handling of message transmission and reception. The important point to note here is that the code executing in the context of the ISR is kept to the minimum. All the CPU intensive operations are carried out at the task level.

Transmitting a Message

  1. SerialPortManager's constructor installs the InterruptServiceRoutine().
  2. Serial Port's constructor initializes the interrupts so that the transmitter empty interrupt is disabled and the receiver ready interrupt is enabled.
  3. A message is enqueued to the SerialPort by invoking the HandleTxMessage() method.
  4. The method enqueues the message in the Transmit Queue and checks if this is the first message in the queue.
  5. Since this is the first message in the queue, the message is removed from the queue and copied into a transmission buffer and the "ready for transmission" flag is set.
  6. The device periodically becomes a bus master, polling for the "ready for transmission" flag in the first transmission buffer.
  7. The flag is set, so the device begins transmission of the buffer.
  8. When all bytes of the message have been transmitted, the device set the "finished transmission" bit in the buffer header.
  9. The device checks the next buffer to determine if it is ready for transmission.
  10. In this scenario, no other buffer is ready for transmission. Device raises the transmission complete interrupt. (If more messages were enqueued, the device would have automatically started transmitting the buffer).
  11. The InterruptServiceRoutine() is invoked.
  12. The ISR polls the SerialPorts to select the interrupting device.
  13. The HandleInterrupt() method of the SerialPort is invoked.
  14. SerialPort checks the interrupt status register to determine the source of the interrupt.
  15. This is a transmit interrupt, so the HandleTxInterrupt() method is invoked.
  16. A transmission complete event is sent to the task.
  17. This event is routed by the SerialPortManager to the SerialPort.
  18. SerialPort checks if the transmit queue has any more messages.
  19. If a message is found, message transmission of the new message is initiated.

Receiving a Message

  1. When the device detects the start of a new message, it accesses the receive buffers and checks the "free buffer" bit in the buffer header.
  2. The device finds a free buffer, so it starts DMA operations to copy all the received bytes into the designated buffer.
  3. The device raises an interrupt when message reception is completed. It also sets the "received message" bit in the buffer header. (If another message reception starts, the device will automatically start receiving that message in the next buffer)
  4. At this point a message receive complete event is dispatched to the task.
  5. The Serial Port's event handler allocates memory for the received message and writes the new message into the receive queue.
  6. Then it cleans up the receive buffer by setting the "free buffer" bit in the buffer header. 

Sample Code and Usage

Here is the code for a typical implementation of this pattern:

Serial Port

Known Uses

This pattern is used to design serial port interfaces with intelligent devices which are capable of performing DMA operations to manage buffers in the memory. These devices only interrupt the processor when complete messages have been received/transmitted.

Related Patterns