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