
The MCR20A Wireless UART application functions as an wireless UART bridge between two (one-to-one) or several (one to many) boards. The application can be used with both a TERM, or with software that is capable of opening a serial port and writing to or reading from it. The characters sent or received are not necessarily ASCII printable characters.
Dependencies: fsl_phy_mcr20a fsl_smac mbed-rtos mbed
Fork of mcr20_wireless_uart by
By default, the application uses broadcast addresses for OTA communication. This way, the application can be directly downloaded and run without any user intervention. The following use case assumes no changes have been done to the project.
- Two (or more) MCR20A platforms (plugged into the FRDM-K64F Freescale Freedom Development platform) have to be connected to the PC using the mini/micro-USB cables.
- The code must be downloaded on the platforms via CMSIS-DAP (or other means).
- After that, two or more TERM applications must be opened, and the serial ports must be configured with the same baud rate as the one in the project (default baud rate is 115200). Other necessary serial configurations are 8 bit, no parity, and 1 stop bit.
- To start the setup, each platform must be reset, and one of the (user) push buttons found on the MCR20A platform must be pressed. The user can press any of the non-reset buttons on the FRDM-K64F Freescale Freedom Development platform as well. *This initiates the state machine of the application so user can start.
Documentation
SMAC Demo Applications User Guide
RF_Drivers_Freescale/PhyTime.c
- Committer:
- FSL\B36402
- Date:
- 2015-03-15
- Revision:
- 15:990a8b5664e1
- Child:
- 16:549f2f246ece
File content as of revision 15:990a8b5664e1:
/*! * Copyright (c) 2015, Freescale Semiconductor, Inc. * All rights reserved. * * \file PhyTime.c * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * o Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * o Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or * other materials provided with the distribution. * * o Neither the name of Freescale Semiconductor, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /************************************************************************************ ************************************************************************************* * Include ************************************************************************************* ************************************************************************************/ #include "EmbeddedTypes.h" //#include "fsl_os_abstraction.h" #include "MCR20Drv.h" #include "MCR20Reg.h" #include "Phy.h" //#include "FunctionLib.h" #include "arm_hal_interrupt.h" /************************************************************************************ ************************************************************************************* * Private macros ************************************************************************************* ************************************************************************************/ #define gPhyTimeMinSetupTime_c (10) /* symbols */ /************************************************************************************ ************************************************************************************* * Public memory declarations ************************************************************************************* ************************************************************************************/ void (*gpfPhyTimeNotify)(void) = NULL; /************************************************************************************ ************************************************************************************* * Private memory declarations ************************************************************************************* ************************************************************************************/ static phyTimeEvent_t mPhyTimers[gMaxPhyTimers_c]; static phyTimeEvent_t *pNextEvent; volatile uint32_t mPhySeqTimeout; volatile uint64_t gPhyTimerOverflow; /************************************************************************************ ************************************************************************************* * Private prototypes ************************************************************************************* ************************************************************************************/ static void PhyTime_OverflowCB( uint32_t param ); static phyTimeEvent_t* PhyTime_GetNextEvent( void ); /************************************************************************************ ************************************************************************************* * Public functions ************************************************************************************* ************************************************************************************/ /*! ********************************************************************************* * \brief Sets the start time of a sequence * * \param[in] startTime the start time for a sequence * ********************************************************************************** */ void PhyTimeSetEventTrigger ( uint32_t startTime ) { uint8_t phyReg, phyCtrl3Reg; arm_enter_critical(); phyReg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL1); phyReg |= cPHY_CTRL1_TMRTRIGEN; // enable autosequence start by TC2 match MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL1, phyReg); phyCtrl3Reg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL3); phyCtrl3Reg &= ~(cPHY_CTRL3_TMR2CMP_EN);// disable TMR2 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyCtrl3Reg); MCR20Drv_DirectAccessSPIMultiByteWrite( (uint8_t) T2PRIMECMP_LSB, (uint8_t *) &startTime, 2); phyReg = MCR20Drv_DirectAccessSPIRead(IRQSTS3); phyReg &= 0xF0; // do not change other IRQs status phyReg &= ~(cIRQSTS3_TMR2MSK); // unmask TMR2 interrupt phyReg |= (cIRQSTS3_TMR2IRQ); // aknowledge TMR2 IRQ MCR20Drv_DirectAccessSPIWrite( (uint8_t) IRQSTS3, phyReg); // TC2PRIME_EN must be enabled in PHY_CTRL4 register phyCtrl3Reg |= cPHY_CTRL3_TMR2CMP_EN; // enable TMR2 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyCtrl3Reg); arm_exit_critical(); } /*! ********************************************************************************* * \brief Disable the time trigger for a sequence. * * \remarks The sequence will start asap * ********************************************************************************** */ void PhyTimeDisableEventTrigger ( void ) { uint8_t phyReg; arm_enter_critical(); phyReg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL1); phyReg &= ~(cPHY_CTRL1_TMRTRIGEN); // disable autosequence start by TC2 match MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL1, phyReg); phyReg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL3); phyReg &= ~(cPHY_CTRL3_TMR2CMP_EN);// disable TMR2 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyReg); phyReg = MCR20Drv_DirectAccessSPIRead(IRQSTS3); phyReg &= 0xF0; // do not change other IRQs status phyReg |= (cIRQSTS3_TMR2MSK); // mask TMR2 interrupt phyReg |= (cIRQSTS3_TMR2IRQ); // aknowledge TMR2 IRQ MCR20Drv_DirectAccessSPIWrite( (uint8_t) IRQSTS3, phyReg); arm_exit_critical(); } /*! ********************************************************************************* * \brief Sets the timeout value for a sequence * * \param[in] pEndTime the absolute time when a sequence should terminate * * \remarks If the sequence does not finish until the timeout, it will be aborted * ********************************************************************************** */ void PhyTimeSetEventTimeout ( uint32_t *pEndTime ) { uint8_t phyReg, phyCtrl3Reg; #ifdef PHY_PARAMETERS_VALIDATION if(NULL == pEndTime) { return; } #endif // PHY_PARAMETERS_VALIDATION arm_enter_critical(); phyCtrl3Reg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL3); phyCtrl3Reg &= ~(cPHY_CTRL3_TMR3CMP_EN);// disable TMR3 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyCtrl3Reg); phyReg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL4); phyReg |= cPHY_CTRL4_TC3TMOUT; // enable autosequence stop by TC3 match MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL4, phyReg); mPhySeqTimeout = *pEndTime & 0x00FFFFFF; MCR20Drv_DirectAccessSPIMultiByteWrite( (uint8_t) T3CMP_LSB, (uint8_t *) pEndTime, 3); phyReg = MCR20Drv_DirectAccessSPIRead(IRQSTS3); phyReg &= 0xF0; // do not change IRQ status // phyReg &= ~(cIRQSTS3_TMR3MSK); // unmask TMR3 interrupt phyReg |= (cIRQSTS3_TMR3IRQ); // aknowledge TMR3 IRQ MCR20Drv_DirectAccessSPIWrite( (uint8_t) IRQSTS3, phyReg); phyCtrl3Reg |= cPHY_CTRL3_TMR3CMP_EN; // enable TMR3 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyCtrl3Reg); arm_exit_critical(); } /*! ********************************************************************************* * \brief Return the timeout value for the current sequence * * \return uint32_t the timeout value * ********************************************************************************** */ uint32_t PhyTimeGetEventTimeout( void ) { return mPhySeqTimeout; } /*! ********************************************************************************* * \brief Disables the sequence timeout * ********************************************************************************** */ void PhyTimeDisableEventTimeout ( void ) { uint8_t phyReg; arm_enter_critical(); phyReg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL4); phyReg &= ~(cPHY_CTRL4_TC3TMOUT); // disable autosequence stop by TC3 match MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL4, phyReg); phyReg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL3); phyReg &= ~(cPHY_CTRL3_TMR3CMP_EN);// disable TMR3 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyReg); phyReg = MCR20Drv_DirectAccessSPIRead(IRQSTS3); phyReg &= 0xF0; // do not change IRQ status phyReg |= cIRQSTS3_TMR3IRQ; // aknowledge TMR3 IRQ MCR20Drv_DirectAccessSPIWrite( (uint8_t) IRQSTS3, phyReg); arm_exit_critical(); } /*! ********************************************************************************* * \brief Reads the absolute clock from the radio * * \param[out] pRetClk pointer to a location where the current clock will be stored * ********************************************************************************** */ void PhyTimeReadClock ( uint32_t *pRetClk ) { #ifdef PHY_PARAMETERS_VALIDATION if(NULL == pRetClk) { return; } #endif // PHY_PARAMETERS_VALIDATION arm_enter_critical(); MCR20Drv_DirectAccessSPIMultiByteRead( (uint8_t) EVENT_TMR_LSB, (uint8_t *) pRetClk, 3); *(((uint8_t *)pRetClk) + 3) = 0; arm_exit_critical(); } /*! ********************************************************************************* * \brief Initialize the Event Timer * * \param[in] pAbsTime pointer to the location where the new time is stored * ********************************************************************************** */ void PhyTimeInitEventTimer ( uint32_t *pAbsTime ) { uint8_t phyCtrl4Reg; #ifdef PHY_PARAMETERS_VALIDATION if(NULL == pAbsTime) { return; } #endif // PHY_PARAMETERS_VALIDATION arm_enter_critical(); phyCtrl4Reg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL4); phyCtrl4Reg |= cPHY_CTRL4_TMRLOAD; // self clearing bit MCR20Drv_DirectAccessSPIMultiByteWrite( (uint8_t) T1CMP_LSB, (uint8_t *) pAbsTime, 3); MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL4, phyCtrl4Reg); arm_exit_critical(); } /*! ********************************************************************************* * \brief Set TMR1 timeout value * * \param[in] pWaitTimeout the timeout value * ********************************************************************************** */ void PhyTimeSetWaitTimeout ( uint32_t *pWaitTimeout ) { uint8_t phyCtrl3Reg, irqSts3Reg; arm_enter_critical(); phyCtrl3Reg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL3); phyCtrl3Reg &= ~(cPHY_CTRL3_TMR1CMP_EN);// disable TMR1 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyCtrl3Reg); MCR20Drv_DirectAccessSPIMultiByteWrite( (uint8_t) T1CMP_LSB, (uint8_t *) pWaitTimeout, 3); irqSts3Reg = MCR20Drv_DirectAccessSPIRead(IRQSTS3); irqSts3Reg &= ~(cIRQSTS3_TMR1MSK); // unmask TMR1 interrupt irqSts3Reg &= 0xF0; // do not change other IRQs status irqSts3Reg |= (cIRQSTS3_TMR1IRQ); // aknowledge TMR1 IRQ MCR20Drv_DirectAccessSPIWrite( (uint8_t) IRQSTS3, irqSts3Reg); phyCtrl3Reg |= cPHY_CTRL3_TMR1CMP_EN; // enable TMR1 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyCtrl3Reg); arm_exit_critical(); } /*! ********************************************************************************* * \brief Disable the TMR1 timeout * ********************************************************************************** */ void PhyTimeDisableWaitTimeout ( void ) { uint8_t phyReg; arm_enter_critical(); phyReg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL3); phyReg &= ~(cPHY_CTRL3_TMR1CMP_EN);// disable TMR1 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyReg); phyReg = MCR20Drv_DirectAccessSPIRead(IRQSTS3); phyReg &= 0xF0; // do not change IRQ status phyReg |= cIRQSTS3_TMR1IRQ; // aknowledge TMR1 IRQ MCR20Drv_DirectAccessSPIWrite( (uint8_t) IRQSTS3, phyReg); arm_exit_critical(); } /*! ********************************************************************************* * \brief Set TMR4 timeout value * * \param[in] pWakeUpTime absolute time * ********************************************************************************** */ void PhyTimeSetWakeUpTime ( uint32_t *pWakeUpTime ) { uint8_t phyCtrl3Reg, irqSts3Reg; arm_enter_critical(); phyCtrl3Reg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL3); // phyCtrl3Reg &= ~(cPHY_CTRL3_TMR4CMP_EN);// disable TMR4 compare // MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyCtrl3Reg); MCR20Drv_DirectAccessSPIMultiByteWrite( (uint8_t) T4CMP_LSB, (uint8_t *) pWakeUpTime, 3); irqSts3Reg = MCR20Drv_DirectAccessSPIRead(IRQSTS3); irqSts3Reg &= ~(cIRQSTS3_TMR4MSK); // unmask TMR4 interrupt irqSts3Reg &= 0xF0; // do not change other IRQs status irqSts3Reg |= (cIRQSTS3_TMR4IRQ); // aknowledge TMR4 IRQ MCR20Drv_DirectAccessSPIWrite( (uint8_t) IRQSTS3, irqSts3Reg); phyCtrl3Reg |= cPHY_CTRL3_TMR4CMP_EN; // enable TMR4 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyCtrl3Reg); arm_exit_critical(); } /*! ********************************************************************************* * \brief Check if TMR4 IRQ occured, and aknowledge it * * \return TRUE if TMR4 IRQ occured * ********************************************************************************** */ bool_t PhyTimeIsWakeUpTimeExpired ( void ) { bool_t wakeUpIrq = FALSE; uint8_t phyReg; arm_enter_critical(); phyReg = MCR20Drv_DirectAccessSPIRead(PHY_CTRL3); phyReg &= ~(cPHY_CTRL3_TMR4CMP_EN);// disable TMR4 compare MCR20Drv_DirectAccessSPIWrite( (uint8_t) PHY_CTRL3, phyReg); phyReg = MCR20Drv_DirectAccessSPIRead(IRQSTS3); if( (phyReg & cIRQSTS3_TMR4IRQ) == cIRQSTS3_TMR4IRQ ) { wakeUpIrq = TRUE; } phyReg &= ~(cIRQSTS3_TMR4MSK); // unmask TMR4 interrupt phyReg &= 0xF0; // do not change other IRQs status phyReg |= (cIRQSTS3_TMR4IRQ); // aknowledge TMR2 IRQ MCR20Drv_DirectAccessSPIWrite( (uint8_t) IRQSTS3, phyReg); arm_exit_critical(); return wakeUpIrq; } /*! ********************************************************************************* * \brief PHY Timer Interrupt Service Routine * ********************************************************************************** */ void PhyTime_ISR(void) { if( pNextEvent->callback == PhyTime_OverflowCB ) { gPhyTimerOverflow++; } if( gpfPhyTimeNotify ) { gpfPhyTimeNotify(); } else { PhyTime_RunCallback(); PhyTime_Maintenance(); } } /*! ********************************************************************************* * \brief Initialize the PHY Timer module * * \return phyTimeStatus_t * ********************************************************************************** */ phyTimeStatus_t PhyTime_TimerInit( void (*cb)(void) ) { if( gpfPhyTimeNotify ) return gPhyTimeError_c; gpfPhyTimeNotify = cb; gPhyTimerOverflow = 0; FLib_MemSet( mPhyTimers, 0, sizeof(mPhyTimers) ); /* Schedule Overflow Calback */ pNextEvent = &mPhyTimers[0]; pNextEvent->callback = PhyTime_OverflowCB; pNextEvent->timestamp = (gPhyTimerOverflow+1) << gPhyTimeShift_c; PhyTimeSetWaitTimeout( (uint32_t*)&pNextEvent->timestamp ); return gPhyTimeOk_c; } /*! ********************************************************************************* * \brief Returns a 64bit timestamp value to be used by the MAC Layer * * \return phyTimeTimestamp_t PHY timestamp * ********************************************************************************** */ phyTimeTimestamp_t PhyTime_GetTimestamp(void) { phyTimeTimestamp_t time = 0; arm_enter_critical(); PhyTimeReadClock( (uint32_t*)&time ); time |= (gPhyTimerOverflow << gPhyTimeShift_c); arm_exit_critical(); return time; } /*! ********************************************************************************* * \brief Schedules an event * * \param[in] pEvent event to be scheduled * * \return phyTimeTimerId_t the id of the alocated timer * ********************************************************************************** */ phyTimeTimerId_t PhyTime_ScheduleEvent( phyTimeEvent_t *pEvent ) { phyTimeTimerId_t tmr; /* Parameter validation */ if( NULL == pEvent->callback ) { return gInvalidTimerId_c; } /* Search for a free slot (slot 0 is reserved for the Overflow calback) */ arm_enter_critical(); for( tmr=1; tmr<gMaxPhyTimers_c; tmr++ ) { if( mPhyTimers[tmr].callback == NULL ) { mPhyTimers[tmr] = *pEvent; break; } } arm_exit_critical(); if( tmr >= gMaxPhyTimers_c ) return gInvalidTimerId_c; /* Program the next event */ if((NULL == pNextEvent) || (NULL != pNextEvent && mPhyTimers[tmr].timestamp < pNextEvent->timestamp)) { PhyTime_Maintenance(); } return tmr; } /*! ********************************************************************************* * \brief Cancel an event * * \param[in] timerId the Id of the timer * * \return phyTimeStatus_t * ********************************************************************************** */ phyTimeStatus_t PhyTime_CancelEvent( phyTimeTimerId_t timerId ) { if( (timerId == 0) || (timerId >= gMaxPhyTimers_c) || (NULL == mPhyTimers[timerId].callback) ) { return gPhyTimeNotFound_c; } arm_enter_critical(); if( pNextEvent == &mPhyTimers[timerId] ) pNextEvent = NULL; mPhyTimers[timerId].callback = NULL; arm_exit_critical(); return gPhyTimeOk_c; } /*! ********************************************************************************* * \brief Cancel all event with the specified paameter * * \param[in] param event parameter * * \return phyTimeStatus_t * ********************************************************************************** */ phyTimeStatus_t PhyTime_CancelEventsWithParam ( uint32_t param ) { uint32_t i; phyTimeStatus_t status = gPhyTimeNotFound_c; arm_enter_critical(); for( i=1; i<gMaxPhyTimers_c; i++ ) { if( mPhyTimers[i].callback && (param == mPhyTimers[i].parameter) ) { status = gPhyTimeOk_c; mPhyTimers[i].callback = NULL; if( pNextEvent == &mPhyTimers[i] ) pNextEvent = NULL; } } arm_exit_critical(); return status; } /*! ********************************************************************************* * \brief Run the callback for the recently expired event * ********************************************************************************** */ void PhyTime_RunCallback( void ) { uint32_t param; phyTimeCallback_t cb; if( pNextEvent ) { arm_enter_critical(); param = pNextEvent->parameter; cb = pNextEvent->callback; pNextEvent->callback = NULL; pNextEvent = NULL; arm_exit_critical(); cb(param); } } /*! ********************************************************************************* * \brief Expire events too close to be scheduled. * Program the next event * ********************************************************************************** */ void PhyTime_Maintenance( void ) { phyTimeTimestamp_t currentTime; phyTimeEvent_t *pEv; PhyTimeDisableWaitTimeout(); while(1) { arm_enter_critical(); pEv = PhyTime_GetNextEvent(); currentTime = PhyTime_GetTimestamp(); /* Program next event if exists */ if( pEv ) { pNextEvent = pEv; if( pEv->timestamp > (currentTime + gPhyTimeMinSetupTime_c) ) { PhyTimeSetWaitTimeout( (uint32_t*)&pEv->timestamp ); pEv = NULL; } } arm_exit_critical(); if( !pEv ) break; PhyTime_RunCallback(); } } /*! ********************************************************************************* * \brief Timer Overflow callback * * \param[in] param * ********************************************************************************** */ static void PhyTime_OverflowCB( uint32_t param ) { (void)param; /* Reprogram the next overflow callback */ mPhyTimers[0].callback = PhyTime_OverflowCB; mPhyTimers[0].timestamp = (gPhyTimerOverflow+1) << 24; } /*! ********************************************************************************* * \brief Search for the next event to be scheduled * * \return phyTimeEvent_t pointer to the next event to be scheduled * ********************************************************************************** */ static phyTimeEvent_t* PhyTime_GetNextEvent( void ) { phyTimeEvent_t *pEv = NULL; uint32_t i; /* Search for the next event to be serviced */ for( i=0; i<gMaxPhyTimers_c; i++ ) { if( NULL != mPhyTimers[i].callback ) { if( NULL == pEv ) { pEv = &mPhyTimers[i]; } /* Check which event expires first */ else if( mPhyTimers[i].timestamp < pEv->timestamp ) { pEv = &mPhyTimers[i]; } } } return pEv; }