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

📄 uartutil.c

📁 freescale的基于802.15.4的无线通讯例程
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************************
* Includes the UART Utils.
*
* (c) Copyright 2006, Freescale Semiconductor, Inc. All rights reserved.
*
* No part of this document must be reproduced in any form - including copied,
* transcribed, printed or by any electronic means - without specific written
* permission from Freescale.
*
* Last Inspected:15.08.2006
* Last Tested:15.08.2006
*
************************************************************************************/
#include "UartUtil.h"
#include "FunctionLib.h"

#if ( gEnableUartUtil_c )
/************************************************************************************
*************************************************************************************
* Private macros
*************************************************************************************
************************************************************************************/

/* Define the Max value of uint8_t type*/
#define gUint8Max_c        0xFF

/*A message is defined as a sequence of caracters (up to gMessageLength_c) delimited by \r\n  */
#define gMessageMark_c     0x0A
#define gMessageMarkCR_c   0x0D
/* Define for Tx data register empty bit in SCI status register */
#define mTxDataRegEmptyBit_c     0x80


/************************************************************************************
*************************************************************************************
* Private prototypes
*************************************************************************************/

/* This function will be called when the UartTxSuccess, UartRxSuccess
   or UartRxTimeout operation completes */
static void AppUARTCallback(irp_t *pIrp);


//-----------------------------------------------------------------------------------
#if ( gEnableUartUtilTxFunctions_c )

/* Sent the data between tail and head if is less than gUint8Max_c,
   else sent gUint8Max_c of data */
static void SendData(void);

/* Trasform from hex to ascii */
static uint8_t HexToAscii(uint8_t hex); 

/* Put the byte in the circular list */
static void PutInTxCirc(uint8_t txByte);

/************************************************************************************
*************************************************************************************
* Private type definitions
*************************************************************************************
************************************************************************************/

/************************************************************************************
*************************************************************************************
* Public memory declarations
*************************************************************************************
************************************************************************************/
 
/************************************************************************************
*************************************************************************************
* Private memory declarations
*************************************************************************************
************************************************************************************/

/* Circular List for TX */
static uint8_t mUartCircListTx[gUartCircListTxLen_c];
/* Tail and head for Circular T x List */
static uint16_t mtUartCircListTx = 0;
static uint16_t mhUartCircListTx = 0;

/* Number of bytes to be sent */ 
static uint16_t mNrBytesSent = 0;
/* The IRPs used for Tx */
static irp_t mIrpDataTx;

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

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

/* Flags for checking the state on Rx */
static uint8_t mUartOnReading = FALSE;
static uint8_t mValuesReturned = FALSE;

/* Used for Rx length */
static uint8_t mRxlen;
static uint8_t mSaveRxlen;

/* The IRPs used for Rx*/
static irp_t mIrpDataRx;

/* Used for recieving data */
static uint8_t mRxBuffer[gFullPacketLength_c];

/* Used for UartUtil_PollMessage() to recieve a complet message ( a ready message );
   add \r\n ( +2 bytes ) if it was recieved gMessageLength_c bytes */ 
static uint8_t mReadyRxBuffer[gMessageLength_c+2];

/* Number of bytes copied to mReadyRxBuffer */
static uint8_t mBytesCopied = 0;

/* Counter for mRxBuffer */
static uint8_t mCount = 0;

#endif /* gEnableUartUtilRxFunctions_c */
//-----------------------------------------------------------------------------------

/************************************************************************************
*************************************************************************************
* Public functions
*************************************************************************************
************************************************************************************/

//-----------------------------------------------------------------------------------
void UartUtil_Init(uint16_t cBaudRate)
{

#if ( gEnableUartUtilRxFunctions_c ) 
  /* Set the IRP struct and sent a init command */ 
  /* Give the BaudRate to be sent */
  mIrpDataRx.reqData.initParams.baudRate = cBaudRate; 
  /* Request a Init operation of the UART module */
  mIrpDataRx.requestType = gInitUart_c;
  (void)UART_Request(&mIrpDataRx);
#elif ( gEnableUartUtilTxFunctions_c ) 
  /* Set the IRP struct and sent a init command */ 
  /* Give the BaudRate to be sent */
  mIrpDataTx.reqData.initParams.baudRate = cBaudRate; 
  /* Request a Init operation of the UART module */
  mIrpDataTx.requestType = gInitUart_c;
  (void)UART_Request(&mIrpDataTx);
#else
  (void)cBaudRate;
#endif
  
}

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

/* This function will be called when the UartTxSuccess,UartRxSuccess
   or UartRxTimeout operation completes */
