emCustomCBlock

Top  Next

emCustomCBlock


set up user's manual C code Block.

Since R2019b

 

Library: embeddedCreatorLib ( Dafulai Electronics) /  emCustomCBlock

 

 

emCustomCBlock

 

 


 

Description


 

Simulink and Our Library provide lots of blocks for embedded system.  However, we cannot cover all peripherals and applications. This block provides a method to let customer write C code manually. Or customers have existing mature C code,  they can still use this C code by this block. Or customers can use AI such as "Copilot"/"Deepseek" to create C code, and put it into this block.

Manual C Code has 2 parts,  one part is to run once for initialization,  the other part is to run periodically in sample time.

For initialization part, you just open your IDE, and look for comment "//Customized Initialization Code for Block: Your block name ------Start".

And add your code just under comment "//Add your code" and before comment "//Customized Initialization Code for Block: Your block name ------End"

 

For periodical part, you just open your IDE, and look for comment "//Customized Step (loop) Code for Block: Your block name ------Start".

And add your code just under comment "//Add your code" and before comment "/Customized Step (loop) Code for Block: Your block name ------End"

 

Before your code, you will see comment "//Input Port1 variable name: U1    Data Type: int16" .... "//Output Port1 variable name: Y1    Data Type: int16" ...

It is convenient to write your code according to these comments for input/output variable name and data type.

 

By this block, you can implement new function in your C code.

For example, you can implement 3 phase PWMs.  You use Configuration Software (such as MCC , STM32CubeMx, or SysConfig) to create PWM initialization code.

Leave "Custom C Block" initialization part empty.  Or you don't use any configuration software, just directly set relative registers by your self for  initialization part of this block.

At last you write C code manually for periodical part to set duty cycle register value.

 

 

 

Notes: 1 Please don't edit any comment which is created by Simulink code.

2 You can find your previous C code for "emCustomCBlock" in Folder:  "Your embedded project directory used in configuration" \ simulink_generated_files_backup.  You can find your C code of the one before the previous for "emCustomCBlock" in Folder:  "Your embedded project directory used in configuration" \ simulink_generated_files_backup_backup.

 

 

Parameters


 

Please double click this block to open parameters dialog below:

 

emCustomCBlock_param

 

 

 

Let us explain parameters.

 

 

Input Port Variable Name in C language — It specifies variable name of input ports. It can be scalar or vector. How many vector elements it has is how many input ports it has. All input ports are scalar. Notes: We cannot put any quotation mark in vector or scalar. For example, you cannot use [ "U1", "U2"], You cannot use " [ U1, U2]". You must use [ U1, U2] or [U1 U2] or [U1; U2]

 

Input Port data type — It specifies data type of all input ports. It can be scalar or vector. element can be "bool","uint8", "int8","uint16","int16","uint32","int32","single". How many vector elements it has is how many input ports it has. Notes: We cannot put any quotation mark in vector or scalar. For example, you cannot use [ "int8", "uint16"], You cannot use " [ int8, uint16]". You must use [ int8, uint16] or  [ int8  uint16] or  [ int8;  uint16].

 

Output Port Variable Name in C language — It specifies variable name of output ports. It can be scalar or vector. How many vector elements it has is how many output ports it has. All output ports are scalar. Notes: We cannot put any quotation mark in vector or scalar. For example, you cannot use [ "Y1", "Y2"], You cannot use " [ Y1, Y2]". You must use [ Y1, Y2] or [Y1 Y2] or [Y1; Y2]

 

Output Port data type — It specifies data type of all output ports. It can be scalar or vector. element can be "bool","uint8", "int8","uint16","int16","uint32","int32","single". How many vector elements it has is how many output ports it has. Notes: We cannot put any quotation mark in vector or scalar. For example, you cannot use [ "int8", "uint16"], You cannot use " [ int8, uint16]". You must use [ int8, uint16] or  [ int8  uint16] or  [ int8;  uint16].

 

Sample time in sec (-1 for inherited): — Sample time for this block. It is the same meaning as general Simulink block .

 

 

 

Ports


 

Input

 

For all input ports, it depends on parameter "Input Port Variable Name in C language". The parameter "Input Port Variable Name in C language" vector element QTY is Input data port QTY. Please read parameter "Input Port Variable Name in C language" above. and see description below

 

Data1 — scalar. Parameter "Input Port Variable Name in C language"  vector's first element decides its input port name. and Parameter "Input Port data type"  vector's first element decides its input port data type.

.

Data2 — scalar. Parameter "Input Port Variable Name in C language"  vector's 2nd element decides its input port name. and Parameter "Input Port data type"  vector's 2nd element decides its input port data type.

 

Data3 — scalar. Parameter "Input Port Variable Name in C language"  vector's 3rd element decides its input port name. and Parameter "Input Port data type"  vector's 3rd element decides its input port data type.

 

......

 

Data n — scalar. Parameter "Input Port Variable Name in C language"  vector's number n element decides its input port name. and Parameter "Input Port data type"  vector's number n element decides its input port data type

 

 

Outport


 

For all output ports, it depends on parameter "Output Port Variable Name in C language". The parameter "Output Port Variable Name in C language" vector element QTY is output data port QTY. Please read parameter "Output Port Variable Name in C language" above. and see description below

 

Data1 — scalar. Parameter "Output Port Variable Name in C language"  vector's first element decides its output port name. and Parameter "Output Port data type"  vector's first element decides its output port data type.

.

Data2 — scalar. Parameter "Output Port Variable Name in C language"  vector's 2nd element decides its output port name. and Parameter "Output Port data type"  vector's 2nd element decides its output port data type.

 

Data3 — scalar. Parameter "Output Port Variable Name in C language"  vector's 3rd element decides its output port name. and Parameter "Output Port data type"  vector's 3rd element decides its output port data type.

 

......

 

Data n — scalar. Parameter "Output Port Variable Name in C language"  vector's number n element decides its output port name. and Parameter "Output Port data type"  vector's number n element decides its output port data type

 

 

 

Examples