]> rtime.felk.cvut.cz Git - jenkicar/rpp-simulink.git/blob - rpp/blocks/tlc_c/sfunction_canreceive.tlc
Change license to MIT
[jenkicar/rpp-simulink.git] / rpp / 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 %% Permission is hereby granted, free of charge, to any person
8 %% obtaining a copy of this software and associated documentation
9 %% files (the "Software"), to deal in the Software without
10 %% restriction, including without limitation the rights to use,
11 %% copy, modify, merge, publish, distribute, sublicense, and/or sell
12 %% copies of the Software, and to permit persons to whom the
13 %% Software is furnished to do so, subject to the following
14 %% conditions:
15
16 %% The above copyright notice and this permission notice shall be
17 %% included in all copies or substantial portions of the Software.
18
19 %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 %% EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21 %% OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 %% NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 %% HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 %% WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 %% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26 %% OTHER DEALINGS IN THE SOFTWARE.
27 %%
28 %% File : sfunction_canreceive.tlc
29 %% Abstract:
30 %%     TLC file for configuring the CAN RX buffer on the TI TMS570LS3137 MCU
31 %%
32 %% References:
33 %%     BlockTypeSetup() : refs/rtw_tlc.pdf p. 277
34 %%     Outputs()        : refs/rtw_tlc.pdf p. 281
35
36
37 %implements sfunction_canreceive "C"
38
39 %include "common.tlc"
40 %include "rpp_can_common.tlc"
41
42 %% Function: BlockTypeSetup ====================================================
43 %function BlockTypeSetup(block, system) void
44     %<LibAddToCommonIncludes("can_message.h")>
45     %<LibAddToCommonIncludes("rpp/rpp.h")>
46     %<RppCommonBlockTypeSetup(block, system)>
47     %<RppCANCommonBlockTypeSetup()>
48 %endfunction
49
50 %% Function: BlockInstanceSetup ================================================
51 %function BlockInstanceSetup(block, system) void
52
53         %assign module_id_par = LibBlockParameterValue(module_id, 0)
54         %assign mailbox_num_par = LibBlockParameterValue(mailbox_id, 0)
55         %assign message_type_par = LibBlockParameterValue(message_type, 0)
56         %assign message_id_par = LibBlockParameterValue(message_id, 0)
57         %assign data_type_par = LibBlockParameterValue(data_type, 0)
58         %assign message_filter_par = LibBlockParameterValue(message_filter, 0)
59         %assign message_mask_par = LibBlockParameterValue(message_mask, 0)
60         %assign mailbox_auto_par = LibBlockParameterValue(mailbox_auto, 0)
61
62     %if message_filter_par == 1
63             %assign mask = 8388607 %% 0x7FFFFF
64     %else
65             %assign mask = message_mask_par
66     %endif
67
68         %% This seems to be the only way to add a record to an array. The
69         %% other way - creating the record with %createrecord and adding
70         %% it later with %addtorecord doesn't work.
71         %addtorecord Rpp.Can.Rx Block { ...
72           HwObj Rpp.Can.Rx.NumBlocks; ...
73           Controller module_id_par; ...
74           MsgObj 0; /% Invalid MsgObj - proper value will be assigned later %/ ...
75           Type message_type_par; ...
76           Id message_id_par; ...
77           Mask mask; ...
78           Name "%<LibGetBlockName(block)>" ...
79         }
80         %assign blockInfo = Rpp.Can.Rx.Block[Rpp.Can.Rx.NumBlocks] /% create alias to the record %/
81         %if %<mailbox_auto_par>==1
82           %<RppPlaceAutomaticMailboxNumber(blockInfo)>
83         %else
84           %<RppPlaceManualMailboxNumber(blockInfo, mailbox_num_par)>
85         %endif
86
87         /% Remember which blockInfo record is associated wit this block %/
88          %addtorecord block RppRxInfo blockInfo
89         %assign Rpp.Can.Rx.NumBlocks = Rpp.Can.Rx.NumBlocks + 1
90 %endfunction
91
92 %% Function: Start =============================================================
93 %function Start(block, system) Output
94
95     %if !SLibCodeGenForSim()
96
97           %if EXISTS(::rpp_can_config_present) == 0
98                 %<LibBlockReportError(block, "CAN buc configuration block not present!")>
99           %endif
100     %endif
101
102
103 %endfunction
104
105 %% Function: Outputs ===========================================================
106 %function Outputs(block, system) Output
107
108   %if !SLibCodeGenForSim()
109         %assign data_type_par = LibBlockParameterValue(data_type, 0)
110         %assign message = LibBlockOutputSignal(1, "", "", 1)
111
112         {
113           struct rpp_can_pdu pdu;
114           int ret;
115
116           ret = rpp_can_read(%<RppRxInfo.HwObj>, &pdu);
117           switch (ret) {
118             case -RPP_EINVAL:
119                 rpp_sci_printf("Receiving CAN message failed (%s).\n", "%<RppRxInfo.Name>");
120                 break;
121             case -RPP_ENODATA:
122                 break;
123             case SUCCESS: {
124                 %if %<data_type_par>==4
125                   // CAN_MESSAGE
126                   %<message>.Length = pdu.dlc;
127                   %<message>.ID = pdu.id;
128                   int i;
129                   for (i = 0; i < pdu.dlc; i++ ) {
130                         %<message>.Data[i] = pdu.data[i];
131                         %%rpp_sci_printf("%X ", pdu.data[i]);
132                   }
133                 %elseif %<data_type_par>==3
134                   // uint32
135                   int msg =     (pdu.data[0]) |
136                   (pdu.data[1]<<8) |
137                   (pdu.data[2]<<16) |
138                   (pdu.data[3]<<24);
139                   %<message> = msg;
140                   %%rpp_sci_printf("32b: %X ", msg);
141                 %elseif %<data_type_par>==2
142                   // uint16
143                   int msg =     (pdu.data[0]) |
144                   (pdu.data[1]<<8);
145                   %<message> = msg;
146                   %%rpp_sci_printf("16b: %X ", msg);
147                 %else
148                   // uint8
149                   int msg = pdu.data[0];
150                   %<message> = msg;
151                   %%rpp_sci_printf("8b: %X ", msg);
152                 %endif
153                 %%rpp_sci_printf("\n");
154
155                 %% Call a function to process the received message via function-call subsystem
156                 %foreach callIdx = NumSFcnSysOutputCalls
157                   %if LibIsEqual(SFcnSystemOutputCall[callIdx].BlockToCall,"unconnected")
158                         %continue
159                   %endif
160                   %% call the downstream system
161                   %<LibBlockExecuteFcnCall(block, callIdx)>\
162                 %endforeach
163                 break;
164             }
165           }
166         }
167   %endif
168 %endfunction
169
170 %% [EOF]