Calling subroutines from interrupts is fine — with care: the main program and the interrupt can interleave calls to the same subroutine, and the subroutine's working devices, timers and accumulators become a shared scratchpad both sides scribble on. The rule in one sentence: the interrupt path's subroutines must be purpose-built, or written as re-entrant pure functions. The sections below cover the risk mechanism, the two approaches, data handshakes and a checklist.
| Shared resource | How it is trampled | Typical symptom |
|---|---|---|
| General M/D used inside the subroutine | The main's call is interrupted mid-way; the interrupt's call of the same subroutine overwrites those values; on return the logic is wrong | Occasional state corruption, near-impossible to reproduce |
| Shared timers/counters | Both sides drive the same timer; results unpredictable | Occasional timing inaccuracy |
| Accumulator / scratch variables | The interrupt rewrites a value the main is computing | Data jumps, statistics off |
| Outputs / SET targets | The interrupt path moves a mechanism's output state | Mechanism moves unexpectedly mid-way (dangerous) |
Note that on single-core PLCs "simultaneous" means interleaved: the interrupt can insert at any instruction boundary. With dedicated resources, interleaving is harmless; with shared resources, interleaving plants mines.
Give the interrupt path its own copy of the subroutine (MAIN uses CALC, the interrupt uses CALC_INT), the latter's internal devices fully dedicated (its own M/D blocks, its own timer numbers), zero sharing with the main path. The price is duplicated code; the return is absolute isolation. Use it when interrupt-side logic is non-trivial (sampling, scaling, latching) and the platform's re-entrancy story is unclear — the safest road.
Supporting discipline: the interrupt-only subroutine's device block is marked in the partition table as "interrupt path only", off-limits to ordinary subroutines.
Make the subroutine "stateless": inputs come only from parameters or designated input areas, results go only to designated output areas, and internal variables are initialised on entry before use — no reliance on leftovers from the last call. Under that style, even interleaved calls are correct provided the read and write areas do not overlap. Fits conversions, comparisons, lookups. The test: run the subroutine twice in a row — same result? Then it is pure and shareable; different (it kept state) and it is not.
| Trait | Pure function | Stateful |
|---|---|---|
| Internal variables initialised first | Yes | No (relies on leftovers) |
| Uses timers/accumulators | Never | Often |
| Run twice, same result | Yes | Maybe not |
| Shareable main/interrupt | Yes (non-overlapping areas) | No — build a dedicated copy |
For data crossing between interrupt and main, use "double buffer + flag" instead of direct sharing: the interrupt writes results to area A and sets "new data"; the main sees the flag, copies A to B, processes B, clears the flag. The interrupt writes only A, the main reads A and writes B — a single writer means no torn reads. Data flowing the other way (main → interrupt) sets the mirror up reversed. Four instructions of handshake buy out a whole class of "occasional wrong value" mysteries.
| Check | Pass criterion |
|---|---|
| Subroutine internal devices | Dedicated block, or pure-function style (initialised on entry) |
| Timer/counter numbers | Interrupt path owns an exclusive number block |
| Variables shared with main | Both directions via double buffer + flag, no direct co-writing |
| Output actions | The interrupt path drives no mechanism outputs (flags only) |
| Subroutine execution time | Counted in the interrupt budget (≤ 30% of the period) |
| Nested calls inside | Each layer checked to the same standard |
| Symptom | Cause | Fix |
|---|---|---|
| Main program statistics occasionally short | The shared subroutine's interrupt call races the accumulator area | Dedicated accumulator area + handshake |
| Timing occasionally off | A shared timer number driven from the interrupt path | Exclusive timer number block for interrupts |
| Odd state after the interrupt returns | Subroutine scratch values trampled | Pure-function style or a dedicated copy |
| Scan-time spikes | Interrupt call chain too deep | Slim the interrupt path, defer processing to main |
A subroutine has no notion of "thread safety" — it is shared code, and its safety depends entirely on whether you give it exclusive resources. The whole craft of calling subroutines from interrupts is two rules: exclusive resources (dedicated copies or pure functions) and data handshakes (double buffer + flag). With both, interleaving is safe; without, faults appear as "occasional, unreproducible".
Subroutine calls (CALL etc.) and interrupt behaviour follow the PLC's programming manual; variable access and atomicity across tasks are documented in each platform's runtime notes; interrupt size versus scan impact follows the watchdog documentation; the independence of safety functions from all this follows GB 5226.1 / IEC 60204-1.
Before writing the interrupt path, carve the "interrupt-only zone" in the partition table and source every device from it — resources carved up, interleaving is safe.