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

📄 ext_comm.c

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

	}
	#endif

    /* Force 8 bits per data value -- this is not true with the TI compiler 
	 * for C30 and C40). This fix has been introduced to avoid the 'confusion'
	 * caused by this exception to the stucture of telegram headers (abuse of
	 * size field)  --  (FW-09-02)
	 */

	/* byte order is important -> this is only determined in 'ProcessConnectResponse1' (below this line); this function determines
	   endianess, byte order, etc. and corrects it accordingly   ---  fw-02-05 */
//	msgHdr.size = 0x08000000;		/* restores '8' which is forced to '0' in ext_svr.c -- targets with reversed byte order (hc12) */
	msgHdr.size = 0x00000008;		/* restores '8' which is forced to '0' in ext_svr.c -- assuming regular order now (-> swap in ExtGetTargetMsg) */

	ProcessConnectResponse1(ES, &msgHdr);
    if (!esIsErrorClear(ES)) goto EXIT_POINT;
    
    //if (esGetVerbosity(ES)) {
	#if DEBUG_MSG_LVL >= 4
	{
        MachByteOrder hostEndian = 
            (*((int8_T *) &one) == 1) ? LittleEndian : BigEndian;

        mexPrintf("host endian mode:\t%s\n",
	  (hostEndian == LittleEndian) ? "Little":"Big");

        mexPrintf("byte swapping required:\t%s\n",
            (esGetSwapBytes(ES)) ? "true":"false");
    }
	#endif


	/*
     * Get the second of two EXT_CONNECT_RESPONSE messages.  The format of the
     * message is:
     *
     * CS1 - checksum 1 (uint32_T)
     * CS2 - checksum 2 (uint32_T)
     * CS3 - checksum 3 (uint32_T)
     * CS4 - checksum 4 (uint32_T)
     * 
     * targetStatus  - the status of the target (uint32_T)
     *
     * nDataTypes    - # of data types          (uint32_T)
     * dataTypeSizes - 1 per nDataTypes         (uint32_T[])
     */
    error = ExtGetTargetMsg(ES,sizeof(msgHdr),&nGot,(char_T *)&msgHdr);
    if (error || (nGot != sizeof(msgHdr))) {
        esSetError(ES, 
                   "ExtGetTargetMsg() call failed on 2nd "
                   "EXT_CONNECT_RESPONSE.\n");
        goto EXIT_POINT;
    }

    #if DEBUG_MSG_LVL >= 4
	{
		int_T	i;

		for(i=0; i<nGot; i++)
			mexPrintf("ExtConnect: message (before copy32) [%d/%d]) %d\n", i+1, nGot, (uint_T)(*(((char_T *)&msgHdr) + i)));

	}
	#endif
    Copy32BitsFromTarget(ES, (uint32_T *)&msgHdr, (char *)&msgHdr, NUM_HDR_ELS);
    #if DEBUG_MSG_LVL >= 4
	{
		int_T	i;

		for(i=0; i<nGot; i++)
			mexPrintf("ExtConnect: message (after copy32) [%d/%d]) %d\n", i+1, nGot, (uint_T)(*(((char_T *)&msgHdr) + i)));

	}
	#endif

    if (!esIsErrorClear(ES)) goto EXIT_POINT;

    if (msgHdr.type != EXT_CONNECT_RESPONSE) {
        esSetError(ES, "Unexpected response from target. "
                       "Expected EXT_CONNECT_RESPONSE 2.\n");
        goto EXIT_POINT;
    }

    /*
     * Allocate space to hold message.
     */
    {
        uint32_T *tmpBuf;
        uint32_T *bufPtr;
        int_T    msgSize = msgHdr.size * esGetHostBytesPerTargetByte(ES);

	    #if DEBUG_MSG_LVL >= 4
		mexPrintf("ExtConnect: Allocating memory for a message with %d bytes\n", msgSize);
		#endif

        tmpBuf = (uint32_T *)malloc(msgSize);
        if (tmpBuf == NULL) {
            esSetError(ES, "Memory allocation failure\n");
            goto EXIT_POINT;
        }
        bufPtr = tmpBuf;

        error = ExtGetTargetMsg(ES,msgSize,&nGot,(char_T *)tmpBuf);

		#if DEBUG_MSG_LVL >= 4
		mexPrintf("ExtConnect: ExtGetTargetMsg returns %d bytes\n", nGot);
		#endif


        if (error || (nGot != msgSize)) {
            free(tmpBuf);
            esSetError(ES, 
                       "ExtGetTargetMsg() call failed on 2nd "
                       "EXT_CONNECT_RESPONSE.\n");
            goto EXIT_POINT;
        }

		#if DEBUG_MSG_LVL >= 4
		{
			int_T	i;

			for(i=0; i<nGot; i++)
				mexPrintf("ExtConnect: message (before copy32) [%d/%d]) %d\n", i+1, nGot, (uint_T)*((char_T *)tmpBuf + i));

		}
		#endif

		/*
         * This message happens to consists of all uint32_T.  Convert them
         * in a batch.
         */
        Copy32BitsFromTarget(ES,tmpBuf,(char *)tmpBuf,msgSize/sizeof(uint32_T));

		#if DEBUG_MSG_LVL >= 4
		{
			int_T	i;

			for(i=0; i<nGot; i++)
				mexPrintf("ExtConnect: message (after copy32) [%d/%d]) %d\n", i+1, nGot, (uint_T)*((char_T *)tmpBuf + i));

		}
		#endif
        
		
		if (!esIsErrorClear(ES)) goto EXIT_POINT;

        /* process 128 bit checksum */
		#if DEBUG_MSG_LVL >= 2
		mexPrintf("CS1: %d\n", bufPtr[0]);
		mexPrintf("CS2: %d\n", bufPtr[1]);
		mexPrintf("CS3: %d\n", bufPtr[2]);
		mexPrintf("CS4: %d\n", bufPtr[3]);
		#endif

        esSetTargetModelCheckSum(ES, 0, *bufPtr++);
        esSetTargetModelCheckSum(ES, 1, *bufPtr++);
        esSetTargetModelCheckSum(ES, 2, *bufPtr++);
        esSetTargetModelCheckSum(ES, 3, *bufPtr++);

        /* process target status */
        esSetTargetSimStatus(ES, (TargetSimStatus)*bufPtr++);

        ProcessTargetDataSizes(ES, bufPtr);
        free(tmpBuf);
        if (!esIsErrorClear(ES)) goto EXIT_POINT;
    }
    
    /*
     * Set up function pointers for Simulink.
     */
    esSetRecvIncomingMsgFcn(ES, ExtRecvIncomingMsg);
    esSetRecvIncomingLogDataFcn(ES, ExtRecvIncomingLogData);

	/* request first data telegram from target (without acknowledgement) */
	RequestDataTelegram(ES);

EXIT_POINT:
    if (!esIsErrorClear(ES)) {
        ExtCloseConnection(ES);
        FreeAndNullUserData(ES);
        mexUnlock();
    }
} /* end ExtConnect */


/* Function: ExtDisconnectRequest ==============================================
 * Abstract:
 *  A request to terminate communication with target has been made.  Notify
 *  the target (if it is up and running).  The conection status will be:
 *  EXTMODE_DISCONNECT_REQUESTED
 */
PRIVATE void ExtDisconnectRequest(
    ExternalSim    *ES,
    int_T          nrhs,
    const mxArray  *prhs[])
{
    MsgHeader msgHdr;
    int       nSet;
    boolean_T error = EXT_NO_ERROR;

	#if DEBUG_MSG_LVL >= 2
	mexPrintf("ExtDisconnectRequest: IN\n");
	#endif

    assert(esGetConnectionStatus(ES) == EXTMODE_DISCONNECT_REQUESTED);

    (void)nrhs; /* unused */
    (void)prhs; /* unused */

    msgHdr.size = 0;
    msgHdr.type = EXT_DISCONNECT_REQUEST;

    Copy32BitsToTarget(ES,(char *)&msgHdr,(uint32_T *)&msgHdr,NUM_HDR_ELS);
    if (!esIsErrorClear(ES)) goto EXIT_POINT;

	#if DEBUG_MSG_LVL >= 2
	mexPrintf("ExtDisconnectRequest: Sending EXT_DISCONNECT_REQUEST message\n");
	#endif

    error = ExtSetTargetMsg(ES,sizeof(msgHdr),(char *)&msgHdr,&nSet);
    if (error || (nSet != sizeof(msgHdr))) {
        esSetError(ES, "ExtSetTargetMsg() call failed on CLOSE.\n"
	               "Ensure target is still running\n");
        goto EXIT_POINT;
    }

EXIT_POINT:

	#if DEBUG_MSG_LVL >= 2
	mexPrintf("ExtDisconnectRequest: OUT\n");
	#endif

    return;
} /* end ExtDisconnectRequest */


