#define S_FUNCTION_NAME s12x_dio_out
// s12x_dio_out 这个必须与文件夹的名字一样
#define S_FUNCTION_LEVEL 2
#include “simstruc.h”
enum {
DIO_PORT=0,
DIO_BIT,
INIT_VALUE,
N_PARAMS
};
//上面是创建一个枚举类型,相当于他的寄存器,上面定义的参数是用户双击simulink里面模块修改
#define DIO_PORT(S) (mxGetScalar(ssGetSFcnParam(S, DIO_PORT)))
#define DIO_BIT(S) (mxGetScalar(ssGetSFcnParam(S, DIO_BIT)))
#define DIO_VALUE(S) (mxGetScalar(ssGetSFcnParam(S, INIT_VALUE)))
/* Function: mdlInitializeSizes ===============================================
- Abstract:
- Initialize the sizes array
*/
static void mdlInitializeSizes(SimStruct *S)
{
/* Set and Check parameter count */
ssSetNumSFcnParams(S, N_PARAMS);
**//上面是设置参数个数,这里枚举里面给的是2**
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) return;
**//上面这一行是通过判断simulink里面到底是不是两个参数,如果不是就return,退出**
ssSetSFcnParamNotTunable(S, 0);
**//上面这一行是设置模型在仿真过程中第0个参数是不可调的**
ssSetSFcnParamNotTunable(S, 1);
ssSetSFcnParamNotTunable(S, 2);
// In and Out Ports Num one input and zero output
if (!ssSetNumInputPorts( S, 1)) return;
if (!ssSetNumOutputPorts(S, 0)) return;
**//上面两行设置1个输入,0个输出**
// if the block is a device output must set this peremater
// very import unless this could cause a mistake when set the code to
// the right placement
ssSetInputPortDirectFeedThrough(S, 0, 1);
**//上面这一行是设置输入是直接馈赠,就是设置simulink里面的连接线起到信号传递的作用。输入信号是否通过连接线直接传递到封装的输出模块中去。也就是设置输入端口input0的信号是否mdlOutputs函数中使用,这儿设置为true。**
// Input Width and DataType
ssSetInputPortWidth( S, 0, 1);
ssSetInputPortDataType( S, 0, SS_BOOLEAN);
**//上面两行是设置输入端口的维数和数据类型,input0为1维**
/* sample times */
ssSetNumSampleTimes(S, 1);
**//设置采样时间个数,这里设置了一个采样时间**
/* options *///不用管
ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_DISALLOW_CONSTANT_SAMPLE_TIME));
} /* end mdlInitializeSizes */
/* Function: mdlInitializeSampleTimes =========================================
- Abstract:
- Initialize the sample times array.
*/
static void mdlInitializeSampleTimes(SimStruct S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
} / end mdlInitializeSampleTimes */
//设置初始化采样时间,INHERITED_SAMPLE_TIME这个意味着采样时间为继承(-1)
/* Function: mdlOutputs =======================================================
- Abstract:
- Compute the outputs of the S-function.
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
} /* end mdlOutputs */
//输出函数里面没有内容但是必须保留这个函数
/* Function: mdlTerminate =====================================================
- Abstract:
- Called when the simulation is terminated.
*/
static void mdlTerminate(SimStruct S)
{
} / end mdlTerminate */
//终止函数里面没有内容但是必须保留这个函数
#define MDL_RTW
#if defined(MDL_RTW) && (defined(MATLAB_MEX_FILE) || defined(NRT))
//下面这个函数的作用是向RTW传递参数的作用
static void mdlRTW(SimStruct S)
{
uint8_T dioPort = (uint8_T) DIO_PORT(S);
uint8_T bit = (uint8_T) DIO_BIT(S);
uint8_T value = (uint8_T) DIO_VALUE(S);
/ Write out parameters for this block.*/
if (!ssWriteRTWParamSettings(S, 3,
SSWRITE_VALUE_DTYPE_NUM,“DIOPort”,
&dioPort,DTINFO(SS_UINT8, COMPLEX_NO),
SSWRITE_VALUE_DTYPE_NUM, "Bit",
&bit,DTINFO(SS_UINT8, COMPLEX_NO),
SSWRITE_VALUE_DTYPE_NUM, "Value",
&value,DTINFO(SS_UINT8, COMPLEX_NO)
)) {
return; /* An error occurred which will be reported by SL */
}
}
3代表三个参数,dioPort 代表DIOPort
bit 代表Bit
value 代表Value
#endif
/*==============================================*
- Enforce use of inlined S-function *
- (e.g. must have TLC file interrupt_init.tlc) *
*===============================================*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
include “simulink.c” /* MEX-file interface mechanism */
#else /* Prevent usage by RTW if TLC file is not found /
#include “cg_sfun.h” / Code generation registration function */
//# error “Attempted use non-inlined S-function interrupt_init.c”
#endif
//最后这个包含是底层驱动模块必须包含的一般不需要改