1 /* Copyright (C) 2013-2014 Czech Technical University in Prague
4 * - Michal Horn <hornmich@fel.cvut.cz>
6 * This document contains proprietary information belonging to Czech
7 * Technical University in Prague. Passing on and copying of this
8 * document, and communication of its contents is not permitted
9 * without prior written authorization.
11 * File : sfunction_cantransmit.c
13 * C-MEX S-function block for RPP CAN bus transmit message.
20 * mex(['-I' matlabroot '/toolbox/shared/can/src/scanutil'], ['-I' matlabroot '/toolbox/rtw/targets/common/can/datatypes'], 'sfunction_cantransmit.c', [matlabroot '/toolbox/rtw/targets/common/can/datatypes/sfun_can_util.c'], [matlabroot '/toolbox/rtw/targets/common/can/datatypes/can_msg.c'])
32 - { name: "Msg", type: "dynamically" }
37 - { name: "Module", type: "Choice", range: "CAN1, CAN2, CAN3" }
38 - { name: "Frame type", type: "Choice", range: "Standard, Extended" }
39 - { name: "Message ID", type: "uint16" }
40 - { name: "Automatic mailbox number", type: "bool" }
41 - { name: "Mailbox number", type: "int8", range: "[1–64]" }
43 # Description and Help is in Markdown mark-up
46 Transmit a CAN message.
48 Expects a uint8, uint16, uint32 or CAN\_MESSAGE object as an input. Use
49 CAN Pack to create the CAN\_MESSAGE object.
51 Matlab expression can be used as Message ID and Mailbox number.
53 Uncheck *Automatic mailbox number*, if user defined mailbox number is
58 This block allows to send a message to the CAN bus. It can be
59 configured for any of the CAN ports (modules) CAN1, CAN2 or CAN3.
61 The message data are read from `Msg` input port. The data type is
62 decided automatically from the input, but it is restricted to uint8,
63 uint16, uint32 and CAN\_MESSAGE. The CAN\_MESSAGE object can be
64 created by the `CAN Pack` block.
66 The message sent by the block will have an ID assigned according to
67 the number in the *Message ID* parameter. The block supports both
68 types of message IDs: Standard (11b) and Extended (29b). If
69 CAN\_MESSAGE is used as input type, the message ID stored in
70 CAN\_MESSAGE object is ignored and the ID from the parameter of this
71 block is used instead.
73 The mailbox number can be assigned automatically or manually. Automatic mailbox
74 numbers are generated in ascending order from 0 to 31. Every mailbox must have a unique
75 number. It is possible to mix blocks with automatically and manually
76 assigned mailboxes. If the manually assigned mailbox number would
77 collide with the automatic one then the automatically generated
78 block will get assigned a next higher non-colliding ID.
79 The mailbox numbers are shared between CAN Transmit and CAN Receive
80 blocks with the same CAN port (module) parameter.
82 In order to use this block, there must be a `CAN Configure` block in the model.
86 - Transmission of the message with configured ID
87 - Automatic generation of mailboxes numbers in combination with
88 manual specification in other blocks. Colliding mailbox numbers
89 are correctly handled.
90 - Input message data type recognition
91 - When unsupported data type is connected to the Msg input port, Simulink generates a reasonable error message
93 - Handling of error states on CAN bus
95 - External mode - throwing syntax error during compilation
97 RPP API functions used:
107 #define S_FUNCTION_NAME sfunction_cantransmit
110 #include "sfun_can_util.h"
112 #define MSG_TYPE_STANDART_MAX 2048
113 #define MSG_TYPE_EXTENDED_MAX 536870912
114 #define MAILBOX_ID_MIN 1
115 #define MAILBOX_MAX_CNT 64
117 #define PARAM_NAME_MODULE_ID "module_id"
118 #define PARAM_NAME_MAILBOX_ID "mailbox_id"
119 #define PARAM_NAME_MESSAGE_TYPE "message_type"
120 #define PARAM_NAME_MESSAGE_ID "message_id"
121 #define PARAM_NAME_MAILBOX_AUTO "mailbox_auto"
123 /** Identifier of the input */
129 /** Identifiers of the block parameters */
139 enum message_id_type {
146 static void mdlInitializeSizes(SimStruct *S)
149 CAN_Common_MdlInitSizes(S);
151 if (!rppSetNumParams(S, PARAM_COUNT)) {
156 * Configure input ports: 1
157 * - Message to be sent
159 if (!ssSetNumInputPorts(S, IN_COUNT)) {
163 rppAddInputPort(S, IN_MSG, DYNAMICALLY_TYPED);
165 /* No output ports */
166 if (!ssSetNumOutputPorts(S, 0)) {
170 /* Set standard options for this block */
171 rppSetStandardOptions(S);
174 #if defined(MATLAB_MEX_FILE)
175 #define MDL_SET_INPUT_PORT_DATA_TYPE
176 void mdlSetInputPortDataType(SimStruct *S, int_T port, DTypeId type)
178 if (port == IN_MSG) {
179 if (type == SS_UINT8 ||
182 /* CAN pack seems to use this data type, but it is
183 * not registered in sfun_can_util.c. Strange. */
184 type == ssGetDataTypeId(S, "CAN_MESSAGE") ||
185 type == ssGetDataTypeId(S, SL_CAN_STANDARD_FRAME_DTYPE_NAME) ||
186 type == ssGetDataTypeId(S, SL_CAN_EXTENDED_FRAME_DTYPE_NAME))
187 ssSetInputPortDataType(S, port, type);
190 sprintf(msg, "Unsupported data type '%s' on Msg input port.",
191 ssGetDataTypeName(S, type));
192 ssSetErrorStatus(S, msg);
198 #ifdef MATLAB_MEX_FILE
199 #define MDL_CHECK_PARAMETERS
201 static void mdlCheckParameters(SimStruct *S){
204 /* Check the parameter mailbox id */
205 if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_MAILBOX_AUTO))[0] == 0) {
206 if (!rppValidParamRange(S, PARAM_MAILBOX_ID, MAILBOX_ID_MIN, MAILBOX_ID_MIN + MAILBOX_MAX_CNT)) {
211 if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_MSG_TYPE))[0] == MSG_ID_STANDART) {
212 max = MSG_TYPE_STANDART_MAX;
215 max = MSG_TYPE_EXTENDED_MAX;
218 /* Check the parameter message id */
219 if (!rppValidParamRange(S, PARAM_MSG_ID, min, max)) {
226 #ifdef MATLAB_MEX_FILE
227 #define MDL_SET_WORK_WIDTHS
228 static void mdlSetWorkWidths(SimStruct *S){
230 if (!ssSetNumRunTimeParams(S, PARAM_COUNT)) {
234 ssRegDlgParamAsRunTimeParam(S, PARAM_MODULE_ID, PARAM_MODULE_ID, PARAM_NAME_MODULE_ID, SS_UINT8);
235 ssRegDlgParamAsRunTimeParam(S, PARAM_MAILBOX_ID, PARAM_MAILBOX_ID, PARAM_NAME_MAILBOX_ID, SS_INT8);
236 ssRegDlgParamAsRunTimeParam(S, PARAM_MSG_TYPE, PARAM_MSG_TYPE, PARAM_NAME_MESSAGE_TYPE, SS_UINT16);
237 ssRegDlgParamAsRunTimeParam(S, PARAM_MSG_ID, PARAM_MSG_ID, PARAM_NAME_MESSAGE_ID, SS_UINT32);
238 ssRegDlgParamAsRunTimeParam(S, PARAM_MAILBOX_AUTO, PARAM_MAILBOX_AUTO, PARAM_NAME_MAILBOX_AUTO, SS_BOOLEAN);
242 #define COMMON_MDLINITIALIZESAMPLETIMES_INHERIT
243 #define UNUSED_MDLOUTPUTS
244 #define UNUSED_MDLTERMINATE