/*
 * File: shcs12_pwm1.c
 *
 *
 *  -------------------------------------------------------------------------
 * | See matlabroot/simulink/src/sfuntmpl_doc.c for a more detailed template |
 *  ------------------------------------------------------------------------- 
 * Created: Sat Sep 13 18:08:10 2008
 * Modified: Mon Nov 24 14:51:13 2008
 * 
 */


#define S_FUNCTION_NAME s12x_pwmfoo1
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"
#define TRUE    1
#define FALSE   0
enum {
    PWMCHANNEL_ARGC=0, 
    PWMPERBYTE_ARGC,
    N_PARAMS
};

#define PWMCHANNEL(S)     (mxGetScalar(ssGetSFcnParam(S,PWMCHANNEL_ARGC)))
#define PWMPERBYTE(S)     (mxGetScalar(ssGetSFcnParam(S,PWMPERBYTE_ARGC)))


/*====================*
 * S-function methods *
 *====================*/


/* Function: mdlInitializeSizes ===============================================
 * Abstract:
 *   Setup sizes of the various vectors.
 */
static void mdlInitializeSizes(SimStruct *S)
{

    ssSetNumSFcnParams(S, N_PARAMS);
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) return;
    ssSetSFcnParamNotTunable(S, 0);
    ssSetSFcnParamNotTunable(S, 1);
    

    /* 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)
{
   /* const real_T   *u0  = (const int_T*) ssGetInputPortSignal(S,0);
    const int_T   p_width0  = mxGetNumberOfElements(PARAM_DEF0(S));
    const int_T   p_width1  = mxGetNumberOfElements(PARAM_DEF1(S));
    const int8_T  *PWM_number  = mxGetData(PARAM_DEF0(S));
    const int8_T  *PWM_period  = mxGetData(PARAM_DEF1(S));*/
    
    
    /* Dummy function, no action required */
}



/* 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)
{
}

/* Function: mdlRTW ============================================================
 * Abstract:
 *    This function is called when the Real-Time Workshop is generating the
 *    model.rtw file. In this routine, you can call the following functions
 *    which add fields to the model.rtw file.
 *
 *    Important! Since this s-function has this mdlRTW method, it is required
 *    to have a correcponding .tlc file so as to work with RTW. 
 */
#define MDL_RTW
static void mdlRTW(SimStruct *S)
{
    uint8_T    pwmCHANNEL    = (uint8_T) PWMCHANNEL(S);    
    uint8_T        pwmPER    = (uint8_T)   PWMPERBYTE(S);

    /* Write out parameters for this block.*/
    if (!ssWriteRTWParamSettings(S, 2, 
                                 SSWRITE_VALUE_DTYPE_NUM,"PWMChannel",
                                 &pwmCHANNEL,DTINFO(SS_UINT8, 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
#include "cg_sfun.h"       /* Code generation registration function */
#endif


