Please enable JavaScript to view this site.

Navigation: ESP32 target > Utilities

Basic custom code block

Scroll Prev Top Next More

How this block appears in a Simulink model?

basic_custom_code_block_1

What can be configured?

basic_custom_code_block_2

Configuration Parameter

Selectable Option/Value

Description

Custom input port labels

 

Define custom input port labels

Custom output port labels

 

Define custom output port labels.

Disable function call string

 

Custom function call script for when the block is disabled.

Enable function call string

 

Custom function call when the Block is enabled.

Included header file names

 

When the respective Edit button is pressed it will show the available header files specified in the include directory above. Select the header files that are needed for the block. When building, Waijung2 will automatically copy these files from the folder to the build folder.

Included source file names

 

When the respective Edit button is pressed it will show the available source files specified in the source directory above. Select the source files that are needed for the block. When building, Waijung2 will automatically copy these files from the folder to the build folder.

Initial function call string

 

Custom function call once MCU startup.

Input port definition (data type) separated by comma

 

Define type and the number of input port(s). Leave empty if NO input port is needed.

Mask display name

 

Enter display name which appears on the block display.

Output function call string

 

Custom function call when the Block is executed in the model step function.

Output port definition (data type) separated by comma

 

Define type and number of output port(s). Leave empty if No output port is needed.

Path for include files (*.h)

 

Browse to the directory where the header files are located.

Path for source files (*.c)

 

Browse to the directory where the source files are located.

Sample time (sec)

-1 for inherited or specify

Enter sample time.

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.

oStep 1: Define input/output ports for the Block.

The number of ports equals the number of elements defined in the Input port definition (data type) separated by a comma e.g. double, single, double, uint8 and Output port definition (data type) separated by a comma e.g. double, single, double, uint8 edit box. Separate each element with a comma. The top port corresponds to the first defined data type. Leave the edit box empty if ports are not required. Supported signal data types are

a.double

b.single

c.uint32

d.int32

e.uint16

f.int16

g.uint8

h.int8

i.boolean

Port labels can be defined in the Custom input/output port labels edit boxes 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 example: double:4x5,int8,int16:3x1.

If dimensions are not defined, a scalar signal (1x1) is assumed.

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

Use Browse buttons to select the directories that include the header files and source files used in this block. Use the Edit buttons to select the header files and source files used in the block. These files will be automatically copied to the build directory by the Waijung2 software. Use Clear buttons to clear all selections.

oStep 3: Define four-function prototypes: initial, enable, output, disable.

When generating code for a Simulink block, Matlab creates these four different functions. Among them, only the output function is mandatory as that is responsible for updating the output port signals of the block.

Initial function

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

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.

Output function

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

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.

Your job is to define function prototypes that will be wrapped inside these automatically-generated MATLAB function calls. The function definitions of the prototypes specified are to be found in the included source and header files. Apart from the main function prototype, additional C code lines can also be specified if necessary in each of the edit box spaces mentioned below.

The Initial function can be defined in the Initial function call string edit box. The Initial function must not have any parameters.

The Enable function can be defined in the Enable function call string edit box.

The Disable function can be defined in the Disable function call string edit box.

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

The Output function can be defined in the Output function call string edit box. The Output function requires input and output (scalar) signals (from input/output ports of the block) to be declared as parameters of the output function. The parameters for the custom code output function must follow the following requirements.

oAlways start with input (if any) and follow by output (if any).

oUse in### and out### for input and output signal respectively, where ### represent digits starting from 1.

For example:

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

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

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

Demo

Demo file : esp32_basic_customcode_demo.slx

basic_custom_code_block_3

Description

In this example, a custom counter block has been implemented using the Basic Custom Code Block. To observe the counter value, the output of the block is connected to a Print block via a String Processing block.

Setup

First, download the basic_customcode_demo_lib.zip and extract the content. Copy the extracted folder to the same folder as the model file. Open the model file from Matlab. In the Basic Custom Code block mask, browse the Path for include files (*.h) and Path for source files (*.c) to the extracted folder.

A serial monitor software application is required to get the expected results. For this example, PuTTY open-source SSH and telnet client is used to monitor the serial output.

What should be happening?

After the model file is compiled and uploaded to the ESP32 board, open the PuTTY software and connect to the relevant serial port with a baud rate of 115200. The following output should be visible on the serial monitor software.

basic_custom_code_block_4

Copyright 2022 Aimagin Co.,Ltd. Rev.339