SX1276Lib updated in order to be RTOS aware
Fork of SX1276Lib by
Revision 21:2e496deb7858, committed 2015-11-26
- Comitter:
- mluis
- Date:
- Thu Nov 26 10:39:03 2015 +0000
- Parent:
- 20:e05596ba4166
- Child:
- 22:7f3aab69cca9
- Commit message:
- Made radio driver API compatible with GitHub radio driver
Changed in this revision
--- a/enums/enums.h Tue Oct 20 12:58:58 2015 +0000 +++ b/enums/enums.h Thu Nov 26 10:39:03 2015 +0000 @@ -16,46 +16,35 @@ #define __ENUMS_H__ /*! - * State of the radio: - * [IDLE, - * RX_RUNNING, RX_TIMEOUT, RX_ERROR, - * TX_RUNNING, TX_TIMEOUT, - CAD] + * Radio driver internal state machine states definition */ -enum RadioState +typedef enum RadioState { - LOWPOWER = 0, - IDLE, - - RX, - RX_TIMEOUT, - RX_ERROR, - - TX, - TX_TIMEOUT, - - CAD, - CAD_DONE -}; + RF_IDLE = 0, + RF_RX_RUNNING, + RF_TX_RUNNING, + RF_CAD, +}RadioState_t; /*! * Type of the modem. [LORA / FSK] */ -enum ModemType +typedef enum ModemType { MODEM_FSK = 0, MODEM_LORA -}; +}RadioModems_t; /*! - * Type of the supported board. [SX1276MB1MAS / SX1276MB1LAS] + * Type of the supported board. [SX1276MB1MAS / SX1276MB1LAS] */ -enum BoardType +typedef enum BoardType { SX1276MB1MAS = 0, SX1276MB1LAS, UNKNOWN -}; +}BoardType_t; + /*! * Radio FSK modem parameters */
--- a/radio/radio.cpp Tue Oct 20 12:58:58 2015 +0000 +++ b/radio/radio.cpp Thu Nov 26 10:39:03 2015 +0000 @@ -14,15 +14,7 @@ */ #include "radio.h" -Radio::Radio( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), - void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool channelActivityDetected ) ) +Radio::Radio( RadioEvents_t *events ) { - this->txDone = txDone; - this->txTimeout = txTimeout; - this->rxDone = rxDone; - this->rxTimeout = rxTimeout; - this->rxError = rxError; - this->fhssChangeChannel = fhssChangeChannel; - this->cadDone = cadDone; + this->RadioEvents = events; } -
--- a/radio/radio.h Tue Oct 20 12:58:58 2015 +0000 +++ b/radio/radio.h Thu Nov 26 10:39:03 2015 +0000 @@ -20,26 +20,18 @@ #include "./enums/enums.h" /*! - * Interface for the radios, contains the main functions that a radio needs, and 5 callback functions + * @brief Radio driver callback functions */ -class Radio +typedef struct { -protected: - - //------------------------------------------------------------------------- - // Callback functions pointers - //------------------------------------------------------------------------- - /*! * @brief Tx Done callback prototype. */ - void ( *txDone )( ); - + void ( *TxDone )( void ); /*! * @brief Tx Timeout callback prototype. */ - void ( *txTimeout ) ( ); - + void ( *TxTimeout )( void ); /*! * @brief Rx Done callback prototype. * @@ -50,46 +42,48 @@ * FSK : N/A ( set to 0 ) * LoRa: SNR value in dB */ - void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); - + void ( *RxDone )( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ); /*! * @brief Rx Timeout callback prototype. */ - void ( *rxTimeout ) ( ); - + void ( *RxTimeout )( void ); /*! * @brief Rx Error callback prototype. */ - void ( *rxError ) ( ); - + void ( *RxError )( void ); /*! * \brief FHSS Change Channel callback prototype. * - * \param [IN] CurrentChannel Index number of the current channel + * \param [IN] currentChannel Index number of the current channel */ - void ( *fhssChangeChannel )( uint8_t CurrentChannel ); + void ( *FhssChangeChannel )( uint8_t currentChannel ); /*! * @brief CAD Done callback prototype. * - * @param [IN] ChannelDetected Channel Activity detected during the CAD + * @param [IN] channelDetected Channel Activity detected during the CAD */ - void ( *cadDone ) ( bool channelActivityDetected ); - + void ( *CadDone ) ( bool channelActivityDetected ); +}RadioEvents_t; + +/*! + * Interface for the radios, contains the main functions that a radio needs, and 5 callback functions + */ +class Radio +{ +protected: + RadioEvents_t* RadioEvents; + public: //------------------------------------------------------------------------- // Constructor //------------------------------------------------------------------------- /*! * @brief Constructor of the radio object, the parameters are the callback functions described in the header. - * @param [IN] txDone - * @param [IN] txTimeout - * @param [IN] rxDone - * @param [IN] rxTimeout - * @param [IN] rxError + * + * @param [IN] events Structure containing the driver callback functions */ - Radio( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), - void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool channelActivityDetected ) ); + Radio( RadioEvents_t *events ); virtual ~Radio( ) {}; //------------------------------------------------------------------------- @@ -97,16 +91,23 @@ //------------------------------------------------------------------------- /*! - * Return current radio status + * @brief Initializes the radio + * + * @param [IN] events Structure containing the driver callback functions + */ + virtual void Init( RadioEvents_t *events ) = 0; + + /*! + * @brief Return current radio status * * @param status Radio status.[RF_IDLE, RF_RX_RUNNING, RF_TX_RUNNING] */ virtual RadioState GetStatus( void ) = 0; /*! - * \brief Configures the radio with the given modem + * @brief Configures the radio with the given modem * - * \param [IN] modem Modem to be used [0: FSK, 1: LoRa] + * @param [IN] modem Modem to be used [0: FSK, 1: LoRa] */ virtual void SetModem( ModemType modem ) = 0; @@ -343,4 +344,3 @@ }; #endif // __RADIO_H__ -
--- a/sx1276/sx1276-hal.cpp Tue Oct 20 12:58:58 2015 +0000 +++ b/sx1276/sx1276-hal.cpp Thu Nov 26 10:39:03 2015 +0000 @@ -33,12 +33,11 @@ { MODEM_LORA, REG_LR_PAYLOADMAXLENGTH, 0x40 }, }; -SX1276MB1xAS::SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), - void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool ChannelActivityDetected ), +SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events, PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset, PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5, PinName antSwitch ) - : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5), + : SX1276( events, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5 ), antSwitch( antSwitch ), #if( defined ( TARGET_NUCLEO_L152RE ) ) fake( D8 ) @@ -46,6 +45,8 @@ fake( A3 ) #endif { + this->RadioEvents = events; + Reset( ); RxChainCalibration( ); @@ -60,25 +61,26 @@ SetModem( MODEM_FSK ); - this->settings.State = IDLE ; + this->settings.State = RF_IDLE ; } -SX1276MB1xAS::SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), - void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool ChannelActivityDetected ) ) +SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events ) #if defined ( TARGET_NUCLEO_L152RE ) - : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, D11, D12, D13, D10, A0, D2, D3, D4, D5, A3, D9 ), // For NUCLEO L152RE dio4 is on port A3 + : SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, A3, D9 ), // For NUCLEO L152RE dio4 is on port A3 antSwitch( A4 ), fake( D8 ) #elif defined( TARGET_LPC11U6X ) - : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ), + : SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ), antSwitch( P0_23 ), fake( A3 ) #else - : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ), + : SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ), antSwitch( A4 ), fake( A3 ) #endif { + this->RadioEvents = events; + Reset( ); boardConnected = UNKNOWN; @@ -96,7 +98,7 @@ SetModem( MODEM_FSK ); - this->settings.State = IDLE ; + this->settings.State = RF_IDLE ; } //-------------------------------------------------------------------------
--- a/sx1276/sx1276-hal.h Tue Oct 20 12:58:58 2015 +0000 +++ b/sx1276/sx1276-hal.h Thu Nov 26 10:39:03 2015 +0000 @@ -33,13 +33,11 @@ static const RadioRegisters_t RadioRegsInit[]; public: - SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), - void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool ChannelActivityDetected ), + SX1276MB1xAS( RadioEvents_t *events, PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset, PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5, PinName antSwitch ); - SX1276MB1xAS( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), - void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool ChannelActivityDetected ) ); + SX1276MB1xAS( RadioEvents_t *events ); virtual ~SX1276MB1xAS( ) { }; protected:
--- a/sx1276/sx1276.cpp Tue Oct 20 12:58:58 2015 +0000 +++ b/sx1276/sx1276.cpp Thu Nov 26 10:39:03 2015 +0000 @@ -41,11 +41,10 @@ }; -SX1276::SX1276( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), - void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool channelActivityDetected ), +SX1276::SX1276( RadioEvents_t *events, PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset, PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5 ) - : Radio( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone ), + : Radio( events ), spi( mosi, miso, sclk ), nss( nss ), reset( reset ), @@ -57,6 +56,8 @@ this->rxBuffer = new uint8_t[RX_BUFFER_SIZE]; previousOpMode = RF_OPMODE_STANDBY; + this->RadioEvents = events; + this->dioIrq = new DioIrqHandler[6]; this->dioIrq[0] = &SX1276::OnDio0Irq; @@ -66,7 +67,7 @@ this->dioIrq[4] = &SX1276::OnDio4Irq; this->dioIrq[5] = NULL; - this->settings.State = IDLE; + this->settings.State = RF_IDLE; } SX1276::~SX1276( ) @@ -75,6 +76,11 @@ delete this->dioIrq; } +void SX1276::Init( RadioEvents_t *events ) +{ + this->RadioEvents = events; +} + void SX1276::RxChainCalibration( void ) { uint8_t regPaConfigInitVal; @@ -619,7 +625,7 @@ { uint32_t txTimeout = 0; - this->settings.State = IDLE; + this->settings.State = RF_IDLE; switch( this->settings.Modem ) { @@ -787,7 +793,7 @@ memset( rxBuffer, 0, ( size_t )RX_BUFFER_SIZE ); - this->settings.State = RX; + this->settings.State = RF_RX_RUNNING; if( timeout != 0 ) { rxTimeoutTimer.attach_us( this, &SX1276::OnTimeoutIrq, timeout ); @@ -876,7 +882,7 @@ break; } - this->settings.State = TX; + this->settings.State = RF_TX_RUNNING; txTimeoutTimer.attach_us( this, &SX1276::OnTimeoutIrq, timeout ); SetOpMode( RF_OPMODE_TRANSMITTER ); } @@ -905,7 +911,7 @@ // DIO3=CADDone Write( REG_DIOMAPPING1, ( Read( REG_DIOMAPPING1 ) & RFLR_DIOMAPPING1_DIO0_MASK ) | RFLR_DIOMAPPING1_DIO0_00 ); - this->settings.State = CAD; + this->settings.State = RF_CAD; SetOpMode( RFLR_OPMODE_CAD ); } break; @@ -1014,7 +1020,7 @@ { switch( this->settings.State ) { - case RX: + case RF_RX_RUNNING: if( this->settings.Modem == MODEM_FSK ) { this->settings.FskPacketHandler.PreambleDetected = false; @@ -1035,20 +1041,20 @@ } else { - this->settings.State = IDLE; + this->settings.State = RF_IDLE; rxTimeoutSyncWord.detach( ); } } - if( ( rxTimeout != NULL ) ) + if( ( this->RadioEvents->RxTimeout != NULL ) ) { - rxTimeout( ); + this->RadioEvents->RxTimeout( ); } break; - case TX: - this->settings.State = IDLE; - if( ( txTimeout != NULL ) ) + case RF_TX_RUNNING: + this->settings.State = RF_IDLE; + if( ( this->RadioEvents->TxTimeout != NULL ) ) { - txTimeout( ); + this->RadioEvents->TxTimeout( ); } break; default: @@ -1062,7 +1068,7 @@ switch( this->settings.State ) { - case RX: + case RF_RX_RUNNING: //TimerStop( &RxTimeoutTimer ); // RxDone interrupt switch( this->settings.Modem ) @@ -1081,7 +1087,7 @@ if( this->settings.Fsk.RxContinuous == false ) { - this->settings.State = IDLE; + this->settings.State = RF_IDLE; rxTimeoutSyncWord.attach_us( this, &SX1276::OnTimeoutIrq, ( 8.0 * ( this->settings.Fsk.PreambleLen + ( ( Read( REG_SYNCCONFIG ) & ~RF_SYNCCONFIG_SYNCSIZE_MASK ) + @@ -1095,9 +1101,9 @@ } rxTimeoutTimer.detach( ); - if( ( rxError != NULL ) ) + if( ( this->RadioEvents->RxError != NULL ) ) { - rxError( ); + this->RadioEvents->RxError( ); } this->settings.FskPacketHandler.PreambleDetected = false; this->settings.FskPacketHandler.SyncWordDetected = false; @@ -1129,7 +1135,7 @@ if( this->settings.Fsk.RxContinuous == false ) { - this->settings.State = IDLE; + this->settings.State = RF_IDLE; rxTimeoutSyncWord.attach_us( this, &SX1276::OnTimeoutIrq, ( 8.0 * ( this->settings.Fsk.PreambleLen + ( ( Read( REG_SYNCCONFIG ) & ~RF_SYNCCONFIG_SYNCSIZE_MASK ) + @@ -1143,9 +1149,9 @@ } rxTimeoutTimer.detach( ); - if( (rxDone != NULL ) ) + if( ( this->RadioEvents->RxDone != NULL ) ) { - rxDone( rxBuffer, this->settings.FskPacketHandler.Size, this->settings.FskPacketHandler.RssiValue, 0 ); + this->RadioEvents->RxDone( rxBuffer, this->settings.FskPacketHandler.Size, this->settings.FskPacketHandler.RssiValue, 0 ); } this->settings.FskPacketHandler.PreambleDetected = false; this->settings.FskPacketHandler.SyncWordDetected = false; @@ -1167,13 +1173,13 @@ if( this->settings.LoRa.RxContinuous == false ) { - this->settings.State = IDLE; + this->settings.State = RF_IDLE; } rxTimeoutTimer.detach( ); - if( ( rxError != NULL ) ) + if( ( this->RadioEvents->RxError != NULL ) ) { - rxError( ); + this->RadioEvents->RxError( ); } break; } @@ -1222,13 +1228,13 @@ if( this->settings.LoRa.RxContinuous == false ) { - this->settings.State = IDLE; + this->settings.State = RF_IDLE; } rxTimeoutTimer.detach( ); - if( ( rxDone != NULL ) ) + if( ( this->RadioEvents->RxDone != NULL ) ) { - rxDone( rxBuffer, this->settings.LoRaPacketHandler.Size, this->settings.LoRaPacketHandler.RssiValue, this->settings.LoRaPacketHandler.SnrValue ); + this->RadioEvents->RxDone( rxBuffer, this->settings.LoRaPacketHandler.Size, this->settings.LoRaPacketHandler.RssiValue, this->settings.LoRaPacketHandler.SnrValue ); } } break; @@ -1236,7 +1242,7 @@ break; } break; - case TX: + case RF_TX_RUNNING: txTimeoutTimer.detach( ); // TxDone interrupt switch( this->settings.Modem ) @@ -1247,10 +1253,10 @@ // Intentional fall through case MODEM_FSK: default: - this->settings.State = IDLE; - if( ( txDone != NULL ) ) + this->settings.State = RF_IDLE; + if( ( this->RadioEvents->TxDone != NULL ) ) { - txDone( ); + this->RadioEvents->TxDone( ); } break; } @@ -1264,7 +1270,7 @@ { switch( this->settings.State ) { - case RX: + case RF_RX_RUNNING: switch( this->settings.Modem ) { case MODEM_FSK: @@ -1296,17 +1302,17 @@ case MODEM_LORA: // Sync time out rxTimeoutTimer.detach( ); - this->settings.State = IDLE; - if( ( rxTimeout != NULL ) ) + this->settings.State = RF_IDLE; + if( ( this->RadioEvents->RxTimeout != NULL ) ) { - rxTimeout( ); + this->RadioEvents->RxTimeout( ); } break; default: break; } break; - case TX: + case RF_TX_RUNNING: switch( this->settings.Modem ) { case MODEM_FSK: @@ -1338,7 +1344,7 @@ { switch( this->settings.State ) { - case RX: + case RF_RX_RUNNING: switch( this->settings.Modem ) { case MODEM_FSK: @@ -1362,9 +1368,9 @@ // Clear Irq Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL ); - if( ( fhssChangeChannel != NULL ) ) + if( ( this->RadioEvents->FhssChangeChannel != NULL ) ) { - fhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) ); + this->RadioEvents->FhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) ); } } break; @@ -1372,7 +1378,7 @@ break; } break; - case TX: + case RF_TX_RUNNING: switch( this->settings.Modem ) { case MODEM_FSK: @@ -1383,9 +1389,9 @@ // Clear Irq Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_FHSSCHANGEDCHANNEL ); - if( ( fhssChangeChannel != NULL ) ) + if( ( this->RadioEvents->FhssChangeChannel != NULL ) ) { - fhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) ); + this->RadioEvents->FhssChangeChannel( ( Read( REG_LR_HOPCHANNEL ) & RFLR_HOPCHANNEL_CHANNEL_MASK ) ); } } break; @@ -1409,18 +1415,18 @@ { // Clear Irq Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_CADDETECTED_MASK | RFLR_IRQFLAGS_CADDONE); - if( ( cadDone != NULL ) ) + if( ( this->RadioEvents->CadDone != NULL ) ) { - cadDone( true ); + this->RadioEvents->CadDone( true ); } } else { // Clear Irq Write( REG_LR_IRQFLAGS, RFLR_IRQFLAGS_CADDONE ); - if( ( cadDone != NULL ) ) + if( ( this->RadioEvents->CadDone != NULL ) ) { - cadDone( false ); + this->RadioEvents->CadDone( false ); } } break;
--- a/sx1276/sx1276.h Tue Oct 20 12:58:58 2015 +0000 +++ b/sx1276/sx1276.h Thu Nov 26 10:39:03 2015 +0000 @@ -110,21 +110,25 @@ void RxChainCalibration( void ); public: - SX1276( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), - void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool channelActivityDetected ), + SX1276( RadioEvents_t *events, PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset, PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5 ); - SX1276( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), - void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool channelActivityDetected ) ); + SX1276( RadioEvents_t *events ); virtual ~SX1276( ); //------------------------------------------------------------------------- // Redefined Radio functions //------------------------------------------------------------------------- /*! + * @brief Initializes the radio + * + * @param [IN] events Structure containing the driver callback functions + */ + virtual void Init( RadioEvents_t *events ); + /*! * Return current radio status * - * @param status Radio status. [IDLE, RX_RUNNING, TX_RUNNING] + * @param status Radio status. [RF_IDLE, RX_RUNNING, TX_RUNNING] */ virtual RadioState GetStatus( void ); @@ -497,4 +501,3 @@ }; #endif //__SX1276_H__ -