# A C-Based Framework for Low-Cost Real-Time Embedded Systems
## 4 Core Framework Components
### Data Exchange Mechanism  
The control task activates whenever there is an incoming event by waiting to receive a message from Q through data flow 1. Upon receiving a cyclic event, it implements most of the control cycle, except for the control algorithm, which the control function is still responsible for. Figure 3 shows the structure of the control cycle in more detail. Each cycle consists of three mandatory phases, followed by one optional phase:  
1. During the computation phase (C phase), the control task executes the time-triggered portion of the real-time algorithms by invoking the control function. The control function operates on input data and stores results into the output data held in abstract process image (PI) variables through the bi-directional data flow 2 shown in Figure 2. These variables are stored in their own memory area, denoted as [A] in the figure and are mapped to local or remote I/O points by the system configuration to be discussed in Section 6. 2. In the output phase (O phase), the control task commits the value of all output PI variables to the corresponding output points, possibly going through a data format conversion stage specified by the configuration system. The conversion is transparent to the control function and adapts the CPU-dependent representation of memory-resident data to the format required by each specific I/O device. All I/O activities are performed by a real-time communication module, assisted by a set of real-time I/O libraries that provide I/O functions specific to each communication medium Rfw supports. 3. Finally, in the input phase (I phase), the control task queries input points and stores their values into the corresponding input PI variables, which are ready to be used during the C phase of the next cycle. In this case, data format conversions are performed as required, and I/O activities are coordinated by the communication module. During both the O and I phases, input and output data are transferred through data flow 1, and the control function is kept inactive to avoid race conditions. The reasons for using this unconventional order, instead of the usual I, C, O, are clarified later in the text. After completing these three phases, the control task starts the S phase, in which it checks whether there are sporadic events waiting in the queue. If there is none, the control task remains idle until a sporadic event arrives or the next cycle is due to begin, as signaled by the arrival of a new cyclic event. If some sporadic events are pending, the control task dispatches them, one at a time, to the control function. Therefore, as also shown in Figure 3, the S phase is composed of zero or more sporadic event handling intervals \(S_{1},\ldots,S_{i}\) interleaved with idle time. Although the handling of further sporadic events stops as soon as a cyclic event is inserted into Q--thanks to the prioritization mechanism discussed previously--the processing of the current sporadic event is allowed to complete before starting the next cycle. This event serialization approach simplifies synchronization within the control task  
Figure 3: Real-time control cycle and sporadic event handling. and control function--because, in fact, there is no internal concurrency. However, it may introduce jitter at control cycle boundaries. Namely, the processing of the last sporadic event \(S_{i}\) in the S phase of cycle \(k\) may delay the beginning of cycle \(k+1\). However, the amount of jitter is upper-bounded and, denoting the processing time of sporadic event \(i\) as \(S_{i}\), the upper bound is \(\max_{i}(S_{i})\), regardless of the number of sporadic events in Q. For sporadic events generated by UI and proxy tasks, the internal structure of the corresponding \(S_{i}\) consists only of activities internal to the controller card performed by the control function. On the contrary, the handling of events coming from a real-time network involves I/O operations to properly refresh PI variables.