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 : sfunction_canreceive.tlc
37 %% TLC file for configuring the CAN RX buffer on the TI TMS570LS3137 MCU
40 %% BlockTypeSetup() : refs/rtw_tlc.pdf p. 277
41 %% Outputs() : refs/rtw_tlc.pdf p. 281
44 %implements sfunction_canreceive "C"
47 %include "rpp_can_common.tlc"
49 %% Function: BlockTypeSetup ====================================================
50 %function BlockTypeSetup(block, system) void
51 %<RppCommonBlockTypeSetup(block, system)>
52 %<RppCANCommonBlockTypeSetup()>
55 %% Function: BlockInstanceSetup ================================================
56 %function BlockInstanceSetup(block, system) void
58 %assign module_id_par = LibBlockParameterValue(module_id, 0)
59 %assign mailbox_num_par = LibBlockParameterValue(mailbox_id, 0)
60 %assign message_type_par = LibBlockParameterValue(message_type, 0)
61 %assign message_id_par = LibBlockParameterValue(message_id, 0)
62 %assign data_type_par = LibBlockParameterValue(data_type, 0)
63 %assign message_filter_par = LibBlockParameterValue(message_filter, 0)
64 %assign message_mask_par = LibBlockParameterValue(message_mask, 0)
65 %assign mailbox_auto_par = LibBlockParameterValue(mailbox_auto, 0)
67 %if message_filter_par == 1
68 %assign mask = 8388607 %% 0x7FFFFF
70 %assign mask = message_mask_par
73 %% This seems to be the only way to add a record to an array. The
74 %% other way - creating the record with %createrecord and adding
75 %% it later with %addtorecord doesn't work.
76 %addtorecord Rpp.Can.Rx Block { ...
77 HwObj Rpp.Can.Rx.NumBlocks; ...
78 Controller module_id_par; ...
79 MsgObj 0; /% Invalid MsgObj - proper value will be assigned later %/ ...
80 Type message_type_par; ...
81 Id message_id_par; ...
83 Name "%<LibGetBlockName(block)>" ...
85 %assign blockInfo = Rpp.Can.Rx.Block[Rpp.Can.Rx.NumBlocks] /% create alias to the record %/
86 %if %<mailbox_auto_par>==1
87 %<RppPlaceAutomaticMailboxNumber(blockInfo)>
89 %<RppPlaceManualMailboxNumber(blockInfo, mailbox_num_par)>
92 /% Remember which blockInfo record is associated wit this block %/
93 %addtorecord block RppRxInfo blockInfo
94 %assign Rpp.Can.Rx.NumBlocks = Rpp.Can.Rx.NumBlocks + 1
97 %% Function: Start =============================================================
98 %function Start(block, system) Output
100 %if !SLibCodeGenForSim()
102 %if EXISTS(::rpp_can_config_present) == 0
103 %<LibBlockReportError(block, "CAN buc configuration block not present!")>
110 %% Function: Outputs ===========================================================
111 %function Outputs(block, system) Output
113 %if !SLibCodeGenForSim()
114 %assign data_type_par = LibBlockParameterValue(data_type, 0)
115 %assign message = LibBlockOutputSignal(1, "", "", 1)
118 struct can_frame sc_frame;
122 ret = recv(can_rx_handles[%<RppRxInfo.HwObj>], &sc_frame, sizeof(sc_frame), 0);
125 printf("Receiving CAN message failed (%s).\n", "%<RppRxInfo.Name>");
127 } else if (ret < sizeof(sc_frame)) {
128 printf("Receiving CAN message (%s) returns truncated length %d.\n", "%<RppRxInfo.Name>", ret);
130 dlc = sc_frame.can_dlc;
133 %if %<data_type_par>==4
135 %<message>.Length = dlc;
136 %<message>.ID = sc_frame.can_id & CAN_EFF_MASK;
138 for (i = 0; i < dlc; i++ ) {
139 %<message>.Data[i] = sc_frame.data[i];
140 printf("%X ", sc_frame.data[i]);
142 %elseif %<data_type_par>==3
144 unsigned int msg = (sc_frame.data[0]) |
145 (sc_frame.data[1]<<8) |
146 (sc_frame.data[2]<<16) |
147 (sc_frame.data[3]<<24);
149 msg &= 0xffffffff << (8 * dlc);
151 printf("32b: %X ", msg);
152 %elseif %<data_type_par>==2
154 unsigned int msg = (sc_frame.data[0]) |
155 (sc_frame.data[1]<<8);
157 msg &= 0xffffffff << (8 * dlc);
159 printf("16b: %X ", msg);
162 unsigned int msg = sc_frame.data[0];
164 printf("8b: %X ", msg);
168 %% Call a function to process the received message via function-call subsystem
169 %foreach callIdx = NumSFcnSysOutputCalls
170 %if LibIsEqual(SFcnSystemOutputCall[callIdx].BlockToCall,"unconnected")
173 %% call the downstream system
174 %<LibBlockExecuteFcnCall(block, callIdx)>\