⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 de_crcgenx.c

📁 simulink s函数
💻 C
字号:
/* 
 * 文件名:de_crcgenx.c
 * 功能说明:对输入的数据进行crc校验,并去掉校验位
 * 参数说明:CRC_LEN CRC校验位的长度
 * 端口说名:1个输入端口2个输出端口。第一个输出端口输出去掉校验位的数据,第二个输出端口作为校验结果输出,如果数据没有错误则输出0,否则输出1。输入输出为流数据
 * 版本号:1.00
 
 * 使用编译器: Microsoft Visual C/C++ 6.0 
 */


#define S_FUNCTION_NAME  de_crcgenx
#define S_FUNCTION_LEVEL 2

#include "simstruc.h"

#define IDX_1 0
#define CRC_LEN ssGetSFcnParam(S,IDX_1) /* Get parameter CRC_length */


/* Function: mdlInitializeSizes ===============================================
 * Abstract:
 *   Setup sizes of the various vectors.
 */
static void mdlInitializeSizes(SimStruct *S)
{
    ssSetNumSFcnParams(S, 1); /* Set parameter number */
    if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
        return;
    }

    if (!ssSetNumInputPorts(S, 1)) return; /* Set input port number is 1 */
    ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED); /* Set input port width is dynamically */ 
    ssSetInputPortDirectFeedThrough(S, 0, 1);/* Exist directfeed */

    if (!ssSetNumOutputPorts(S, 2)) return; /* Set output port number is 1 */
    ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED); /* Set output port width is dynamically */
    ssSetOutputPortWidth(S, 1, 1);
    ssSetNumSampleTimes(S, 1); /* Set the number of sampletime is 1 */
    ssSetNumRWork(S, 0);
    ssSetNumIWork(S, 0);
    ssSetNumPWork(S, 0);
    ssSetNumModes(S, 0);
    ssSetNumNonsampledZCs(S, 0);

    ssSetOptions(S,
                 SS_OPTION_WORKS_WITH_CODE_REUSE |
                 SS_OPTION_EXCEPTION_FREE_CODE |
                 SS_OPTION_USE_TLC_WITH_ACCELERATOR);
}

#if defined(MATLAB_MEX_FILE)
# define MDL_SET_INPUT_PORT_WIDTH
  static void mdlSetInputPortWidth(SimStruct *S, int_T port,
                                    int_T inputPortWidth)
  {
      int_T crc_num;
      crc_num = (int_T)*mxGetPr(CRC_LEN); 
      ssSetInputPortWidth(S,port,inputPortWidth);
      ssSetOutputPortWidth(S,0,inputPortWidth-crc_num);
      ssSetOutputPortWidth(S,1,1);
  }

# define MDL_SET_OUTPUT_PORT_WIDTH
  static void mdlSetOutputPortWidth(SimStruct *S, int_T port,
                                     int_T outputPortWidth)
  {
      int_T crc_num;
      crc_num = (int_T)*mxGetPr(CRC_LEN); 
      if ( port == 0 )
      {
          ssSetInputPortWidth(S,0,outputPortWidth+crc_num); 
          ssSetOutputPortWidth(S,0,outputPortWidth);
      }
      ssSetOutputPortWidth(S,port,outputPortWidth);
  }
#endif

/* Function: mdlInitializeSampleTimes =========================================
 * Abstract:
 *    Specifiy that we inherit our sample time from the driving block.
 */
static void mdlInitializeSampleTimes(SimStruct *S)
{
    ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME); /* Set the sample time is inherited */
    ssSetOffsetTime(S, 0, 0.0);
}


/* Function: mdlOutputs ======================================================= */
static void mdlOutputs(SimStruct *S, int_T tid)
{
    int_T crc_num = (int_T)*mxGetPr(CRC_LEN); /* Get the crc code length */
    InputRealPtrsType u = ssGetInputPortRealSignalPtrs(S,0); /* Get the pointer to the input signal */
    real_T       *y = ssGetOutputPortRealSignal(S,0); /* Get the pointer to the output signal */
    real_T       *y1 = ssGetOutputPortRealSignal(S,1);
    int_T inw = ssGetInputPortWidth(S,0); /* Get the input port width */
    int_T gen_12[] = {1,1,0,0,0,0,0,0,0,1,1,1,1};  /* define the genpoly */
    uint32_T reg_12[] = {0,0,0,0,0,0,0,0,0,0,0,0,0};  /* define buffer */ 
    int_T gen_16[] = {1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
    uint32_T reg_16[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    int_T i, j, k, *g, *r;
    int_T flg = 0;
    
    if ( crc_num == 12 ) 
    {
        g = gen_12; /* pointer to the array gen_12 */
        r = reg_12; /* pointer to the array reg_12 */
    }
    else if ( crc_num == 16 )
    {
        g = gen_16; /* pointer to the array gen_16 */
        r = reg_16; /* pointer to the array reg_16 */
    }
    else
    {
        ssSetErrorStatus(S,"The CRC length must be 12 or 16!");
        return;
    }
    
    /*switch ( crc_num )
    {
        case 8: g = gen_8; r = gen_8;
        case 16: g = gen_16; r = gen_16;
        default: break;
    }*/
    
    /* 以下为产生CRC校验码的算法 */
    for ( i = 0; i < inw; i++ )
    {
        /* 缓冲区术据左移一位,输入术据移入一位到缓冲区 */
        for ( k = 0; k < crc_num; k++ )
            *(r+k) = *(r+k+1);
        *(r+crc_num) = *u[i];
               
        /* 如果缓冲区第一位为1,则从第2位开始与生成多项式的相应位进行模2加 */
        if ( *r == 1)
        {
            for ( j = 1; j < crc_num+1; j++ )
            {
                *(r+j) = *(r+j) + *(g+j);
                if ( *(r+j) == 2 ) *(r+j)=0;
            }
        }
    }
    for ( i = 0; i<=crc_num; i++ )
    {
        if ( *r++ != 0 )
            flg = 1;
    }
    for ( i = 0; i < inw-crc_num; i++ )
        *y++ = *u[i]; /* output the input data first */
    *y1 = (real_T)flg;
    
    
}


/* Function: mdlTerminate =====================================================
 * Abstract:
 *    No termination needed, but we are required to have this routine.
 */
static void mdlTerminate(SimStruct *S)
{
}



#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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -