OSEK OS in Practice
Last time out, we took a look at the theory behind the OSEK/VDX OS, the backbone of the Autosar OS. Today we find how we can use tasks, events and ISR's.
Before we begin, this article is a follow-up to our last article about the OSEK/VDX OS, where we explain the theory behind it. Today we'll focus on how these concepts can be used in practice. So, if you haven't checked it out, please read the first part, OSEK OS Overview (opens in a new tab)), as it will give you the basis you need for this one.
As we discussed in the first part, OIL (Osek/VDX Implementation Language) is the configuration language for all the resources you create within your OS. Even for Autosar, as the version 3 of the OIL standard is used by it, at least in older versions (Autosar partially parted ways with OIL to ARXML, but there is still some support from the Autosar tools for OIL files). Then, these files will be used to generate code. In the end of this article, you will be a pro when it comes to it. Well, not quite, but you'll be in a good spot.
So, let's start with tasks. How can we declare a task in an OIL file? Easy:
Task myTask {
AUTOSTART = TRUE {
APPMODE = myAppMode;
};
PRIORITY = 1;
ACTIVATION = 1;
SCHEDULE = NON;
}
Does not look that complicated, does it? Let's see what each parameter means:
- Autostart - defines if the task starts automatically, or through an activation.
- Priority - priority of the task. It's defined statically, so, you cannot change it in runtime.
- Activation - number of task activations. 1 if you have an extended task or an ISR.
- Schedule - Scheduling policy of the task. There are two options: FULL, which means the task is preemptable, and NON, meaning the task cannot be preempted.
After generating code, you are going to find something like this:
TASK (myTask) {
/*task functionality goes here, along with task API's*/
}
Now, let's look at some task-related API's. There are more, but I trust you'll look into the references to further improve your knowledge, if you are curious. These will be more useful than learning OIL, as you are going to find a certain variation of it on the Autosar OS:
-
StatusType GetTaskID(TaskRefType TaskID) - Get the ID of the currently running task. StatusType is something you'll find often in the Autosar world. In Autosar, the best practice adopted is to return the API result (E_OK or E_NOT_OK, and some extra codes, such as if the task is not valid), to verify if the API failed or succeeded. The output parameters are always returned as a reference.
-
StatusType GetTaskState(TaskType TaskID, TaskStateRefType State) - Returns the state of the task (RUNNING, READY, SUSPENDED and WAITING).
-
StatusType Schedule(void) - Used by non-preemptive tasks to call a scheduling point, to give the opportunity for other, higher priotity tasks to run in its place. In case there are only lower priority tasks running, the task keeps executing. You cannot call a scheduling point if the task has disabled interrupts (E_OS_CALLEVEL) and if a resource is held (E_OS_RESOURCE).
-
StatusType ActivateTask(TaskType TaskID) - Activates a task. If there are already too many activations for this task (E_OS_LIMIT), the operation will not go through.
-
StatusType TerminateTask(void) - Places the current task in SUSPENDED state. This operation cannot go through if resources are still locked (E_OS_RESOURCE).
-
StatusType ChainTask(TaskType TaskID) - Terminates the current task and starts the specified task. It ensures the next task runs as early as possible. It cannot be called if resources are still locked (E_OS_RESOURCE).
Now, let's take a look at events. We already explained that events are a consumable for extended tasks, that can only be consumed by 1 task. Since they are associated to an extended task, your task that depends on the event also needs to reference it. The OIL file will look something like this:
Task myTask {
// everything else related to the task
EVENT = myEvent;
}
EVENT myEvent {
MASK = 1;
}
As you can see in the example above, the event gets a mask, and is also associated with the consumer task. Regarding the API's available for an event, you have the following at your disposal:
-
StatusType SetEvent(TaskType TaskID, EventMaskType Mask) - Sets an event of the specified TaskID according to the mask. The call can fail if referenced task is suspended (E_OS_STATE) or if the referenced task is not extended (E_OS_TASK). It's a non-blocking call that can be used within tasks or ISR2's.
-
StatusType ClearEvent(EventMaskType Mask) - clear the task events specified by the mask.
-
StatusType GetEvent(TaskType TaskID, EventMaskRefType Event) - Gets the current task event bits (including those not included in the mask).
-
StatusType WaitEvent(EventMaskType Mask) - Wait for an event bits specified by the mask. It will put the task into the waiting state until the event is set. Since the task will be put into the waiting state, all resources used by it must be released, or an error will occur (E_OS_RESOURCE).
Regarding ISR's, you can check how they are described in OIL below. In it, you'll find the category, priority, stack size and its source, which is linked to the interrupt vector of the microcontroller.
ISR myCat2ISR {
CATEGORY = 2;
PRIORITY = 5;
STACKSIZE = 1024;
SOURCE = SPI1_RX;
}
The generated C code will look like this:
ISR (myCat2ISR) {
/*ISR2 functionality goes here, along with authorized API's*/
}
For the first part, we'll stop here, just to keep this article as a digestable piece, instead of a lecture. Next time out, we'll dive into counters, alarms and hooks. In the meantime, we encourage you to play around TrampolineRTOS (opens in a new tab) and the RTA-OSEK manual, which will enrich your learning experience even further. Stay tuned for the next part!
Author: Micael Coutinho (opens in a new tab)
References:
- Specification of Operating System - Autosar Specification (opens in a new tab)
- RTA-OSEK Reference Guide - ETAS (opens in a new tab)
© AutosarToday —@LinkedIn