CHAPTER 8 TIME MANAGEMENT FUNCTIONS


This chapter describes the time management functions performed by the RI600V4.

8.1 Outline

The RI600V4's time management function provides methods to implement time-related processing (Timer Operations: Delay task, Time-out, Cyclic handlers, Alarm Handlers and System Time) by using base clock timer interrupts that occur at constant intervals, as well as a function to manipulate and reference the system time.

8.2 System Time

The system time is a time used by the RI600V4 for performing time management (in millisecond).

After initialization to 0 by the Kernel Initialization Module (vsta_knl, ivsta_knl), the system time is updated based on the base clock interval defined by Denominator of base clock interval time (tic_deno) and Denominator of base clock interval time (tic_deno) in System Information (system) when creating a system configuration file.

8.2.1 Base clock timer interrupt

To realize the time management function, the RI600V4 uses interrupts that occur at constant intervals (base clock timer interrupts).

When a base clock timer interrupt occurs, processing related to the RI600V4 time (system time update, task time-out/delay, cyclic handler activation, alarm handler activation, etc.) is executed.

Basically, either of channel 0-3 of the compare match timer (CMT) implemented in the MCU is used for base clock time. The channel number is specified by Selection of timer channel for base clock (timer)in Base Clock Interrupt Information (clock). in the system configuration file.

The hardware initialization to generate base clock timer interrupt is implemented by "void __RI_init_cmt(void)" in "ri_cmt.h". The "ri_cmt.h" file is generated by the cfg600. The Boot processing function (PowerON_Reset_PC( )) must call _RI_init_cmt().

8.2.2 Base clock interval

In the RI600V4, service call parameters for time specification are specified in msec units.

It is desirable to set 1 msec for the occurrence interval of base clock timer interrupts, but it may be difficult depending on the target system performance (processing capability, required time resolution, or the like).

In such a case, the occurrence interval of base clock timer interrupt can be specified by Denominator of base clock interval time (tic_deno) and Denominator of base clock interval time (tic_deno) in System Information (system) when creating a system configuration file.

By specifying the base clock interval, processing regards that the time equivalent to the base clock interval elapses during a base clock timer interrupt.

8.3 Timer Operations

The RI600V4's timer operation function provides Delay task, Time-out, Cyclic handlers, Alarm Handlers and System Time, as the method for realizing time-dependent processing.

8.4 Delay task

Delayed task 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 wake-up is implemented by issuing the following service call from the processing program.

dly_tsk

8.5 Time-out

Time-out 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 time-out is implemented by issuing the following service call from the processing program.

tslp_tsk, twai_sem, twai_flg, tsnd_dtq, trcv_dtq, trcv_mbx, tloc_mtx, tsnd_mbf, trcv_mbf, tget_mpf, tget_mpl

8.6 Cyclic handlers

The cyclic handler is a routine dedicated to cycle processing that is activated periodically at a constant interval (activation cycle).

The RI600V4 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.

8.6.1 Basic form of cyclic handlers

The Extended information (exinf) in Cyclic Handler Information (cyclic_hand[]) is passed to the exinf.
The following shows the basic form of cyclic handlers.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void cychdr (VP_INT exinf)
 {
     /* ......... */
 
     return;                         /*Terminate cyclic handler*/
 }


Note The cfg600 outputs the prototype declaration for the handler function to kernel_id.h.

8.6.2 Processing in cyclic handler

- Stack
A cyclic handler uses the system stack.


- Service call
The RI600V4 handles the cyclic handler as a "non-task".
The cyclic handler can issue service calls whose "Useful range" is "Non-task".



Note If a service call (isig_sem, iset_flg, etc.) which causes 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 RI600V4 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.

- PSW register when processing is started

Table 8-1 PSW Register When Cyclic Handler is Started

Bit

Value

Note

I

1

IPL

Do not lower IPL more than the start of processing.

PM

0

Supervisor mode

U

0

System stack

C, Z, S, O

Undefined

Others

0



8.6.3 Create cyclic handler

In the RI600V4, the method of creating a cyclic handler is limited to "static creation".

