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?
0 个评论
回答(2 个)
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:
0 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Code Generation for Custom Blocks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!