// -----------------------------------------------------------------------------
//! \brief      This callback is invoked on Write completion
//!
//! \param[in]  handle - handle to the UART port
//! \param[in]  ptr    - pointer to data to be transmitted
//! \param[in]  size   - size of the data
//!
//! \return     void
// -----------------------------------------------------------------------------
void NPITLUART_writeCallBack(UART_Handle handle, void *ptr, size_t size)
{
  _npiCSKey_t key;
  key = NPIUtil_EnterCS();

#if (NPI_FLOW_CTRL == 1)
  // This is a CC26xx/CC13xx Driverlib specific call (not optimal...)
  // This is necessary because the write call back needs to know if any bytes 
  // have been received from the peer device. If no bytes have been received
  // the transaction can be ended. However, If a simultaneous read/write occurs
  // within the same transaction then simple checking if RxActive or even the
  // state variable of the read call back is not enough. In testing the UART 
  // read call back was never invoked prior to the write call back so we must
  // bypass the driver and directly check if there are bytes in the RX FIFO
  if ( !UARTCharsAvail(((UARTCC26XX_HWAttrsV1 const *)(handle->hwAttrs))->baseAddr) )
  {
    UART_readCancel(uartHandle);
    RxActive = FALSE;

    if (npiTransmitCB)
    {
      if (NPITLUART_validPacketFound() == NPI_SUCCESS)
      {
        // Decrement as to not include trailing FCS byte
        TransportRxLen--;
      }
      else
      {
        // Did not receive valid packet so denote RX length as zero in CB
        TransportRxLen = 0;
      }

      npiTransmitCB(TransportRxLen,TransportTxLen);
    }
  }

  TxActive = FALSE;
#else
  if (npiTransmitCB)
  {
    npiTransmitCB(0,TransportTxLen);
  }
#endif // NPI_FLOW_CTRL = 1

  NPIUtil_ExitCS(key);
}
Esempio n. 2
0
// -----------------------------------------------------------------------------
//! \brief      This callback is invoked on Write completion
//!
//! \param[in]  handle - handle to the UART port
//! \param[in]  ptr    - pointer to data to be transmitted
//! \param[in]  size   - size of the data
//!
//! \return     void
// -----------------------------------------------------------------------------
void NPITLUART_writeCallBack(UART_Handle handle, void *ptr, size_t size)
{
    _npiCSKey_t key;
    key = NPIUtil_EnterCS();

#ifdef POWER_SAVING
    if (!RxActive)
    {
        UART_readCancel(uartHandle);

        if (npiTransmitCB)
        {
            if (NPITLUART_validPacketFound() == NPI_SUCCESS)
            {
                // Decrement as to not include trailing FCS byte
                TransportRxLen--;
            }
            else
            {
                // Did not receive valid packet so denote RX length as zero in CB
                TransportRxLen = 0;
            }

            npiTransmitCB(TransportRxLen,TransportTxLen);
        }
    }

    TxActive = FALSE;

#else
    if (npiTransmitCB)
    {
        npiTransmitCB(0,TransportTxLen);
    }
#endif //POWER_SAVING

    NPIUtil_ExitCS(key);
}
Esempio n. 3
0
// -----------------------------------------------------------------------------
//! \brief      This callback is invoked on Read completion of readSize/receive
//!             timeout
//!
//! \param[in]  handle - handle to the UART port
//! \param[in]  ptr    - pointer to buffer to read data into
//! \param[in]  size   - size of the data
//!
//! \return     void
// -----------------------------------------------------------------------------
void NPITLUART_readCallBack(UART_Handle handle, void *ptr, size_t size)
{
#ifndef POWER_SAVING
    uint16_t packetSize;
    uint16_t sofIndex;
#endif //POWER_SAVING
    _npiCSKey_t key;
    key = NPIUtil_EnterCS();

    if (size)
    {
        if (size != NPITLUART_readIsrBuf(size))
        {
            // Buffer overflow imminent. Cancel read and pass to higher layers
            // for handling
#ifdef POWER_SAVING
            RxActive = FALSE;
#endif //POWER_SAVING
            if (npiTransmitCB)
            {
                npiTransmitCB(npiBufSize,TransportTxLen);
            }
        }
    }

#ifdef POWER_SAVING
    // Read has been cancelled by transport layer, or bus timeout and no bytes in FIFO
    //    - do not invoke another read
    if (!UARTCharsAvail(((UARTCC26XX_HWAttrs const *)(uartHandle->hwAttrs))->baseAddr) &&
            remRdy_flag)
    {
        RxActive = FALSE;

        // If TX has also completed then we are safe to issue call back
        if (!TxActive && npiTransmitCB)
        {
            if (NPITLUART_validPacketFound() == NPI_SUCCESS)
            {
                // Decrement as to not include trailing FCS byte
                TransportRxLen--;
            }
            else
            {
                // Did not receive valid packet so denote RX length as zero in CB
                TransportRxLen = 0;
            }

            npiTransmitCB(TransportRxLen,TransportTxLen);
        }
    }
    else
    {
        UART_read(uartHandle, &isrRxBuf[0], UART_ISR_BUF_SIZE);
    }
#else
    while (NPITLUART_validPacketFound() == NPI_SUCCESS &&
            npiTransmitCB)
    {
        // The Rx Buffer can contain more than the one valid packet
        // Must only return the number of bytes for the first valid packet and
        // shift the remaining bytes to the beginning of the RX buffer after the
        // CB has completed

        // SOF has already been removed from npiRxBuf here. It is removed
        // when bytes are copied from the ISR Rx buffer to npiRxBuf
        packetSize = (uint16) npiRxBuf[0];
        packetSize += ((uint16) npiRxBuf[1]) << 8;
        packetSize += NPI_UART_MSG_HDR_LEN;

        npiTransmitCB(packetSize,0);

        // Must look for SOF of next packet, first eligible byte is after the
        // FCS of the valid packet
        sofIndex = packetSize + 1;
        while(sofIndex < TransportRxLen)
        {

            if (npiRxBuf[sofIndex] == NPI_UART_MSG_SOF)
            {
                break;
            }

            sofIndex++;
        }

        // New RX len does not include bytes prior to and include the
        // next found SOF byte. If no next SOF byte then len is 0
        TransportRxLen = (TransportRxLen == sofIndex) ? 0 :
                         TransportRxLen - sofIndex - 1;

        // Copy all bytes following next found SOF to beginning of the RX buf
        // and check again for another valid packet
        memcpy(npiRxBuf,&npiRxBuf[sofIndex + 1],TransportRxLen);
    }

    UART_read(uartHandle, &isrRxBuf[0], UART_ISR_BUF_SIZE);
#endif //POWER_SAVING

    NPIUtil_ExitCS(key);
}
// -----------------------------------------------------------------------------
//! \brief      This callback is invoked on Read completion of readSize/receive
//!             timeout
//!
//! \param[in]  handle - handle to the UART port
//! \param[in]  ptr    - pointer to buffer to read data into
//! \param[in]  size   - size of the data
//!
//! \return     void
// -----------------------------------------------------------------------------
void NPITLUART_readCallBack(UART_Handle handle, void *ptr, size_t size)
{
  static uint16_t payloadLen = 0;
  _npiCSKey_t key;
  key = NPIUtil_EnterCS();

  switch (readState)
  {
    case NPITLUART_READ_SOF:
      // Should only have read one byte in this state. 
      if (size == NPI_UART_MSG_SOF_LEN && npiRxBuf[0] == NPI_UART_MSG_SOF)
      {
        // Recevied SOF, Read HDR next. Do not save SOF byte
        UART_read(uartHandle, npiRxBuf, NPI_UART_MSG_HDR_LEN);
        readState = NPITLUART_READ_HDR;
      }
      break;
    
    case NPITLUART_READ_HDR:
      if (size == NPI_UART_MSG_HDR_LEN)
      {
        // Header has been read. Increment RxLen
        TransportRxLen += size;
        
        // Determine length of remainder of packet, add an extra byte for FCS
        payloadLen = BUILD_UINT16(npiRxBuf[0],npiRxBuf[1]) + 1;
        
        // Check to see if payload can fit in the remainder of the RxBuf
        if (payloadLen <= npiBufSize - TransportRxLen)
        {
          // Read remainder of packet
          UART_read(uartHandle, &npiRxBuf[TransportRxLen], payloadLen);
          readState = NPITLUART_READ_PLD;
        }
        else
        {
          // Read remainder of packet bytes but ignore them
          UART_read(uartHandle, npiRxBuf, npiBufSize);
          readState = NPITLUART_IGNORE;
        }
      }
      else
      {
        // Error has occured. Reset read state
        readState = NPITLUART_READ_SOF;
      }
    break;
    
    case NPITLUART_READ_PLD:
      if (payloadLen == size)
      {
        // All bytes are read
        TransportRxLen += size;
        
        // Check if FCS is valid
        if (NPITLUART_validPacketFound() == NPI_SUCCESS)
        {
          // Valid Packet. Decrement RxLen to not include FCS since it is
          // checked and valid
          TransportRxLen--;
          
#if (NPI_FLOW_CTRL == 1)
          RxActive = FALSE;
          
          // If TX has also completed then we are safe to issue call back
          if (!TxActive && npiTransmitCB)
          {
            npiTransmitCB(TransportRxLen,TransportTxLen);
          }
#else
          if (npiTransmitCB) 
          {
            npiTransmitCB(TransportRxLen,0);
          }
#endif // NPI_FLOW_CTRL = 1
        }
      }

      // Reset State. Full packet has been read or error has occurred
      readState = NPITLUART_READ_SOF;
      break;
    
    case NPITLUART_IGNORE:
      if (payloadLen == size)
      {
        // All bytes of oversized payload have been read. Reset state
        readState = NPITLUART_READ_SOF;
      }
      else
      {
        // Bytes remaining to be read
        payloadLen -= size;
        
        if (payloadLen > npiBufSize)
        {
          UART_read(uartHandle, npiRxBuf, npiBufSize);
        }
        else
        {
          UART_read(uartHandle, npiRxBuf, payloadLen);
        }
      }
      break;
    
    default:
      // Should not get here. If so reset read state
      readState = NPITLUART_READ_SOF;
      break;
  }

#if (NPI_FLOW_CTRL == 0)
  // Initiate next read of SOF byte
  if (readState == NPITLUART_READ_SOF)
  {
    TransportRxLen = 0;
    UART_read(uartHandle, npiRxBuf, NPI_UART_MSG_SOF_LEN);
  }
#endif // NPI_FLOW_CTRL = 0
  
  NPIUtil_ExitCS(key);
}