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
SimStructoverhead - 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):
- Simulink generates
mymodel.rtwintermediate representation. - TLC processes
.rtwwith system, block, and inlined S-Function target files. - Optimized C code (
mymodel.c) and support files are produced. - 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:
- Create C MEX S-Function for simulation.
- Implement
mdlRTW()to export parameters to.rtw. - Write corresponding
.tlcfile (same base name). - Place
.tlcin 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 parametersStart: Hardware initializationOutputs: Main I/O logicTerminate: Cleanup at termination
🏗 Chapter 6: Complete Development Workflow #
- Write C MEX S-Function (
pcl726fordi.c) - Compile MEX for Simulink simulation
- Create
pcl726fordi.tlc - Add S-Function block to Simulink model
- Run RTW build → generates fully inlined code
- 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