Target Language Compiler    

Input Signal Functions

LibBlockInputSignal(portIdx, ucv, lcv, sigIdx)

Based on the input port number (portIdx), the user control variable (ucv), the loop control variable (lcv), the signal index (sigIdx), and where this input signal is coming from, LibBlockInputSignal returns the appropriate reference to a block input signal.

The returned string value is a valid rvalue (right-side value) for an expression. The block input signal can come from another block, a state vector, an external input, or it can be a literal constant (e.g, 5.0).

Since the returned value can be a literal constant, you should not use LibBlockInputSignal to access the address of an input signal. To access the address of an input signal, use LibBlockInputSignalAddr. Accessing the address of the signal via LibBlockInputSignal may result in a reference to a literal constant (e.g., 5.0).

For example, the following would not work.

If %<u> refers to an invariant signal with a value of 4.95, the statement (after being processed by the pre-processor) would be generated as

or, if the input signal sources to ground, the statement could come out as

neither of these would compile.

Avoid any such situations by using LibBlockInputSignalAddr.

Real-Time Workshop tracks signals and parameters accessed by their address and declares them in addressable memory.

Input Arguments

The following table summarizes the input arguments to LibBlockInputSignal.

Table 9-1: LibBlockInputSignal Arguments
Argument
Description
portIdx
Integer specifying the input port index (zero-based).
Note: for certain built-in blocks, portIdx can be a string identifying the port (such as "enable" or "trigger").
ucv
User control variable. Must be a string, either an indexing expression or "".
lcv
Loop control variable. Must be a string, either an indexing expression or "".
sigIdx

Either an integer literal or a string of the form

  • %<tRealPart>Integer
    %<tImagPart>Integer
    

For example, the following signifies the real part of the signal and the imaginary part of the signal starting at 5:

  • "%<tRealPart>5"
    "%<tImagPart>5" 
    

General Usage

Uses of LibBlockInputSignal fall into the categories described below.

Direct indexing. .   If ucv == "" and lcv == "", LibBlockInputSignal returns an indexing expression for the element specified by sigIdx.

Loop rolling/unrolling.   In this case, lcv and sigIdx are generated by the %roll directive, and ucv must be "". A non-empty value for lcv is only allowed when generated by the %roll directive and when using the Roller TLC file (or a user supplied Roller TLC file that conforms to the same variable/signal offset handling). In addition, calls to LibBlockInputSignal with lcv should occur only when "U" or a specific input port (e.g. "u0") is passed to the %roll directive via the roll variables argument.

The following example is appropriate for a single input/single output port S-function.

With the %roll directive, sigIdx is always the starting index of the current roll region and lcv will be "" or an indexing variable. The following are examples of valid values:

In Example 1, LibBlockInputSignal returns rtB.blockname[2] when the input port is connected to the output of another block and:

In Example 2, LibBlockInputSignal returns u[i] when the current roll region is above the roll threshold and the input port width is non-scalar (wide). In this case, the Roller TLC file sets up a local variable, u, to point to the input signal and the code in the current %roll directive is placed within a for loop.

For another example, suppose we have a block with multiple input ports where each port has a width greater than or equal to 1 and at least one port has width equal to 1. The following code sets the output signal to the sum of the squares of all the input signals.

Since the first parameter of LibBlockInputSignal is 0-indexed, you must index the foreach loop to start from 0 and end at NumDataInputPorts-1.

User Control Variable (ucv) Handling..   This is an advanced mode and generally not needed by S-function authors.

If ucv != "", LibBlockInputSignal returns an rvalue for the input signal using the user control variable indexing expression. The control variable indexing expression has the following form.

rvalue_id is obtained by looking at the integer part of sigIdx. Specifying sigIdx is required because the input to this block can be discontinuous, meaning that the input can come from several different memory areas (signal sources) and sigIdx is used to identify the area of interest for the ucv. Also, sigIdx is used to determine whether the real or imaginary part of a signal is to be accessed.

optional_real_or_imag_part is obtained by the string part of sigIdx (i.e. "re", or "im", or "").

Note: the value for lcv is ignored and sigIdx must point to the same element in the input signal to which the ucv initially points.

The handling of ucv with LibBlockInputSignal requires care. Consider a discontinuous input signal feeding an input port as in the following block diagram.

To use ucv in a robust manner, you must use the %roll directive with a roll threshold of 1 and a Roller TLC file that has no loop header/trailer setup for this input signal. In addition, you need to use ROLL_ITERATIONS to determine the width of the current roll region, as in the following TLC code.

