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

📄 ext_convert.c

📁 simulink real-time workshop for dragon12 development board from
💻 C
📖 第 1 页 / 共 2 页
字号:

        if (swapBytes) {
            slCopyFourBytes(dst, dst, n, swapBytes);
        }
        break;
    }

    case 8:
        slCopyEightBytes(dst, src, n, swapBytes);
        break;

    default:
        /* This implementations supports 64 and 32 bit reals only. */
        assert(FALSE);
        break;
    } /* end switch */
} /* end Double_HostToTarget */


/* Function: Double_TargetToHost ===============================================
 * Abstract:
 *  Convert target real_T value to host double value (8 bytes).  No assumptions
 *  may be made about the alignment of the src ptr.  The dst pointer is aligned
 *  for type double.  As implemented, this function supports only 32 and 64 bit
 *  target real values (ieee).
 */
static void Double_TargetToHost(
    ExternalSim   *ES,
    void          *voidDst,
    const char    *src,
    const int     n,
    const int     dType) /* internal Simulink data type id */
{
    boolean_T swapBytes = esGetSwapBytes(ES);
    double    *dst      = (double *)voidDst;
#   define    MAX_ELS     (1024)

    int sizeofTargetDouble = esGetSizeOfTargetDataTypeFcn(ES)(ES, dType) *
                                esGetHostBytesPerTargetByte(ES);

    switch(sizeofTargetDouble) {
    case 4:
        if (swapBytes || !IS_ALIGNED(src,4)) {
            int  count = 0;
            while(count < n) {
                int   i;
                float tmp[MAX_ELS];
                int   nLeft  = n - count;
                int   nEls   = (MAX_ELS >= nLeft) ? nLeft : MAX_ELS;
                int   nBytes = nEls * 4;

                /* get the floats properly aligned and byte swapped */
                slCopyFourBytes(tmp, src, nEls, swapBytes);

                for (i=0; i<nEls; i++) {
                    dst[count++] = (double)tmp[i];
                }
                src += nBytes;
            }
        } else {
            int   i;
            float *fptr = (float *)src;

            for (i=0; i<n; i++) {
                dst[i] = (double)(*fptr++);
            }
        }
        break;

    case 8:
        slCopyEightBytes(dst, src, n, swapBytes);
        break;

    default:
        /* This implementations supports 64 and 32 bit reals only. */
        assert(FALSE);
        break;
    } /* end switch */
#undef MAX_ELS
} /* end Double_TargetToHost */


/* Function: Bool_HostToTarget =================================================
 * Abstract:
 *  Convert Simulink (hosts) bool value (uint8_T) to target boolean_T value.
 *  No assumptions may be made about the alignment of the dst ptr.
 *  The src pointer is aligned for type uin8_T.  As implemented, this function
 *  supports either uint8_T boolean values on the target or uint32_T booleans
 *  on the target (for dsps that support only 32-bit words).
 */
static void Bool_HostToTarget(
    ExternalSim   *ES,
    char          *dst,
    const void    *voidSrc,
    const int     n,
    const int     dType) /* internal Simulink data type id */
{
    boolean_T     swapBytes = esGetSwapBytes(ES);
    const uint8_T *src      = (const uint8_T *)voidSrc;

    int sizeofTargetBool = esGetSizeOfTargetDataTypeFcn(ES)(ES, dType) *
                                  esGetHostBytesPerTargetByte(ES);

    switch(sizeofTargetBool) {

    case 1:
        /* 
         * Since we assume uint8_t on both target and host, we have no byte
         * swapping issues.  This is just a straight copy.
         */
        (void)memcpy(dst, src, n);
        break;

    case 4:
    {
        int  i;
        char *dstPtr = dst;

        for (i=0; i<n; i++) {
	    uint32_T tmp = (uint32_T)src[i];
            (void)memcpy(dstPtr, &tmp, 4);
            dstPtr += 4;
        }

        if (swapBytes) {
            slCopyFourBytes(dst, dst, n, swapBytes);
        }
        break;
    }

    default:
        /* This implementation supports only 8 and 32 bit bools on target */
        assert(FALSE);
        break;
    } /* end switch */
} /* end Bool_HostToTarget */


/* Function: Bool_TargetToHost =================================================
 * Abstract:
 *  Convert target bool value to host bool value (uint8_t).  No assumptions may
 *  be made about the alignment of the src ptr.  The dst pointer is aligned for
 *  type uin8_T.  As implemented, this function supports either uint8_T boolean
 *  values on the target or uint32_T booleans on the target (for dsps that
 *  support only 32-bit words).
 */
