%% Copyright (C) 2014 Czech Technical University in Prague %% %% Authors: %% Michal Horn %% Michal Sojka %% %% Redistribution and use in source and binary forms, with or without %% modification, are permitted provided that the following conditions are %% met: %% %% 1. Redistributions of source code must retain the above copyright %% notice, this list of conditions and the following disclaimer. %% %% 2. Redistributions in binary form must reproduce the above copyright %% notice, this list of conditions and the following disclaimer in the %% documentation and/or other materials provided with the %% distribution. %% %% 3. Neither the name of the copyright holder nor the names of its %% contributors may be used to endorse or promote products derived %% from this software without specific prior written permission. %% %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT %% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR %% A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT %% HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, %% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT %% LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, %% DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY %% THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT %% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE %% OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. %% %% File : sfunction_canreceive.tlc %% Abstract: %% TLC file for configuring the CAN RX buffer on the TI TMS570LS3137 MCU %% %% References: %% BlockTypeSetup() : refs/rtw_tlc.pdf p. 277 %% Outputs() : refs/rtw_tlc.pdf p. 281 %implements sfunction_canreceive "C" %include "common.tlc" %include "rpp_can_common.tlc" %% Function: BlockTypeSetup ==================================================== %function BlockTypeSetup(block, system) void % % %endfunction %% Function: BlockInstanceSetup ================================================ %function BlockInstanceSetup(block, system) void %assign module_id_par = LibBlockParameterValue(module_id, 0) %assign mailbox_num_par = LibBlockParameterValue(mailbox_id, 0) %assign message_type_par = LibBlockParameterValue(message_type, 0) %assign message_id_par = LibBlockParameterValue(message_id, 0) %assign data_type_par = LibBlockParameterValue(data_type, 0) %assign message_filter_par = LibBlockParameterValue(message_filter, 0) %assign message_mask_par = LibBlockParameterValue(message_mask, 0) %assign mailbox_auto_par = LibBlockParameterValue(mailbox_auto, 0) %if message_filter_par == 1 %assign mask = 8388607 %% 0x7FFFFF %else %assign mask = message_mask_par %endif %% This seems to be the only way to add a record to an array. The %% other way - creating the record with %createrecord and adding %% it later with %addtorecord doesn't work. %addtorecord Rpp.Can.Rx Block { ... HwObj Rpp.Can.Rx.NumBlocks; ... Controller module_id_par; ... MsgObj 0; /% Invalid MsgObj - proper value will be assigned later %/ ... Type message_type_par; ... Id message_id_par; ... Mask mask; ... Name "%" ... } %assign blockInfo = Rpp.Can.Rx.Block[Rpp.Can.Rx.NumBlocks] /% create alias to the record %/ %if %==1 % %else % %endif /% Remember which blockInfo record is associated wit this block %/ %addtorecord block RppRxInfo blockInfo %assign Rpp.Can.Rx.NumBlocks = Rpp.Can.Rx.NumBlocks + 1 %endfunction %% Function: Start ============================================================= %function Start(block, system) Output %if !SLibCodeGenForSim() %if EXISTS(::rpp_can_config_present) == 0 % %endif %endif %endfunction %% Function: Outputs =========================================================== %function Outputs(block, system) Output %if !SLibCodeGenForSim() %assign data_type_par = LibBlockParameterValue(data_type, 0) %assign message = LibBlockOutputSignal(1, "", "", 1) { struct can_frame sc_frame; int ret; int dlc; ret = recv(can_rx_handles[%], &sc_frame, sizeof(sc_frame), 0); if (ret == -1) { if (errno!=EAGAIN) { printf("Receiving CAN message failed (%s).\n", "%"); } } else if (ret < sizeof(sc_frame)) { printf("Receiving CAN message (%s) returns truncated length %d.\n", "%", ret); } else { dlc = sc_frame.can_dlc; if (dlc > 8) dlc = 8; %if %==4 // CAN_MESSAGE %.Length = dlc; %.ID = sc_frame.can_id & CAN_EFF_MASK; int i; for (i = 0; i < dlc; i++ ) { %.Data[i] = sc_frame.data[i]; printf("%X ", sc_frame.data[i]); } %elseif %==3 // uint32 unsigned int msg = (sc_frame.data[0]) | (sc_frame.data[1]<<8) | (sc_frame.data[2]<<16) | (sc_frame.data[3]<<24); if (dlc < 4) msg &= 0xffffffff << (8 * dlc); % = msg; printf("32b: %X ", msg); %elseif %==2 // uint16 unsigned int msg = (sc_frame.data[0]) | (sc_frame.data[1]<<8); if (dlc < 2) msg &= 0xffffffff << (8 * dlc); % = msg; printf("16b: %X ", msg); %else // uint8 unsigned int msg = sc_frame.data[0]; % = msg; printf("8b: %X ", msg); %endif printf("\n"); %% Call a function to process the received message via function-call subsystem %foreach callIdx = NumSFcnSysOutputCalls %if LibIsEqual(SFcnSystemOutputCall[callIdx].BlockToCall,"unconnected") %continue %endif %% call the downstream system %\ %endforeach } } %endif %endfunction %% [EOF]