Everything

CHAPTER 7 TIME MANAGEMENT FUNCTIONS


This chapter describes the time management functions performed by the RI78V4.
7.1 Outline
The time management functions of the RI78V4 include Delayed Wakeup, Timeout, and Cyclic Handlers that use timer interrupts created as fixed intervals, as means for realizing time-dependent processing.
Note The RI78V4 does not execute initialization of hardware that creates timer interrupts (clock controller, etc.). This initialization processing must therefore be coded by the user in the Boot Processing or Initialization Routine.
7.2 Timer Handler
The timer handler is a dedicated time control processing routine that consists of the processing required to realize delayed wakeup of tasks, timeout during the WAITING state, and cyclic handler activation, and is called from the interrupt handler that is activated upon output of a timer interrupt.
Note The timer handler is part of the functions provided by the RI78V4. The user therefore need not code the processing contents of the timer handler.
7.2.1 Define timer handler
Timer handler registration is registerd by CF78V4 based on the clock timer interrupt source in system configuration file. So it is not necessory to discribe the timer handler by user.
7.3 Delayed Wakeup
Delayed wakeup the operation that makes the invoking task transit from the RUNNING state to the WAITING state during the interval until a given length of time has elapsed, and makes that task move from the WAITING state to the READY state once the given length of time has elapsed.
Delayed wakeup is implemented by issuing the following service call from the processing program.
Table 7-1 Delayed Wakeup
Service Call
Function
Delay task.

7.4 Timeout
Timeout is the operation that makes the target task move from the RUNNING state to the WAITING state during the interval until a given length of time has elapsed if the required condition issued from a task is not immediately satisfied, and makes that task move from the WAITING state to the READY state regardless of whether the required condition is satisfied once the given length of time has elapsed.
A timeout is implemented by issuing the following service call from the processing program.
Table 7-2 Timeout
Service Call
Function
Put task to sleep.
Acquire semaphore resource.
Wait for eventflag.
Send to data queue.
Receive from data queue.
Receive from mailbox.
Acquire fixed-sized memory block.

7.5 Cyclic Handlers
The cyclic handler is a routine dedicated to cycle processing that is activated periodically at a constant interval (activation cycle), and is called from the Timer Handler.
The RI78V4 handles the cyclic handler as a "non-task (module independent from tasks)". Therefore, even if a task with the highest priority in the system is being executed, the processing is suspended when a specified activation cycle has come, and the control is passed to the cyclic handler.
7.5.1 Create cyclic handler
In the RI78V4, the method of creating a cyclic handler is limited to "static creation by the Kernel Initialization Module".
Cyclic handlers therefore cannot be created dynamically using a method such as issuing a service call from a processing program.
- Static create
Static cyclic handler creation is realized by defining Cyclic handler information in the system configuration file.
The RI78V4 executes cyclic handler creation processing based on data stored in information files, using the Kernel Initialization Module, and handles the created cyclic handlers as management targets.
7.5.2 Delete cyclic handler
In the RI78V4, cyclic handlers created statically by the Kernel Initialization Module cannot be deleted dynamically using a method such as issuing a service call from a processing program.
7.5.3 Basic form of cyclic handlers
Write cyclic handlers using void type functions that do not have arguments (function: any).
The following shows the basic form of cyclic handlers.
[ C Language ]
 #include    <kernel.h>          /*Standard header file definition*/
 #include    <kernel_id.h>       /*System information header file definition*/
 
 void
 func_cychdr ( void )
 {
     /* ............ */          /*Main processing*/
 
     return;                     /*Terminate cyclic handler*/
 }

[ Assembly Language ]
 $INCLUDE    (kernel.inc)        ;Standard header file definition
 $INCLUDE    (kernel_id.inc)     ;System information header file definition
 
     .PUBLIC  _func_cychdr
     .SECTION .text, TEXT
 _func_cychdr:
     ; ............              ;Main Processing
 
     RET                         ;Terminate cyclic handler

