]> rtime.felk.cvut.cz Git - socketcan-simulink.git/blob - blocks/sfunction_cansetup.c
sfunction_cansetup: correct debug message for rx/tx sockets open.
[socketcan-simulink.git] / blocks / sfunction_cansetup.c
1 /* Copyright (C) 2014 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Michal Horn <hornmich@fel.cvut.cz>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * File : sfunction_cansetup.c
35  * Abstract:
36  *     C-MEX S-function block for RPP CAN bus setup.
37  *
38  * References:
39  *     header.c
40  *     trailer.c
41  *
42  * Compile with:
43  *     <matlabroot>/bin/mex sfunction_cansetup.c
44  */
45
46 /*
47 %YAML 1.2
48 ---
49 Name: CAN Setup
50 Category: CAN
51 Header: rpp/can.h
52 Mnemonic: CANC
53
54 Inputs:
55
56 Outputs:
57
58 Parameters:
59   - { name: "Baud rate for CAN bus port 1 [bps]", type: "uint32", range: "1 – 10000000" }
60   - { name: "Baud rate for CAN bus port 2 [bps]", type: "uint32", range: "1 – 10000000" }
61   - { name: "Baud rate for CAN bus port 3 [bps]", type: "uint32", range: "1 – 10000000" }
62
63 # Description is in Markdown mark-up
64 Description: |
65
66   This block configures the CAN bus controllers. Exactly one CAN bus
67   configuration block must be in the model if any of the CAN Receive
68   or CAN Transmit block is used.
69
70 Status:
71   Tested:
72     - Configuring CAN1, CAN2 and CAN3
73   Untested:
74   Not working:
75     - Receiving at baudrate higher than 700kb
76     - External mode - throwing syntax error during compilation
77
78 RPP API functions used:
79     - rpp_can_init()
80
81 Relevant demos:
82     - cantransmit
83     - can_demo
84 ...
85 */
86
87
88 #define S_FUNCTION_NAME sfunction_cansetup
89 #include "header.c"
90 #include <stdio.h> 
91
92 #define BAUDRATE_MIN    1
93 #define BAUDRATE_MAX    1000000
94
95 #define BAUDRATE_CAN1_PARAM_NAME        "baudrate_can1"
96 #define BAUDRATE_CAN2_PARAM_NAME        "baudrate_can2"
97 #define BAUDRATE_CAN3_PARAM_NAME        "baudrate_can3"
98
99 /** Identifiers of the block parameters */
100 enum params{
101         PARAM_CAN1_BAUDRATE,
102         PARAM_CAN2_BAUDRATE,
103         PARAM_CAN3_BAUDRATE,    
104         PARAM_COUNT
105 };
106
107 static void mdlInitializeSizes(SimStruct *S)
108 {
109     if (!rppSetNumParams(S, PARAM_COUNT)) {
110         return;
111     }
112         
113         /* No input ports */
114     if (!ssSetNumInputPorts(S, 0)) {
115         return;
116     }
117  
118         /* No output ports */
119     if (!ssSetNumOutputPorts(S, 0)) {
120         return;
121     }
122  
123     rppSetStandardOptions(S);
124     void CAN_Common_MdlInitSizes(SimStruct *S);
125 }    
126
127
128 #ifdef MATLAB_MEX_FILE
129 #define MDL_CHECK_PARAMETERS
130 static void mdlCheckParameters(SimStruct *S)
131 {
132     /* Check the parameter CAN1 baudrate */
133     if (!rppValidParamRange(S, PARAM_CAN1_BAUDRATE, BAUDRATE_MIN, BAUDRATE_MAX)) {
134         return;
135     }
136     /* Check the parameter CAN2 baudrate */
137     if (!rppValidParamRange(S, PARAM_CAN2_BAUDRATE, BAUDRATE_MIN, BAUDRATE_MAX)) {
138         return;
139     }
140     /* Check the parameter CAN3 baudrate */
141     if (!rppValidParamRange(S, PARAM_CAN3_BAUDRATE, BAUDRATE_MIN, BAUDRATE_MAX)) {
142         return;
143     }
144 }
145 #endif
146
147
148 #ifdef MATLAB_MEX_FILE
149 #define MDL_SET_WORK_WIDTHS
150 static void mdlSetWorkWidths(SimStruct *S)
151 {
152     
153     if (!ssSetNumRunTimeParams(S, PARAM_COUNT)) {
154         return;
155     }
156     
157         ssRegDlgParamAsRunTimeParam(S, PARAM_CAN1_BAUDRATE, PARAM_CAN1_BAUDRATE, BAUDRATE_CAN1_PARAM_NAME, SS_UINT32);     
158         ssRegDlgParamAsRunTimeParam(S, PARAM_CAN2_BAUDRATE, PARAM_CAN2_BAUDRATE, BAUDRATE_CAN2_PARAM_NAME, SS_UINT32);
159         ssRegDlgParamAsRunTimeParam(S, PARAM_CAN3_BAUDRATE, PARAM_CAN3_BAUDRATE, BAUDRATE_CAN3_PARAM_NAME, SS_UINT32);
160 }
161 #endif
162
163 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
164 #define UNUSED_MDLOUTPUTS
165 #define UNUSED_MDLTERMINATE
166 #include "trailer.c"