Basic Custom Code

<< Click to Display Table of Contents >>

Navigation:  Waijung Blockset > Block References > Custom Code >

Basic Custom Code

Previous pageReturn to chapter overviewNext page

User Interface

How this block appears in a Simulink model?

basiccustomcodeblock

What can be configured?

basic_custom_code_mask

When to use this block?

When you want to add your own custom C code to the system.

How does this block work?

To use the Basic Custom Code Block you need to complete the following steps.

1.Define input / output ports for the Block.

2. Define include and source paths/files for the custom code.

3. Define three function prototypes: enable, output, disable.

1. Define input / output ports for the Basic Custom Code Block

The number of ports equal the number of elements defined in the "Input port definition (data type) separated by comma e.g. double, single, double, uint8" and "Output port definition  (data type) separated by comma e.g. double, single, double, uint8" editbox. Separate each element with a comma. The top port correspond to the first defined data type.

Leave the editbox empty if ports are not required.

Support signal data types are

odouble
osingle
ouint32
oint32
ouint16
oint16
ouint8
oint8
oboolean

Port labels can be defined in the Custom input / output port labels editboxes i.e. "Custom input port labels (Ex. In1, In2, In3)" and "Custom output port labels (Ex. Out1, Out2, Out3)".

Input and Output signals can be either scalar, vector, or 2-D matrix type.

The dimensions of input signals are automatically inherited from the supplied input signals.

The dimensions of output signals must be implicitly defined using the following format.

DataType:nRowsxnCols

For examples: double:4x5,int8,int16:3x1. If dimensions are not defined, scalar signal (1x1) is assumed. See the following for using the Basic Custom Code Block with non-scalar signals.

2. Define include and source paths/files for the custom code

2.1 Include Path

"Path for include files (*.h), defined as cell array of string." editbox accepts Matlab row cell array of string. All header files (*.h) present in the declared include path will be used during the build process. This parameter tells Matlab where to look for header files.

2.2 Source Path

"Path for source files (*.c), defined as cell array of string." editbox accepts Matlab row cell array of string. All source files (*.c) present in the declared include path will be used during the build process. This parameter tells Matlab where to look for source files.

2.2 Source Path

"Path for source files (*.c), defined as cell array of string." editbox accepts Matlab row cell array of string. All source files (*.c) present in the declared include path will be used during the build process. This parameter tells Matlab where to look for source files.

2.3 Header Files

"Included header file names with extension (.h), defined as cell array of string." editbox accepts Matlab row cell array of string. Header files (*.h) present in this editbox will added in waijung_hwdrvlib.h as #include statement. Declare each source file name with and extension .h.

2.4 Source Files

"Included source file names with extension (.c), defined as cell array of string." editbox accepts Matlab row cell array of string. Source files (*.c) present in this editbox will be included in the build process. Declare each source file name with and extension .c.

3. Define three function prototypes: enable, output, disable.

After code generation, each Simulink block must have at least three functions that are called at different point of time during target system execution. These are:

3.1 Initial function

An initial function will be called once at MCU start-up.

3.2 Enable function

An enable function will be called at system initialization and every time the block is enabled, e.g. in a case where a block is located in an Enabled Subsystem.

3.3 Output function

An output function will be called every time step to compute output values.

3.4 Disable function

A disable function will be called at system termination and every time the block is disabled, e.g. in a case where a block is located in an Enabled Subsystem.

The code generation mechanism generates correct function prototypes and function calls for each block automatically. Your job is to define function prototypes that are wrapped inside these automatically-generated Matlab function calls.

The Enable function can be defined in "Enable function call string" editbox.

The Disable function can be defined in "Disable function call string" editbox.

The Enable function and Disable function must not have any parameters. These function will be called every time  the system is initialized/terminated and enabled/disabled.

The output function require input and output (scalar) signal to be declared as parameter of the output function.

Name convention for custom code output function parameters

The parameters for custom code output function must follow the following requirements.

a.Always start with input (if any) and follow by output (if any).
b.Use in### and out### for input and output signal respectively, where ### represent digits starting from 1.

For examples

The block has 2 inputs and 3 outputs, output function prototype is void custom_output_function_name(in1,in2,out1,out2,out3);

The block has 0 inputs and 3 outputs, output function prototype is void custom_output_function_name(out1,out2,out3);

The block has 2 inputs and 0 outputs, output function prototype is void custom_output_function_name(in1,in2);

Demos

Basic Custom Code Block Demo

Basic Custom Code (Non-Scalar Signal)

Basic Custom Code demo for UART-WiFi module