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

📄 uartutil.c

📁 freescale的基于802.15.4的无线通讯例程
💻 C
📖 第 1 页 / 共 2 页
字号:
  uint16_t space;
  uint8_t flagCircListIsEmpty = FALSE;
  
  /* Check for NULL case*/
  if(pString==0)
    return;
  
  /* Check for Circular List empty case*/
  if((mtUartCircListTx - mhUartCircListTx)==0)
    {
     flagCircListIsEmpty=TRUE;
    }
  /* Check for room in Tx buffer */
  if(mtUartCircListTx > mhUartCircListTx) 
    {
     space = mtUartCircListTx - mhUartCircListTx-1;
    }
   else
    {
     space = gUartCircListTxLen_c -( mhUartCircListTx-mtUartCircListTx+1);
    }
    /* Write data payload until no space left */
  for (i = 0; (*pString != '\0'); ) 
   {
    if(i < space)
    {
        if(*pString == '\n' )
        {
             PutInTxCirc('\r');
			 i++;
        }
        if(*pString != '\r' )
        {
            PutInTxCirc(*pString);
            i++;
        }
		pString++;
    }
    else
    {
     /* The circular list is full */
     /* You will lose data here */
     break;
    }

   } 
  /* If the circular list was empty, sent data now*/
  if( flagCircListIsEmpty )
  {
   SendData();
  }
}


//-----------------------------------------------------------------------------------

/* Trasform from hex to ascii */
static uint8_t HexToAscii(uint8_t hex)
{
  hex &= 0x0F;
  return hex + ((hex <= 9) ? '0' : ('A'-10));
}

//-----------------------------------------------------------------------------------
void UartUtil_PrintHex(uint8_t *hex, uint8_t len, uint8_t flags)
{
  uint8_t i=0; 
  uint8_t hexString[3];
  
  if(! (flags & gPrtHexBigEndian_c))
   {
    hex = hex + (len-1);
   }
   
  while(len)
  {
    hexString[1] = HexToAscii( *hex );
    hexString[0] = HexToAscii((*hex)>>4);
    hexString[2] ='\0';
  
    UartUtil_Print(hexString);
    if(flags & gPrtHexCommas_c)
     {
      UartUtil_Print(",");
     }
    if(flags & gPrtHexSpaces_c) 
     { 
      UartUtil_Print(" ");
     }
    hex = hex + (flags & gPrtHexBigEndian_c ? 1 : -1);
    len--;
  }
  if(flags & gPrtHexNewLine_c)
   {
    UartUtil_Print("\n");
   }
}

/*------------------------------------------------------------------------------------*/

/* Check if UART TX buffer is empty.*/
bool_t UartUtil_CheckIdle(void)
{
  if ( UartUtil_IsNotPendingTx() )
  {
    uint8_t stat = SCIxS1;

    if((stat & mTxDataRegEmptyBit_c) == mTxDataRegEmptyBit_c)
    {        
      return TRUE;
    }
    else
    {        
      return FALSE;
    }
  }
  else
  {    
    return FALSE;  
  }
}

/*------------------------------------------------------------------------------------*/

#else
/* Create  dummy functions for the task scheduler */

//-----------------------------------------------------------------------------------
void UartUtil_Print(char * pString){
/* Avoid compiler warning */
(void)pString;
}

void UartUtil_PrintHex(uint8_t *hex, uint8_t len, uint8_t flags){
/* Avoid compiler warning */
(void)hex;
(void)len;
(void)flags;
}

void UartUtil_Tx(uint8_t * pData, uint8_t length){
/* Avoid compiler warning */
(void)pData;
(void)length;
}

uint8_t UartUtil_IsNotPendingTx(void){
/* Avoid compiler warning */
return 0;
}

bool_t UartUtil_CheckIdle(void){
/* Avoid compiler warning */
return 0;
}

//-----------------------------------------------------------------------------------
 
#endif /* gEnableUartUtilTxFunctions_c */


//-----------------------------------------------------------------------------------
#if ( gEnableUartUtilRxFunctions_c )

uint8_t UartUtil_Poll(uint8_t * pBuffer)
{

  if (mUartOnReading)
  {
    /* The Uart is on reading state */
    return 0;
  }
  else 
    {
        if (mValuesReturned) 
        {
           mValuesReturned=FALSE;
           
           /* we recieved something */
           if(mRxlen) 
           {
             /* Copy the recieved buffer in user buffer */
             FLib_MemCpy( pBuffer, &mRxBuffer, mRxlen );
             return mRxlen;
           } 
           else 
            {
             return 0;
            }
           
        }
        else  
        { /* Now is going on reading again */
          mUartOnReading=TRUE;
          /* Set the IRP struct for starting a receive operation */  
          mIrpDataRx.reqData.rxtxParams.pBuffer = mRxBuffer;        
          mIrpDataRx.reqData.rxtxParams.len = gFullPacketLength_c ;
          mIrpDataRx.reqData.rxtxParams.pfCallback = &AppUARTCallback;
          mIrpDataRx.requestType = gRxUart_c;
            
          (void) UART_Request( &mIrpDataRx );
          return 0;
        }
    }
  
}

