TRON Forum

3. μT-Kernel 3.0 programming

3-1. Programs for creating and executing tasks

The basic unit of a program that runs on μT-Kernel is a task. A user program consists of one or more tasks. Let us create a program that creates and executes a task.

Program 3.1: Create and execute a task

Let us create and execute a single task. The task terminates with the message "Start Task-A" on the serial communication output for debugging.

The following program is created.

API used in this program

The task control API used in this program is explained below.

Create Task

A task is created by the task creation API tk_cre_tsk.

The API specification for tk_cre_tsk is described below.

Task creation API
Function definition ID tk_cre_tsk( CONST T_CTSK *pk_ctsk );
Argument T_CTSK *pk_ctsk Task creation information
Return value Task ID number, or error code
Feature description Creates a task based on the task creation information specified in *pk_ctsk.
Returns the ID number of the created task as the return value.

Task creation information T_CTSK is a structure that contains information for creating a task. Pass a pointer pointing to this structure as the argument of tk_cre_tsk.

The members of the T_CTSK structure are set as needed, but the following members must be set.

Task attribute ‘tskatr’

Specify the task attribute to be created (see "2-2 Basics of Tasks").

Attributes for tasks of typical applications are TA_HLNG and TA_RNG3. TA_HLNG attribute means that the task is written in C. TA_RNG3 means that the task is a general user program.

μT-Kernel 3.0, which does not use memory protection by MMU, does not change the actual operation even if the different protection level is specified. However, for compatibility with systems with memory protection, it is recommended to set the protection level according to the intended use of the task.

Task start address 'task'

This is the program execution start address of the task. If the task has TA_HLNG attribute (written in C language), it specifies a pointer to the task execution function.

Task execution functions are in the following signature.

void task( INT stacd, void *exinf);

The first argument, 'stacd', is specified by the task startup API tk_sta_tsk, described below. The second argument, 'exinf', can be specified during task creation (not used in this article).

Task startup priority 'itskpri'

Initial value of the task priority. When a task starts execution, it operates at the priority specified here.

Task priority should generally be determined by system design that takes the holistic view of the user program. In the program described in this article, the priority level is 10.

Stack size stksz

The size of the stack used by the task. Memory of this size is allocated as a stack during task creation. If the stack exceeds the specified size during the execution of a task, there is a risk of a serious error.

The stack size should be calculated from the stack requirements of the code of that task and should be larger than that. In the program described in this article, the stack size of the application task is set to 1,024 bytes, which is large enough.

tk_cre_tsk returns the ID number of the newly created task as the return value. Thereafter, this ID number is used to perform operations on the created tasks.

Execute tasks

The created task is in the DORMANT state and not yet executed. Call the task startup API tk_sta_tsk to make the task ready for execution.

The API specification for tk_sta_tsk is described below.

Task start API
Function definition ER tk_sta_tsk( ID tskid, INT stacd );
Argument Argument ID tskid Task ID number
INT stacd Task start code
Return value Error code
Feature description Make the task specified by tskid ready for execution from the DORMANT state.
The value set in stacd is passed to an argument of the task's execution function.
Wait task wakeup (Suspend tasks)

To temporarily stop task operation, call the task wake-up waiting API tk_slp_tsk to put the task in the wake-up waiting state. A task in the wake-up waiting state stops its operation until it is woken up by another task.

The API specification for tk_slp_tsk is described below.

Wait task wakeup API
Function definition ER tk_slp_tsk( TMO tmout );
Argument TMO tmout Timeout duration (unit: milliseconds)
Return value Error code
Feature description Put the task that called the API in the wake-up waiting state.
The waiting state is released when another task wakes it up. If no other task wakes it up within the time specified in tmout, the waiting state is released, and a timeout error is returned.
Exit task

You must call the Exit Task API tk_ext_tsk to terminate a task. Note that if a task's execution function reaches the end and returns without terminating the task, the task will run out of control.

The API specification for tk_ext_tsk is described below.

Exit Task API
Function definition void tk_ext_tsk( void );
Argument None
Return value None
Feature description Terminates the task that called the API.

Program example

The following is a list of programs in "Program 3-1: Create and execute a task."

List 3-1 Create and execute a task

