How to read the DWORK vector information from the current block

9 次查看(过去 30 天)
I am creating a .tlc file to generate code for a block which will perform a safety check on its parents DWORK vector.
This block will take the dwork vector from the parent block (in this case the block is running safety critical code) and create a copy of the dwork vector state and perform a bitwise complement on it, so that I can ensure the information in my safety critical code is not corrupted from an external source.
How would I read the name and struct typedef of the parent atomic subsystems DWORK memory in order to do this?

回答(2 个)

Charu
Charu 2025-6-17
Hello Duncan,
According to my understanding you are tyring to implement TLC file for a Simulink block that performs a safety and then generate code that creates a copy of the parent’s internal state (DWork) and applies a bitwise complement to it.
To access the parent atomic subsystem’s Dwork in a TLC file, for a Simulink block that performs a safety check by accessing its parent atomic subsystem’s DWork vector, you can use TLC function “LibParentsBlock”.
%assign parent = LibParentBlock(block)
“LibBlockDWorkName”, “LibBlockDWorkDataTypeName”, and “LibBlockDWorkWidth”, these functions can be used to extract details about each Dwork element.
%assign numDWorks = LibBlockDWorkCount(parent)
%foreach i = 0:numDWorks-1
%assign dworkName = LibBlockDWorkName(parent, i)
%assign dworkType = LibBlockDWorkDataTypeName(parent, i)
%assign dworkWidth = LibBlockDWorkWidth(parent, i)
%end
To get the C struct typedef name and instance used in generated code, use “LibGetRecordIdentifier("DWork", sysIdx)” and “LibGetRecordIdentifier("DWorkInstance", sysIdx)”, where sysIdx is obtained from “LibParentSystemIdx(parent)”. This allows to reference the parent’s DWork vector in generated C code, apply the bitwise complement, and integrate it into safety check logic. Keeping in mind that the parent subsystem is atomic and has explicitly defined DWork.
%assign dworkStructType = LibGetRecordIdentifier("DWork", sysIdx)
%assign dworkInstance = LibGetRecordIdentifier("DWorkInstance", sysIdx)
Here is the documentation links for the above used functions:

Deepak
Deepak 2025-6-17
I understand that you are looking to access the DWORK memory of a parent atomic subsystem from a custom TLC file in order to perform a safety check by copying and bitwise-complementing the state. In the TLC file, you can retrieve the parent DWORK’s C variable name, type, and size using standard Embedded Coder TLC functions like "LibBlockDWorkName", "LibBlockDWorkDataTypeName", and "LibBlockDWorkWidth".
For example, within the "Update" or "Outputs" function of your TLC:
%assign dworkName = LibBlockDWorkName(0)
%assign dworkType = LibBlockDWorkDataTypeName(0, "")
%assign dworkWidth = LibBlockDWorkWidth(0)
%<dworkType> dworkCopy[%<dworkWidth>];
for (int i = 0; i < %<dworkWidth>; ++i) {
dworkCopy[i] = ~(%<dworkName>[i]);
}
This will generate C code that copies and bitwise-inverts each element of the parent block's DWORK, helping ensure integrity in your safety-critical logic.
I hope this hleps.
  1 个评论
Duncan Hennion
Duncan Hennion 2025-6-17
编辑:Duncan Hennion 2025-6-17
Thanks for the response @Deepak,
This suggestion only seems to give me the DWorkName and Types for the DWORK assigned to this sfunction. The parent models DWORK which I want this block to check is upwards in the rtw heirarchy.
I made my own functions to get the parent models dwork vector name and type strings like so. For now it doesn't contain much checking.
%function FcnGetDworkName(system)
%assign dwVGIdx = system.DWorkVarGroupIndex[0]
%assign dwName = ::CompiledModel.VarGroups.VarGroup[dwVGIdx].Name
%return dwName
%endfunction
%function FcnGetDworkTypeStr(system)
%assign dwVGIdx = system.DWorkVarGroupIndex[0]
%assign dwVarGroup = ::CompiledModel.VarGroups.VarGroup[dwVGIdx]
%assign dwCGTypeIdx = dwVarGroup.CGTypeIdx
%assign dwType = LibCGTypeName(dwCGTypeIdx)
%return dwType
%endfunction
I can then use dwType and dwName to create the data and provide functions to access it in the generated code for the model block.
%% Collect the DWork vector data type and name
%assign dwType = FcnGetDworkTypeStr(system)
%assign dwName = FcnGetDworkName(system)
%% assign a name for the copy of the dwork vector
%% we will make a compliment for.
%assign dwNameCpl = dwName + "_cpl"
%% define a copy of the d work vector and place in c file definitions section
%assign srcFile = LibCreateSourceFile("Source","Simulink",system.SystemSourceFileName)
%openfile tmpbuf
/*Assign data storage for DWork compliment vector for S-Function (czv_lvl2_check_cmpl)*/
#define START_SECTION_Mo_RamCpl
#include "mo_pragma.h"
static %<dwType> %<dwNameCpl> ;
#define STOP_SECTION_Mo_RamCpl
#include "mo_pragma.h"
%closefile tmpbuf
%<LibSetSourceFileSection(srcFile, "Definitions", tmpbuf)>
%% define a copy of the d work vector and place in c file definitions section
%openfile funcBuff
/*Fucntion definitions for S-Function (czv_lvl2_check_cmpl)*/
/*function: czv_lvl2_write cmpl
* Description: Writes the bitwise complement of the dwork vector
*into a copy of the models dwork vector*/
void czv_lvl2_write_cmpl(void);
void czv_lvl2_write_cmpl() {
uint8 * p = (uint8 *)&%<dwName>;
uint8 * pcmpl = (uint8 *)&%<dwNameCpl>;
uint32 i = 0;
for(i=0; i < sizeof(%<dwType>); i++)
{
pcmpl[i] = (p[i])^0xFFu;
}
}
/*function: czv_lvl2_check_cmpl cmpl
* Description: Checks the complement of the dwork vector is valid
* return: u8, true if the check has passed*/
uint8 czv_lvl2_check_cmpl(void);
uint8 czv_lvl2_check_cmpl() {
uint8 * p = (uint8 *)&%<dwName>;
uint8 * pcmpl = (uint8 *)&%<dwNameCpl>;
uint8 result = 0xFFu;
uint32 i = 0;
for(i=0; i < sizeof(%<dwType>); i++)
{
result = (p[i] ^ pcmpl[i]) & result;
}
return (result == 0xFFu);
}
%closefile funcBuff
%<LibSetSourceFileSection(srcFile, "Functions", funcBuff)>
%openfile updateBuff
/* S-Function (czv_lvl2_check_cmpl)
* Write complement NEEDS to be placed at the end of all LVL2 update code*/
czv_lvl2_write_cmpl();
%closefile updateBuff
%<LibSystemUpdateCustomCode(system,updateBuff,"trailer")>
%<LibSystemInitializeCustomCode(system,updateBuff,"trailer")>

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Code Generation for Custom Blocks 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by