%% File : s12x_encoder.tlc
%%
%% $Revision: 1.1.2.1 $
%% $Date: 2009/01/24 20:00:00 $
%%
%% Copyright 2001-2009 The MathWorks, Inc.
%%
%% Description: 
%%   Real-Time Workshop Embedded Coder device driver 
%%   for S12X TIM Encoder Module on the DEMO9S12XDT512 board.
%%

%implements "s12x_encoder" "C"
%%   SFcnParamSettings.
%%   IcNumber        -- scalar
%%   Edge            -- scalar

%include "driver_utils.tlc"

%% Function: blockTypeSetup ==========================================================
%%
%% Purpose:
%%      Add some macro defines for the analog registers.
%%
%function BlockTypeSetup(block, system) void

%openfile tmpBuf
   #ifndef _%<::S12xChipDefFile>
   #include <%<::S12xChipDefFile>.h>
   #define _%<::S12xChipDefFile>
   #endif
%closefile tmpBuf
  %assign hdrFile = LibGetModelDotHFile()
  %<LibSetSourceFileSection(hdrFile, "Includes", tmpBuf)>

%endfunction

%% ============================================================================
%% Function: BlockInstanceSetup
%% Abstract:
%%
%% ============================================================================
%function BlockInstanceSetup(block, system) void  
 
    %assign icNumber     = CAST("Number", SFcnParamSettings.IcNumber)
    
    %openfile tmpbuf
       extern uint16_T  S12XtimerCtr%<icNumber>;
       extern int16_T   S12XencoderCtr;
    %closefile tmpbuf
    
    %assign hdrFile = LibGetModelDotHFile()
    %<LibSetSourceFileSection(hdrFile, "Declarations", tmpbuf)>
  
%endfunction %% BlockInstanceSetup


%% ============================================================================
%% Function: Start
%% Abstract:
%%
%% ============================================================================
%function Start(block, system) Output

    %assign icNumber     = CAST("Number", SFcnParamSettings.IcNumber)
    %assign edge         = CAST("Number", SFcnParamSettings.Edge)
    
    /* S-Function "s12x_encoder" initialization Block: %<Name> */
     %% Tim0.tflg1.bit.c%<icNumber>f = 1;/* Clear Timer Flag Interrupt TC%<icNumber> */
    %% TFLG1 = %<setBitVal("TFLG1",icNumber, 1)>;
      %%Tim0.tie.bit.c%<icNumber>i = 1;/* Enable Timer Interrupt TC%<icNumber> */
    %% TIE = %<setBitVal("TIE",icNumber, 1)>;
     %%Tim0.tios.bit.ios%<icNumber> = 0;/* Select TC%<icNumber> as Input capture */
    %% TIOS = %<setBitVal("TIOS", icNumber,0)>;
    %% %if icNumber > 3
       %%assign regName = "Tim0.tctl3.byte"
     %%  %assign regName = "TCTL3"
     %%  %assign regValue = edge << 2*(icNumber-4)
  %%  %else
        %%assign regName = "Tim0.tctl4.byte"
   %%     %assign regName = "TCTL4"
    %%    %assign regValue = edge << (2*icNumber)
  %%  %endif
  %%  %<regName> |= %<regValue>;/* Select TC%<icNumber> edge direction */

    /* Enable the timer */
    %% %assign ddr =  "DDRT"
    %% %<ddr> &= ~0x81;
     DDRT   &= ~0x81;
     PACTL   =  0x50;
     PBCTL   =  0x40;
     TCTL4   =  0x01;
     PACN32  =  0x0000;
     PACN10  =  0x0000;
  %%  TSCR1 |= 0x80;
  %%  DDRP;
  
%endfunction %% Start

%% ============================================================================
%% Function: Terminate
%% Abstract:
%%
%% ============================================================================
%function Terminate(block, system) Output
 
    %assign icNumber     = CAST("Number", SFcnParamSettings.IcNumber)
    
      /* S-Function "s12x_encoder" Block: %<Name> */
      %%Tim0.tie.bit.c%<icNumber>i = 0;/* Disable Timer Interrupt TC%<icNumber> */
    %%  %assign rhs = setBitVal("TIE", icNumber, 0)
    %%  TIE = %<rhs>;
     PACN32  =  0x0000;
     PACN10  =  0x0000;
 
%endfunction %% Terminate

%% ============================================================================
%% Function: InitializeConditions
%% Abstract:
%%
%% ============================================================================
%function InitializeConditions(block, system) Output
%endfunction %% InitializeConditions

%% ============================================================================
%% Function: Outputs
%% Abstract:
%%
%% ============================================================================
%function Outputs(block, system) Output
  
    %assign icNumber     = CAST("Number", SFcnParamSettings.IcNumber)
    %assign edge         = CAST("Number", SFcnParamSettings.Edge)

    /* S-Function "s12x_encoder" Block: %<Name> */
    %assign y          = LibBlockOutputSignal(0, "", "", 0)  
    %assign regs = encRegs()
    %<y> = (%<regs.PACN32> - %<regs.PACN10>)+10000;
  %%  %assign S12XencoderCtr = PACN32 - PACN10;
   %% %<y> = S12XencoderCtr;
   %% %<y> = S12XtimerCtr%<icNumber>;

  
%endfunction %% Outputs