/* Function: ExtDisconnectConfirmed ============================================
 * Abstract:
 *  Terminate communication with target.  This should only be called after the
 *  target has been notified of the termination (if the target is up and 
 *  running).  The conection status will be: EXTMODE_DISCONNECT_CONFIRMED.
 */
PRIVATE void ExtDisconnectConfirmed(
    ExternalSim    *ES,
    int_T          nrhs,
    const mxArray  *prhs[])
{
	#if DEBUG_MSG_LVL >= 2
	mexPrintf("ExtDisconnectConfirmed: IN\n");
	#endif

    assert(esGetConnectionStatus(ES) == EXTMODE_DISCONNECT_CONFIRMED);

    (void)nrhs; /* unused */
    (void)prhs; /* unused */
    
    ExtCloseConnection(ES);
    FreeAndNullUserData(ES);
    
	mexUnlock();

	#if DEBUG_MSG_LVL >= 2
	mexPrintf("ExtDisconnectConfirmed: OUT\n");
	#endif

    return;
} /* end ExtDisconnectConfirmed */


/* Function: ExtSendGenericMsg =================================================
 * Abstact:
 *  Send generic message to the target.  This function simply passes the
 *  already formatted and packed message to the target.
 */
PRIVATE void ExtSendGenericMsg(
    ExternalSim    *ES,
    int_T          nrhs,
    const mxArray  *prhs[])
{
    int        nSet;
    MsgHeader  msgHdr;
    int        msgSize;
    boolean_T  error = EXT_NO_ERROR;
    

    (void)nrhs; /* unused */
    (void)prhs; /* unused */

    msgSize = esGetCommBufSize(ES);
    
    msgHdr.type = (uint32_T)esGetAction(ES);
    msgHdr.size = (uint32_T)(msgSize/esGetHostBytesPerTargetByte(ES));
    
	#if DEBUG_MSG_LVL >= 3
	mexPrintf("Message type (before copy32): %d\n", msgHdr.type);
	mexPrintf("Message size (before copy32): %d\n", msgHdr.size);
	#endif

    //Copy32BitsToTarget(ES, (char *)&msgHdr, (uint32_T *)&msgHdr, NUM_HDR_ELS);
	
	#if DEBUG_MSG_LVL >= 3
	mexPrintf("Message type (after copy32): %d\n", msgHdr.type);
	mexPrintf("Message size (after copy32): %d\n", msgHdr.size);
	#endif

    if (!esIsErrorClear(ES)) goto EXIT_POINT;

    /*
     * Send message header.
     */
    error = ExtSetTargetMsg(ES,sizeof(msgHdr),(char *)&msgHdr,&nSet);
    if (error || (nSet != sizeof(msgHdr))) {
        esSetError(ES,"ExtSetTargetMsg() call failed for ExtSendGenericMsg().\n"
	              "Ensure target is still running\n");
        goto EXIT_POINT;
    }

	#if DEBUG_MSG_LVL >= 1
	printf("Message Sent type %d size %d\n",msgHdr.type,msgHdr.size);
	#endif

    /*
     * Send message data - if any.
     */
    if (msgSize > 0) {
        error = ExtSetTargetMsg(ES,msgSize,esGetCommBuf(ES),&nSet);
        if (error || (nSet != msgSize)) {
			
			mexPrintf("Download data (%d bytes) exceeds current buffer size (%d bytes)\n", msgSize, nSet);
			{
				DWORD err = GetLastError();
				mexPrintf("Error code: %ld\n", err);
			}

			esSetError(ES, "ExtSetTargetMsg() call failed for ExtSendGenericMsg().\nEnsure target is still running\n");
            goto EXIT_POINT;

        }
    }

	#if DEBUG_MSG_LVL >= 2
	{
		unsigned int i;

		if(msgHdr.size > 0) {
			mexPrintf("\nData Sent ... \n\t");
			for(i=0;i<msgHdr.size;i++)
				mexPrintf("%d ",esGetCommBuf(ES)[i]);
			mexPrintf("\n");
		}
	}
	#endif

EXIT_POINT:
    return;
} /* end ExtSendGenericMsg */


/* Function: ExtGetParams ======================================================
 * Abstact:
 *  Send request to the target to upload the accessible parameters.  Block
 *  until the parameters arrive.  This is done during the connection process
 *  when inline parameters is 'on'.
 */