//-----------------------------------------------------------------------------------

uint8_t UartUtil_PollMessage(uint8_t * pBuffer)
{
  uint8_t messageDone = FALSE;
  uint8_t cReturnBytes;
  
  if (mUartOnReading)
  {
    /* The Uart is on reading state */
    return 0;
  }
  else 
  {
    if (mValuesReturned) 
      {
       mValuesReturned = FALSE;
       /* We recieved something here */
       if (mRxlen) 
          {
          /* Init the indexs count and lenght */
          mCount = 0;
          mSaveRxlen = mRxlen; 
          }
          else 
          {
           return 0;
          }    
      } 
       else
        {
         /* Form a complet message in mReadyRxBuffer */
         while ((!messageDone) && ( mCount!= mSaveRxlen ))
         {
          mReadyRxBuffer[mBytesCopied++] = mRxBuffer[mCount++];
          /* Check for recieving a \r */
          if(gMessageMarkCR_c  == mReadyRxBuffer[mBytesCopied- 1])
                 {
                  /* If the next is 0x0A */
                  if(gMessageMark_c == mRxBuffer[mCount])
                    {
                     mReadyRxBuffer[mBytesCopied++] = mRxBuffer[mCount++];
                    } 
                  else
                    {
                     mReadyRxBuffer[mBytesCopied++] = gMessageMark_c ;
                    }
                  /* The complet massage is formed (break) */
                  messageDone = TRUE;
                  break;
                 } 
          /* Check for recieving gMessageLength_c bytes ( max length of a message) */
          if(gMessageLength_c  == mBytesCopied)
                {
                  /* Put \r\n at the end of the message */
                  mReadyRxBuffer[mBytesCopied++] = gMessageMarkCR_c ;
                  mReadyRxBuffer[mBytesCopied++] = gMessageMark_c ;
                  /* The complet massage is formed (break) */
                  messageDone = TRUE;
                  break;
                }
         }
         
         /* if we have a complet message */
         if(messageDone)
          {
           cReturnBytes = mBytesCopied;
           /* copy the ready message to user buffer */
           FLib_MemCpy( pBuffer, &mReadyRxBuffer, cReturnBytes );
           mBytesCopied =0;
           return cReturnBytes;
          }
         else 
          {
               /*If we cover all the mRxBuffer;
                 set the IRP struct and make an Rx command */
               if ( mCount == mSaveRxlen )
                {
                  /* Now is going on reading again */
                  mUartOnReading=TRUE;
                  /* Set the IRP for receiving */
                  mIrpDataRx.reqData.rxtxParams.pBuffer = mRxBuffer;        
                  mIrpDataRx.reqData.rxtxParams.len = gFullPacketLength_c ;
                  mIrpDataRx.reqData.rxtxParams.pfCallback = &AppUARTCallback;
                  mIrpDataRx.requestType = gRxUart_c;
                    
                  (void) UART_Request( &mIrpDataRx );
                  return 0;
                }
          }
        }           
  }
}
//-----------------------------------------------------------------------------------

#else  /* gEnableUartUtilRxFunctions_c */
/* Create dummy functions for the task scheduler */

//-----------------------------------------------------------------------------------

uint8_t UartUtil_Poll(uint8_t *pBuffer ) {
/* Avoid compiler warning */
(void)pBuffer;
return 0;
}

uint8_t UartUtil_PollMessage(uint8_t *pBuffer ){
/* Avoid compiler warning */
(void)pBuffer;
return 0;
}
//-----------------------------------------------------------------------------------

#endif /* gEnableUartUtilRxFunctions_c */

//-----------------------------------------------------------------------------------

#else   /* gEnableUartUtil_c */
/* Create  dummy functions for the task scheduler */

//-----------------------------------------------------------------------------------
void UartUtil_Init(uint16_t cBaudRate){
/* Avoid compiler warning */
(void)cBaudRate;
}

void UartUtil_ConfigureStopMode(bool_t enterStopMode){
(void)enterStopMode;  
}

void UartUtil_Print(char * pString){
/* Avoid compiler warning */
(void)pString;
}

void UartUtil_PrintHex(uint8_t *hex, uint8_t len, uint8_t flags){
/* Avoid compiler warning */
(void)hex;
(void)len;
(void)flags;
}

void UartUtil_Tx(uint8_t * pData, uint8_t length){
/* Avoid compiler warning */
(void)pData;
(void)length;
}

uint8_t UartUtil_IsNotPendingTx(void){
/* Avoid compiler warning */
return 0;
}

uint8_t UartUtil_Poll(uint8_t *pBuffer ) {
/* Avoid compiler warning */
(void)pBuffer;
return 0;
}

uint8_t UartUtil_PollMessage(uint8_t *pBuffer ){
/* Avoid compiler warning */
(void)pBuffer;
return 0;
}
//-----------------------------------------------------------------------------------

#endif /* gEnableUartUtil_c */

⌨️ 快捷键说明

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