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

📄 updown.c

📁 simulink real-time workshop for dragon12 development board from
💻 C
📖 第 1 页 / 共 5 页
字号:
/* $Revision: 1.41 $
 * Copyright 1994-2002 The MathWorks, Inc.
 */


/* modifications: (FW-02-03)
 *
 * (1)  modified function UploadBufAddTimePoint() to make a conditional section 
 *      unconditional (see comment 'FW-02-03')
 *
 */


#include <stdlib.h>
#include <string.h>

#include <mc9s12dp256.h>		      /* port definitions etc. */
#include "mc_signal.h"     				/* abort_LED etc. */

#define  MAXRBUFSIZE_9S12  2048


/*Real Time Workshop headers*/
#include "tmwtypes.h"
#include "simstruc_types.h"

#include "ext_share.h"
#include "ext_svr.h"
#include "updown_util.h"
#include "dt_info.h"
#include <stdio.h>

/**********************
 * External Variables *
 **********************/
#ifdef VXWORKS
# include <sockLib.h>
# include <inetLib.h>
# include <selectLib.h>
extern SEM_ID uploadSem;

#endif

/******************************************************************************
 * Parameter Download                                                         *
 ******************************************************************************/

#ifdef VERBOSE
/* Function: DType2Real_T ======================================================
 * Convert a built-in data type to a real_T value.  Return the real_T value
 * as well as a string (by reference) corresponding to the original data type.
 * If the data type is not recognized as a Simulink built-in data type, then
 * NULL is returned forthe dTypeName and 0 is returned for the dTypeValue.
 */
PRIVATE double DType2Double(
    const char              *vPtr,
    const int               dTypeIdx,
    const DataTypeTransInfo *dtInfo,
    const char              **dTypeName)
{
    real_T     outVal;
    char * const *dTypeNames = dtGetDataTypeNames(dtInfo);
    const char *thisName     = dTypeNames[dTypeIdx];

    *dTypeName = thisName;

    if (strcmp(thisName, "real_T") == 0) {
        outVal = (real_T) (((const real_T*)vPtr)[0]);
    } else if (strcmp(thisName, "real32_T") == 0) {
        outVal = (real_T) (((const real32_T*)vPtr)[0]);
    } else if (strcmp(thisName, "int8_T") == 0) {
        outVal = (real_T) (((const int8_T*)vPtr)[0]);
    } else if (strcmp(thisName, "uint8_T") == 0) {
        outVal = (real_T) (((const uint8_T*)vPtr)[0]);
    } else if (strcmp(thisName, "int16_T") == 0) {
        outVal = (real_T) (((const int16_T*)vPtr)[0]);
    } else if (strcmp(thisName, "uint16_T") == 0) {
        outVal = (real_T) (((const uint16_T*)vPtr)[0]);
    } else if (strcmp(thisName, "int32_T") == 0) {
        outVal = (real_T) (((const int32_T*)vPtr)[0]);
    } else if (strcmp(thisName, "uint32_T") == 0) {
        outVal = (real_T) (((const uint32_T*)vPtr)[0]);
    } else if (strcmp(thisName, "boolean_T") == 0) {
        outVal = (real_T) (((const boolean_T*)vPtr)[0]);
    } else {
        outVal    = 0;
        dTypeName = NULL;
    }
    return(outVal);
} /* end DType2Double */
#endif


/* Function: SetParam ==========================================================
 * Install new parameters.
 *
 * NOTE: pbuf looks like:
 *  [NPARAMS
 *   B S W DI DATA   % pVal 0
 *   B S W DI DATA   % pVal 1
 *   B S W DI DATA   % pVal N
 *  ]
 *
 *  No assumptions about the aligment of pbuf may be made.
 *
 *  where,
 *      B   : Data type transition index.  Note that on the target the data
 *            type transition index provides both the base address (B)
 *              of the transition as well as whether or not
 *              that section of the array contains complex elements.
 *      S   : starting offset of the data from the DT_TRANS_IDX
 *      W   : number of elements for this param
 *      DI  : index into rtw data type table (inUse idx)
 *      DATA: the param values (in target format).
 *
 *  and,
 *      All values, excluding DATA, are int32_T.
 */
