User`s manual

64 digi.com Multitasking with Dynamic C
5.5.2.1 Costate Within a Cofunc
In all but trivial cases (where the costate is really not necessary), a costate within a cofunc causes execu-
tion problems ranging from never completing the cofunc to unexpected interrupts or target lockups. To
avoid these problems, do not introduce costates with nested wfd cofuncs into a cofunc. If you find yourself
coding such a thing, consider these alternatives:
1. Intermediate regular functions can be used between the cofuncs to isolate them.
2. A regular waitfor(function) can be substituted for the top level costate's wfd cofunction.
3. The nested costates with wfd cofuncs can be moved up into the body of the calling function, replacing
the top-level costate with the wfd cofunc.
A compiler error will be generated if a costate is found within a cofunction.
5.5.2.2 Using the IX Register
Functions called from within a cofunction may use the IX register if they restore it before the cofunction is
exited, which includes an exit via an incomplete waitfordone statement.
In the case of an application that uses the #useix directive, the IX register will be corrupted when any
stack-variable using function is called from within a cofunction, or if a stack-variable using function con-
tains a call to a cofunction.
5.5.3 CoData Structure
The CoData structure discussed in Section 5.4.1 applies to cofunctions; each cofunction has an associated
CoData structure.
5.5.4 Firsttime Functions
The firsttime functions discussed in “Firsttime Functions” on page 62 can also be used inside cofunc-
tions. They should be called inside a waitfor statement. If you call these functions from inside a wfd
statement, no compiler error is generated, but, since these delay functions do not yield while waiting for
the desired time to elapse, but instead return 0 to indicate that the desired time has not yet elapsed, the wfd
statement will consider a return value to be completion of the firsttime function and control will pass
to the statement following the wfd.
5.5.5 Types of Cofunctions
There are three types of cofunctions: simple, indexed and single-user. Which one to use depends on the
problem that is being solved. A single-user, indexed cofunction is not valid.
5.5.5.1 Simple Cofunction
A simple cofunction has only one instance and is similar to a regular function with a costate taking up
most of the function’s body.
5.5.5.2 Indexed Cofunction
An indexed cofunction allows the body of a cofunction to be called more than once with different parame-
ters and local variables. The parameters and the local variable that are not declared static have a special
lifetime that begins at a first time call of a cofunction instance and ends when the last curly brace of the
cofunction is reached or when an abort or return is encountered.