%% File : s12x_pwm.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 S12S PWM Module on the DEMO9S12XDT512 board.
%%
%% From hc12_sfcn_pwm.tlc (original hc12tgt)

%implements  s12x_pwm "C"
%%   SFcnParamSettings.
%%   PWMPOLbitBalue     -- scalar
%%   PWMCLKbitBalue     -- scalar
%%   PWMCAEbitBalue     -- scalar
%%   PWMPERbyteBalue    -- scalar


%% Function: Start ==========================================================
%%
%% Purpose:
%%      PWM initialization code.
%%
%function Start(block, system) Output
  %if !SLibCodeGenForSim()
    /* S-Function "s12x_pwm" initialization Block: %<Name> */
    %%
      %assign pwmChannel = CAST("Number", SFcnParamSettings.PWMChannel)
      %assign pwmPOL     = CAST("Number", SFcnParamSettings.PWMPOLbitBalue)
      %assign pwmCLK     = CAST("Number", SFcnParamSettings.PWMCLKbitBalue)
      %assign pwmCAE     = CAST("Number", SFcnParamSettings.PWMCAEbitBalue)
      %assign pwmPER     = CAST("Unsigned", SFcnParamSettings.PWMPERbyteBalue)
      %%Pwm.pwmpol.bit.ppol%<pwmChannel>  = %<pwmPOL>;/* Configure polarity */
      %assign pwmpolreg = PWMPOL%<pwmpol>
      %<pwmpolreg> = %<setBitVal(pwmpolreg, pwmChannel, pwmPOL)>;
      %%Pwm.pwmclk.bit.pclk%<pwmChannel>  = %<pwmCLK>;/* Configure clock select*/
      
      PWMCLK = %<setBitVal("PWMCLK", pwmChannel, pwmCLK);
      %%Pwm.pwmcae.bit.cae%<pwmChannel>  = %<pwmCAE>;/* Configure center alignment*/
      PWMCAE = %<setBitVal("PWMCAE", pwmChannel, pwmCAE)>
      %%Pwm.pwmper[%<pwmChannel>].byte = %<pwmPER>;/* Configure channel period */
      %assign pwmreg = PWMPER%<pwmChannel>
      %<pwmreg> = %<pwmPER>;
      %%Pwm.pwme.bit.pwme%<pwmChannel>  = 1;/* Enable pwm channel */
      PWME = %<setBitVal("PWME", pwmChannel, 1)  
  %endif
%endfunction


%% Function: Outputs ==========================================================
%%
%% Purpose:
%%      PWM initialization code.
%%
%function Outputs(block, system) Output
   %if !SLibCodeGenForSim()
     /* S-Function "s12x_pwm" 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>) {
          Pwm.pwmper[%<pwmChannel>].byte = %<u>; 
      } else {
          Pwm.pwmper[%<pwmChannel>].byte = %<pwmPER>; 
      }
  %endif
%endfunction


%% Function: Terminate ========================================================
%%
%% Purpose:
%%      Code generation rules for mdlTerminate function.
%%
%function Terminate(block, system) Output
  %if !SLibCodeGenForSim()
    /* S-Function "s12x_pwm" terminate Block: %<Name> */
    %assign pwmChannel = CAST("Number", SFcnParamSettings.PWMChannel)
    Pwm.pwme.bit.pwme%<pwmChannel>  = 0;/* Disable pwm channel */
  %endif
%endfunction %% Terminate

%% [EOF] shcs12_pwm.tlc