PUBLIC void SetParam(RTWExtModeInfo  *ei, const char *pbuf)
{
    int        i;
    int32_T    nParams;
    const char *bufPtr    = pbuf;
    const int  B          = 0; /* index into dtype tran table (base address)  */
    const int  SI         = 1; /* starting index - wrt to base address        */
    const int  W          = 2; /* width of section (number of elements)       */
    const int  DI         = 3; /* index into data type tables                 */
    const int  tmpBufSize = sizeof(int32_T) * 4;
    int32_T    tmpBuf[4];

    const DataTypeTransInfo *dtInfo = rteiGetModelMappingInfo(ei);
    const DataTypeTransitionTable *dtTable = dtGetParamDataTypeTrans(dtInfo);
    const uint_T *dtSizes = dtGetDataTypeSizes(dtInfo);

    /* unpack NPARAMS */
    (void)memcpy(&nParams, bufPtr, sizeof(int32_T));
    bufPtr += sizeof(int32_T);
    
#ifdef VERBOSE
    printf("\nUpdating %d parameters....\n", nParams);
#endif

    /*
     * Unpack the data and install the new parameters.
     */
    for (i=0; i<nParams; i++) {
        int_T   elSize;
        int_T   nBytes;
        char_T  *start;
        char_T  *tranAddress;
        int_T   tranIsComplex;

        /* unpack B SI W DI */
        (void)memcpy(tmpBuf, bufPtr, tmpBufSize);
        bufPtr += tmpBufSize;

        /* 
         * Find starting address and size (nBytes) for this parameters 
         * section of memory.
         */
        tranAddress   = dtTransGetAddress(dtTable, tmpBuf[B]);
        tranIsComplex = dtTransGetComplexFlag(dtTable, tmpBuf[B]);

        elSize = dtSizes[tmpBuf[DI]] * (tranIsComplex ? 2 : 1);
        nBytes = (uint_T)tmpBuf[W] * elSize;
        start = tranAddress + (tmpBuf[SI] * elSize);

        /* Install the params. */
        (void)memcpy(start, bufPtr, (uint16_T)nBytes);
        bufPtr += nBytes;

#ifdef VERBOSE
        /*
         * It is safe to assume that once the params are installed into
         * the param vector that they are properly aligned.  So we
         * do our verbosity print-out here.
         */
        {
            double     val;
            const char *dTypeName;
            const char *vPtr = (const char *)start;

            val = DType2Double(start, tmpBuf[DI], dtInfo, &dTypeName);
            printf("\n\tParam| "
                   "DT_Trans: %d, index: %d, nEls: %d, data type: [%s, %d]\n",
                   tmpBuf[B], tmpBuf[SI], tmpBuf[W],
                   (dTypeName != NULL) ? dTypeName : "",tmpBuf[DI]);

            if (!tranIsComplex) {
                int j;
                for (j=0; j<tmpBuf[W]; j++) {
                    val = DType2Double(vPtr, tmpBuf[DI], dtInfo, &dTypeName);
                    printf("\t\t%f\n", val);
                    vPtr += dtSizes[tmpBuf[DI]];
                }
            } else {
                int j;
                for (j=0; j<tmpBuf[W]; j++) {
                    val = DType2Double(vPtr, tmpBuf[DI], dtInfo, &dTypeName);
                    printf("\t\t%f + ", val);
                    vPtr += dtSizes[tmpBuf[DI]];

                    val = DType2Double(vPtr, tmpBuf[DI], dtInfo, &dTypeName);
                    printf("\t\t%fi\n", val);
                    vPtr += dtSizes[tmpBuf[DI]];
                }
            }
        }
#endif
    }
} /* end SetParam */


/******************************************************************************
 * Parameter Upload                                                           *
 ******************************************************************************/
#include "upsup_public.h"
#define DUMP_MSG (0)


/*=============================================================================
 * Circular buffer stuff.
 *============================================================================*/
typedef struct BufMemList_tag {

#if ASSERTS_ON
    int_T  maxBufs; /* for debugging */
#endif

    int_T  nActiveBufs; /* num non-empty bufs                  */
    int_T  tids[NUMST]; /* tid associated with each active buf */

    BufMem *bufs; /* sections of each buffer to upload */
} BufMemList;