static void Bool_TargetToHost(
    ExternalSim   *ES,
    void          *voidDst,
    const char    *src,
    const int     n,
    const int     dType) /* internal Simulink data type id */
{
#   define    MAX_ELS     (1024)
    boolean_T swapBytes = esGetSwapBytes(ES);
    uint8_T   *dst      = (uint8_T *)voidDst;

    int  sizeofTargetBool = esGetSizeOfTargetDataTypeFcn(ES)(ES, dType) *
                                  esGetHostBytesPerTargetByte(ES);

    switch (sizeofTargetBool) {

    case 1:
        /* 
         * Since we assume uint8_t on both target and host, we have no byte
         * swapping issues.  This is just a straight copy.
         */
        (void)memcpy(dst, src, n);
        break;

    case 4:
        if (swapBytes || !IS_ALIGNED(src,4)) {
            int  count = 0;
            while(count < n) {
                int      i;
                uint32_T tmp[MAX_ELS];
                int      nLeft  = n - count;
                int      nEls   = (MAX_ELS >= nLeft) ? nLeft : MAX_ELS;
                int      nBytes = nEls * 4;

                /* get the uint32_T's properly aligned and byte swapped */
                slCopyFourBytes(tmp, src, nEls, swapBytes);

                for (i=0; i<nEls; i++) {
                    dst[count++] = (uint8_T)tmp[i];
                }
                src += nBytes;
            }
        } else {
            int      i;
            uint32_T *tmp = (uint32_T *)src;

            for (i=0; i<n; i++) {
                dst[i] = (uint8_T)(*tmp++);
            }
        }
        break;

    default:
        /* This implementation supports only 8 and 32 bit bools on target */
        assert(FALSE);
        break;
    } /* end switch */
#undef MAX_ELS
} /* end Bool_TargetToHost */


/* Function: Generic_HostToTarget ==============================================
 * Abstract:
 *  Convert generic data type from host to target format.  This function
 *  may be used with any data type where the number of bits is known to
 *  be the same on both host and target (e.g., int32_T, uint16_t, etc).
 *  It simply copies the correct number of bits from target to host performing
 *  byte swapping if required.  If any other conversion is required, then
 *  a custom HostToTarget function must be used.
 */
static void Generic_HostToTarget(
    ExternalSim   *ES,
    char          *dst,
    const void    *src,
    const int     n,
    const int     dType) /* internal Simulink data type id */
{
    int       dTypeSize = esGetSizeOfDataTypeFcn(ES)(ES, dType);
    boolean_T swapBytes = esGetSwapBytes(ES);
   
    slCopyNBytes(dst, src, n, swapBytes, dTypeSize);
} /* end Generic_HostToTarget */


/* Function: Generic_TargetToHost ==============================================
 * Abstract:
 *  Convert generic data type from target to host format.  This function
 *  may be used with any data type where the number of bits is known to
 *  be the same on both host and target (e.g., int32_T, uint16_t, etc).
 *  It simply copies the correct number of bits from host to target performing
 *  byte swapping if required.  If any other conversion is required, then
 *  a custom TargetToHost function must be used.
 */
static void Generic_TargetToHost(
    ExternalSim  *ES,
    void         *dst,
    const char   *src,
    const int    n,
    const int    dType) /* internal Simulink data type id */
{
    int       dTypeSize = esGetSizeOfDataTypeFcn(ES)(ES, dType);
    boolean_T swapBytes = esGetSwapBytes(ES);

    slCopyNBytes(dst, src, n, swapBytes, dTypeSize);
} /* end Generic_TargetToHost */


/* Function: Copy32BitsToTarget ================================================
 * Abstract:
 *  Copy 32 bits to the target.  It is assumed that the only conversion needed
 *  is bytes swapping (if needed) (e.g., uint32, int32).  Note that this fcn
 *  does not rely on the Simulink Internal data type id.
 */
void Copy32BitsToTarget(
    ExternalSim  *ES,
    char         *dst,
    const void   *src,
    const int    n)
{
    boolean_T swapBytes = esGetSwapBytes(ES);
    slCopyNBytes(dst, src, n, swapBytes, 4);
} /* end Copy32BitsToTarget */


/* Function: Copy32BitsFromTarget ==============================================
 * Abstract:
 *  Copy 32 bits from the target.  It is assumed that the only conversion needed
 *  is bytes swapping (if needed) (e.g., uint32, int32).  Note that this fcn
 *  does not rely on the Simulink Internal data type id.
 */
void Copy32BitsFromTarget(
    ExternalSim *ES,
    void        *dst,
    const char  *src,
    const int    n)
{
    boolean_T swapBytes = esGetSwapBytes(ES);
    slCopyNBytes(dst, src, n, swapBytes, 4);
} /* end Copy32BitsFromTarget */


