Pedram Agand
← Projects
Project

MATLAB S-Function for Real-Time Kernel I/O

matlabsimulinkreal-timeembedded-systemscontrol-systems

Custom MATLAB S-function enabling real-time communication with hardware kernel peripherals — high-priority I/O for hardware-in-the-loop simulation and control.

MATLAB S-functions are the standard extension point for adding custom blocks to Simulink simulations. This implementation goes further: it accesses kernel peripherals at the OS level, enabling high-priority I/O that is necessary for hardware-in-the-loop (HIL) testing where timing matters.

What It Enables

The S-function wraps low-level system calls to give Simulink blocks direct access to hardware I/O channels — serial ports, GPIO, DAQ cards — without going through MATLAB's standard toolbox layer. The main advantage is timing: by running at kernel priority rather than userspace priority, the communication loop is less susceptible to jitter from OS scheduling.

This matters in control systems research where the simulation clock and the physical hardware need to stay synchronized within milliseconds. A userspace serial port write can be delayed 10–100ms by the OS scheduler; a kernel-priority call is predictable at sub-millisecond resolution.

Use Case

Built during robotics and control system research at SFU, where Simulink models needed to communicate with embedded controllers in real-time test rigs. The S-function allowed us to close control loops between the MATLAB simulation and physical hardware without building a separate embedded interface layer.

Structure

The S-function implements the standard four callback methods (mdlInitializeSizes, mdlStart, mdlOutputs, mdlTerminate) plus the kernel peripheral access through the appropriate platform-specific calls. Works on Linux with real-time kernel patches applied.

I write about this kind of work — reliability, uncertainty, building things that work in production. One email per month.