typedef struct CircularBuf_tag {
    int_T    empty;

    int_T    bufSize;
    char_T   *buf;
    
    char_T   *head;
    char_T   *tail;

    char_T   *newTail;

    struct {
        int_T count;
    } preTrig;
} CircularBuf;


/*==============================================================================
 * Trigger stuff.
 *============================================================================*/
typedef enum {
    TRIGGER_UNARMED,
    TRIGGER_HOLDING_OFF,
    TRIGGER_ARMED,
    TRIGGER_DELAYED,
    TRIGGER_FIRED,
    TRIGGER_TERMINATING,
    TRIGGER_SENDING_TERM_MSG
} TriggerState;


/*==============================================================================
 * General stuff.
 *============================================================================*/

/*
 * A BIOSection defines a contiguous section of the blockio structure.  Each
 * section consists of elements of the same data type and same complexity.
 * xxx 'start' should be a const pointer
 */
typedef struct BIOSection_tag {
    void   *start;
    int_T  nBytes;
} BIOSection;

/*
 * A BIOMap is an array of BIOSections.  Typically a map consists of all of the
 * sections of the blockio structure that are relevent to a given task.
 */
typedef struct BIOMap_tag {
    int32_T    nSections;
    BIOSection *sections;

    int_T nBytes;  /* total number of bytes in this map */
} BIOMap;


/*
 * Each system contains a table of BIOMap's (one for each tid).  If no data
 * uploading is being done for a given tid, the bioMap pointer is NULL.
 * The enableState field indicates whether the system in question is active.
 *
 * For a model with 5 total tids, the table looks like:
 *
 *               tid 0    tid 1    tid 2   tid 3   tid 4
 * bioMap      -------------------------------------------
 *             |  ptr  |  NULL  |  NULL  |  ptr  |  NULL |
 *             ----|-------------------------|------------
 *                 |                         |
 *                 v                         v
 *               BIOMap                    BIOMap
 *              for tid 0                 for tid 3
 */
typedef struct SysUploadTable_tag {
    int_T   *enableState;
    BIOMap  *bioMap[NUMST];
} SysUploadTable;


typedef struct TriggerInfo_tag {
    TriggerState            state;
    int_T                   tid;
    int32_T                 duration;
    int32_T                 holdOff;
    int32_T                 delay;
    int_T                   lookForRising;
    int_T                   lookForFalling;
    real_T                  level;
    int_T                   count;
    int_T                   overFlow;

    BIOMap                  trigSignals;
    real_T                  *oldTrigSigVals;
    int_T                   haveOldTrigSigVal;

    struct {
        int32_T    duration;
        int32_T    count;

        int_T      checkUnderFlow; /* ??? */
    } preTrig;
} TriggerInfo;


/*
 * The BdUploadInfo contains all information regarding data logging.
 */
struct BdUploadInfo_tag {
    int32_T        nSys;       /* # of sys's for which data logging is active */
    SysUploadTable *sysTables; /* array of SysUploadTables                    */

    CircularBuf  circBufs[NUMST];  /* circular buffers to store upload data */
    BufMemList   bufMemList;       /* A list of buffer memory that contains
                                    * data to upload */

    TriggerInfo  trigInfo;
};


/*==============================================================================
 * Global upload data.
 */

/*
 * Definitions that must match Simulink definitions.
 */

#define UPLOAD_RISING_TRIGGER                   ((int32_T)  0)
#define UPLOAD_FALLING_TRIGGER                  ((int32_T)  1)
#define UPLOAD_EITHER_RISING_OR_FALLING_TRIGGER ((int32_T)  2)

/*
 * Definitions.
 */
#define TRIGMODE_ONESHOT (-1)

static BdUploadInfo  uploadInfo;


/* Function ====================================================================
 * Dump the signal selection message (EXT_SELECT_SIGNALS).  The message looks
 * like:
 *
 * nSys - the number of systems that contain upload blocks (length of the
 *        (BdUploadInfo list)
 *
 * enableIdx - the index into the "mode vector" that tells whether or not
 *             a given system is active
 *
 * nTids - the number of tids in a system that contain upload blocks (number

⌨️ 快捷键说明

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