]> rtime.felk.cvut.cz Git - socketcan-simulink.git/blob - blocks/tlc_c/sfunction_cansetup.tlc
The first round of changes to port RPP code to Linux SocketCAN.
[socketcan-simulink.git] / blocks / tlc_c / sfunction_cansetup.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_cansetup.tlc
36 %% Abstract:
37 %%     TLC file for configuring the CAN modules 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_cansetup "C"
45
46 %include "common.tlc"
47 %include "rpp_can_common.tlc"
48
49 %% Function: BlockTypeSetup ====================================================
50 %function BlockTypeSetup(block, system) void
51
52     %% Ensure required header files are included
53     %<RppCommonBlockTypeSetup(block, system)>
54         %% %<LibAddToCommonIncludes("<sys/ti_drv_dmm.h>")>
55
56         %assign ::rpp_can_config_present = 1
57
58         %<RppCANCommonBlockTypeSetup()>
59 %endfunction
60
61 %function BlockInstanceSetup(block, system) void
62     %if EXISTS("rpp_canc_in_model") == 0
63         %assign ::rpp_canc_in_model = 1
64     %else
65         %<LibBlockReportError(block, "Only one CAN Setup block is allowed in the model.")>
66     %endif
67 %endfunction
68
69
70 %% Function: Start =============================================================
71 %function Start(block, system) Output
72  %openfile buffer
73
74         enum ert_can_msg_types {
75                 ERT_CAN_STANDARD,
76                 ERT_CAN_EXTENDED,
77                 ERT_CAN_MIXED,
78         };
79
80         struct ert_can_channel_config {
81                 unsigned int baudrate;
82                 const char *net_dev_name;
83         };
84
85         struct ert_can_tx_config {
86                 int channel;
87                 int id_type;
88                 canid_t id;
89                 int msg_obj;
90         };
91
92         struct ert_can_rx_config {
93                 int channel;
94                 int id_type;
95                 canid_t id;
96                 canid_t mask;
97                 int msg_obj;
98         };
99
100         struct ert_can_config {
101                 int num_tx_obj;
102                 int num_rx_obj;
103                 struct ert_can_tx_config *tx_config;
104                 struct ert_can_rx_config *rx_config;
105                 struct ert_can_channel_config *channel_config;
106         };
107
108         #define CAN_TX_COUNT %<Rpp.Can.Tx.NumBlocks>
109         #define CAN_RX_COUNT %<Rpp.Can.Rx.NumBlocks>
110
111         struct ert_can_channel_config can_channel_config[3] = {
112                 {
113                         .baudrate = %<LibBlockParameterValue(baudrate_can1, 0)>
114                 },
115                 {
116                         .baudrate = %<LibBlockParameterValue(baudrate_can2, 0)>
117                 },
118                 {
119                         .baudrate = %<LibBlockParameterValue(baudrate_can3, 0)>
120                 }
121         };
122
123         struct ert_can_tx_config tx_config[CAN_TX_COUNT] = {
124           %foreach id = Rpp.Can.Tx.NumBlocks
125                 %with Rpp.Can.Tx.Block[id]
126                 // %<Name>
127                 {
128                   %if %<Type> == 1
129                         .id_type = ERT_CAN_STANDARD,
130                   %elseif %<Type> == 2
131                         .id_type = ERT_CAN_EXTENDED,
132                   %else
133                         .id_type = ERT_CAN_MIXED,
134                   %endif
135                   .id = %<Id>,
136                   .channel = %<Controller>,
137                   .msg_obj = %<MsgObj>,
138                 },
139                 %endwith
140           %endforeach
141         };
142
143         struct ert_can_rx_config rx_config[CAN_RX_COUNT] = {
144           %foreach id = Rpp.Can.Rx.NumBlocks
145                 %with Rpp.Can.Rx.Block[id]
146                 // %<Name>
147                 {
148                   %if %<Type>==1
149                         .id_type = ERT_CAN_STANDARD,
150                   %elseif %<Type>==2
151                         .id_type = ERT_CAN_EXTENDED,
152                   %else
153                         .id_type = ERT_CAN_MIXED,
154                   %endif
155                   .id = %<Id>,
156                   .mask = %<SPRINTF("%#x", Mask)>,
157                   .channel = %<Controller>,
158                   .msg_obj = %<MsgObj>,
159                 },
160                 %endwith
161           %endforeach
162         };
163
164         const struct ert_can_config can_config = {
165                 .num_tx_obj = CAN_TX_COUNT,
166                 .num_rx_obj = CAN_RX_COUNT,
167                 .tx_config = tx_config,
168                 .rx_config = rx_config,
169                 .channel_config = can_channel_config
170         };
171
172         int can_rx_handles[CAN_RX_COUNT];
173
174         int can_tx_handles[CAN_RX_COUNT];
175
176         %closefile buffer
177         %<LibSetSourceFileSection(LibGetModelDotCFile(), "Declarations", buffer)>
178         int returnstatus;
179
180         returnstatus = rpp_can_init(&can_config);
181         if (returnstatus < 0) {
182             printf("CAN driver initialization error.\n");
183         }
184         %%else {
185         %%      printf("CAN communication initialized.\n");
186         %%}
187  %endfunction
188
189 %% [EOF]