]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/blob - rpp/blocks/tlc_c/rpp_can_common.tlc
Reworked CAN TLCs
[jenkicar/rpp-simulink.git] / rpp / blocks / tlc_c / rpp_can_common.tlc
1 %% Copyright (C) 2014 Czech Technical University in Prague
2 %%
3 %% Authors:
4 %%     Michal Horn  <hornmich@fel.cvut.cz>
5 %%     Michal Sojka <sojkam1@fel.cvut.cz>
6 %%
7 %% This document contains proprietary information belonging to Czech
8 %% Technical University in Prague. Passing on and copying of this
9 %% document, and communication of its contents is not permitted
10 %% without prior written authorization.
11 %%
12 %% File : rpp_can_common.tlc
13 %% Abstract:
14 %%     TLC file with common functions and variables for sfunction_canreceive, sfunction_cansetup and sfunction_cantransmit
15 %%
16 %% References:
17 %%     Coding Convention: refs/rtw_tlc.pdf p. 58 (TODO)
18 %%
19
20 %% Function: RppCANCommonBlockTypeSetup ========================================
21 %%      Declares and initializes all common variables and constants.
22
23 %function RppCANCommonBlockTypeSetup() void
24   %if EXISTS("::_RPP_MAX_MAILBOX_CNT") == 0
25         %assign ::_RPP_MAX_MAILBOX_CNT = 64
26
27         /% Global data structure used in call CAN blocks %/
28         %createrecord ::Rpp { Can { ...
29           Tx { NumBlocks 0 } ...
30           Rx { NumBlocks 0 } ...
31         }}
32
33         /% Add records for helping with message object allocation  %/
34         %foreach i = 3  %% We have 3 CAN controllers
35           %addtorecord Rpp.Can Ctrl {}
36         %endforeach
37   %endif
38 %endfunction
39
40 %function RppCanAssignMsgObj(msgObjNum, blockInfo) void
41   %if msgObjNum < 1 || msgObjNum > ::_RPP_MAX_MAILBOX_CNT
42         %<LibBlockReportError(block, "Invalid mailbox number!")>
43   %endif
44   %addtorecord Rpp.Can.Ctrl[blockInfo.Controller-1] MsgObj%<msgObjNum>UsedBy blockInfo
45   %assign blockInfo.MsgObj = msgObjNum
46 %endfunction
47
48 %function RppCanGetBlockInfoFromMsgObj(module, mailbox_number)
49   %return Rpp.Can.Ctrl[module-1].MsgObj%<mailbox_number>UsedBy
50 %endfunction
51
52 %function RppCanIsMsgObjAssigned(module, mailbox_number)
53   %return ISFIELD(Rpp.Can.Ctrl[%<module-1>], "MsgObj%<mailbox_number>UsedBy")
54 %endfunction
55
56 %% Function: RppFindFreeMailboxNumber =========================================
57 %%      Returns the lowest possible free mailbox number or 0 if no mailbox is free.
58
59 %function RppFindFreeMailboxNumber(module) void
60         %foreach mn = ::_RPP_MAX_MAILBOX_CNT + 1
61             %if mn == 0
62                   %continue
63                 %endif
64                 %if RppCanIsMsgObjAssigned(module, mn) == 0
65                         %return %<mn>
66                 %endif
67         %endforeach
68         %return 0
69 %endfunction
70
71 %% Function: RppPlaceAutomaticMailboxNumber ===================================
72 %%      Assignes an automaticaly determined mailbox number for the CAN block.
73
74 %function RppPlaceAutomaticMailboxNumber(blockInfo) void
75         %assign msg_obj_number = RppFindFreeMailboxNumber(blockInfo.Controller)
76         %if %<msg_obj_number> == 0
77                 %<LibBlockReportError(block, "Too many blocks in one module!")>
78         %endif
79         %addtorecord blockInfo ManualMsgObj 0 /% For MsgObj collision detection %/
80         %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>
81 %endfunction
82
83 %% Function: RppPlaceManualMailboxNumber ======================================
84
85 %% Assigns a manualy specified mailbox number to the CAN blocks.
86 %% Reports collisions with other manually assigned blocks and optionally
87 %% reassignes mailbox numbers of previously assigned automatic blocks.
88 %%
89 %%      Params: msg_obj_num             The requested number of the mailbox.
90
91 %function RppPlaceManualMailboxNumber(blockInfo, msg_obj_number) void
92         %if RppCanIsMsgObjAssigned(blockInfo.Controller, msg_obj_number)
93           %assign prevBlockInfo = RppCanGetBlockInfoFromMsgObj(blockInfo.Controller, msg_obj_number)
94           %if prevBlockInfo.ManualMsgObj
95                 %<LibBlockReportError(block, "Colliding mailbox number found!")>
96           %endif
97           %assign new_msg_obj_number = RppFindFreeMailboxNumber(blockInfo.Controller)
98           %if new_msg_obj_number == 0
99                 %<LibBlockReportError(block, "Too many blocks in one module!")>
100           %endif
101           %<RppCanAssignMsgObj(new_msg_obj_number, prevBlockInfo)>
102         %endif
103         %addtorecord blockInfo ManualMsgObj 1 /% For MsgObj collision detection %/
104         %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>
105 %endfunction
106
107 %% [EOF]