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

📄 rvt_task.c

📁 是一个手机功能的模拟程序
💻 C
字号:
/**************************************************************************
*			
* rvt_task.c
*
* This contains the core of the trace task.
*
* (C) Texas Instruments, all rights reserved
*
* Version number   : 0.1
*
* History		   : 0.1 (7/5/2000) - Created
*
* Date             : 7/5/2000
*
* Author           : Guido Pagana
*
* Update           : Pascal Puel
*                  : David Lamy-Charrier (changes for Riviera 1.6)
***************************************************************************/


#include "nucleus.h"

#include "general.h"
#include "rv_general.h"
#include "rvf_api.h"
#include "rvt_gen.h"
#include "rvt_def_i.h"
#include "rvt_env.h"
#include "rvm_use_id_list.h"

#include "serialswitch.h"

#ifndef _WINDOWS
  #include "swconfig.cfg"
#endif

#include <string.h>


/* period between two alive messages from the trace task */
#define RVT_ALIVE_POLLING_TIME (RVF_MS_TO_TICKS(20000))

extern UINT32 rvf_lost_msg_cpt;

extern T_RVT_USER_ID rv_trace_user_id;

#define WAIT_FOR_HEADER      1
#define WAIT_FOR_DATA        2

extern T_RVT_USER_DB rvt_user_db [];

extern NU_HISR TI_rcv_HISR;

extern T_RVF_MB_ID		rvt_mb_id;

/* External functions declaration */
extern char Num2Char(UINT8 value);

/* pointer to the message used to indicate that there is not enough memory for traces */
T_RVT_TRACE_RQST * rvt_lost_msg_ptr = NULL;

