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

📄 ext_svr.c

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

	extUD = ExtUserDataCreate();
	
	if(extUD == NULL) abort_LED(42);

    /* block serial upload until 'StartMsg' has been received */
	extUD->waitForStartMsg = TRUE;
	
	/* request all messages to be acknowledged (default) */
	extUD->acknowledgeMsg = TRUE;

    /* initialise serial interface SCI1 */
	ExtInit();
	
  /* allow all interrupts -> SCI1 active */
  asm cli

	/* enable data buffering */
	bufenable = TRUE;

    UploadLogInfoReset();

} /* end rt_ExtModeInit */


/* Function: rt_ExtModeSleep ===================================================
 * Abstract:
 *  Called by grt_main, ert_main, and grt_malloc_main  to "pause".  
 *
 *  currently inactive -> calls an empty function (FW-02-03)
 *
 */
PUBLIC void rt_ExtModeSleep(
    long sec,  /* number of seconds to wait       */
    long usec) /* number of micro seconds to wait */
{
    ExtModeSleep(extUD,sec,usec);
} /* end rt_ExtModeSleep */


/* Function: rt_MsgServerWork ==================================================
 * Abstract:
 *  If not connected, establish communication of the message line and the
 *  data upload line.  If connected, send/receive messages and parameters
 *  on the message line.
 */
PUBLIC boolean_T rt_MsgServerWork(RTWExtModeInfo  *ei,
                                  boolean_T       *stopReq)
{
    MsgHeader  msgHdr;
    boolean_T  hdrAvail;
    boolean_T  error             = EXT_NO_ERROR;
    boolean_T  disconnectOnError = FALSE;
    

    //blinky(1000);
    
    #if DEBUG2SCI0 >= 3
    SCI0_OutString("rt_MsgServerWork : IN\n\r");
    #endif    

    /* Wait for a message. */
    error = GetMsgHdr(&msgHdr, &hdrAvail);
    if (error != EXT_NO_ERROR) {
		abort_LED(20);
		//  disconnectOnError = TRUE;
        goto EXIT_POINT;
    }
    
    if (!hdrAvail) goto EXIT_POINT; /* nothing to do */

    /*
     * This is the first message.  Should contain the string:
     * 'ext-mode'.  Its contents are not important to us.
     * It is used as a flag to start the handshaking process.
     */
    if (!commInitialized) {

		/* any message header other than EXT_DATA_UPLD_NOACK_REQUEST causes the communication to be reinitialised */
		if(msgHdr.type != EXT_DATA_UPLD_NOACK_REQUEST) msgHdr.type = EXT_CONNECT;
    }

    /* 
     * At this point we know that we have a message: process it.
     */
    switch(msgHdr.type) {

    /* host driven flow control: log data (FW-10-02) */
	case EXT_DATA_UPLD_NOACK_REQUEST:

		// enable transmission (for one data telegram only)
		TXactive = TRUE;

    #ifdef ERASE
		#ifdef LCDUSE4ERRORS
 		dispLCD_uint(alldatasent++, "Data req: ", 0);
    #endif    
		#endif

		/* check if an upload of user data from target -> host is required */
		if(num_user_channels_active > 0) {
	
			/* indeed -> initiate upload */
			ProcessSetUserData();

		}

        break;

    case EXT_GET_TIME:
	    //{
	    //    time_T t = rteiGetT(ei);
        //
		//	error = SendMsgToHost(EXT_GET_TIME_RESPONSE,sizeof(time_T), (char_T *)&t);
		//	if (error != EXT_NO_ERROR) goto EXIT_POINT;
	    //}
        break;

    case EXT_ARM_TRIGGER:

        UploadArmTrigger();

		/* allow for upload */
		TXactive = TRUE;

        break;

    case EXT_SELECT_SIGNALS:
	    {
		    const char *msg;

	        msg = GetMsg((uint_T)msgHdr.size);
		    if (msg == NULL) {
			    error = EXT_ERROR;
				goto EXIT_POINT;
			}

	        error = UploadLogInfoInit(ei, msg);
		    if (error != NO_ERR) {
				abort_LED(21);
				goto EXIT_POINT;
	        }
		    break;
		}

    case EXT_SELECT_TRIGGER: 
	    {
		    const char *msg;
	
		    msg = GetMsg((uint_T)msgHdr.size);
			if (msg == NULL) {
				error = EXT_ERROR;
	            goto EXIT_POINT;
		    }

			error = UploadInitTrigger(ei, msg);
	        if (error != EXT_NO_ERROR) {
				abort_LED(22);
			    goto EXIT_POINT;
	        }
		    break;
		}

    case EXT_CONNECT:

		/* enable data buffering */
		bufenable = TRUE;

        error = ProcessConnectMsg(ei);
        if (error != EXT_NO_ERROR) goto EXIT_POINT;
        
		break;

    case EXT_RECEIVE_USER_DATA:

        /* FW-12-02 */
        error = ProcessReceiveUserDataMsg((uint_T)msgHdr.size);
        if (error != EXT_NO_ERROR) goto EXIT_POINT;

        break;

    case EXT_SETPARAM:

        error = ProcessSetParamMsg(ei, (uint_T)msgHdr.size);
        if (error != EXT_NO_ERROR) goto EXIT_POINT;
        
		break;

    case EXT_GETPARAMS:

        error = ProcessGetParamsMsg(ei);
        if (error != EXT_NO_ERROR) goto EXIT_POINT;
        
		break;

    case EXT_DISCONNECT_REQUEST:

		/*
         * Note that from the target's point of view this is
         * more a "notify" than a "request".  The host needs to
         * have this acknowledged before it can begin closing
         * the connection.
         */
        error = SendMsgToHost(EXT_DISCONNECT_REQUEST_RESPONSE, 0, NULL);
        if (error != EXT_NO_ERROR) goto EXIT_POINT;

        DisconnectFromHost();

        break;

    case EXT_MODEL_START:

        startModel = TRUE;
        
        error = SendMsgToHost(EXT_MODEL_START_RESPONSE, 0, NULL);
        if (error != EXT_NO_ERROR) goto EXIT_POINT;
        
		break;

    case EXT_MODEL_STOP:

		*stopReq = TRUE;
		
		break;

    case EXT_MODEL_PAUSE:

		modelStatus = TARGET_STATUS_PAUSED;
        startModel  = FALSE;

        error = SendMsgToHost(EXT_MODEL_PAUSE_RESPONSE, 0, NULL);
        if (error != EXT_NO_ERROR) goto EXIT_POINT;

        break;

    case EXT_MODEL_STEP:

        if ((modelStatus == TARGET_STATUS_PAUSED) && !startModel) {
            startModel = TRUE;
        }
        
        error = SendMsgToHost(EXT_MODEL_STEP_RESPONSE, 0, NULL);
        if (error != EXT_NO_ERROR) goto EXIT_POINT;

        break;

    case EXT_MODEL_CONTINUE:

		if (modelStatus == TARGET_STATUS_PAUSED) {
            modelStatus = TARGET_STATUS_RUNNING;
            startModel  = FALSE;
        }
        
        error = SendMsgToHost(EXT_MODEL_CONTINUE_RESPONSE, 0, NULL);
        if (error != EXT_NO_ERROR) goto EXIT_POINT;

        break;

    case EXT_CANCEL_LOGGING:

		UploadCancelLogging();

		/* allow for upload */
		TXactive = TRUE;

        break;

    default:
		//abort_LED(23);

		/* at this point there has been a communication problem -> send EXT_RESET_SER_BUFFER_REQUEST telegram */
		ExtClearReceptionBuffer();          /* fw-02-05 */


        /* signal need to re-synchronise */
		error = SendMsgToHost(EXT_RESET_SER_BUFFER_REQUEST, 0, NULL);
        if (error != EXT_NO_ERROR) abort_LED(23);

		/* also upload current message header */
		error = SendMsgToHost((uint16_T)msgHdr.type, 0, NULL);
        if (error != EXT_NO_ERROR) abort_LED(23);

        break;
    } /* end switch */


EXIT_POINT:
    if (error != EXT_NO_ERROR) {
        if (disconnectOnError) {
			abort_LED(24); 
			//DisconnectFromHost(S);
        }
    }

    #if DEBUG2SCI0 >= 3
    SCI0_OutString("rt_MsgServerWork : OUT\n\r");
    #endif    

    return(error);
} /* end rt_MsgServerWork */


