Skip to main content

Inlined RtLinux Drivers with S-Functions and TLC: Full Guide

·573 words·3 mins
RTLinux S-Functions TLC Real-Time Systems Embedded Coder EPIC Drivers MATLAB RTW Hardware Integration
Table of Contents

Inlined RtLinux Drivers with S-Functions and TLC: Full Guide

This technical series presents a complete methodology for developing fully inlined hardware drivers under RtLinux using MATLAB Real-Time Workshop (RTW) S-Functions and Target Language Compiler (TLC). The approach reduces runtime overhead, improves determinism, and seamlessly integrates with real-time applications.


⚡ Chapter 1: Introduction – The Need for Inlined Drivers
#

In real-time environments like RtLinux, precise timing is essential. Standard non-inlined S-Functions in RTW often fail to meet stringent performance requirements when interacting with hardware I/O boards.

Solution Overview:

  • Develop hardware drivers (e.g., PCL-726) using S-Functions with TLC.
  • Achieve fully inlined drivers inside RTW-generated code.

Benefits:

  • Minimized memory footprint
  • Elimination of extra function calls and SimStruct overhead
  • Enhanced real-time performance
  • Single driver usable in both simulation and real-time execution

🛠 Chapter 2: Fundamentals of S-Functions and TLC
#

S-Functions are custom dynamic blocks in Simulink. This guide focuses on Fully Inlined S-Functions:

  • Simulation: Implemented as C MEX S-Functions
  • Code Generation: Re-implemented using TLC for inlined code
Feature Non-Inlined S-Function Inlined S-Function (TLC)
Runtime Overhead High Minimal
Memory Consumption Large Low
Real-Time Suitability Limited Excellent
Hardware Interaction Inefficient Highly efficient

TLC (Target Language Compiler) enables inlining, optimization, and embedding of hardware-specific code directly into the generated C output.


🔧 Chapter 3: TLC Implementation Mechanism
#

RTW Code Generation Flow (mymodel example):

  1. Simulink generates mymodel.rtw intermediate representation.
  2. TLC processes .rtw with system, block, and inlined S-Function target files.
  3. Optimized C code (mymodel.c) and support files are produced.
  4. Output resides in mymodel_rtw_rtw/.

Note: TLC is only required for inlined S-Functions, replacing calls with direct inline code.


💻 Chapter 4: Implementing Inlined S-Functions
#

Steps:

  1. Create C MEX S-Function for simulation.
  2. Implement mdlRTW() to export parameters to .rtw.
  3. Write corresponding .tlc file (same base name).
  4. Place .tlc in the TLC search path.

Example: mdlRTW() in pcl726fordi.c:

#define MDL_RTW
#if defined(MDL_RTW) && ( defined(MATLAB_MEX_FILE) || defined(NRT))
static void mdlRTW(SimStruct *S)
{
    char _T addrStr[15];
    mxGetString(_PARAM_1(S), addrStr, sizeof(addrStr));
    
    if (!ssWriteRTWParamSettings(S, 1,
            SSWRITE_VALUE_QSTR, "P1", addrStr, 10))
    {
        return; /* Error reported by Simulink */
    }
}
#endif

This routine saves parameters like board addresses for TLC to use in generated code.


⚙️ Chapter 5: Detailed Analysis of pcl726fordi.tlc
#

Hardware-specific code (e.g., #include <asm/io.h>, register access) resides solely in the TLC file.

TLC Structure:

%function BlockTypeSetup(block, system) void
  /* Parameter configuration */
%endfunction

%function Start(block, system) Output
  /* Initialization code */
%endfunction

%function Outputs(block, system) Output
  int _T flag = 0;
  %assign u = LibBlockInputSignal(0, "", "", 0)
  %assign y = LibBlockOutputSignal(0, "", "", 0)
  
  outb(%<u> >> 8, %<base> + 0);
  outb(%<u> & 0xff, %<base> + 1);
%endfunction

%function Terminate(block, system) Output
  /* Cleanup */
%endfunction

Workflow:

  • BlockTypeSetup: Configure parameters
  • Start: Hardware initialization
  • Outputs: Main I/O logic
  • Terminate: Cleanup at termination

🏗 Chapter 6: Complete Development Workflow
#

  1. Write C MEX S-Function (pcl726fordi.c)
  2. Compile MEX for Simulink simulation
  3. Create pcl726fordi.tlc
  4. Add S-Function block to Simulink model
  5. Run RTW build → generates fully inlined code
  6. Compile and execute on RtLinux

Advantages:

  • No extra system calls during real-time execution
  • Lower latency, improved determinism
  • Reduced code footprint

✅ Chapter 7: Conclusion and Recommendations
#

By combining S-Functions with TLC, developers can produce fully inlined, high-performance drivers compatible with RTW real-time applications on RtLinux.

Modern Considerations (2026):

  • Embedded Coder offers similar functionality
  • Consider upgrading to modern real-time targets (e.g., Speedgoat, Linux RT PREEMPT)
  • TLC remains vital for advanced code customization

References:

  • System Simulation Technology
  • Linux Device Drivers (Alessandro Rubini)
  • MATLAB & C Mixed Programming resources

Related

MUAV Airborne Computer Design with RTLinux and Sensor Fusion
·662 words·4 mins
RTLinux MUAV Embedded Systems Sensor Fusion Flight Control Real-Time Systems UAV Autonomous Systems
RTOS for Marine Observation: VxWorks, Antelope, and RTLinux
·700 words·4 mins
RTOS Marine Observation VxWorks RTLinux Antelope Embedded Systems Real-Time Systems Ocean Technology
High-Accuracy Angular Position Measurement with RTLinux
·615 words·3 mins
RTLinux Embedded Systems Angular Measurement Sensors Real-Time Systems Resolver Inductosyn Signal Processing