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

📄 uart_drv.c

📁 freescale的基于802.15.4的无线通讯例程
💻 C
📖 第 1 页 / 共 2 页
字号:
*
* Arguments:
*   pIrp - pointer to Irp structure.
*
* Return value:
*   gIrpAccepted_c:    Request will be executed
*   gIrpInvalidReq_c:  Invalid/unknown request
*   gIrpInvalidArg_c:  Invalid parameters/arguments
* 
* Revision history:
* date   Author   Comments
* ------ ------   --------
* 240406 AL,FSL  Updated
*****************************************************************************/
uint8_t UART_Request(irp_t *pIrp)
{  
  uint8_t dummy;                     
  
 	switch (pIrp->requestType) {

/*--------------------------------------------------------------------------*/
 	
		case gInitUart_c:
#if (gUART_ReinitEnabled_d == 1)
			/* Check if any Rx IRP's were pending */
			if (mpRxIrp) {
    		/* Disable the Rx interrupt and stop the timer */
			  TMR_StopTimer(gUART_TimerID_c);			
			  /* Cancel it */
			  mpRxIrp->returnValue = gUartReinit_c;
			  mpRxIrp->reqData.rxtxParams.pfCallback( mpRxIrp );
			  mpRxIrp = NULL;
			}
			/* Check if any Tx IRP's were pending */
			if (mpTxIrp) {
			  SCIx_DisableTxInts(); /* Stop TX'ing */
			  /* Cancel it */
			  mpTxIrp->returnValue = gUartReinit_c;
			  mpTxIrp->reqData.rxtxParams.pfCallback( mpTxIrp );
			  mpTxIrp = NULL;
			}			  
			
			/* Clear FIFO */
			mhFIFO = mtFIFO =0;
#endif
        
  	  /* Set the baud rate, irrespective of UART id - there's only one */
	    SCIxBDH =
  	    (uint8_t)((pIrp->reqData.initParams.baudRate >> 8) & 0xFF);
      SCIxBDL =
        (uint8_t)(pIrp->reqData.initParams.baudRate & 0xFF);	
      
	  /* Set other UART settings (data bits, parity etc.) */
      SCIxC1 = gUART_InitSCIxC1_c;
      SCIxC2 = gUART_InitSCIxC2_c; 
      SCIxC3 = gUART_InitSCIxC3_c;

      dummy = SCIxD; /* Dummy read to clear Rx status */
      
      /* Do not allow data to be sent to the driver (we have no IRP yet) */
      DisableFlow();      
      
      break;
      
/*--------------------------------------------------------------------------*/
                		  
		case gRxUart_c:

#if (gUART_Verification_d == 1)		
      /* Deal with degenerate cases */
      if ( !pIrp->reqData.rxtxParams.pBuffer ||
           !pIrp->reqData.rxtxParams.len ||
           !pIrp->reqData.rxtxParams.pfCallback ) {
  			return gIrpInvalidArg_c;
      }
      else {
        /* Do we already have an IRP pending for Rx */
        if (mpRxIrp) {
          return gIrpNotReady_c; /* IRP already pending */
        }
      }
        
#endif /* gUART_Verification_d */
      
      /* Store for later to be returned to the application */
      mpRxIrp = pIrp;
       
      mFIFOWaterMark = gUART_FIFOWaterMark_c;
      if( pIrp->reqData.rxtxParams.len < mFIFOWaterMark) {
        mFIFOWaterMark =  pIrp->reqData.rxtxParams.len;
      }
      
      mcRx = 0; /* For counting the received data */
      
   		/* Start the timer */
			TMR_StartTimer(gUART_TimerID_c, gUART_RxTimeout_c, UART_Timeout);
			
		  /* Enable the flow control and Rx interrupt */
		  EnableFlow();
      SCIx_EnableRxInts();
  	                	
  		break;

/*--------------------------------------------------------------------------*/
                		
		case gTxUart_c:	

#if (gUART_Verification_d == 1)		
      /* Deal with degenerate cases */
      if ( !pIrp->reqData.rxtxParams.pBuffer ||
           !pIrp->reqData.rxtxParams.len ||
           !pIrp->reqData.rxtxParams.pfCallback ) {		         		                
		    return gIrpInvalidArg_c;
      }
      else {
        /* Do we already have an IRP pending for Rx */
        if (mpTxIrp) {
          return gIrpNotReady_c; /* IRP already pending */
        }
      }       
#endif /* gUART_Verification_d */		      
		                
      /* Store for later to be returned to the application */
      mpTxIrp = pIrp;
      
      /* For counting the transmit data */
      mcTx = 0;
      
      /* Enable Tx interrupt */
      SCIx_EnableTxInts();
      
  		break;

/*--------------------------------------------------------------------------*/
  
	  default:      
	  
      return gIrpInvalidReq_c;
	            
	} /* End switch */
  
  return gIrpAccepted_c;		
}

