1 %% Copyright (C) 2014 Czech Technical University in Prague
4 %% Michal Horn <hornmich@fel.cvut.cz>
5 %% Michal Sojka <sojkam1@fel.cvut.cz>
7 %% Redistribution and use in source and binary forms, with or without
8 %% modification, are permitted provided that the following conditions are
11 %% 1. Redistributions of source code must retain the above copyright
12 %% notice, this list of conditions and the following disclaimer.
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
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.
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.
35 %% File : rpp_can_common.tlc
37 %% TLC file with common functions and variables for sfunction_canreceive, sfunction_cansetup and sfunction_cantransmit
40 %% Coding Convention: refs/rtw_tlc.pdf p. 58 (TODO)
43 %% Function: RppCANCommonBlockTypeSetup ========================================
44 %% Declares and initializes all common variables and constants.
46 %function RppCANCommonBlockTypeSetup() void
47 %if EXISTS("::_RPP_MAX_MAILBOX_CNT") == 0
48 %assign ::_RPP_MAX_MAILBOX_CNT = 64
50 /% Global data structure used in call CAN blocks %/
51 %createrecord ::Rpp { Can { ...
52 Tx { NumBlocks 0 } ...
53 Rx { NumBlocks 0 } ...
56 /% Add records for helping with message object allocation %/
57 %foreach i = 3 %% We have 3 CAN controllers
58 %addtorecord Rpp.Can Ctrl {}
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>")>
73 %function RppCanAssignMsgObj(msgObjNum, blockInfo) void
74 %if msgObjNum < 1 || msgObjNum > ::_RPP_MAX_MAILBOX_CNT
75 %<LibBlockReportError(block, "Invalid mailbox number!")>
77 %addtorecord Rpp.Can.Ctrl[blockInfo.Controller-1] MsgObj%<msgObjNum>UsedBy blockInfo
78 %assign blockInfo.MsgObj = msgObjNum
81 %function RppCanGetBlockInfoFromMsgObj(module, mailbox_number)
82 %return Rpp.Can.Ctrl[module-1].MsgObj%<mailbox_number>UsedBy
85 %function RppCanIsMsgObjAssigned(module, mailbox_number)
86 %return ISFIELD(Rpp.Can.Ctrl[%<module-1>], "MsgObj%<mailbox_number>UsedBy")
89 %% Function: RppFindFreeMailboxNumber =========================================
90 %% Returns the lowest possible free mailbox number or 0 if no mailbox is free.
92 %function RppFindFreeMailboxNumber(module) void
93 %foreach mn = ::_RPP_MAX_MAILBOX_CNT + 1
97 %if RppCanIsMsgObjAssigned(module, mn) == 0
104 %% Function: RppPlaceAutomaticMailboxNumber ===================================
105 %% Assignes an automaticaly determined mailbox number for the CAN block.
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!")>
112 %addtorecord blockInfo ManualMsgObj 0 /% For MsgObj collision detection %/
113 %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>
116 %% Function: RppPlaceManualMailboxNumber ======================================
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.
122 %% Params: msg_obj_num The requested number of the mailbox.
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!")>
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!")>
134 %<RppCanAssignMsgObj(new_msg_obj_number, prevBlockInfo)>
136 %addtorecord blockInfo ManualMsgObj 1 /% For MsgObj collision detection %/
137 %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>