Driver for the SX1276 RF Transceiver
Dependents: LoRa_PIR LoRaWAN-lmic-app SX1276PingPong LoRaWAN-lmic-app ... more
Diff: sx1276/sx1276-hal.cpp
- Revision:
- 26:d09a8ef807e2
- Parent:
- 22:7f3aab69cca9
diff -r 7f3aab69cca9 -r d09a8ef807e2 sx1276/sx1276-hal.cpp --- a/sx1276/sx1276-hal.cpp Thu Nov 26 16:55:15 2015 +0000 +++ b/sx1276/sx1276-hal.cpp Mon Apr 24 09:26:23 2017 +0000 @@ -21,25 +21,25 @@ PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5, PinName antSwitch ) : SX1276( events, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5 ), - antSwitch( antSwitch ), + AntSwitch( antSwitch ), #if( defined ( TARGET_NUCLEO_L152RE ) ) - fake( D8 ) + Fake( D8 ) #else - fake( A3 ) + Fake( A3 ) #endif { this->RadioEvents = events; Reset( ); - + RxChainCalibration( ); - + IoInit( ); - + SetOpMode( RF_OPMODE_SLEEP ); - + IoIrqInit( dioIrq ); - + RadioRegistersInit( ); SetModem( MODEM_FSK ); @@ -47,36 +47,36 @@ this->settings.State = RF_IDLE ; } -SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events ) +SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events ) #if defined ( TARGET_NUCLEO_L152RE ) : 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 ) + AntSwitch( A4 ), + Fake( D8 ) #elif defined( TARGET_LPC11U6X ) : SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ), - antSwitch( P0_23 ), - fake( A3 ) + AntSwitch( P0_23 ), + Fake( A3 ) #else : SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ), - antSwitch( A4 ), - fake( A3 ) + AntSwitch( A4 ), + Fake( A3 ) #endif { this->RadioEvents = events; Reset( ); - + boardConnected = UNKNOWN; - + DetectBoardType( ); - + RxChainCalibration( ); - + IoInit( ); - + SetOpMode( RF_OPMODE_SLEEP ); IoIrqInit( dioIrq ); - + RadioRegistersInit( ); SetModem( MODEM_FSK ); @@ -91,9 +91,9 @@ { if( boardConnected == UNKNOWN ) { - antSwitch.input( ); + this->AntSwitch.input( ); wait_ms( 1 ); - if( antSwitch == 1 ) + if( this->AntSwitch == 1 ) { boardConnected = SX1276MB1LAS; } @@ -101,7 +101,7 @@ { boardConnected = SX1276MB1MAS; } - antSwitch.output( ); + this->AntSwitch.output( ); wait_ms( 1 ); } return ( boardConnected ); @@ -128,7 +128,7 @@ nss = 1; spi.format( 8,0 ); uint32_t frequencyToSet = 8000000; - #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) ) + #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) ) spi.frequency( frequencyToSet ); #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate spi.frequency( frequencyToSet * 2 ); @@ -140,18 +140,18 @@ void SX1276MB1xAS::IoIrqInit( DioIrqHandler *irqHandlers ) { -#if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) ) - dio0.mode(PullDown); - dio1.mode(PullDown); - dio2.mode(PullDown); - dio3.mode(PullDown); - dio4.mode(PullDown); +#if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) ) + dio0.mode( PullDown ); + dio1.mode( PullDown ); + dio2.mode( PullDown ); + dio3.mode( PullDown ); + dio4.mode( PullDown ); #endif - dio0.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[0] ) ); - dio1.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[1] ) ); - dio2.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[2] ) ); - dio3.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[3] ) ); - dio4.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[4] ) ); + dio0.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[0] ) ) ); + dio1.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[1] ) ) ); + dio2.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[2] ) ) ); + dio3.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[3] ) ) ); + dio4.rise( mbed::callback( this, static_cast< TriggerMB1xAS > ( irqHandlers[4] ) ) ); } void SX1276MB1xAS::IoDeInit( void ) @@ -159,6 +159,68 @@ //nothing } +void SX1276MB1xAS::SetRfTxPower( int8_t power ) +{ + uint8_t paConfig = 0; + uint8_t paDac = 0; + + paConfig = Read( REG_PACONFIG ); + paDac = Read( REG_PADAC ); + + paConfig = ( paConfig & RF_PACONFIG_PASELECT_MASK ) | GetPaSelect( this->settings.Channel ); + paConfig = ( paConfig & RF_PACONFIG_MAX_POWER_MASK ) | 0x70; + + if( ( paConfig & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST ) + { + if( power > 17 ) + { + paDac = ( paDac & RF_PADAC_20DBM_MASK ) | RF_PADAC_20DBM_ON; + } + else + { + paDac = ( paDac & RF_PADAC_20DBM_MASK ) | RF_PADAC_20DBM_OFF; + } + if( ( paDac & RF_PADAC_20DBM_ON ) == RF_PADAC_20DBM_ON ) + { + if( power < 5 ) + { + power = 5; + } + if( power > 20 ) + { + power = 20; + } + paConfig = ( paConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power - 5 ) & 0x0F ); + } + else + { + if( power < 2 ) + { + power = 2; + } + if( power > 17 ) + { + power = 17; + } + paConfig = ( paConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power - 2 ) & 0x0F ); + } + } + else + { + if( power < -1 ) + { + power = -1; + } + if( power > 14 ) + { + power = 14; + } + paConfig = ( paConfig & RF_PACONFIG_OUTPUTPOWER_MASK ) | ( uint8_t )( ( uint16_t )( power + 1 ) & 0x0F ); + } + Write( REG_PACONFIG, paConfig ); + Write( REG_PADAC, paDac ); +} + uint8_t SX1276MB1xAS::GetPaSelect( uint32_t channel ) { if( channel > RF_MID_BAND_THRESH ) @@ -197,50 +259,47 @@ void SX1276MB1xAS::AntSwInit( void ) { - antSwitch = 0; + this->AntSwitch = 0; } void SX1276MB1xAS::AntSwDeInit( void ) { - antSwitch = 0; + this->AntSwitch = 0; } -void SX1276MB1xAS::SetAntSw( uint8_t rxTx ) +void SX1276MB1xAS::SetAntSw( uint8_t opMode ) { - if( this->rxTx == rxTx ) + switch( opMode ) { - //no need to go further - return; - } - - this->rxTx = rxTx; - - if( rxTx != 0 ) - { - antSwitch = 1; - } - else - { - antSwitch = 0; + case RFLR_OPMODE_TRANSMITTER: + this->AntSwitch = 1; + break; + case RFLR_OPMODE_RECEIVER: + case RFLR_OPMODE_RECEIVER_SINGLE: + case RFLR_OPMODE_CAD: + this->AntSwitch = 0; + break; + default: + this->AntSwitch = 0; + break; } } bool SX1276MB1xAS::CheckRfFrequency( uint32_t frequency ) { - //TODO: Implement check, currently all frequencies are supported + // Implement check. Currently all frequencies are supported return true; } - void SX1276MB1xAS::Reset( void ) { - reset.output(); + reset.output( ); reset = 0; wait_ms( 1 ); - reset.input(); + reset.input( ); wait_ms( 6 ); } - + void SX1276MB1xAS::Write( uint8_t addr, uint8_t data ) { Write( addr, &data, 1 );