Cyclic handlers therefore cannot be created dynamically using a method such as issuing a service call from a processing program.

Static cyclic handler creation means defining of cyclic handlers using static API "cyclic_hand[]" in the system configuration file.

For details about the static API "cyclic_hand[]", refer to "19.16 Cyclic Handler Information (cyclic_hand[])".

8.6.4 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, ista_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 RI600V4.
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 (phsatr) is specified for the target cyclic handler during configuration.



- If the TA_PHS attribute is specified
The target cyclic handler activation timing is set based on the Activation phase (phs_counter) and Activation cycle (interval_counter) 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.




Figure 8-1 TA_PHS Attribute: Specified



- If the TA_PHS attribute is not specified
The target cyclic handler activation timing is set based on the activation phase (Activation cycle (interval_counter)) 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.




Figure 8-2 TA_PHS Attribute: Not Specified



The following describes an example for coding these service calls.

 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      cycid = 1;              /*Declares and initializes variable*/
 
     /* ......... */
 
     sta_cyc (cycid);                /*Start cyclic handler operation*/
 
     /* ......... */
 }


8.6.5 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, istp_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 RI600V4 until issuance of sta_cyc or ista_cyc.
The following describes an example for coding these service calls.



 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      cycid = 1;              /*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.

8.6.6 Reference cyclic handler state

A cyclic handler status by issuing the following service call from the processing program.

- ref_cyc, iref_cyc
Stores cyclic handler state packet (current state, time until the next activation, etc.) of the cyclic handler specified by parameter cycid in the area specified by parameter pk_rcyc.
The following describes an example for coding these service calls.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      cycid = 1;              /*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 current state*/
     lefttim = pk_rcyc.lefttim;      /*Reference time left before the next */
                                     /*activation*/
 
     /* ......... */
 }


Note For details about the cyclic handler state packet, refer to "[Cyclic handler state packet: T_RCYC]".

8.7 Alarm Handlers

The alarm handler is a routine started when the specified time passes.

The RI600V4 handles the alarm 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 time has elapsed, and the control is passed to the alarm handler.

8.7.1 Basic form of alarm handler

The Extended information (exinf) in Alarm Handler Information (alarm_handl[]) is passed to the exinf.
The following shows the basic form of alarm handlers.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void almhdr (VP_INT exinf)
 {
     /* ......... */
 
     return;                         /*Terminate alarm handler*/
 }


Note The cfg600 outputs the prototype declaration for the handler function to kernel_id.h.

8.7.2 Processing in alarm handler

- Stack
A alarm handler uses the system stack.


- Service call
The RI600V4 handles the alarm handler as a "non-task".
The alarm handler can issue service calls whose "Useful range" is "Non-task".



Note If a service call (isig_sem, iset_flg, etc.) which causes dispatch processing (task scheduling processing) is issued in order to quickly complete the processing in the alarm handler during the interval until the processing in the alarm handler ends, the RI600V4 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 alarm handler, upon which the actual dispatch processing is performed in batch.

- PSW register when processing is started

Table 8-2 PSW Register When Alarm Handler is Started)

Bit

Value

Note

I

1

IPL

Do not lower IPL more than the start of processing.

PM

0

Supervisor mode

U

0

System stack

C, Z, S, O

Undefined

Others

0



8.7.3 Create alarm handler

In the RI600V4, the method of creating a alarm handler is limited to "static creation".

Alarm handlers therefore cannot be created dynamically using a method such as issuing a service call from a processing program.

Static alarm handler creation means defining of alarm handlers using static API "alarm_hand[]" in the system configuration file.

For details about the static API "alarm_hand[]", refer to "19.17 Alarm Handler Information (alarm_handl[])".

8.7.4 Start alarm handler operation

Moving to the operational state (STA state) is implemented by issuing the following service call from the processing program.

- sta_alm, ista_alm
This service call sets the activation time of the alarm handler specified by almid in almtim (msec), and moves the alarm handler from the non-operational state (STP state) to operational state (STA state).
As a result, the target alarm handler is handled as an activation target of the RI600V4.
The following describes an example for coding these service calls.



 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      almid = 1;              /*Declares and initializes variable*/
 
     /* ......... */
 
     sta_alm (almid);                /*Start alarm handler operation*/
 
     /* ......... */
 }


