Back to Fprime

Drv::LinuxUartDriver

Drv/LinuxUartDriver/docs/sdd.md

4.2.27.8 KB
Original Source

Drv::LinuxUartDriver

1. Introduction

The LinuxUartDriver component provides a Linux-specific implementation of a UART (Universal Asynchronous Receiver-Transmitter) serial communication driver. It implements the byte stream driver model interface (see Drv.ByteStreamDriver) to enable serial communication with external devices through UART ports on Linux systems.

The component wraps Linux termios API functionality to provide configurable serial communication with support for various baud rates, flow control options, and parity settings. It implements bidirectional communication using a dedicated receive thread and synchronous send operations.

For more information on the ByteStreamDriverModel see: Drv::ByteStreamDriverModel.

2. Requirements

NameDescriptionValidation
LINUX-UART-COMP-001The LinuxUartDriver component shall implement the Drv.ByteStreamDriver interfaceinspection
LINUX-UART-COMP-002The LinuxUartDriver component shall provide configurable baud rates from 9600 to 4MHzinspection
LINUX-UART-COMP-003The LinuxUartDriver component shall provide configurable flow control (none/hardware)inspection
LINUX-UART-COMP-004The LinuxUartDriver component shall provide configurable parity (none/odd/even)inspection
LINUX-UART-COMP-005The LinuxUartDriver component shall provide a dedicated read thread for receiving datainspection
LINUX-UART-COMP-006The LinuxUartDriver component shall report telemetry for bytes sent and receivedinspection
LINUX-UART-COMP-007The LinuxUartDriver component shall handle UART errors and report them via eventsinspection
LINUX-UART-COMP-008The LinuxUartDriver component shall support buffer allocation for receive operationsinspection

3. Design

The LinuxUartDriver component implements the design specified by the Drv.ByteStreamDriver interface.

3.1 Architecture

The component consists of the following key elements:

  • UART Configuration: Handles device opening, baud rate, flow control, and parity settings using Linux termios API
  • Send Handler: Synchronous transmission of data via the send port (guarded input port)
  • Receive Thread: Asynchronous reception of data via a dedicated thread that calls the recv output port
  • Buffer Management: Integration with F´ buffer allocation system for memory management
  • Telemetry Reporting: Tracks and reports bytes sent and received statistics
  • Error Handling: Comprehensive error detection and event reporting

3.2 Send Operation

When data is sent via the send input port:

  1. The component validates the file descriptor and buffer
  2. Data is written to the UART device using the Linux write() system call
  3. Bytes sent counter is updated for telemetry
  4. Status is returned indicating success or failure

3.3 Receive Operation

The receive operation runs in a separate thread:

  1. A buffer is allocated from the buffer manager
  2. The thread blocks on read() waiting for incoming data
  3. Received data is packaged in the buffer and sent via recv output port
  4. Bytes received counter is updated for telemetry
  5. Errors are logged and reported via events

3.4 Threading Model

The component uses a single dedicated thread for receive operations (serialReadTaskEntry). This thread:

  • Runs continuously until quitReadThread() is called
  • Allocates buffers for each receive operation
  • Handles timeouts and errors gracefully
  • Can be started with configurable priority and stack size

4. Usage

The LinuxUartDriver must be configured with device parameters before use. The typical usage pattern is:

  1. Open Device: Configure the UART device with desired parameters
  2. Start Receive Thread: Begin the receive thread for incoming data
  3. Send/Receive Data: Use the ByteStreamDriverModel ports for communication
  4. Shutdown: Stop the receive thread and close the device

4.1 Configuration Example

The LinuxUartDriver should be instantiated in the FPP topology and configured using separate functions following F´ patterns:

cpp
// Configuration function - called during topology setup
void configureTopology() {
    // Open UART device with configuration
    bool success = uart.open("/dev/ttyUSB0",                    // Device path
                             Drv::LinuxUartDriver::BAUD_115K,   // 115200 baud rate
                             Drv::LinuxUartDriver::NO_FLOW,     // No flow control
                             Drv::LinuxUartDriver::PARITY_NONE, // No parity
                             1024);                             // Buffer size
    if (!success) {
        // Handle configuration error
    }
    ...
}

// Startup function - called when starting tasks
void setupTopology() {
    // Start receive thread
    uart.start(UART_PRIORITY,           // Thread priority
               32 * 1024,               // Thread stack size
               Os::Task::TASK_DEFAULT); // Thread CPU affinity mask
}

// Shutdown function - called during teardown
void teardownTopology() {
    uart.quitReadThread();
    uart.join();
}

4.2 Integration with Rate Groups

The component includes a run input port for telemetry reporting that should be connected to a rate group in the FPP topology:

fpp
# In topology.fpp connections section
connections RateGroups {
  # Connect UART driver to rate group for telemetry
  rateGroup1Comp.RateGroupMemberOut[N] -> uart.run
}

5. Configuration

5.1 Device Parameters

ParameterTypeDescriptionValid Values
deviceconst char*Path to UART deviceLinux device path (e.g., "/dev/ttyUSB0")
baudDrv::LinuxUartDriver::UartBaudRateCommunication baud rateSee baud rate enumeration
fcDrv::LinuxUartDriver::UartFlowControlFlow control settingNO_FLOW, HW_FLOW
parityDrv::LinuxUartDriver::UartParityParity settingPARITY_NONE, PARITY_ODD, PARITY_EVEN
allocationSizeFwSizeTypeReceive buffer sizePositive integer (bytes)

5.2 Baud Rate Options

The component supports the following baud rates:

EnumerationNumeric ValueAvailability
BAUD_96009600All platforms
BAUD_1920019200All platforms
BAUD_3840038400All platforms
BAUD_5760057600All platforms
BAUD_115K115200All platforms
BAUD_230K230400All platforms
BAUD_460K460800Linux only
BAUD_921K921600Linux only
BAUD_1000K1000000Linux only
BAUD_1152K1152000Linux only
BAUD_1500K1500000Linux only
BAUD_2000K2000000Linux only
BAUD_2500K2500000Linux only (if supported)
BAUD_3000K3000000Linux only (if supported)
BAUD_3500K3500000Linux only (if supported)
BAUD_4000K4000000Linux only (if supported)

5.3 Thread Configuration

The receive thread can be configured with:

ParameterTypeDefaultDescription
priorityFwTaskPriorityTypeTASK_PRIORITY_DEFAULTThread priority
stackSizeOs::Task::ParamTypeTASK_DEFAULTThread stack size
cpuAffinityOs::Task::ParamTypeTASK_DEFAULTCPU affinity mask

5.4 Events and Telemetry

The component generates the following events:

  • OpenError: UART device open failures
  • ConfigError: UART configuration failures
  • WriteError: Data transmission errors
  • ReadError: Data reception errors
  • PortOpened: Successful device configuration
  • NoBuffers: Buffer allocation failures
  • BufferTooSmall: Insufficient buffer size

The component reports the following telemetry:

  • BytesSent: Total bytes transmitted
  • BytesRecv: Total bytes received