/* Function: rt_ExtModeShutdown ================================================
 * Abstract:
 *  Called when target program terminates to enable cleanup of external 
 *  mode.
 */
PUBLIC boolean_T rt_ExtModeShutdown(void)
{
    boolean_T           error              = EXT_NO_ERROR;
    boolean_T           dataPending        = IsAnyDataReadyForUpload();
    const ExtModeAction shutdownAction     = (dataPending) ?
        EXT_MODEL_SHUTDOWN_DATA_PENDING : EXT_MODEL_SHUTDOWN;
    const char          *shutdownActionStr = (dataPending) ?
        "EXT_MODEL_SHUTDOWN_DATA_PENDING" : "EXT_MODEL_SHUTDOWN";

	/* flush upload ring buffer */
	ringBuf.head = ringBuf.tail = 0;

	/*
     * Make sure buffers are flushed so that the final points get to
     * host (this is important for the case of the target reaching tfinal
     * while data is uploading is in progress).
     */
    UploadPrepareForFinalFlush();

	/* transfer data from the RTW buffer to the upload buffer */
	put_data_buffer();

	/* upload termination telegram: '80005000' */
	TXactive	= TRUE;
    (void)rt_UploadServerWork();

    UploadLogInfoTerm();
    if (msgBuf != NULL) {
		free(msgBuf);
		msgBuf = NULL;			/* FW-10-02 */
		msgBufSize = 0;			/* FW-10-02 */
	}
    
	if(bufenable) {
        
		error = SendMsgToHost(shutdownAction, 0, NULL);
        if (error != EXT_NO_ERROR) {
			abort_LED(27);
        }

		/* disable data buffering */
		bufenable = FALSE;

		/* reset communication status and model status */
		commInitialized = FALSE;
		modelStatus		= TARGET_STATUS_WAITING_TO_START;
    }

    /* clear communication (ring) buffer -> all pts are reset to zero, memory is NOT released */
	  //ExtClearReceptionBuffer();  /* all buffer macros are now confined to ext_svr_transport.c -- fw-02-05 */
		ExtRemoveReceptionBuffer();          /* fw-03-05 */


    ExtUserDataDestroy(extUD);
    extUD = NULL;				/* FW-10-02 */

    return(error);
} /* end rt_ExtModeShutdown */


/* [EOF] ext_svr.c */

⌨️ 快捷键说明

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