static void AppUARTCallback(irp_t *pIrp)
{
  switch (pIrp->returnValue)
  {    
      case gUartTxSuccess_c:
        #if ( gEnableUartUtilTxFunctions_c  )
        
          /* Print the next bytes from the CircList if the CircList is not empty */
          if ( (mtUartCircListTx+mNrBytesSent) == gUartCircListTxLen_c )
           {
            mtUartCircListTx=0;
           }
          else
           {
            mtUartCircListTx+=mNrBytesSent;
           }
          
          if ( mhUartCircListTx != mtUartCircListTx )
            {
              SendData();
            }
        #endif /* gEnableUartUtilTxFunctions_c */
        
        break;
       
      case gUartRxSuccess_c:
        #if ( gEnableUartUtilRxFunctions_c  )  
        
           /* Here Uart is not on reading (recieved all data) and return the lenght */
           mValuesReturned = TRUE;
           mUartOnReading = FALSE;
           mRxlen = pIrp->reqData.rxtxParams.len;
        
        #endif /* gEnableUartUtilRxFunctions_c */
        
        break;
        
      case gUartRxTimeout_c:
        #if ( gEnableUartUtilRxFunctions_c )
        
           /* No data is coming, so... Uart is not on reading and return the lenght */
           mValuesReturned=TRUE;
           mUartOnReading = FALSE;
           mRxlen = pIrp->reqData.rxtxParams.len;
        
        #endif  /* gEnableUartUtilRxFunctions_c */          
        
        break;
      default:
        break;
  }
}

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

/* Configure UART to work in stop mode or in normal mode*/
void UartUtil_ConfigureStopMode(bool_t enterStopMode)
{
  uint16_t cnt;  
  uint16_t nops = 0xFF;

  if ( TRUE == enterStopMode )
  {      
    /* Configure UART before entering stop mode */      
    SCIxC2 &= ~0x04;   /* Disable SCI Rx : RE=0 */

    /* Do some nops */
    cnt = nops;
    while(cnt)
    {
      cnt--;
      __asm nop;
    }        

    PTEDD |= 0x01;     /* Set Tx line as output */
    PTEDD &=~0x02;     /* Set Rx line as input  */      
    PTED  |= 0x01;     /* Send a 1 = idle line  */              
    PTEPE |= 0x02;     /* Active pullup for Rx  */
  }
  else
  {
    /* Restore UART settings */            
    PTEDD &= ~0x01;    /* Set Tx as input       */
    PTEDD |=  0x02;    /* Set Rx as output       */
    SCIxC2 |= 0x04;    /* Enable SCI RX : RE=1  */
  }
}


//-----------------------------------------------------------------------------------
#if ( gEnableUartUtilTxFunctions_c )

/* Sent the data between tail and head if is less than gUint8Max_c,
   else sent gUint8Max_c of data */
static void SendData(void)
{
 /* This is to avoid the warning */
  uint16_t cCircListDimWarningOff = gUartCircListTxLen_c; 
 
    if (mtUartCircListTx != mhUartCircListTx)
    {
      /* Give a pointer to the buffer containing the data to be sent */
      mIrpDataTx.reqData.rxtxParams.pBuffer = &mUartCircListTx[mtUartCircListTx]; 
            
      /* Provide a callback function so we get notified when Tx is done */
      mIrpDataTx.reqData.rxtxParams.pfCallback = &AppUARTCallback; 
       
       /* Tell how much data we want to transmit */
       if(mtUartCircListTx < mhUartCircListTx)
         { 
          if( (mhUartCircListTx-mtUartCircListTx) < gUint8Max_c )
           { 
            mIrpDataTx.reqData.rxtxParams.len = mhUartCircListTx-mtUartCircListTx;
           }
          else
           {
            mIrpDataTx.reqData.rxtxParams.len = gUint8Max_c ;
           }
         }              
       else
       { 
        if( (gUartCircListTxLen_c-mtUartCircListTx) < gUint8Max_c )
         {
          mIrpDataTx.reqData.rxtxParams.len = 
                              (uint8_t)(cCircListDimWarningOff - mtUartCircListTx);
         }
        else
         {
          mIrpDataTx.reqData.rxtxParams.len = gUint8Max_c ;
         }
       }                         
      mNrBytesSent=mIrpDataTx.reqData.rxtxParams.len;        
      
      /* Request a Tx operation of the UART module */
      mIrpDataTx.requestType = gTxUart_c; 

      (void)UART_Request(&mIrpDataTx);
      
    }   
      
}


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

void UartUtil_Tx(uint8_t * pData, uint8_t length)
{
  uint16_t i;
  uint16_t space;
  uint8_t flagCircListIsEmpty = FALSE;
  
  /* Check to see if the circular list is empty */
  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; i <length; ) 
  {
    if(i < space)
    {
      PutInTxCirc(*pData++);
      i++;
    }
    else
    {
    /* The circular list is full */
    /* You will lose data here */
     break;
    }
  }
  /* If the circular list was empty sent data */
  if( flagCircListIsEmpty )
  {
   SendData();
  } 
}

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

uint8_t UartUtil_IsNotPendingTx(void)
{
  /* Check for room in Tx buffer */
  if( mtUartCircListTx == mhUartCircListTx ) 
  {  
    return TRUE;
  } 
  else 
  {
    return FALSE;
  }
}

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

/* Put the byte in the circular list */
static void PutInTxCirc(uint8_t txByte)
{
  mUartCircListTx[mhUartCircListTx] = txByte;
  if ( mhUartCircListTx==gUartCircListTxLen_c-1 )
   {
    mhUartCircListTx=0;
   }
  else
   {
    mhUartCircListTx++;
   }
}

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

void UartUtil_Print(uint8_t* pString)
{
  uint16_t i;

⌨️ 快捷键说明

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