]> rtime.felk.cvut.cz Git - pes-rpp/rpp-simulink.git/blob - rpp/blocks/tlc_c/sfunction_cansetup.tlc
doc: Update documentation of external mode and related stuff
[pes-rpp/rpp-simulink.git] / rpp / 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 %% This document contains proprietary information belonging to Czech
8 %% Technical University in Prague. Passing on and copying of this
9 %% document, and communication of its contents is not permitted
10 %% without prior written authorization.
11 %%
12 %% File : sfunction_cansetup.tlc
13 %% Abstract:
14 %%     TLC file for configuring the CAN modules on the TI TMS570LS3137 MCU
15 %%
16 %% References:
17 %%     BlockTypeSetup() : refs/rtw_tlc.pdf p. 277
18 %%     Outputs()        : refs/rtw_tlc.pdf p. 281
19
20
21 %implements sfunction_cansetup "C"
22
23 %include "common.tlc"
24 %include "rpp_can_common.tlc"
25
26 %% Function: BlockTypeSetup ====================================================
27 %function BlockTypeSetup(block, system) void
28
29     %% Ensure required header files are included
30     %<RppCommonBlockTypeSetup(block, system)>
31     %<LibAddToCommonIncludes("<sys/ti_drv_dmm.h>")>
32     %<LibAddToCommonIncludes("can_message.h")>
33     %<LibAddToCommonIncludes("rpp/rpp.h")>
34     %assign ::rpp_can_config_present = 1
35     %assign ::rpp_can_present=1
36
37     %<RppCANCommonBlockTypeSetup()>
38 %endfunction
39
40 %function BlockInstanceSetup(block, system) void
41     %if EXISTS("rpp_canc_in_model") == 0
42         %assign ::rpp_canc_in_model = 1
43     %else
44         %<LibBlockReportError(block, "Only one CAN Setup block is allowed in the model.")>
45     %endif
46 %endfunction
47
48
49 %% Function: Start =============================================================
50 %function Start(block, system) Output
51  %openfile buffer
52         #define CAN_TX_COUNT %<Rpp.Can.Tx.NumBlocks>
53         #define CAN_RX_COUNT %<Rpp.Can.Rx.NumBlocks>
54
55         struct rpp_can_ctrl_config can_ctrl_cfg[3] = {
56                 {
57                     .baudrate = %<LibBlockParameterValue(baudrate_can1, 0)>,
58                     .clk = 80000000,
59                     .timing_calc_method = RPP_CAN_TIMING_CALC_AUTO,
60                     .timing_config = NULL
61                 },
62                 {
63                     .baudrate = %<LibBlockParameterValue(baudrate_can2, 0)>,
64                     .clk = 80000000,
65                     .timing_calc_method = RPP_CAN_TIMING_CALC_AUTO,
66                     .timing_config = NULL
67                 },
68                 {
69                     .baudrate = %<LibBlockParameterValue(baudrate_can3, 0)>,
70                     .clk = 80000000,
71                     .timing_calc_method = RPP_CAN_TIMING_CALC_AUTO,
72                     .timing_config = NULL
73                 }
74         };
75
76         struct rpp_can_tx_config tx_config[CAN_TX_COUNT] = {
77           %foreach id = Rpp.Can.Tx.NumBlocks
78                 %with Rpp.Can.Tx.Block[id]
79                 // %<Name>
80                 {
81                   %if %<Type> == 1
82                         .type = RPP_CAN_STANDARD,
83                   %elseif %<Type> == 2
84                         .type = RPP_CAN_EXTENDED,
85                   %else
86                         .type = RPP_CAN_MIXED,
87                   %endif
88                   .controller = %<Controller>,
89                   .msg_obj = %<MsgObj>
90                 },
91                 %endwith
92           %endforeach
93         };
94
95         struct rpp_can_rx_config rx_config[CAN_RX_COUNT] = {
96           %foreach id = Rpp.Can.Rx.NumBlocks
97                 %with Rpp.Can.Rx.Block[id]
98                 // %<Name>
99                 {
100                   %if %<Type>==1
101                         .type = RPP_CAN_STANDARD,
102                   %elseif %<Type>==2
103                         .type = RPP_CAN_EXTENDED,
104                   %else
105                         .type = RPP_CAN_MIXED,
106                   %endif
107                   .controller = %<Controller>,
108                   .msg_obj = %<MsgObj>,
109                   .id = %<Id>,
110                   .mask = %<SPRINTF("%#x", Mask)>
111                 },
112                 %endwith
113           %endforeach
114         };
115
116         const struct rpp_can_config can_config = {
117                 .num_tx_obj = CAN_TX_COUNT,
118                 .num_rx_obj = CAN_RX_COUNT,
119                 .tx_config = tx_config,
120                 .rx_config = rx_config,
121                 .ctrl = can_ctrl_cfg
122         };
123
124         %closefile buffer
125         %<LibSetSourceFileSection(LibGetModelDotCFile(), "Declarations", buffer)>
126         int returnstatus;
127
128         returnstatus = rpp_can_init(&can_config);
129         if (returnstatus == FAILURE) {
130             rpp_sci_printf("CAN driver initialization error.\n");
131         }
132         %%else {
133         %%      rpp_sci_printf("CAN communication initialized.\n");
134         %%}
135  %endfunction
136
137 %% [EOF]