/* Function ====================================================================
 * Process the first of two EXT_CONNECT_RESPONSE messages from the target.
 * This message consists of nothing but a message header.  In this special
 * instance we interpret the size field as the number of bits in a target
 * byte (not always 8 - see TI compiler for C30 and C40).
 *
 * This function is responsible for deducing the endian format of the target,
 * validating that the number of bits per target byte and setting up pointers
 * to data conversion functions.
 *
 * NOTE: The caller must check that the error status is clear after calling
 *       this function (i.e., esIsErrorClear(ES)).
 */
void ProcessConnectResponse1(ExternalSim *ES, MsgHeader *msgHdr)
{
    /*
     * Deduce the endian-ness of the target.
     */
    if (msgHdr->type == EXT_CONNECT_RESPONSE) {
        esSetSwapBytes(ES, FALSE);
    } else {
        const boolean_T swapBytes = TRUE;
        slCopyFourBytes(msgHdr, msgHdr, NUM_HDR_ELS, swapBytes);
        if (msgHdr->type != EXT_CONNECT_RESPONSE) {
            esSetError(ES, "Invalid EXT_CONNECT_RESPONSE message.\n");
            goto EXIT_POINT;
        }
        esSetSwapBytes(ES, swapBytes);
    }

    /*
     * Process bits per target byte.
     */
    {
        int_T bitsPerTargetByte      = msgHdr->size;
        int_T hostBytesPerTargetByte = bitsPerTargetByte/8;
        
        assert(bitsPerTargetByte%8 == 0);
        esSetHostBytesPerTargetByte(ES, (uint8_T)hostBytesPerTargetByte);

        #if DEBUG_MSG_LVL >= 4
	    mexPrintf("ProcessConnectResponse1: msgHdr->size %d\n", msgHdr->type);
	    mexPrintf("ProcessConnectResponse1: msgHdr->size %d\n", msgHdr->size);
	    mexPrintf("ProcessConnectResponse1: Bits per target byte %d\n", bitsPerTargetByte);
	    mexPrintf("ProcessConnectResponse1: Setting number of host bytes per target byte to %d\n", hostBytesPerTargetByte);
	    #endif

    }

    /*
     * Set up fcn ptrs for data conversion - Simulink data types.
     */
    esSetDoubleTargetToHostFcn(ES, Double_TargetToHost);
    esSetDoubleHostToTargetFcn(ES, Double_HostToTarget);
    
    esSetSingleTargetToHostFcn(ES, Generic_TargetToHost); /* assume 32 bit */
    esSetSingleHostToTargetFcn(ES, Generic_HostToTarget); /* assume 32 bit */
    
    esSetInt8TargetToHostFcn(ES, Generic_TargetToHost);
    esSetInt8HostToTargetFcn(ES, Generic_HostToTarget);

    esSetUInt8TargetToHostFcn(ES, Generic_TargetToHost);
    esSetUInt8HostToTargetFcn(ES, Generic_HostToTarget);

    esSetInt16TargetToHostFcn(ES, Generic_TargetToHost);
    esSetInt16HostToTargetFcn(ES, Generic_HostToTarget);

    esSetUInt16TargetToHostFcn(ES, Generic_TargetToHost);
    esSetUInt16HostToTargetFcn(ES, Generic_HostToTarget);

    esSetInt32TargetToHostFcn(ES, Generic_TargetToHost);
    esSetInt32HostToTargetFcn(ES, Generic_HostToTarget);

    esSetUInt32TargetToHostFcn(ES, Generic_TargetToHost);
    esSetUInt32HostToTargetFcn(ES, Generic_HostToTarget);

    esSetBoolTargetToHostFcn(ES, Bool_TargetToHost);
    esSetBoolHostToTargetFcn(ES, Bool_HostToTarget);

EXIT_POINT:
    return;
} /* end ProcessConnectResponse1 */


/* Function ====================================================================
 * Process the data sizes information from the second EXT_CONNECT_RESPONSE 
 * messages.  The data passed into this function is of the form:
 * 
 * nDataTypes    - # of data types          (uint32_T)
 * dataTypeSizes - 1 per nDataTypes         (uint32_T[])
 *
 * NOTE: The caller must check that the error status is clear after calling
 *       this function (i.e., esIsErrorClear(ES)).
 */
void ProcessTargetDataSizes(ExternalSim *ES, uint32_T *bufPtr)
{
    uint32_T  i;

    /* nDataTypes */
    if (esGetNumDataTypes(ES) != *bufPtr++) {
        esSetError(ES, "Unexpected number of data types returned from host.\n");
        goto EXIT_POINT;
    }

    /* data type sizes */
    for (i=0; i<esGetNumDataTypes(ES); i++) {
        esSetDataTypeSize(ES, i, *bufPtr++);
    }

EXIT_POINT:
    return;
} /* end ProcessTargetDataSizes */


/* [EOF] ext_convert.c */

⌨️ 快捷键说明

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