1 %% Copyright (C) 2014 Czech Technical University in Prague
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
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are
12 %% 1. Redistributions of source code must retain the above copyright
13 %% notice, this list of conditions and the following disclaimer.
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
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.
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.
36 %% File : rpp_can_common.tlc
38 %% TLC file with common functions and variables for sfunction_canreceive, sfunction_cansetup and sfunction_cantransmit
41 %% Coding Convention: refs/rtw_tlc.pdf p. 58 (TODO)
44 %% Function: RppCANCommonBlockTypeSetup ========================================
45 %% Declares and initializes all common variables and constants.
47 %function RppCANCommonBlockTypeSetup() void
48 %if EXISTS("::_RPP_MAX_MAILBOX_CNT") == 0
49 %assign ::_RPP_MAX_MAILBOX_CNT = 64
51 /% Global data structure used in call CAN blocks %/
52 %createrecord ::Rpp { Can { ...
53 Tx { NumBlocks 0 } ...
54 Rx { NumBlocks 0 } ...
57 /% Add records for helping with message object allocation %/
58 %foreach i = 3 %% We have 3 CAN controllers
59 %addtorecord Rpp.Can Ctrl {}
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>")>
74 %function RppCanAssignMsgObj(msgObjNum, blockInfo) void
75 %if msgObjNum < 1 || msgObjNum > ::_RPP_MAX_MAILBOX_CNT
76 %<LibBlockReportError(block, "Invalid mailbox number!")>
78 %addtorecord Rpp.Can.Ctrl[blockInfo.Controller-1] MsgObj%<msgObjNum>UsedBy blockInfo
79 %assign blockInfo.MsgObj = msgObjNum
82 %function RppCanGetBlockInfoFromMsgObj(module, mailbox_number)
83 %return Rpp.Can.Ctrl[module-1].MsgObj%<mailbox_number>UsedBy
86 %function RppCanIsMsgObjAssigned(module, mailbox_number)
87 %return ISFIELD(Rpp.Can.Ctrl[%<module-1>], "MsgObj%<mailbox_number>UsedBy")
90 %% Function: RppFindFreeMailboxNumber =========================================
91 %% Returns the lowest possible free mailbox number or 0 if no mailbox is free.
93 %function RppFindFreeMailboxNumber(module) void
94 %foreach mn = ::_RPP_MAX_MAILBOX_CNT + 1
98 %if RppCanIsMsgObjAssigned(module, mn) == 0
105 %% Function: RppPlaceAutomaticMailboxNumber ===================================
106 %% Assignes an automaticaly determined mailbox number for the CAN block.
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!")>
113 %addtorecord blockInfo ManualMsgObj 0 /% For MsgObj collision detection %/
114 %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>
117 %% Function: RppPlaceManualMailboxNumber ======================================
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.
123 %% Params: msg_obj_num The requested number of the mailbox.
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!")>
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!")>
135 %<RppCanAssignMsgObj(new_msg_obj_number, prevBlockInfo)>
137 %addtorecord blockInfo ManualMsgObj 1 /% For MsgObj collision detection %/
138 %<RppCanAssignMsgObj(msg_obj_number, blockInfo)>