# TargetLink Function Blocks ----------------------------- [TOC] ## Why​ To insert a function block in a subsystem will insert a function call in the C code generated from the model. This will reduce the complexity numbers, increase testability and protect against memory problems if there is an interrupt​ and the function needs to be saved temporary to the memory. ## How ### ​Insert FUNCTION Block Insert a green FUNCTION block in the subsystem. Double click the green block and set "Step function name" to `$N_$B` and "Init function name" to `INIT_$N_$B`. ![function_block](../images/target_link/function_block.jpg) ## Important Aspects in the Model There are two important aspects that need attention. ### Signal Feedback​​​ When using feedback from one subsystem block to another, it is important to use the unit delay block when one uses function blocks. Strangely one also needs a [rename block](./naming_convention.md#rename-simulink-block) after, since the unit delay block in this case can not have a readable signal configuration. ### Model Output Signals Model output signals, prefix starting with *sVc* and *yVc*, needs to have copies that are local readables, prefix *rVc* and *xVc*, otherwise they will be removed by the current code optimization. ## Code Example ``` c Void VcModelName(Void) { /* SLStaticLocalInit: Default storage class for static local variables with initvalue | Width: 8 */ static Bool X_SModelName2_UnitDelay1 = 0; /* call of function: VcModelName/VcModelName/2_SignalCalc */ VcModelName_2_SignalCalc(); /* VcModelName/VcModelName/RenameSignal/InPort */ xVcModelName_B_ConvEffCmplt = X_SModelName2_UnitDelay1; /* call of function: VcModelName/VcModelName/1_Entry */ VcModelName_1_Entry(); /* call of function: VcModelName/VcModelName/3_Evaluation */ VcModelName_3_Evaluation(); /* TargetLink outport: VcModelName/sVcModelName_rt_CnvnMeasd1 */ sVcModelName_m_NoxUs = SModelName44_Switch; /* call of function: VcModelName/VcModelName/4_Judgement */ VcModelName_4_Judgement(); ​} #include "MemMap_CVC_STOP.h" ​ /**************************************************************************************************\ *** FUNCTION: *** VcModelName_1_Entry *** *** DESCRIPTION: *** *** *** PARAMETERS: *** Type Name Description *** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *** *** RETURNS: *** Void *** *** SETTINGS: *** \**************************************************************************************************/ #include "MemMap_CVC_START.h" Void VcModelName_1_Entry(Void) {​ ```