/*****************************************************************************
******************************************************************************
* Private functions
******************************************************************************
*****************************************************************************/

/*****************************************************************************
* When transmit interrupt is enabled this ISR will execute
* and pumps data out through SCIx Tx pin. 
*
* Interface assumptions:
* 
* Return value:
* None
* 
* Revision history:
* date   Author   Comments
* ------ ------   --------
* 240406 AL,FSL  Updated
*****************************************************************************/
INTERRUPT_KEYWORD void UART_SCITxIsr(void)
{
  uint8_t dummy;
  
  if ( mcTx == mpTxIrp->reqData.rxtxParams.len ) {
    SCIx_DisableTxInts();
    TS_SendEvent(gUartTaskID_c, mTxSuccessEvent_c);    
  }
  else {      
    dummy = SCIxS1; /* Allow next write to Tx register */
    SCIxD = mpTxIrp->reqData.rxtxParams.pBuffer[mcTx++];
  }
}

/*****************************************************************************
* When data is received in SCIxD ISR will be called
* this routine puts SCIxD into Rx FIFO.
*
* Interface assumptions:
* 
* Return value:
* None
* 
* Revision history:
* date   Author   Comments
* ------ ------   --------
* 240406 AL,FSL  Updated
*****************************************************************************/
INTERRUPT_KEYWORD void UART_SCIRxIsr(void)
{
  uint8_t dummy;
  uint8_t cBuf; /* Counts the characters from buffer  */

	/* Receive register full and no errors? */
	if ( (SCIxS1 & gUART_DataRegMask_c) != gUART_DataRegEmpty_c ) {
	                        /* SCIS1:5 (RDRF) = 0: Data register empty,
	                                 1: full. SCIS1:0-4 Error flags */
      maFIFO[mhFIFO++] = SCIxD;	/* Read the data */
		  
   	/* Calculate bytes remaining free before notifying */
     	if ( mhFIFO > mtFIFO ) {
        cBuf = mhFIFO - mtFIFO;
    }
    else {
      cBuf = gUART_FIFOLen_c - (mtFIFO - mhFIFO);              
    }

    /* Check the water mark and send a event to empty the buffer */
   	if ( cBuf == mFIFOWaterMark ) {
      /* Disable the flow control */
      DisableFlow(); 
      TS_SendEvent(gUartTaskID_c, mRxWaterMarkEvent_c);
    }                                    
       	                                        
	  if ( mhFIFO == gUART_FIFOLen_c ) {
      mhFIFO = 0;
	  }                                   		                                                          
	}
	else {
    /* Dummy read because the error flags are only cleared when
       the data register is read */
		dummy = SCIxD; 
	}
}

#endif /* ((gUART_OnSCI1_d == 1) || (gUART_OnSCI2_d == 1)) */

#else /* gUART_Enabled_d */
/*-----------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
The following functions are used when the UART driver is disabled. They make sure
that the targets can still compile and link, at very little code overhead.
-------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------*/

/* Create an empty function for the task scheduler */
void UART_Main(event_t events)
{
  (void)events; /* Avoid compiler warning */
}

/* Create empty function for UART requests */
uint8_t UART_Request(irp_t *pIrp)
{  
  (void)pIrp; /* Avoid compiler warning */
  
  return gIrpInvalidReq_c; /* UART not enabled */
}
  
#endif /* gUART_Enabled_d */

⌨️ 快捷键说明

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