]> rtime.felk.cvut.cz Git - socketcan-simulink.git/blob - blocks/tlc_c/sfunction_cantransmit.tlc
7ef9b689ea55939c18edabc2e5539f5bf39727be
[socketcan-simulink.git] / 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 %% 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_cantransmit.tlc
36 %% Abstract:
37 %%     TLC file for configuring the CAN TX 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_cantransmit "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   %assign module_id_par = LibBlockParameterValue(module_id, 0)
58   %assign mailbox_num_par = LibBlockParameterValue(mailbox_id, 0)
59   %assign message_type_par = LibBlockParameterValue(message_type, 0)
60   %assign message_id_par = LibBlockParameterValue(message_id, 0)
61   %assign mailbox_auto_par = LibBlockParameterValue(mailbox_auto, 0)
62
63   %% This seems to be the only way to add a record to an array. The
64   %% other way - creating the record with %createrecord and adding
65   %% it later with %addtorecord doesn't work.
66   %addtorecord Rpp.Can.Tx Block { ...
67     HwObj Rpp.Can.Tx.NumBlocks; ...
68     Controller module_id_par; ...
69     MsgObj 0; /% Invalid MsgObj - proper value will be assigned later %/ ...
70     Type message_type_par; ...
71     Id message_id_par; ...
72     Name "%<LibGetBlockName(block)>" ...
73   }
74   %assign blockInfo = Rpp.Can.Tx.Block[Rpp.Can.Tx.NumBlocks] /% create alias to the record %/
75   %if %<mailbox_auto_par>==1
76     %<RppPlaceAutomaticMailboxNumber(blockInfo)>
77   %else
78     %<RppPlaceManualMailboxNumber(blockInfo, mailbox_num_par)>
79   %endif
80
81   /% Remember which blockInfo record is associated wit this block %/
82   %addtorecord block RppTxInfo blockInfo
83   %assign Rpp.Can.Tx.NumBlocks = Rpp.Can.Tx.NumBlocks + 1
84 %endfunction
85
86 %% Function: Start =============================================================
87 %function Start(block, system) Output
88
89   %if !SLibCodeGenForSim()
90     %if EXISTS(::rpp_can_config_present) == 0
91       %<LibBlockReportError(block, "CAN bus configuration block not present!")>
92     %endif
93   %endif
94
95
96 %endfunction
97
98 %% Function: Outputs ===========================================================
99 %function Outputs(block, system) Output
100   %if !SLibCodeGenForSim()
101     %assign message = LibBlockInputSignal(0, "", "", 0)
102     %assign msginput_data_type = LibBlockInputSignalDataTypeId(0)
103
104     {
105       struct can_frame sc_frame;
106       int ret;
107
108       %if %<msginput_data_type>==3
109         // Transmit uint8
110         sc_frame.can_dlc = 1;
111         sc_frame.data[0]= %<message>;
112
113       %elseif %<msginput_data_type>==5
114         // Transmit uint16
115         sc_frame.can_dlc = 2;
116         sc_frame.data[0]= (%<message>&0xFF);
117         sc_frame.data[1]= (%<message>&0xFF00)>>8;
118       %elseif %<msginput_data_type>==7
119         // Transmit uint32
120         sc_frame.can_dlc = 4;
121         sc_frame.data[0]= (%<message>&0xFF);
122         sc_frame.data[1]= (%<message>&0xFF00)>>8;
123         sc_frame.data[2]= (%<message>&0xFF0000)>>16;
124         sc_frame.data[3]= (%<message>&0xFF000000)>>24;
125       %else
126         // Transmit CAN_MESSAGE
127         sc_frame.can_dlc = %<message>.Length;
128         %foreach msg_index = 8
129           sc_frame.data[%<msg_index>]= %<message>.Data[%<msg_index>];
130         %endforeach
131       %endif
132       sc_frame.can_id=%<RppTxInfo.Id>;
133       if (%<RppTxInfo.Type> == 2)
134         sc_frame.can_id|=CAN_EFF_FLAG;
135       %%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>);
136       %%for (i = 0; i < pdu.dlc; i++ ) {
137         %%      printf("%X ", pdu.data[i]);
138         %%}
139         %%printf("\n");
140         ret = send(can_tx_handles[%<RppTxInfo.HwObj>], &sc_frame, sizeof(sc_frame), 0);
141         if (ret < sizeof(sc_frame)) {
142           /% 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>); %/
143         }
144       }
145     %endif
146   %endfunction
147
148   %% [EOF]