Note 1 When 0 is specified for almtim, the alarm handler will start at the next base clock interruption.

Note 2 When the target alarm handler has already started (STA state), this service call sets the activation time of the target alarm handler in almtim (msec) after canceling the activation time.

8.7.5 Stop alarm handler operation

Moving to the non-operational state (STP state) is implemented by issuing the following service call from the processing program.

- stp_alm, istp_alm
This service call moves the alarm handler specified by parameter cycid from the operational state (STA state) to non-operational state (STP state).
As a result, the target alarm handler is excluded from activation targets of the RI600V4 until issuance of sta_alm or ista_alm.
The following describes an example for coding these service calls.



 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      almid = 1;              /*Declares and initializes variable*/
 
     /* ......... */
 
     stp_alm (almid);                /*Stop alarm handler operation*/
 
     /* ......... */
 }


Note This service call does not perform queuing of stop requests. If the target alarm handler has been moved to the non-operational state (STP state), therefore, no processing is performed but it is not handled as an error.

8.7.6 Reference alarm handler state

A alarm handler status by issuing the following service call from the processing program.

- ref_alm, iref_alm
Stores alarm handler state packet (current state, time until the next activation, etc.) of the alarm handler specified by parameter cycid in the area specified by parameter pk_rcyc.
The following describes an example for coding these service calls.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     ID      almid = 1;              /*Declares and initializes variable*/
     T_RALM  pk_ralm;                /*Declares data structure*/
     STAT    almstat;                /*Declares variable*/
     RELTIM  lefttim;                /*Declares variable*/
 
     /* ......... */
 
     ref_alm (almid, &pk_ralm);      /*Reference alarm handler state*/
 
     almstat = pk_ralm.almstat;      /*Reference current state*/
     lefttim = pk_ralm.lefttim;      /*Reference time left */
 
     /* ......... */
 }


Note For details about the alarm handler state packet, refer to "[Alarm handler state packet: T_RALM]".

8.8 System Time

8.8.1 Set system time

The system time can be set by issuing the following service call from the processing program.

Note that even if the system time is changed, the actual time at which the time management requests made before that (e.g., task time-outs, task delay by dly_tsk, cyclic handlers, and alarm handlers) are generated will not change.

- set_tim, iset_tim
These service calls change the system time (unit: msec) to the time specified by parameter p_systim.
The following describes an example for coding these service calls.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     SYSTIM  p_systim;               /*Declares data structure*/
 
     p_systim.ltime = 3600;          /*Initializes data structure*/
     p_systim.utime = 0;             /*Initializes data structure*/
 
     /* ......... */
 
     set_tim (&p_systim);            /*Set system time*/
 
     /* ......... */
 }


Note For details about the system time packet SYSTIM, refer to "[System time packet: SYSTIM]".

8.8.2 Reference system time

The system time can be referenced by issuing the following service call from the processing program.

- get_tim, iget_tim
These service calls store the system time (unit: msec) into the area specified by parameter p_systim.
The following describes an example for coding these service calls.


 #include    "kernel.h"              /*Standard header file definition*/
 #include    "kernel_id.h"           /*Header file generated by cfg600*/
 
 void task (VP_INT exinf)
 {
     SYSTIM  p_systim;               /*Declares data structure*/
     UW      ltime;                  /*Declares variable*/
     UH      utime;                  /*Declares variable*/
 
     /* ......... */
 
     get_tim (&p_systim);            /*Reference System Time*/
 
     ltime = p_systim.ltime;         /*Acquirer system time (lower 32 bits)*/
     utime = p_systim.utime;         /*Acquirer system time (higher 16 bits)*/
 
     /* ......... */
 }


Note For details about the system time packet SYSTIM, refer to "[System time packet: SYSTIM]".

8.9 Initialize Base Clock Timer

The cfg600 outputs the file "ri_cmt.h" which the base clock timer initialization function (void _RI_init_cmt(void)) is described. The Boot processing function (PowerON_Reset_PC( )) should call the base clock timer initialization function.