]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/tlc_c/sfunction_cantransmit.tlc
Change license to MIT
[pes-rpp/rpp-simulink.git] / rpp / blocks / tlc_c / sfunction_cantransmit.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_cantransmit.tlc
29 %% Abstract:
30 %%     TLC file for configuring the CAN TX 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_cantransmit "C"
38
39 %include "common.tlc"
40 %include "rpp_can_common.tlc"
41
42 %% Function: BlockTypeSetup ====================================================
43 %function BlockTypeSetup(block, system) void
44   %<RppCommonBlockTypeSetup(block, system)>
45   %<RppCANCommonBlockTypeSetup()>
46   %<LibAddToCommonIncludes("can_message.h")>
47   %<LibAddToCommonIncludes("rpp/rpp.h")>
48
49 %endfunction
50
51 %% Function: BlockInstanceSetup ================================================
52 %function BlockInstanceSetup(block, system) void
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 mailbox_auto_par = LibBlockParameterValue(mailbox_auto, 0)
58
59   %% This seems to be the only way to add a record to an array. The
60   %% other way - creating the record with %createrecord and adding
61   %% it later with %addtorecord doesn't work.
62   %addtorecord Rpp.Can.Tx Block { ...
63     HwObj Rpp.Can.Tx.NumBlocks; ...
64     Controller module_id_par; ...
65     MsgObj 0; /% Invalid MsgObj - proper value will be assigned later %/ ...
66     Type message_type_par; ...
67     Id message_id_par; ...
68     Name "%<LibGetBlockName(block)>" ...
69   }
70   %assign blockInfo = Rpp.Can.Tx.Block[Rpp.Can.Tx.NumBlocks] /% create alias to the record %/
71   %if %<mailbox_auto_par>==1
72     %<RppPlaceAutomaticMailboxNumber(blockInfo)>
73   %else
74     %<RppPlaceManualMailboxNumber(blockInfo, mailbox_num_par)>
75   %endif
76
77   /% Remember which blockInfo record is associated wit this block %/
78   %addtorecord block RppTxInfo blockInfo
79   %assign Rpp.Can.Tx.NumBlocks = Rpp.Can.Tx.NumBlocks + 1
80 %endfunction
81
82 %% Function: Start =============================================================
83 %function Start(block, system) Output
84
85   %if !SLibCodeGenForSim()
86     %if EXISTS(::rpp_can_config_present) == 0
87       %<LibBlockReportError(block, "CAN bus configuration block not present!")>
88     %endif
89   %endif
90
91
92 %endfunction
93
94 %% Function: Outputs ===========================================================
95 %function Outputs(block, system) Output
96   %if !SLibCodeGenForSim()
97     %assign message = LibBlockInputSignal(0, "", "", 0)
98     %assign msginput_data_type = LibBlockInputSignalDataTypeId(0)
99
100     {
101       struct rpp_can_pdu pdu;
102
103       %if %<msginput_data_type>==3
104         // Transmit uint8
105         pdu.dlc = 1;
106         pdu.data[0]= %<message>;
107
108       %elseif %<msginput_data_type>==5
109         // Transmit uint16
110         pdu.dlc = 2;
111         pdu.data[0]= (%<message>&0xFF);
112         pdu.data[1]= (%<message>&0xFF00)>>8;
113       %elseif %<msginput_data_type>==7
114         // Transmit uint32
115         pdu.dlc = 4;
116         pdu.data[0]= (%<message>&0xFF);
117         pdu.data[1]= (%<message>&0xFF00)>>8;
118         pdu.data[2]= (%<message>&0xFF0000)>>16;
119         pdu.data[3]= (%<message>&0xFF000000)>>24;
120       %else
121         // Transmit CAN_MESSAGE
122         pdu.dlc = %<message>.Length;
123         %foreach msg_index = 8
124           pdu.data[%<msg_index>]= %<message>.Data[%<msg_index>];
125         %endforeach
126       %endif
127       pdu.id=%<RppTxInfo.Id>;
128
129
130       %%rpp_sci_printf("CAN%d (hw_obj: %d): Sending message id 0x%X from mailbox %d. Data: ", %<module_id_par>, %<::rpp_can_tx_hw_obj_cnt>, pdu.id, %<mailbox_num_par>);
131       %%for (i = 0; i < pdu.dlc; i++ ) {
132         %%      rpp_sci_printf("%X ", pdu.data[i]);
133         %%}
134         %%rpp_sci_printf("\n");
135         if (rpp_can_write(%<RppTxInfo.HwObj>, &pdu) != SUCCESS) {
136           /% rpp_sci_printf("CAN%d (hw_obj: %d): Sending a message id 0x%X from mailbox %d failed.\n", %<module_id_par>, %<::rpp_can_tx_hw_obj_cnt>, pdu.id, %<mailbox_num_par>); %/
137         }
138       }
139     %endif
140   %endfunction
141
142   %% [EOF]