// -----------------------------------------------------------------------------
//! \brief      This routine is used to handle an MRDY transition from a task
//!             context. Certain operations such as UART_read() cannot be
//!             performed from a HWI context
//!
//! \return     void
// -----------------------------------------------------------------------------
void NPITL_handleRemRdyEvent(void)
{
	_npiCSKey_t key;
	key = NPIUtil_EnterCS();
	//If the UART port is closed, then open it
    if(HS_GPIO_STATE & handshakingState)
    {
    	NPITL_setPM();
    	//Close the GPIO, then
    	//Open the UART
    	 PIN_close(hNpiHandshakePins);
    	 transportOpen(npiTLParams.portBoardID,
					  &npiTLParams.portParams.uartParams,
					  NPITL_transmissionCallBack, NPITL_chirpRecievedCB);
    	 handshakingState |=  HS_WAITFORCHIRP|HS_UART_STATE;
    	 //Clear GPIO flag, we are no longer in this state
    	 handshakingState &= ~HS_GPIO_STATE;
    }
    else
    {
    	//Once UART is open
        //Handle the RemRdy Event
        transportRemRdyEvent();
    }
    NPIUtil_ExitCS(key);
}
// -----------------------------------------------------------------------------
//! \brief      This is a HWI function handler for the MRDY pin. Some MRDY
//!             functionality can execute from this HWI context. Others
//!             must be executed from task context hence the taskCBs.remRdyCB()
//!
//! \param[in]  hPin - PIN Handle
//! \param[in]  pinId - ID of pin that triggered HWI
//!
//! \return     void
// -----------------------------------------------------------------------------
static void NPITL_remRdyPINHwiFxn(PIN_Handle hPin, PIN_Id pinId)
{
    // The pin driver does not currently support returning whether the int
    // was neg or pos edge so we must use a variable to keep track of state. 
    remRdy_state ^= 1;
    
    if (remRdy_state == 0)
    {
        mrdyPktStamp = txPktCount;
        NPITL_setPM();
    }
    else
    {
        transportStopTransfer();
        npiRxActive = FALSE;
    }
    
    // Signal to registered task that Rem Ready signal has changed state
    if (taskCBs.remRdyCB)
    {
        taskCBs.remRdyCB(remRdy_state);
    }
    
    // Check the physical state of the pin to see if it matches the variable 
    // state. If not trigger another task call back
    if (remRdy_state != PIN_getInputValue(remRdyPIN))
    {
        remRdy_state = PIN_getInputValue(remRdyPIN);
        
        if (taskCBs.remRdyCB)
        {
            taskCBs.remRdyCB(remRdy_state);
        }
    }
}
// -----------------------------------------------------------------------------
//! \brief      This is a HWI function handler for the MRDY pin. Some MRDY
//!             functionality can execute from this HWI context. Others
//!             must be executed from task context hence the taskCBs.remRdyCB()
//!
//! \param[in]  hPin - PIN Handle
//! \param[in]  pinId - ID of pin that triggered HWI
//!
//! \return     void
// -----------------------------------------------------------------------------
static void NPITL_remRdyPINHwiFxn(PIN_Handle hPin, PIN_Id pinId)
{ 
    NPITL_setPM();
    // Signal to registered task that Rem Ready signal has changed state
    if (taskCBs.remRdyCB)
    {
        taskCBs.remRdyCB(remRdy_state);
    }
}
// -----------------------------------------------------------------------------
//! \brief      This is a HWI function handler for the UART Rx pin. It will wake
//!             the TL and signal the app to open the TL by closing UART and 
//!             opening the GPIO
//!
//! \param[in]  hPin - PIN Handle
//! \param[in]  pinId - ID of pin that triggered HWI
//!
//! \return     void
// -----------------------------------------------------------------------------
static void NPITL_rxPinHwiFxn(PIN_Handle hPin, PIN_Id pinId)
{
    NPITL_setPM();
    // Signal to registered task that TL is awake, needs to open
    if (taskCBs.tlOpenCB)
    {
        taskCBs.tlOpenCB();
    }
}
// -----------------------------------------------------------------------------
//! \brief      This routine calls the UART transport open
//!
//! \param[in]  initiatorState - Whether the UART is being opened by initiator
//!             or responder. 1 - initiator 0 - responsder
//!
//! \return     void
// -----------------------------------------------------------------------------
void NPITL_openTransportPort(hsTransactionRole role)
{
  NPITL_setPM();
  // Close the RxPin
  PIN_close(hNpiUartRxPin);
  transportOpen(npiTLParams.portBoardID,
                &npiTLParams.portParams.uartParams, role);
  trasnportLayerState = TL_busy;
}
// -----------------------------------------------------------------------------
//! \brief      This is a HWI function handler for the MRDY pin. Some MRDY
//!             functionality can execute from this HWI context. Others
//!             must be executed from task context hence the taskMrdyCB()
//!
//! \param[in]  hPin - PIN Handle
//! \param[in]  pinId - ID of pin that triggered HWI
//!
//! \return     void
// -----------------------------------------------------------------------------
static void NPITL_MRDYPinHwiFxn(PIN_Handle hPin, PIN_Id pinId)
{
    // The pin driver does not currently support returning whether the int
    // was neg or pos edge so we must use a variable to keep track of state.
    // If the physical state of the pin was used then a very quick toggle of
    // of MRDY could be missed.
    mrdy_state ^= 1;

    if (mrdy_state == 0)
    {
        mrdyPktStamp = txPktCount;
        NPITL_setPM();
        if ( taskMrdyCB )
        {
            taskMrdyCB();
        }
    }
    else
    {
        transportStopTransfer();
    }

    // Check the physical state of the pin to see if it matches the variable
    // state. If not then edge has been missed
    if (mrdy_state != PIN_getInputValue(MRDY_PIN))
    {
        mrdy_state = PIN_getInputValue(MRDY_PIN);

        if (mrdy_state == 0)
        {
            mrdyPktStamp = txPktCount;
            NPITL_setPM();

            if (taskMrdyCB)
            {
                taskMrdyCB();
            }
        }
        else
        {
            transportStopTransfer();
        }
    }
}
// -----------------------------------------------------------------------------
//! \brief      This routine initializes the transport layer and opens the port
//!             of the device. Note that based on project defines, either the
//!             UART, or SPI driver can be used.
//!
//! \param[in]  params - Transport Layer parameters
//!
//! \return     void
// -----------------------------------------------------------------------------
void NPITL_openTL(NPITL_Params *params)
{
    _npiCSKey_t key;
    key = NPIUtil_EnterCS();
    
    // Set NPI Task Call backs
    memcpy(&taskCBs, &params->npiCallBacks, sizeof(params->npiCallBacks));
    
    // Allocate memory for Transport Layer Tx/Rx buffers
    npiBufSize = params->npiTLBufSize;
    npiRxBuf = NPIUTIL_MALLOC(params->npiTLBufSize);
    memset(npiRxBuf, 0, npiBufSize);
    npiTxBuf = NPIUTIL_MALLOC(params->npiTLBufSize);
    memset(npiTxBuf, 0, npiBufSize);

    // This will be updated to be able to select SPI/UART TL at runtime
    // Now only compile time with the NPI_USE_[UART,SPI] flag
#if defined(NPI_USE_UART)
    transportOpen(params->portBoardID, 
                  &params->portParams.uartParams, 
                  NPITL_transmissionCallBack);
#elif defined(NPI_USE_SPI)
    transportOpen(params->portBoardID, 
                  &params->portParams.spiParams, 
                  NPITL_transmissionCallBack);
#endif //NPI_USE_UART
    
#if (NPI_FLOW_CTRL == 1)
    // Assign PIN IDs to remRdy and locRrdy
#ifdef NPI_MASTER
    remRdyPIN = (params->srdyPinID & IOC_IOID_MASK);
    locRdyPIN = (params->mrdyPinID & IOC_IOID_MASK);
#else
    remRdyPIN = (params->mrdyPinID & IOC_IOID_MASK);
    locRdyPIN = (params->srdyPinID & IOC_IOID_MASK);
#endif //NPI_MASTER
    
    // Add PIN IDs to PIN Configuration
    npiHandshakePinsCfg[REM_RDY_PIN_IDX] |= remRdyPIN;
    npiHandshakePinsCfg[LOC_RDY_PIN_IDX] |= locRdyPIN;
    
    // Initialize LOCRDY/REMRDY. Enable int after callback registered
    hNpiHandshakePins = PIN_open(&npiHandshakePins, npiHandshakePinsCfg);
    PIN_registerIntCb(hNpiHandshakePins, NPITL_remRdyPINHwiFxn);
    PIN_setConfig(hNpiHandshakePins, 
                  PIN_BM_IRQ, 
                  remRdyPIN | PIN_IRQ_BOTHEDGES);

    // Enable wakeup
    PIN_setConfig(hNpiHandshakePins, 
                  PINCC26XX_BM_WAKEUP, 
                  remRdyPIN | PINCC26XX_WAKEUP_NEGEDGE);
    
    remRdy_state = PIN_getInputValue(remRdyPIN);
    
    // If MRDY is already low then we must initiate a read because there was
    // a prior MRDY negedge that was missed
    if (!remRdy_state) 
    {
        NPITL_setPM();
        if (taskCBs.remRdyCB)
        {
            transportRemRdyEvent();
            LocRDY_ENABLE();
        }
    }
#endif // NPI_FLOW_CTRL = 1

#if (NPI_FLOW_CTRL == 0)
    // This call will start repeated Uart Reads when Power Savings is disabled
    transportRead();
#endif // NPI_FLOW_CTRL = 0

    NPIUtil_ExitCS(key);
}