The contents of the program in List 3-1 are described step-by-step.

  1. Task A creation information and related data
    The prototype declaration of the execution function for Task A, ‘task_a’, and the variable 'tskid_a' for storing the ID number of Task A, and the variable 'ctsk_a' for Task A creation information are specified here.
  2. Execution function of task A
    Function 'task_a' is the execution function of task A. In this program, after outputting the message "Start Task-A" with the debugging output function 'tm_printf', Task A terminates itself by calling the Exit task API tk_ext_tsk.
  3. usermain function
    The 'usermain' function calls the task creation API tk_cre_tsk to create Task A. The task startup API tk_sta_tsk is then called to start execution of the created Task A.
    Finally, the 'usermain' function executes the task wake-up waiting API tk_slp_tsk and enters the wake-up waiting state. This is to prevent the termination of the 'usermain' function from terminating the entire μT-Kernel system.
    The initial task executing the 'usermain' function has the highest priority (priority 1), so task A with priority 10 is not executed while the 'usermain' function is running.
    When the initial task enters the waiting state by a call to tk_slp_tsk, task A is executed.

3-2. Multitask program example

Let us create a multitasking program that creates multiple tasks and executes them simultaneously.

Here, we will control an LED and a switch simultaneously using separate tasks. You can see that tasks that were included in separate programs in the original book run simultaneously in a single program.

No specific hardware is assumed, and the program is written assuming there are LED and switch control functions.

The following program is created.

Program-2.2: Execute multitasking

Create and execute two tasks and verify that the features of the two tasks work simultaneously. The two tasks operate as follows

  1. Switch control task
    This monitors button switch and displays "SW-ON" in debug output when the button switch is pressed.
  2. LED control task
    This blinks the LEDs at 0.5 second intervals.

API used in this program

The μT-Kernel API to be used is the same as in "3-1 Create and execute a task."

We assume the following functions exist to control an LED and a switch. To run the actual program, these functions must be created for the hardware on which the program will be executed.

LED control function
LED control function
Function definition void led_ctl( UINT on_off );
Argument The state of UINT on_off LED (0 to turn off light, otherwise light on)
Return value None
Feature description LED is controlled according to the on_off argument.
Switch control function
Switch control function
Function definition UW get_sw( void );
Argument None
Return value Switch state (Off at 0, otherwise on)
Feature description Returns the switch state in the return value.

Program example

The following is a list of programs in "Program 3-2: Execute multitasking."

List 3-2 Execute multitasking

The contents of the program in List 3-2 are described step-by-step.

  1. Creation information of switch control task and related data
    The prototype declaration of the switch control task execution function, 'task_sw', the variable 'tskid_sw' for storing the ID number of the switch control task, and the variable 'ctsk_sw' for switch control task generation information are specified.
  2. Creation information of LED control task and related data
    The prototype declaration of the LED control task execution function, 'task_led', the variable 'tskid_led' for storing the ID number of the LED control task, and the variable 'ctsk_led' for LED control task creation information are specified.
  3. Function body of the switch control task
    Function 'task_sw' is the function body of the switch control task.
    Although Exit Task API tk_ext_tsk is placed at the end of the function, this API is never executed because this function does not get out of the infinite loop of the while statement.
  4. Function body of the LED control task
    Function 'task_led' is the function body of the LED control task.
    Although Exit Task API tk_ext_tsk is placed at the end of the function, this API is never executed because this function does not get out of the infinite loop of the while statement.
  5. usermain function
    The 'usermain' function creates and executes the switch control task and LED control task.
    Next, the 'usermain' function executes the task wake-up waiting API tk_slp_tsk and enters the wake-up waiting state. This is to prevent the termination of the 'usermain' function from terminating the entire μT-Kernel system. When the initial task enters the waiting state by the call to tk_slp_tsk, the LED control task and the switch control task begin execution.

3-3. Program that uses an event flag

Between tasks executing in multitasking, event flags can be used to notify the occurrence of events and synchronize their operations (see "2-4 Event Flags").

In "3-2. Multitasking," individual tasks control an LED and a switch. This time, let us use an event flag to notify the event that a button switch is pressed between the tasks and synchronize their operations (assuming there are LED and switch control functions as in "3-2. Multitasking").

The following program is created.

Program-3-3: Synchronization by an event flag

A program that lights an LED for three seconds when a button switch is pressed is realized by synchronizing the two tasks with an event flag.

The two tasks operate as follows

  1. Switch control task
    This monitors a button switch and sets the event flag when the button switch is pressed.
  2. LED control task
    This waits for the event flag to be set and turn on the LED for three seconds.

API used in this program

The API for event flags used in this program is described below. The task control API is described in "3-1 Create and execute a task."

Create event flag

Event flags are created by the event flag creation API tk_cre_flg.

The API specification for tk_cre_flg is described below.

Event flag creation API
Function definition ID tk_cre_flg( CONST T_CFLG *pk_cflg );
Argument T_CFLG *pk_cflg Event flag creation information
Return value Event flag ID number, or error code
Feature description Creates event flags based on the event flag creation information specified in *pk_cflg.
Returns the ID number of the created event flag as the return value.

