⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 freeportcomms_rxd.tlc

📁 simulink real-time workshop for dragon12 development board from
💻 TLC
字号:
%% File : freePortComms_rxd.tlc
%%
%%
%implements  freePortComms_rxd "C"

%% P1 SAMPLE_TIME      /* Sample time in seconds */
%% P2 CHANNEL_NO       /* communication channel number (up to MAX_FREECOM_CHANNELS) */
%% P3 NUM_ELEMENTS     /* block output width ->  # of elements */
%% P4 DATA_TYPE        /* data type to be expected at block input */
%% P5 PORT             /* communication port ([COM]1 - [COM]4) */
%% P6 BAUDRATE         /* baudrate (300 [bps] - 115200 [bps]) */
%%
%% === RTWdata ===
%% 'portStr'            ('0', '1')
%% 'portNameStr'        ('SCI0', 'SCI1')
%% 'baudrateStr'        ('300' - '115200')


%% add required include files... -- fw-04-05
%<LibAddToCommonIncludes("<stdlib.h>")>
%<LibAddToCommonIncludes("<mc9s12dp256.h>")>
%<LibAddToCommonIncludes("tmwtypes.h")>
%<LibAddToCommonIncludes("freePortComms.h")>
%<LibAddToCommonIncludes("mc_signal.h")>


%% Function: BlockTypeSetup =================================================
%%
%% Purpose:
%%      FreePort communication block setup code.
%%
%function BlockTypeSetup(block, system) void

  %% Place function prototypes in the model's header file
  
  %openfile buffer
    extern myUsrBuf *AllocateUserBuffer(uint_T channel, uint16_T bufsize, uint8_T data_type_len);
  %closefile buffer
    
  %<LibCacheFunctionPrototype(buffer)>

%endfunction


%% Function: Start ==========================================================
%%
%% Purpose:
%%      FreePort RX communication initialization code.
%%
%function Start(block, system) Output

	%assign CHANNEL_NO    = %<CAST( "Number", LibBlockParameterValue(P2,1) )>
	%assign NUM_ELEMENTS  = %<CAST( "Number", LibBlockParameterValue(P3,1) )>
	%assign DATA_TYPE     = %<CAST( "Number", LibBlockParameterValue(P4,1) )>
	%assign BuiltInDTypesSizes = [4, 1, 1, 2, 2, 4, 4, 2]
	%assign DATA_TYPE_LEN = %<BuiltInDTypesSizes>[%<DATA_TYPE>-1]
	%assign BUF_SIZE      = %<DATA_TYPE_LEN> * %<NUM_ELEMENTS>
	%assign pAdminStruct  = LibBlockPWork("", "", "", 0)
	%%
	%assign baudrate      = %<block.RTWdata.baudrateStr>
	%assign portName      = "%<block.RTWdata.portNameStr>"
	%assign port          = %<block.RTWdata.portStr>
	%assign format        = %<block.RTWdata.formatStr>
	%%
	%% reset CHANNEL_NO to '0' for 'raw data' transmissions
	%if format == 1
	%assign CHANNEL_NO    = 0
	%endif
	%%
	%assign y = LibBlockOutputSignal(0, "", "", 0)
	
	
	/* S-Function "freePortComms_rxd" initialization Block: %<Name> */
	{
		
		myUsrBuf	*admin;
		uint8_T		*blockOutput = (uint8_T *)&%<y>;
		int			i;
		
		/* allocate memory for buffer (%<BUF_SIZE> bytes) and its admin structure, returns the access pointer */
		if((admin = AllocateUserBuffer(%<CHANNEL_NO>, %<BUF_SIZE>, %<DATA_TYPE_LEN>)) == NULL) abort_LED(34);
		
		/* store the access pointer in the workspace variable */
		freecomTelBuf[%<CHANNEL_NO>] = admin;
		
		/* retain a local copy for fast access */
		%<pAdminStruct> = admin;
		
		/* open free serial port (%<portName>), initialize reception ring buffers */
		FreePort_Init(%<port>, BAUD_%<baudrate>);
		
		/* initialize output with '0' */
		for(i=0; i<%<BUF_SIZE>; i++) blockOutput[i] = 0;
		
	}

%endfunction


%% Function: Outputs ==========================================================
%%
%% Purpose:
%%      Code generation rules for mdlOutputs function.
%%
%function Outputs(block, system) Output

	%assign CHANNEL_NO    = %<CAST( "Number", LibBlockParameterValue(P2,1) )>
	%assign y = LibBlockOutputSignal(0, "", "", 0)
	%%
	%assign format        = %<block.RTWdata.formatStr>
	%assign port          = %<block.RTWdata.portStr>
	%%
	%% reset CHANNEL_NO to '0' for 'raw data' transmissions
	%if format == 1
	%assign CHANNEL_NO    = 0
	%endif
	
	
	/* S-Function "freePortComms_rxd" Block: %<Name> */
	{
		
		myUsrBuf	*admin = freecomTelBuf[%<CHANNEL_NO>];
		
		/* attempt to fetch a FreePort telegram from the freePort ring buffer */
		%if port == 0
		process_fpdata_SCI0(%<format>);
		%else
		process_fpdata_SCI1(%<format>);
		%endif
		
		/* check if output needs updated... */
		if(admin->buffer_full) {
			
			uint8_T *blockOutput = (uint8_T *)&%<y>;
			
			/* new data available for this instance -> update output */
			(void)memcpy(blockOutput, admin->buf, (uint16_T)admin->buf_size);
			
			/* clear buffer full flag */
			admin->buffer_full = 0;
			
		}
		
	}

%endfunction


%% Function: Terminate ==========================================================
%%
%% Purpose:
%%      Code generation rules for mdlTerminate function.
%%
%function Terminate(block, system) Output

	%assign pAdminStruct = LibBlockPWork("", "", "", 0)
	%assign CHANNEL_NO   = %<CAST( "Number", LibBlockParameterValue(P2,1) )>
	%%
	%assign format        = %<block.RTWdata.formatStr>
	%%
	%% reset CHANNEL_NO to '0' for 'raw data' transmissions
	%if format == 1
	%assign CHANNEL_NO    = 0
	%endif
	
	/* free instance local data buffer */
	(void)free(((myUsrBuf *)%<pAdminStruct>)->buf);
	(void)free((myUsrBuf *)%<pAdminStruct>);

	/* reset global buffer access pointer to NULL */
	freecomTelBuf[%<CHANNEL_NO>] = NULL;
  
%endfunction

%% [EOF] freePortComms_sfcn_9S12.tlc

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -