emCustomCBlock | Top Next

emCustomCBlock set up user's manual C code Block. Since R2019b Library: embeddedCreatorLib ( Dafulai Electronics) / 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"/"Gemini"/"DeepSeek" to create C code, and put it into this block. Manual C Code has 3 parts, the 1st part is to define global variables and implementation of interrupt service functions or callback functions or regular functions which are used in this block, the 2nd part is to run once for initialization, the 3rd part is to run periodically in sample time, the 3rd part code must be non-blocking C code. For the 1st part, you just open your IDE, and open .c file, which is under folder "Simulink Generated Files", and look for comment "//Customized ISR or Global Code for Block: Your block name ------Start". And add declaration of your global variables and implementation of functions or ISR or callback just under comment "//Add your code" and before comment "//Customized ISR or Global Code for Block: Your block name ------End" For initialization part, it is the same .c file as the 1st part, 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". This part C code can be blocking or non-blocking. Don't care. For periodical part, it is the same .c file as the 1st part, 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". This part C code must be non-blocking. Or blocking but maximum block time is less than 0.5ms. 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. Your emCustomCBlock is reusable. You can copy to the other project. Notes: 1 Please don't edit any comment which is created by Simulink code. 2 For MplabX IDE project, You can find your previous C code Simulink created including your customized C code 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 in Folder: "Your embedded project directory used in configuration" \ simulink_generated_files_backup_backup. For STM32Cube and TI C2000 CCS Project, You can find your previous C code Simulink created including your customized C code in Folder: "Your Computer User directory" \ Simulink Model name (without suffix slx) \ simulink_generated_files_backup. You can find your C code of the one before the previous in Folder: "Your Computer User directory" \ Simulink Model name (without suffix slx) \ simulink_generated_files_backup_backup. 3 If you modified C code of emCustomCBlock in your IDE, and If you want "this emCustomCBlock" to keep your customized C source code, please execute "build" command from simulink after you debug in IDE. Or your emCustomCBlock cannot keep the latest customized C source code when you copy "this emCustomCBlock" to the other simulink project model. Parameters Please double click this block to open parameters dialog below: 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