#define RVT_LOST_MSG_LENGTH 80

 					 
/********************************************************************************/
/*                                                                              */
/*    Function Name:   rvt_task_core											*/
/*                                                                              */
/*    Purpose:         Core of Trace task.                                      */
/*                                                                              */
/*    Note:                                                                     */
/*        None.                                                                 */
/*                                                                              */
/********************************************************************************/
T_RVM_RETURN rvt_task_core (void)
{
  T_RV_HDR * msg;
  UINT8 msg_format;
  UINT16 event;
  UINT32 nb_bytes_sent;
  UINT32 msg_length;
 
  /* allocate a buffer for lost traces at the beginning, once for all */
  if( rvf_get_buf( rvt_mb_id, RVT_LOST_MSG_LENGTH, (T_RVF_BUFFER**) &rvt_lost_msg_ptr) != RVF_RED)
  {	  UINT8 * body = (UINT8*)rvt_lost_msg_ptr + sizeof(T_RVT_TRACE_RQST);

	  rvt_lost_msg_ptr->header.msg_id  = RVT_TRACE_RQST_ID;
	  rvt_lost_msg_ptr->format         = RVT_BINARY_FORMAT;
	  rvt_lost_msg_ptr->user_id        = rv_trace_user_id;
	  rvt_lost_msg_ptr->do_not_free    = 1;
	  rvt_lost_msg_ptr->msg_length     = 24;

	  body[0] = 0;
	  body[1] = 0;
	  body[2] = 0;
	  body[3] = 0;
	  body[4] = 1;
	  memcpy( body + 5, " RVT: Lost Message ", 19);

  }

  /* request for the level of filtering, as well as the 32-bit
  mask related to the software entities to be monitored. */
#ifdef FRAMING_PROTOCOL
 {
	UINT8  trace_level_request[] = {RVM_INVALID_USE_ID, 0, 0, 0, 0, (RV_TRACE_LEVEL_ERROR - 1), 0, 0, 0, 0};

	/* note that the level is defined as invalid */
	trace_level_request[0] = (char) rv_trace_user_id;

	/* transmit an 'empty' message */
	nb_bytes_sent = 0;
	while (nb_bytes_sent < sizeof (trace_level_request))
	{
		nb_bytes_sent += SER_tr_WriteNBytes (SER_LAYER_1,
											 trace_level_request + nb_bytes_sent,
											 sizeof (trace_level_request) - nb_bytes_sent);
	}
 }
#endif

  // Start the "Alive Polling Timer"
#if (OP_WCP == 0)
  rvf_start_timer(RVF_TIMER_0, RVT_ALIVE_POLLING_TIME, TRUE);
#endif

  while(1)
  {  	
    /* Infinite wait on Trace Task Mailbox or timer event */
    event = rvf_wait((1<<RVT_TRACE_MAILBOX) | (RVF_TIMER_0_EVT_MASK), 0);

    if(event & EVENT_MASK(RVT_TRACE_MAILBOX))
    {
      /* Read message from mailbox */
      msg = (T_RV_HDR *) rvf_read_mbox(RVT_TRACE_MAILBOX);

      if(msg != NULL)
      {
		  if (msg->msg_id == RVT_TRACE_RQST_ID)
			{   
				/* retrieve length from message */
				msg_length = ((T_RVT_TRACE_RQST *)msg)->msg_length;

#ifndef FRAMING_PROTOCOL
				/* Send message to UART	without byte stuffing
				loop until all chars have been sent for serial switch */
				nb_bytes_sent = 0;
				while( nb_bytes_sent < msg_length)
				{	nb_bytes_sent += SER_tr_WriteNChars(SER_LAYER_1, msg + RVT_HEADER_SIZE + nb_bytes_sent, msg_length - nb_bytes_sent );
				}

#else
				/* retrieve format from message  */
				msg_format = ((T_RVT_TRACE_RQST *)msg)->format;
				
				/* copy the user_id at the begining */
				((UINT8*)msg+RVT_HEADER_SIZE-1)[0] = ((T_RVT_TRACE_RQST *)msg)->user_id;
				msg_length++;
				
				switch (msg_format)                                                      
				{                                                                       
				case RVT_ASCII_FORMAT:                                                    
				/* Send message to UART	without byte stuffing
					loop until all chars have been sent for serial switch */
					{   nb_bytes_sent = 0;
					while( nb_bytes_sent < msg_length)
					{
						nb_bytes_sent += SER_tr_EncapsulateNChars(SER_LAYER_1, (char*)msg + RVT_HEADER_SIZE -1 + nb_bytes_sent, msg_length - nb_bytes_sent );
					}
					break;                                                              
					}
					
				case RVT_BINARY_FORMAT:                                                  
				/* Send message to UART	with byte stuffing
					loop until all bytes have been sent for serial switch */
					{    nb_bytes_sent = 0;
					while( nb_bytes_sent < msg_length)
					{
						nb_bytes_sent += SER_tr_WriteNBytes(SER_LAYER_1, (UINT8*) msg + RVT_HEADER_SIZE -1 + nb_bytes_sent, msg_length - nb_bytes_sent );
					}
					break;                                                              
					}
				}  // end of switch
				
        
#endif

#ifndef FRAMING_PROTOCOL   // send the \n \r characters only for Hyperterminal
				msg[0] = '\n';
				msg[1] = '\r';
				msg_length = 2;
				nb_bytes_sent = 0;

				while( nb_bytes_sent < msg_length)
				{
					nb_bytes_sent += SER_tr_WriteNChars(SER_LAYER_1, msg + nb_bytes_sent, msg_length - nb_bytes_sent );
				}
#endif

				/* free the msg buffer, except if it has to be re-used */
				if( ((T_RVT_TRACE_RQST *)msg)->do_not_free == 1)
				{	rvt_lost_msg_ptr = (T_RVT_TRACE_RQST *)msg;
				}
				else
				{	rvf_free_buf(msg);
				}

			} // end of if(msg->msg_id == RVT_TRACE_RQST_ID)

	 }   // end of if(msg != NULL)

      
    }  // end of if(event & EVENT_MASK(RVT_TRACE_MAILBOX))

	if (event & (RVF_TIMER_0_EVT_MASK))
	{	// Time-out event
		UINT32 rvt_current_time = rvf_get_tick_count();
		char alive_msg[50];
		UINT8 alive_msg_length = 26;
		int j;
        alive_msg[0] = (char) rv_trace_user_id;
        alive_msg[1] = 0;
        alive_msg[2] = 0;
        alive_msg[3] = 0;
        alive_msg[4] = 0;
        alive_msg[5] = 1;
		memcpy( alive_msg + 6, "Riviera System Time=", 20);

		// add the system time
		for (j=0; j<8; j++)
		{
			alive_msg[alive_msg_length + j] = Num2Char((UINT8)((rvt_current_time<<(j*4))>>28));  																
		}

#ifndef FRAMING_PROTOCOL
		/* Send message to UART	without byte stuffing
		loop until all chars have been sent for serial switch */
		/* send the '\n' '\r" only for hyper terminal */
		alive_msg_length = 30;
		alive_msg[34] = '\n';
		alive_msg[35] = '\r';
		nb_bytes_sent = 0;
		while( nb_bytes_sent < alive_msg_length)
		{	nb_bytes_sent += SER_tr_WriteNChars(SER_LAYER_1, alive_msg + nb_bytes_sent + 6, msg_length - nb_bytes_sent );
		}
#else

		alive_msg_length = 26 + 8;
        nb_bytes_sent = 0;
        while( nb_bytes_sent < alive_msg_length)
		{
            nb_bytes_sent += SER_tr_WriteNBytes(SER_LAYER_1, (UINT8*) alive_msg + nb_bytes_sent, alive_msg_length - nb_bytes_sent);
		}	
#endif

	} // end of timer event

  } // end of while

}		