Event flag creation information T_CFLG is a structure that contains information for creating an event flag. A pointer pointing to this structure is passed as the argument of tk_cre_tsk.

The members of the T_CFLG structure are set as needed, but the following members must be set.

Event flag attribute flgatr

Attributes represent the nature of the event flag. The main attributes are listed in Table 3-3-1.

Table 3-3-1 Main attributes of event flag
Attribute name Explanation
TA_TFIFO Waiting tasks are ordered on a first-come, first-served basis (FIFO order) (※1)
TA_TPRI Waiting tasks are ordered by priority (※1)
TA_WSGL Do not allow multiple tasks to wait (※2)
TA_WMUL Allow multiple tasks to wait (※2)

(※1)One of either TA_TFIFO or TA_TPRI must be specified.

(※2)One of either TA_WSGL or TA_WMUL must be specified.

Event flag initial value 'iflgptn'

The initial value when an event flag is created. If any event has not occurred yet, the initial value is set to 0.

Set event flag

An event flag is set by using the event flag set API tk_set_flg.

The API specification for tk_set_flg is described below.

Set event flag API
Function definition ER tk_set_flg( ID flgid, UINT flgptn );
Argument ID flgid Event flag ID number
UINT flgptn Bit pattern to be set
Return value Error code
Feature description Sets the bit pattern indicated by flgptn to the event flag specified by flgid. Specifically, the value of flgptn is taken as the logical OR of the value of the event flag at that time.
Releases a task for which the wait condition is satisfied from the task(s) waiting for the flag.

Wait for event flag

The event flag wait API tk_wai_flg is used to wait for the event flag to be set.

The API specification of tk_wai_flg is described below.

Event flag waiting API
Function definition ER tk_wai_flg( ID flgid, UINT waiptn, UINT wfmode, UINT *p_flgptn, TMO tmout );
Argument ID flgid Event flag ID number
UINT waiptn Wait bit pattern
UINT wfmode Wait mode
UINT *p_flgptn Bit pattern of releasing
TMO tmout Timeout duration (unit: milliseconds)
Return value Error code
Feature description Waits for the event flag specified by flgid to be set with the bit pattern specified by waiptn and the mode specified by wfmode.
When the condition is satisfied and the wait is released, the bit pattern at the time of release is returned to *p_flgptn. If the condition is not satisfied within the time specified by tmout, the waiting state is released, and a timeout error is returned.

Waiting modes for event flags are shown in Table 3-3-2.

Table 3-3-2 Wait Event Flag mode
Attribute name Explanation
TWF_ANDW Wait until all specified bits are set (※)
TWF_ORW Wait until one of the specified bits is set (※)
TWF_CLR Clear all bits when the condition is satisfied.
TWF_BITCLR Clear the bits where the condition is satisfied.

(*) One of either TWF_ANDW or TWF_ORW must be specified.

Program example

The following is a listing of the program in "Program-3-3: Synchronization by an event flag."

List 3-3 Synchronization by an event flag

The contents of the program in List 3-3 are described step-by-step.

  1. Event flag creation information and related data
    'flgid_a' is a variable to store the ID number of the event flag.
    'cflg_a' is a variable for the event flag creation information. The event flag attributes are TA_TFIFO (waiting tasks are first-come, first-served) and TA_WMUL (multiple tasks are allowed to wait), but since only one task is waiting for the event this time, it does not affect the actual operation.
    The initial value of the event flag is 0, meaning that the event has not yet occurred.
    Since the only content of the event flag is an event notifying that a switch has been pressed, the 0-th bit of the event flag is defined as the switch-on event as follows.
    #define FLG_SW_ON (1<<0)
  2. Creation information of switch control task and related data
    Same as "Program-3.2: Multitasking"
  3. Creation information of LED control task and related data
    Same as "Program-3.2: Multitasking"
  4. Function body of the switch control task
    The function 'task_sw' polls the switch status at 0.1 second intervals, and when it detects that the switch has been pressed, it calls the event flag set API tk_set_flg to set the switch-on event.
  5. Function body of the LED control task
    The function 'task_led' first calls the event flag wait API tk_wai_flg, which enters the event wait state and suspends execution. In this case, TWF_BITCLR mode is specified so that the condition bit is cleared when the wait is released.
    When the event flag is set, the LED is turned on and then the task's delay API tk_dly_tsk is called to stop the task operation for three seconds. When three seconds elapse and the operation resumes, the LED is turned off.
    Thereafter, this operation is repeated.
  6. usermain function
    The 'usermain' function first calls the event flag creation API tk_cre_flg to create an event flag. The rest of this section is the same as "Program-3.2: Multitasking."

Return Top