%% File : s12x_pwmfoo1.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 PWM output on the DEMO9S12XDT512 board.
%%

%implements  s12x_pwmfoo1 "C"
%include "driver_utils.tlc"

%% Function: BlockTypeSetup ===================================================
%%
%% Purpose:
%%      Set up external references for wrapper functions in the 
%%      generated code.
%%
%function BlockTypeSetup(block, system) Output

  %% Include the header file for board.

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


%function Start(block, system) Output
  %%if !SLibCodeGenForSim()

    /* S-Function "s12x_pwmfoo1" initialization Block: %<Name> */

    %% Configure PWM
   %% DDRB = DDRB | 0xf0;   /* Configure pins 4-7 of PORTB as ouputs */
    %%PORTB = PORTB & 0xf0;   /* Initial state*/
   %% PWMPOL=0xff;

    %assign regs = pwmRegs()
    %assign pwmChannel = CAST("Number", SFcnParamSettings.PWMChannel)
    %assign pwmPER     = CAST("Unsigned", SFcnParamSettings.PWMPERbyteBalue)

  %<regs.PWMPRCLK>=0x30;
    %assign regs = pwmRegs()
  %<regs.PWMSCLB>=0x06;
    %assign regs = pwmRegs()
  %<regs.PWMCLK>=0x0C;
   %% PWMCLK = %<setBitVal("PWMCLK", pwmChannel, pwmCLK);
    %assign regs = pwmRegs()
  %<regs.PWMPOL>=0x0C;
    %assign regs = pwmRegs()
  %<regs.PWMCAE>=0;
    %assign regs = pwmRegs()
  %<regs.PWMCTL>=0;
    %assign regs = pwmRegs()
  %<regs.PWMPER2>=%<pwmPER>;
    %assign regs = pwmRegs()
  %<regs.PWMDTY2>=30;
    %assign regs = pwmRegs()
  %<regs.PWMCNT2>=0;
    %assign regs = pwmRegs()
  %<regs.PWME>=0x0C;

 %%endif
%endfunction

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

   /* S-Function "s12x_pwmfoo" Block: %<Name> */

  %%assign u0 = LibBlockInputSignal(0, "", "", 0)
  %%assign bit = LibBlockParameter(PWMNUMBER, "", "", 0)
 %% if (%<u0> == 0) {
 %%    PORTB = PORTB & (0xff & ~((unsigned char)(1 << %<bit>)));
 %% } else {
 %%    PORTB = PORTB | (%<u0> << %<bit>);
 %% }
     /* S-Function "s12x_pwmfoo" Block: %<Name> 
      *
      * Set PWM duty cycle (byte) equal to value of input signal "U" 
      */
      %% Scalar inputs only. Width zero only.
      %assign pwmPER     = CAST("Unsigned", SFcnParamSettings.PWMPERbyteBalue)
      %assign pwmChannel = CAST("Number", SFcnParamSettings.PWMChannel)
      %assign u = LibBlockInputSignal(0,"","",0)


      if (%<u> < %<pwmPER>) {
    %assign regs = pwmRegs()
    %<regs.PWMDTY2>=%<u>;
      } else {
    %assign regs = pwmRegs()
    %<regs.PWMDTY2>=%<pwmPER>;
    }
   %% assign regs = pwmRegs()
  %%<regs.PWMCNT2>=0;
   %%assign regs = pwmRegs()
  %%<regs.PWME>=0x04;

   %%endif
%endfunction %% Outputs

%% Function: Terminate ========================================================
%%
%% Purpose:
%%      Code generation rules for mdlTerminate function.
%%
%function Terminate(block, system) Output
  
    /* S-Function "s12x_pwmfoo" terminate Block: %<Name> */
        %assign regs = pwmRegs()
       %<regs.PWME>=0x00;
     
%endfunction %% Terminate


%% [EOF] s12x_pwmfoo.tlc