/********************************************************************************/
/*                                                                              */
/*    Function Name:   rvt_RX_process										    */
/*                                                                              */
/*    Purpose:         This function is called when characters are received     */
/*                     on the serial port on receive HISR.                      */
/*    Input Parameters:                                                         */
/*        None.                                                                 */
/*                                                                              */
/*    Output Parameters:                                                        */
/*        none.														            */
/*													                            */
/*    Global Parameters:                                                        */
/*        None.                                                                 */
/*                                                                              */
/*    Note:                                                                     */
/*        None.                                                                 */
/*                                                                              */
/********************************************************************************/
void rvt_RX_process (void)
{
	UINT32        bytesRead;
	static UINT8  inBuffer[255];

	#ifdef FRAMING_PROTOCOL
		static BOOL               eof              = FALSE;
		static UINT8              rcv_state        = WAIT_FOR_HEADER;
		static UINT32             total_bytesRead  = 0;
		static RVT_CALLBACK_FUNC  rx_callback_func = NULL;
		
		// Read and destuff the UART RX FIFO and fill inBuffer with received bytes
		bytesRead = SER_tr_ReadNBytes (SER_LAYER_1,
									   (char *) (inBuffer + total_bytesRead),
									   sizeof (inBuffer) - total_bytesRead,
									   &eof);
		if ((rcv_state == WAIT_FOR_HEADER) && !(total_bytesRead))
		{
			if ((inBuffer[0] == RVT_RV_HEADER) ||
				(inBuffer[0] == RVT_L1_HEADER) ||
				(inBuffer[0] == RVT_L23_HEADER) ||
				(inBuffer[0] == RVT_TM_HEADER) ||
				(inBuffer[0] == RVT_RNET_HEADER) ||
				(inBuffer[0] == RVT_PROF_HEADER) ||
				(inBuffer[0] == RVT_GTTBACK_HEADER) ||
				(inBuffer[0] == RVT_OTHER_HEADER))
			{
				UINT8  idtab = 0;
				
				// Search for the ID in the table
				for (idtab = 0;
					 rvt_user_db[idtab].user_id != RVT_INVALID_HEADER;
					 idtab++)
				{
					if (rvt_user_db[idtab].user_id == inBuffer[0])
					{
						rx_callback_func = rvt_user_db[idtab].rx_callback_func;
						break;
					}
				}
				rcv_state = WAIT_FOR_DATA;
			}
		}
		total_bytesRead += bytesRead;

		// Call the corresponding callback function when message is complete
		if (eof)
		{
			// Reset static variables for the next frame to come
			rcv_state = WAIT_FOR_HEADER;
			eof       = FALSE;

			// Invoke the callback function
			if (rx_callback_func != NULL)
			{
				rx_callback_func ((T_RVT_BUFFER) (inBuffer + 1), total_bytesRead - 1);
				rx_callback_func = NULL;
			}
			total_bytesRead = 0;
			return;
		}

		// inBuffer may be full due to synchro lost problems
		if (total_bytesRead == sizeof (inBuffer))
		{
			// If still waiting for the header, discard received characters and
			// reset static variables for the next frame to come
			if (rcv_state == WAIT_FOR_HEADER)
			{
				total_bytesRead = 0;
				return;
			}

			// Just discard characters received as payload
			total_bytesRead = sizeof (inBuffer[0]);
		}

	#else

		// Read the UART RX FIFO and fill inBuffer with received bytes
		bytesRead = SER_tr_ReadNChars (SER_LAYER_1,
									   (char *) inBuffer,
									   sizeof (inBuffer));

		// Invoke the Testmode callback function : this is the only one able,
		// for the moment, to send an external command !!  WARNING : This
		// should be the same name than the one already registered in
		// create_RVtasks.c.
		tm_receive ((T_RVT_BUFFER) inBuffer,
					bytesRead);
	#endif
}



/********************************************************************************/
/*                                                                              */
/*    Function Name:   rvt_activate_RX_HISR                                     */
/*                                                                              */
/*    Purpose:         This function is called when an RX interrupt occurs      */
/*                                                                              */
/*    Input Parameters:                                                         */
/*        None.                                                                 */
/*                                                                              */
/*    Output Parameters:                                                        */
/*        none.														            */
/*													                            */
/*    Global Parameters:                                                        */
/*        None.                                                                 */
/*                                                                              */
/*    Note:                                                                     */
/*        None.                                                                 */
/*                                                                              */
/********************************************************************************/
void rvt_activate_RX_HISR (void)                                    
{                                                         
  NU_Activate_HISR(&TI_rcv_HISR);
}

⌨️ 快捷键说明

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