Note, the FlatRoller has no loop header/trailer setup (rollVars is ignored). Its purpose is to walk the RollRegions of the block.

Alternatively, you can force a contiguous input signal to your block by specifying

in your S-function.

In this case, the TLC code simplifies to

If you create your own roller and the indexing does not conform to the way the Roller TLC file provided by the MathWorks operates, then you will need to use ucv instead of lcv.

Input Arguments (ucv, lcv, and sigIdx) Handling

Consider the following cases :

Function (case 1, 2, 3,4)
Example Return Value
LibBlockInputSignal(0, "i", "", sigIdx)
rtB.blockname[i]
LibBlockInputSignal(0, "i", "", sigIdx)
rtU.signame[i]
LibBlockInputSignal(0, "", lcv, sigIdx)
u0[i1]
LibBlockInputSignal(0, "", lcv, sigIdx)
rtB.blockname[0]

The value returned depends on what the input signal is connected to in the block diagram and how the function is invoked (e.g. in a %roll or directly). In the above example:

When called within a %roll directive, this function looks at ucv, lcv, and sigIdx, the current roll region, and the current roll threshold to determine the return value. The variable ucv has highest precedence, lcv has the next highest precedence, and sigIdx has the lowest precedence. That is, if ucv is specified, it will be used (thus, when called in a %roll directive it is usually ""). If ucv is not specified and lcv and sigIdx are specified, the returned value depends on whether or not the current roll region is being placed in a for loop or being expanded. If the roll region is being placed in a loop, then lcv is used, otherwise, sigIdx is used.

A direct call to this function (inside or outside of a %roll directive) will use sigIdx when ucv and lcv are specified as "".

For an example of this function, see matlabroot/toolbox/simulink/blocks/tlc_c/sfun_multiport.tlc. See also matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalAddr(portIdx, ucv, lcv, sigIdx)

Returns the appropriate string that provides the memory address of the specified block input port signal.

When you need an input signal address, you must use this function instead of appending an "&" to the string returned by LibBlockInputSignal. For example, LibBlockInputSignal can return a literal constant, such as 5 (i.e., an invariant input signal). Real-Time Workshop tracks when LibBlockInputSignalAddr is called on an invariant signal and declares the signal as "const" data (which is addressable), instead of being placed as a literal constant in the generated code (which is not addressable).

Note, unlike LibBlockInputSignal(), the last input argument, sigIdx, is not overloaded. Hence, if the input signal is complex, the address of the complex container is returned.

Example

To get the address of a wide input signal and pass it to a user-function for processing, you could use

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalConnected(portIdx)

Returns 1 if the specified input port is connected to a block other than the Ground block and 0 otherwise.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalDataTypeId(portIdx)

Returns the numeric identifier (id) corresponding to the data type of the specified block input port.

If the input port signal is complex, this function returns the data type of the real part (or the imaginary part) of the signal.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalDataTypeName(portIdx, reim)

Returns the name of the data type (e.g., int_T, ... creal_T) corresponding to the specified block input port.

Specify the reim argument as "" if you want the complete signal type name. For example, if reim=="" and the first output port is real and complex, the data type name placed in dtname will be creal_T.

Specify the reim argument as tRealPart if you want the raw element type name. For example, if reim==tRealPart and the first output port is real and complex, the data type name returned will be real_T.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalDimensions(portIdx)

Returns the dimensions vector of specified block input port, e.g., [2,3].

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalIsComplex(portIdx)

Returns 1 if the specified block input port is complex, 0 otherwise.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalIsFrameData(portIdx)

Returns 1 if the specified block input port is frame based, 0 otherwise.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalLocalSampleTimeIndex(portIdx)

Returns the local sample time index corresponding to the specified block input port.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalNumDimensions(portIdx)

Returns the number of dimensions of the specified block input port.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalOffsetTime(portIdx)

Returns the offset time corresponding to the specified block input port.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalSampleTime(portIdx)

Returns the sample time corresponding to the specified block input port.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalSampleTimeIndex(portIdx)

Returns the sample time index corresponding to the specified block input port.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.

LibBlockInputSignalWidth(portIdx)

Returns the width of the specified block input port index.

See function in matlabroot/rtw/c/tlc/lib/blkiolib.tlc.


  Target Language Compiler Functions Output Signal Functions