]> rtime.felk.cvut.cz Git - socketcan-simulink.git/blob - blocks/tlc_c/rpp_can_common.tlc
4c038c0d23e3f49d55ddbf31787701ec650793aa
[socketcan-simulink.git] / 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 %% Redistribution and use in source and binary forms, with or without
8 %% modification, are permitted provided that the following conditions are
9 %% met:
10 %%
11 %% 1. Redistributions of source code must retain the above copyright
12 %%    notice, this list of conditions and the following disclaimer.
13 %%
14 %% 2. Redistributions in binary form must reproduce the above copyright
15 %%    notice, this list of conditions and the following disclaimer in the
16 %%    documentation and/or other materials provided with the
17 %%    distribution.
18 %%
19 %% 3. Neither the name of the copyright holder nor the names of its
20 %%    contributors may be used to endorse or promote products derived
21 %%    from this software without specific prior written permission.
22 %%
23 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 %% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 %% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 %% A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 %% HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 %% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 %% LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 %% DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 %% THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 %% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 %% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 %%
35 %% File : rpp_can_common.tlc
36 %% Abstract:
37 %%     TLC file with common functions and variables for sfunction_canreceive, sfunction_cansetup and sfunction_cantransmit
38 %%
39 %% References:
40 %%     Coding Convention: refs/rtw_tlc.pdf p. 58 (TODO)
41 %%
42
43 %% Function: RppCANCommonBlockTypeSetup ========================================
44 %%      Declares and initializes all common variables and constants.
45
46 %function RppCANCommonBlockTypeSetup() void
47   %if EXISTS("::_RPP_MAX_MAILBOX_CNT") == 0
48         %assign ::_RPP_MAX_MAILBOX_CNT = 64
49
50         /% Global data structure used in call CAN blocks %/
51         %createrecord ::Rpp { Can { ...
52           Tx { NumBlocks 0 } ...
53           Rx { NumBlocks 0 } ...
54         }}
55
56         /% Add records for helping with message object allocation  %/
57         %foreach i = 3  %% We have 3 CAN controllers
58           %addtorecord Rpp.Can Ctrl {}
59         %endforeach
60
61         %<LibAddToCommonIncludes("<stdio.h>")>
62         %<LibAddToCommonIncludes("<sys/socket.h>")>
63         %<LibAddToCommonIncludes("<net/if.h>")>
64         %<LibAddToCommonIncludes("<sys/ioctl.h>")>
65         %<LibAddToCommonIncludes("<linux/can.h>")>
66         %<LibAddToCommonIncludes("<linux/can/raw.h>")>
67         %<LibAddToCommonIncludes("<stdlib.h>")>
68         %<LibAddToCommonIncludes("<fcntl.h>")>
69         %<LibAddToCommonIncludes("<errno.h>")>
70   %endif
71 %endfunction
72
73 %function RppCanAssignMsgObj(msgObjNum, blockInfo) void
74   %if msgObjNum < 1 || msgObjNum > ::_RPP_MAX_MAILBOX_CNT
75         %<LibBlockReportError(block, "Invalid mailbox number!")>
76   %endif
77   %addtorecord Rpp.Can.Ctrl[blockInfo.Controller-1] MsgObj%<msgObjNum>UsedBy blockInfo
78   %assign blockInfo.MsgObj = msgObjNum
79 %endfunction
80
81 %function RppCanGetBlockInfoFromMsgObj(module, mailbox_number)
82   %return Rpp.Can.Ctrl[module-1].MsgObj%<mailbox_number>UsedBy
83 %endfunction
84
85 %function RppCanIsMsgObjAssigned(module, mailbox_number)
86   %return ISFIELD(Rpp.Can.Ctrl[%<module-1>], "MsgObj%<mailbox_number>UsedBy")
87 %endfunction
88
89 %% Function: RppFindFreeMailboxNumber =========================================
90 %%      Returns the lowest possible free mailbox number or 0 if no mailbox is free.
91
92 %function RppFindFreeMailboxNumber(module) void
93         %foreach mn = ::_RPP_MAX_MAILBOX_CNT + 1
94             %if mn == 0
95                   %continue
96                 %endif
97                 %if RppCanIsMsgObjAssigned(module, mn) == 0
98                         %return %<mn>
99                 %endif
100         %endforeach
101         %return 0
102 %endfunction
103
104 %% Function: RppPlaceAutomaticMailboxNumber ===================================
105 %%      Assignes an automaticaly determined mailbox number for the CAN block.
106
107 %function RppPlaceAutomaticMailboxNumber(blockInfo) void
108         %assign msg_obj_number = RppFindFreeMailboxNumber(blockInfo.Controller)
109         %if %<msg_obj_number> == 0
110                 %<LibBlockReportError(block, "Too many blocks in one module!")>
111         %endif
112         %addtorecord blockInfo ManualMsgObj 0 /% For MsgObj collision detection %/
113         %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>
114 %endfunction
115
116 %% Function: RppPlaceManualMailboxNumber ======================================
117
118 %% Assigns a manualy specified mailbox number to the CAN blocks.
119 %% Reports collisions with other manually assigned blocks and optionally
120 %% reassignes mailbox numbers of previously assigned automatic blocks.
121 %%
122 %%      Params: msg_obj_num             The requested number of the mailbox.
123
124 %function RppPlaceManualMailboxNumber(blockInfo, msg_obj_number) void
125         %if RppCanIsMsgObjAssigned(blockInfo.Controller, msg_obj_number)
126           %assign prevBlockInfo = RppCanGetBlockInfoFromMsgObj(blockInfo.Controller, msg_obj_number)
127           %if prevBlockInfo.ManualMsgObj
128                 %<LibBlockReportError(block, "Colliding mailbox number found!")>
129           %endif
130           %assign new_msg_obj_number = RppFindFreeMailboxNumber(blockInfo.Controller)
131           %if new_msg_obj_number == 0
132                 %<LibBlockReportError(block, "Too many blocks in one module!")>
133           %endif
134           %<RppCanAssignMsgObj(new_msg_obj_number, prevBlockInfo)>
135         %endif
136         %addtorecord blockInfo ManualMsgObj 1 /% For MsgObj collision detection %/
137         %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>
138 %endfunction
139
140 %% [EOF]