7.5.4 Internal processing of cyclic handler
The RI78V4 handles the cyclic handler as a "non-task".
Moreover, the RI78V4 executes "original pre-processing" when passing control to the cyclic handler, as well as "original post-processing" when regaining control from the cyclic handler.
Therefore, note the following points when coding cyclic handlers.
- Coding method
Code cyclic handlers using C or assembly language in the format shown in "7.5.3 Basic form of cyclic handlers".
- Stack switching
The RI78V4 executes processing to switch to the system stack when passing control to the cyclic handler, and processing to switch to the stack for the switch destination processing program (system stack or task stack) when regaining control from the cyclic handler.
The user is therefore not required to code processing related to stack switching in cyclic handlers.
- Interrupt status
Maskable interrupt acknowledgement is prohibited in the RI78V4 when control is passed to the cyclic handler.
To change (enable) the interrupt status in the cyclic handler, calling of the __EI function are therefore required.
- Service call issuance
The RI78V4 handles the cyclic handler as a "non-task".
Service calls that can be issued in cyclic handlers are limited to the service calls that can be issued from non-tasks.
Note 1 For details on the valid issuance range of each service call, refer to Table 12-8 to Table 12-17.
Note 2 If a service call (ichg_pri, isig_sem, etc.) accompanying dispatch processing (task scheduling processing) is issued in order to quickly complete the processing in the cyclic handler during the interval until the processing in the cyclic handler ends, the RI78V4 executes only processing such as queue manipulation, counter manipulation, etc., and the actual dispatch processing is delayed until a return instruction is issued by the cyclic handler, upon which the actual dispatch processing is performed in batch.
7.5.5 Start cyclic handler operation
Moving to the operational state (STA state) is implemented by issuing the following service call from the processing program.
- sta_cyc
This service call moves the cyclic handler specified by parameter cycid from the non-operational state (STP state) to operational state (STA state).
As a result, the target cyclic handler is handled as an activation target of the RI78V4.
The following describes an example for coding this service call.
 #include    <kernel.h>          /*Standard header file definition*/
 #include    <kernel_id.h>       /*System information header file definition*/
 
 void
 func_task ( VP_INT exinf )
 {
     ID      cycid = ID_cycA;    /*Declares and initializes variable*/
 
     /* ............ */
 
     sta_cyc ( cycid );          /*Start cyclic handler operation*/
 
     /* ............ */
 }

Note The relative interval from when either of this service call is issued until the first activation request is issued varies depending on whether the TA_PHS attribute is specified for the target cyclic handler during configuration.
[ Cyclic handler activation image(the TA_PHS attribute is specified) ]
The target cyclic handler activation timing is set based on the activation phases (initial activation phase cycphs and activation cycle cyctim) defined during configuration.
If the target cyclic handler has already been started, however, no processing is performed even if this service call is issued, but it is not handled as an error.
The following shows a cyclic handler activation timing image.
[ Cyclic handler activation image(the TA_PHS attribute is not specified) ]
The target cyclic handler activation timing is set based on the activation phase (activation cycle cyctim) when this service call is issued.
This setting is performed regardless of the operating status of the target cyclic handler.
The following shows a cyclic handler activation timing image.
7.5.6 Stop cyclic handler operation
Moving to the non-operational state (STP state) is implemented by issuing the following service call from the processing program.
- stp_cyc
This service call moves the cyclic handler specified by parameter cycid from the operational state (STA state) to non-operational state (STP state).
As a result, the target cyclic handler is excluded from activation targets of the RI78V4 until issuance of sta_cyc.
The following describes an example for coding this service call.
 #include    <kernel.h>          /*Standard header file definition*/
 #include    <kernel_id.h>       /*System information header file definition*/
 
 void
 func_task ( VP_INT exinf )
 {
     ID      cycid = ID_cycA;    /*Declares and initializes variable*/
 
     /* ............ */
 
     stp_cyc ( cycid );          /*Stop cyclic handler operation*/
 
     /* ............ */
 }

Note This service call does not perform queuing of stop requests. If the target cyclic handler has been moved to the non-operational state (STP state), therefore, no processing is performed but it is not handled as an error.
7.5.7 Reference cyclic handler state
A cyclic handler status by issuing the following service call from the processing program.
- ref_cyc
Stores cyclic handler state packet (such as current status) of the cyclic handler specified by parameter cycid in the area specified by parameter pk_rcyc.
The following describes an example for coding this service call.
 #include    <kernel.h>          /*Standard header file definition*/
 #include    <kernel_id.h>       /*System information header file definition*/
 
 void
 func_task ( VP_INT exinf )
 {
     ID      cycid = ID_cycA;    /*Declares and initializes variable*/
     T_RCYC  pk_rcyc;            /*Declares data structure*/
     STAT    cycstat;            /*Declares variable*/
     RELTIM  lefttim;            /*Declares variable*/
 
     /* ............ */
 
     ref_cyc ( cycid, &pk_rcyc );/*Reference cyclic handler state*/
 
     cycstat = pk_rcyc.cycstat;  /*Reference cyclic handler operational state*/
     lefttim = pk_rcyc.lefttim;  /*Reference time left before the next activation*/
 
     /* ............ */
 }

Note For details about the cyclic handler state packet, refer to "12.5.8 Cyclic handler state packet".