]> rtime.felk.cvut.cz Git - socketcan-simulink.git/blob - blocks/tlc_c/rpp_can_common.tlc
e38b474d3810c4731bc75e2b5c84607507b24963
[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("<fcntl.h>")>
68         %<LibAddToCommonIncludes("<errno.h>")>
69   %endif
70 %endfunction
71
72 %function RppCanAssignMsgObj(msgObjNum, blockInfo) void
73   %if msgObjNum < 1 || msgObjNum > ::_RPP_MAX_MAILBOX_CNT
74         %<LibBlockReportError(block, "Invalid mailbox number!")>
75   %endif
76   %addtorecord Rpp.Can.Ctrl[blockInfo.Controller-1] MsgObj%<msgObjNum>UsedBy blockInfo
77   %assign blockInfo.MsgObj = msgObjNum
78 %endfunction
79
80 %function RppCanGetBlockInfoFromMsgObj(module, mailbox_number)
81   %return Rpp.Can.Ctrl[module-1].MsgObj%<mailbox_number>UsedBy
82 %endfunction
83
84 %function RppCanIsMsgObjAssigned(module, mailbox_number)
85   %return ISFIELD(Rpp.Can.Ctrl[%<module-1>], "MsgObj%<mailbox_number>UsedBy")
86 %endfunction
87
88 %% Function: RppFindFreeMailboxNumber =========================================
89 %%      Returns the lowest possible free mailbox number or 0 if no mailbox is free.
90
91 %function RppFindFreeMailboxNumber(module) void
92         %foreach mn = ::_RPP_MAX_MAILBOX_CNT + 1
93             %if mn == 0
94                   %continue
95                 %endif
96                 %if RppCanIsMsgObjAssigned(module, mn) == 0
97                         %return %<mn>
98                 %endif
99         %endforeach
100         %return 0
101 %endfunction
102
103 %% Function: RppPlaceAutomaticMailboxNumber ===================================
104 %%      Assignes an automaticaly determined mailbox number for the CAN block.
105
106 %function RppPlaceAutomaticMailboxNumber(blockInfo) void
107         %assign msg_obj_number = RppFindFreeMailboxNumber(blockInfo.Controller)
108         %if %<msg_obj_number> == 0
109                 %<LibBlockReportError(block, "Too many blocks in one module!")>
110         %endif
111         %addtorecord blockInfo ManualMsgObj 0 /% For MsgObj collision detection %/
112         %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>
113 %endfunction
114
115 %% Function: RppPlaceManualMailboxNumber ======================================
116
117 %% Assigns a manualy specified mailbox number to the CAN blocks.
118 %% Reports collisions with other manually assigned blocks and optionally
119 %% reassignes mailbox numbers of previously assigned automatic blocks.
120 %%
121 %%      Params: msg_obj_num             The requested number of the mailbox.
122
123 %function RppPlaceManualMailboxNumber(blockInfo, msg_obj_number) void
124         %if RppCanIsMsgObjAssigned(blockInfo.Controller, msg_obj_number)
125           %assign prevBlockInfo = RppCanGetBlockInfoFromMsgObj(blockInfo.Controller, msg_obj_number)
126           %if prevBlockInfo.ManualMsgObj
127                 %<LibBlockReportError(block, "Colliding mailbox number found!")>
128           %endif
129           %assign new_msg_obj_number = RppFindFreeMailboxNumber(blockInfo.Controller)
130           %if new_msg_obj_number == 0
131                 %<LibBlockReportError(block, "Too many blocks in one module!")>
132           %endif
133           %<RppCanAssignMsgObj(new_msg_obj_number, prevBlockInfo)>
134         %endif
135         %addtorecord blockInfo ManualMsgObj 1 /% For MsgObj collision detection %/
136         %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>
137 %endfunction
138
139 %% [EOF]