]> rtime.felk.cvut.cz Git - socketcan-simulink.git/blob - blocks/tlc_c/sfunction_canreceive.tlc
eb67561f2c426edcd3cd196b7f441cae6a1a8d74
[socketcan-simulink.git] / blocks / tlc_c / sfunction_canreceive.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 : sfunction_canreceive.tlc
36 %% Abstract:
37 %%     TLC file for configuring the CAN RX buffer on the TI TMS570LS3137 MCU
38 %%
39 %% References:
40 %%     BlockTypeSetup() : refs/rtw_tlc.pdf p. 277
41 %%     Outputs()        : refs/rtw_tlc.pdf p. 281
42
43
44 %implements sfunction_canreceive "C"
45
46 %include "common.tlc"
47 %include "rpp_can_common.tlc"
48
49 %% Function: BlockTypeSetup ====================================================
50 %function BlockTypeSetup(block, system) void
51     %<RppCommonBlockTypeSetup(block, system)>
52         %<RppCANCommonBlockTypeSetup()>
53 %endfunction
54
55 %% Function: BlockInstanceSetup ================================================
56 %function BlockInstanceSetup(block, system) void
57
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)
66
67     %if message_filter_par == 1
68             %assign mask = 8388607 %% 0x7FFFFF
69     %else
70             %assign mask = message_mask_par
71     %endif
72
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; ...
82           Mask mask; ...
83           Name "%<LibGetBlockName(block)>" ...
84         }
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)>
88         %else
89           %<RppPlaceManualMailboxNumber(blockInfo, mailbox_num_par)>
90         %endif
91
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
95 %endfunction
96
97 %% Function: Start =============================================================
98 %function Start(block, system) Output
99
100     %if !SLibCodeGenForSim()
101
102           %if EXISTS(::rpp_can_config_present) == 0
103                 %<LibBlockReportError(block, "CAN buc configuration block not present!")>
104           %endif
105     %endif
106
107
108 %endfunction
109
110 %% Function: Outputs ===========================================================
111 %function Outputs(block, system) Output
112
113   %if !SLibCodeGenForSim()
114         %assign data_type_par = LibBlockParameterValue(data_type, 0)
115         %assign message = LibBlockOutputSignal(1, "", "", 1)
116
117         {
118           struct can_frame sc_frame;
119           int ret;
120           int dlc;
121           int recv_len = -1;
122
123           /* TODO decide what is desired behavior if there are more pendind messages */
124           do {
125             ret = recv(can_rx_handles[%<RppRxInfo.HwObj>], &sc_frame, sizeof(sc_frame), 0);
126             if (ret == -1) {
127               if (errno == EAGAIN)
128                 break;
129               printf("Receiving CAN message failed (%s).\n", "%<RppRxInfo.Name>");
130               break;
131             } else {
132               recv_len = ret;
133             }
134           } while (ret > 0);
135
136           if (recv_len < sizeof(sc_frame)) {
137             if (recv_len >= 0)
138               printf("Receiving CAN message (%s) returns truncated length %d.\n", "%<RppRxInfo.Name>", ret);
139           } else {
140             dlc = sc_frame.can_dlc;
141             if (dlc > 8)
142               dlc = 8;
143             %if %<data_type_par>==4
144                 // CAN_MESSAGE
145                 memset(&%<message>, 0, sizeof(%<message>));
146                 %<message>.Length = dlc;
147                 %<message>.ID = sc_frame.can_id & CAN_EFF_MASK;
148                 %<message>.Extended = sc_frame.can_id & CAN_EFF_FLAG? 1: 0;
149                 int i;
150                 for (i = 0; i < dlc; i++ ) {
151                   %<message>.Data[i] = sc_frame.data[i];
152                   %%printf("%X ", sc_frame.data[i]);
153                 }
154             %elseif %<data_type_par>==3
155                 // uint32
156                 unsigned int msg = (sc_frame.data[0]) |
157                   (sc_frame.data[1]<<8) |
158                   (sc_frame.data[2]<<16) |
159                   (sc_frame.data[3]<<24);
160                 if (dlc < 4)
161                   msg &= 0xffffffff << (8 * dlc);
162                 %<message> = msg;
163                 %%printf("32b: %X ", msg);
164             %elseif %<data_type_par>==2
165                 // uint16
166                 unsigned int msg = (sc_frame.data[0]) |
167                     (sc_frame.data[1]<<8);
168                 if (dlc < 2)
169                   msg &= 0xffffffff << (8 * dlc);
170                 %<message> = msg;
171                 %%printf("16b: %X ", msg);
172             %else
173                 // uint8
174                 unsigned int msg = sc_frame.data[0];
175                 %<message> = msg;
176                 %%printf("8b: %X ", msg);
177             %endif
178             %%printf("\n");
179
180             %% Call a function to process the received message via function-call subsystem
181             %foreach callIdx = NumSFcnSysOutputCalls
182                 %if LibIsEqual(SFcnSystemOutputCall[callIdx].BlockToCall,"unconnected")
183                   %continue
184                 %endif
185                 %% call the downstream system
186                 %<LibBlockExecuteFcnCall(block, callIdx)>\
187             %endforeach
188           }
189         }
190   %endif
191 %endfunction
192
193 %% [EOF]