1 /* Copyright (C) 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_canrecive.c
13 * C-MEX S-function block for RPP CAN bus receive message.
20 * mex(['-I' matlabroot '/toolbox/shared/can/src/scanutil'], ['-I' matlabroot '/toolbox/rtw/targets/common/can/datatypes'], 'sfunction_canreceive.c', [matlabroot '/toolbox/rtw/targets/common/can/datatypes/sfun_can_util.c'], [matlabroot '/toolbox/rtw/targets/common/can/datatypes/can_msg.c'])
34 - { name: "f()", type: "Function call" }
35 - { name: "Msg", type: "uint8, uint16, uint32 or CAN MESSAGE" }
38 - { name: "Module", type: "Choice", range: "CAN1, CAN2, CAN3" }
39 - { name: "Frame type", type: "Choice", range: "Standard, Extended, Mixed" }
40 - { name: "Message ID filter", type: "Choice", range: "Single ID, ID and mask" }
41 - { name: "Message ID", type: "uint32" }
42 - { name: "Message ID mask", type: "uint8" }
43 - { name: "Data type", type: "Choice", range: "uint8, uint16, uint32, CAN MESSAGE TYPE" }
44 - { name: "Automatic mailbox number", type: "bool" }
45 - { name: "Mailbox number", type: "int8", range: "[1-64]" }
47 # Description and Help is in Markdown mark-up
50 Receive a messages from the CAN bus.
52 The output can be a uint8, uint16, uint32 number or CAN\_MESSAGE object.
53 CAN Unpack block can be used to unpack the data from CAN\_MESSAGE
56 A Function-call subsystem is triggered every time when a new message has
59 When Mixed is selected in the *Message type*, the block is able to
60 receive both, the mesages with Standard and Extended identifier.\
61 **NOTE:** When Mixed is selected, the *Message ID* and eventualy the
62 *Message ID mask* have to be shifted by 18 bits to the left.
64 If the *Message filter* is set to Single ID, only the messages with the
65 one identifier specified in *Message ID* will be accepted by the block.
66 But when ID & mask is selected, all messages with identifier compliant
67 to logical AND of the *Message ID* and *Message ID mask* will be
70 Uncheck *Automatic mailbox number*, if user defined mailbox number is
75 This block allows receiving messages from the CAN bus. It can be
76 configured for any of the CAN ports (modules) CAN1, CAN2 or CAN3.
78 The acceptance rules for message reception can be specified by a
79 *Message ID* parameter and optionally by a *Message ID mask*.
80 Specifying the mask allows to receive messages with multiple IDs.
81 The block supports both, the Standard (11b ID) and the Extended (29b
82 ID) frame formats. Note that if Mixed message ID type is selected,
83 the blocks will receive both frame types, but the Standard Message
84 ID and optionally the Message ID mask has to be shifted by 18 bits
85 to the left to correspond with the extended IDs and masks. For
86 example, if Message ID parameter is set to 0x80000 and mask
87 to 0x1ffbfff, the block will receive SFF messages with IDs 0x002 and
88 0x003 and EFF IDs 0x00080000 and 0x000c0000.
90 The mailbox number can be assigned automatically or manually. Automatic mailbox
91 numbers are generated in ascending order from 0 to 31. Every mailbox must have a unique
92 number. It is possible to mix blocks with automatically and manually
93 assigned mailboxes. If the manually assigned mailbox number would
94 collide with the automatic one then the automatically generated
95 block will get assigned a next higher non-colliding ID.
96 The mailbox numbers are shared between CAN Transmit and CAN Receive
97 blocks with the same CAN port (module) parameter.
99 The output of this block is a message data in selected format: uint8, uint16, uint32
100 or CAN\_MESSAGE. The CAN\_MESSAGE object can be unpacked by `CAN Unpack` block.
102 Every time a message is received, the function call on `f()` output
103 signal is triggered and the received message data is appears on the
104 `Msg` output port. See `cantransmit.slx` demo for examples of
105 different configurations and the usage of the CAN blocks.
107 In order to use this block, there must be a `CAN Configure` block in the model.
111 RPP API functions used:
121 #define S_FUNCTION_NAME sfunction_canreceive
125 #include "sfun_can_util.h"
126 #include "simstruc.h"
128 #define MSG_TYPE_STANDART_MAX 2048
129 #define MSG_TYPE_EXTENDED_MAX 536870912
130 #define MAILBOX_ID_MIN 1
131 #define MAILBOX_MAX_CNT 64
133 #define PARAM_NAME_MODULE_ID "module_id"
134 #define PARAM_NAME_MAILBOX_ID "mailbox_id"
135 #define PARAM_NAME_MSG_TYPE "message_type"
136 #define PARAM_NAME_MSG_ID "message_id"
137 #define PARAM_NAME_DATA_TYPE "data_type"
138 #define PARAM_NAME_MSG_FILTER "message_filter"
139 #define PARAM_NAME_MSG_MASK "message_mask"
140 #define PARAM_NAME_MAILBOX_AUTO "mailbox_auto"
142 /** Identifiers of the block parameters */
155 enum message_data_type {
157 DATA_TYPE_UINT16 = 2,
158 DATA_TYPE_UINT32 = 3,
159 DATA_TYPE_CAN_MESSAGE = 4
162 enum message_id_type {
174 static bool msg_received;
176 static void mdlInitializeSizes(SimStruct *S){
178 CAN_Common_MdlInitSizes(S);
182 if(!rppSetNumParams(S, PARAM_COUNT)) {
187 if(!ssSetNumInputPorts(S, 0)) {
192 * Configure output ports: 1
194 * - Message is received
196 if(!ssSetNumOutputPorts(S, OUT_COUNT)) {
199 rppAddOutputPort(S, OUT_FNC_CALL, SS_FCN_CALL);
201 if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_DATA_TYPE))[0] == DATA_TYPE_UINT8) {
202 rppAddOutputPort(S, OUT_MSG, SS_UINT8);
204 else if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_DATA_TYPE))[0] == DATA_TYPE_UINT16) {
205 rppAddOutputPort(S, OUT_MSG, SS_UINT16);
207 else if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_DATA_TYPE))[0] == DATA_TYPE_UINT32) {
208 rppAddOutputPort(S, OUT_MSG, SS_UINT32);
211 rppAddOutputPort(S, OUT_MSG, ssGetDataTypeId(S, "CAN_MESSAGE"));
214 /* Set standard options for this block */
215 rppSetStandardOptions(S);
219 #ifdef MATLAB_MEX_FILE
220 #define MDL_CHECK_PARAMETERS
222 static void mdlCheckParameters(SimStruct *S){
225 /* Check the parameter mailbox number */
226 if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_MAILBOX_AUTO))[0] == 0) {
227 if (!rppValidParamRange(S, PARAM_MAILBOX_ID, MAILBOX_ID_MIN, MAILBOX_ID_MIN + MAILBOX_MAX_CNT)) {
232 if ((int_T)mxGetPr(ssGetSFcnParam(S, PARAM_MSG_TYPE))[0] == MSG_ID_STANDART) {
233 max = MSG_TYPE_STANDART_MAX;
236 max = MSG_TYPE_EXTENDED_MAX;
239 /* Check the parameter message identifier */
240 if (!rppValidParamRange(S, PARAM_MSG_ID, min, max)) {
246 static void mdlInitializeSampleTimes(SimStruct *S)
248 ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
249 ssSetOffsetTime(S, 0, 0);
251 ssSetCallSystemOutput(S,0); /* call on first element */
255 #ifdef MATLAB_MEX_FILE
256 #define MDL_SET_WORK_WIDTHS
257 static void mdlSetWorkWidths(SimStruct *S){
259 if(!ssSetNumRunTimeParams(S, PARAM_COUNT)) {
263 ssRegDlgParamAsRunTimeParam(S, PARAM_MODULE_ID, PARAM_MODULE_ID, PARAM_NAME_MODULE_ID, SS_UINT8);
264 ssRegDlgParamAsRunTimeParam(S, PARAM_MAILBOX_ID, PARAM_MAILBOX_ID, PARAM_NAME_MAILBOX_ID, SS_INT8);
265 ssRegDlgParamAsRunTimeParam(S, PARAM_MSG_TYPE, PARAM_MSG_TYPE, PARAM_NAME_MSG_TYPE, SS_UINT16);
266 ssRegDlgParamAsRunTimeParam(S, PARAM_MSG_ID, PARAM_MSG_ID, PARAM_NAME_MSG_ID, SS_UINT32);
267 ssRegDlgParamAsRunTimeParam(S, PARAM_DATA_TYPE, PARAM_DATA_TYPE, PARAM_NAME_DATA_TYPE, SS_UINT8);
268 ssRegDlgParamAsRunTimeParam(S, PARAM_MSG_FILTER, PARAM_MSG_FILTER, PARAM_NAME_MSG_FILTER, SS_UINT8);
269 ssRegDlgParamAsRunTimeParam(S, PARAM_MSG_MASK, PARAM_MSG_MASK, PARAM_NAME_MSG_MASK, SS_UINT32);
270 ssRegDlgParamAsRunTimeParam(S, PARAM_MAILBOX_AUTO, PARAM_MAILBOX_AUTO, PARAM_NAME_MAILBOX_AUTO, SS_BOOLEAN);
274 #define UNUSED_MDLOUTPUTS
275 #define UNUSED_MDLTERMINATE