PRIVATE void ExtGetParams(
    ExternalSim    *ES,
    int_T          nrhs,
    const mxArray  *prhs[])
{
    int            nSet;
    int            nGot;
    int            bytesToGet;
    MsgHeader      msgHdr;
    boolean_T      pending;
    boolean_T      error        = EXT_NO_ERROR;
    const long int timeOutSecs  = 60;
    const long int timeOutUSecs = 0;
    char_T         *buf         = esGetIncomingMsgDataBuf(ES);
    UserData	   *userData	= (UserData *)esGetUserData(ES);

	mexPrintf("\n\n\nExtGetParams\n\n\n");

    /*
     * Instruct target to send parameters.
     */
    msgHdr.type = (uint32_T)esGetAction(ES);
    msgHdr.size = 0;

    assert(msgHdr.type == EXT_GETPARAMS);
    
    Copy32BitsToTarget(ES, (char *)&msgHdr, (uint32_T *)&msgHdr, NUM_HDR_ELS);
    if (!esIsErrorClear(ES)) goto EXIT_POINT;

    error = ExtSetTargetMsg(ES,sizeof(msgHdr),(char *)&msgHdr,&nSet);
    if (error || (nSet != sizeof(msgHdr))) {

        esSetError(ES,"ExtSetTargetMsg() call failed for ExtSendGenericMsg().\nEnsure target is still running\n");
        goto EXIT_POINT;

    }

    /*
     * Wait for start of parameters message - error if timeOut.
     */
	if(userData->TelType != 0) {

		/* no previously intercepted message pending -> check for new messages */
		error = ExtTargetTelPending(ES, &pending, timeOutSecs, timeOutUSecs);
		if (error) {
			esSetError(ES, "ExtTargetTelPending() call failed while checking for target msg\n");
			goto EXIT_POINT;
		}
	}
    assert(pending);

    /*
     * Get message header - assume that it's all there.
     */
    error = ExtGetTargetMsg(ES,sizeof(msgHdr),&nGot,(char_T *)&msgHdr);
    if (error || (nGot != sizeof(msgHdr))) {
        
		esSetError(ES, "ExtGetTargetMsg() call failed while checking target msg's\n");
        goto EXIT_POINT;

    }

    /*
     * Convert size to host format/host bytes & verify message type.
     */
    assert(msgHdr.type == EXT_GETPARAMS_RESPONSE);
    Copy32BitsFromTarget(ES, (uint32_T *)&msgHdr, (char *)&msgHdr, NUM_HDR_ELS);
    if (!esIsErrorClear(ES)) goto EXIT_POINT;
            
    bytesToGet = msgHdr.size * esGetHostBytesPerTargetByte(ES);

    /*
     * Get the parameters.
     */
    while(bytesToGet != 0) {
        /*
         * Look for any pending data.  If we ever go more than
         * 'timeOutVal' seconds, bail with an error.
         */
//        error = ExtTargetTelPending(ES, &pending, timeOutSecs, timeOutUSecs);
//       if (error) {
//
//            esSetError(ES, "ExtTargetMsgPending() call failed while checking for target msg\n");
//            goto EXIT_POINT;
//
//        }

        /*
         * Grab the data.
         */
        error = ExtGetTargetMsg(ES,bytesToGet,&nGot,buf);
        if (error) {
            
			esSetError(ES, "ExtGetTargetMsg() call failed while checking target msg's\n");
            goto EXIT_POINT;

        }

        bytesToGet -= nGot;
        assert(bytesToGet >= 0);
    }

EXIT_POINT:
    return;

} /* end ExtGetParams */


#ifdef ERASE
/* Function: WaitForTargetResponse =================================================
 * Abstact:
 *  Await the specified response telegram of the target. (FW-12-02)
 */

#define esGetIncomingMsg(ESim)  ((ESim)->incomingMsg.msg)

 PRIVATE void WaitForTargetResponse(
    ExternalSim		*ES,
	uint_T			awaitedMsg)
{
    uint32_T  recvdMsg = 0;

	#if DEBUG_MSG_LVL >= 1
	mexPrintf("WaitForTargetResponse: IN\n");
	#endif

	while(recvdMsg == 0 || recvdMsg != awaitedMsg) {
		ExtRecvIncomingMsg(ES, 0, NULL);
        recvdMsg = esGetIncomingMsg(ES);
	}

	#if DEBUG_MSG_LVL >= 1
	mexPrintf("WaitForTargetResponse: OUT\n");
	#endif

} /* end WaitForTargetResponse */
#endif


/*******************************************************************
 * Include ext_main.c, the mex file wrapper containing mexFunction *
 *******************************************************************/
#include "ext_main.c"


/* [EOF] ext_comm.c */




⌨️ 快捷键说明

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