/*
 *   SHCS12_PWM C-MEX S-function for HCS12 PWM Module
 *                  
 *  Copyright 2002-2004 The MathWorks, Inc.
 *  $Revision: 1.1.2.1 $  
 *  $Date: 2005/01/28 15:45:01 $
 *
 */

#define S_FUNCTION_NAME  s12x_pwm
#define S_FUNCTION_LEVEL 2

/*========================*
 * General Defines/macros *
 *========================*/

/*
 * Need to include simstruc.h for the definition of the SimStruct and
 * its associated macro definitions.
 */
#include "simstruc.h" 

#define TRUE    1
#define FALSE   0

enum {
    PWMCHANNEL_ARGC=0, 
    PWMPOLBIT_ARGC, 
    PWMCLKBIT_ARGC, 
    PWMCAEBIT_ARGC, 
    PWMPERBYTE_ARGC,
    N_PARAMS
};

#define PWMCHANNEL(S)     (mxGetScalar(ssGetSFcnParam(S,PWMCHANNEL_ARGC)))
#define PWMPOLBIT(S)      (mxGetScalar(ssGetSFcnParam(S,PWMPOLBIT_ARGC)))
#define PWMCLKBIT(S)      (mxGetScalar(ssGetSFcnParam(S,PWMCLKBIT_ARGC)))
#define PWMCAEBIT(S)      (mxGetScalar(ssGetSFcnParam(S,PWMCAEBIT_ARGC)))
#define PWMPERBYTE(S)     (mxGetScalar(ssGetSFcnParam(S,PWMPERBYTE_ARGC)))

/*====================*
 * S-function methods *
 *====================*/
static void mdlInitializeSizes(SimStruct *S)
{
    ssSetNumSFcnParams(S, N_PARAMS);
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) return;
    ssSetSFcnParamNotTunable(S, 0);
    ssSetSFcnParamNotTunable(S, 1);
    ssSetSFcnParamNotTunable(S, 2);
    ssSetSFcnParamNotTunable(S, 3);
    ssSetSFcnParamNotTunable(S, 4);

    /* Single input port of width 1 */
    if ( !ssSetNumInputPorts(  S, 1 ) ) return;
    ssSetInputPortWidth(       S, 0, 1 );
    
    //ssSetInputPortOptimOpts(S, 0, SS_REUSABLE_AND_LOCAL);

    /* No output port of width equal to nBits */
    if ( !ssSetNumOutputPorts( S, 1 ) ) return;
     ssSetOutputPortWidth(       S, 0, 1 );
    /* 
     * Input and output datatypes are uint8
     */
    ssSetInputPortDataType(  S, 0, SS_UINT8 );
    ssSetOutputPortDataType(  S, 0, SS_UINT8 );
    ssSetInputPortDirectFeedThrough(S, 0, 1);
    
    /* sample times */
    ssSetNumSampleTimes( S, 1 );
    
    /* options */
    ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE |
                     SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME));

}

/* Function: mdlInitializeSampleTimes =========================================
 * Abstract:
 *    Specifiy  the sample time.
 */
static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
    ssSetOffsetTime(S, 0, 0.0);
}

/* Function: mdlOutputs =======================================================
 *
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
}


/* Function: mdlTerminate =====================================================
 * Abstract:
 *    In this function, you should perform any actions that are necessary
 *    at the termination of a simulation.  For example, if memory was
 *    allocated in mdlStart, this is the place to free it.
 */
static void mdlTerminate(SimStruct *S)
{
}

#define MDL_RTW
static void mdlRTW(SimStruct *S)
{
    uint8_T    pwmCHANNEL    = (uint8_T) PWMCHANNEL(S);    
    boolean_T      pwmPOL    = (boolean_T) PWMPOLBIT(S);
    boolean_T      pwmCLK    = (boolean_T) PWMCLKBIT(S);
    boolean_T      pwmCAE    = (boolean_T) PWMCAEBIT(S);
    uint8_T        pwmPER    = (uint8_T)   PWMPERBYTE(S);

    /* Write out parameters for this block.*/
    if (!ssWriteRTWParamSettings(S, 5, 
                                 SSWRITE_VALUE_DTYPE_NUM,"PWMChannel",
                                 &pwmCHANNEL,DTINFO(SS_UINT8, COMPLEX_NO),
    
                                 SSWRITE_VALUE_DTYPE_NUM,"PWMPOLbitBalue",
                                 &pwmPOL,DTINFO(SS_BOOLEAN, COMPLEX_NO),

                                 SSWRITE_VALUE_DTYPE_NUM,"PWMCLKbitBalue",
                                 &pwmCLK,DTINFO(SS_BOOLEAN, COMPLEX_NO),
                                 
                                 SSWRITE_VALUE_DTYPE_NUM,"PWMCAEbitBalue",
                                 &pwmCAE,DTINFO(SS_BOOLEAN, COMPLEX_NO),
                                 
                                 SSWRITE_VALUE_DTYPE_NUM,"PWMPERbyteBalue",
                                 &pwmPER,DTINFO(SS_UINT8, COMPLEX_NO)
                                 )) {
        return; /* An error occurred which will be reported by SL */
    }
}


#ifdef  MATLAB_MEX_FILE    /* Is this file being compiled as a MEX-file? */
#include "simulink.c"      /* MEX-file interface mechanism */
#else
# error "Attempted use non-inlined S-function shcs12_pwm.c"
#endif

/* [EOF] shcs12_pwm.c */

