Sille Van Landschoot / SX1272Lib

Fork of SX1272Lib by Semtech

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sx1272-hal.cpp Source File

sx1272-hal.cpp

00001 /*
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C) 2015 Semtech
00008 
00009 Description: -
00010 
00011 License: Revised BSD License, see LICENSE.TXT file include in the project
00012 
00013 Maintainers: Miguel Luis, Gregory Cristian and Nicolas Huguenin
00014 */
00015 #include "sx1272-hal.h"
00016 
00017 const RadioRegisters_t  SX1272MB2xAS::RadioRegsInit[] = RADIO_INIT_REGISTERS_VALUE;
00018 
00019 SX1272MB2xAS::SX1272MB2xAS( RadioEvents_t *events,
00020                             PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
00021                             PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5
00022 #if defined ( TARGET_MOTE_L152RC )
00023                            , PinName rfSwitchCntr1, PinName rfSwitchCntr2 )
00024 #elif defined ( TARGET_MTS_MDOT_F411RE )
00025                             ,PinName txctl, PinName rxctl )
00026 #elif defined ( TARGET_MBED_LPC1768 )
00027                             ) // nothing extra needed
00028 #else
00029                             ,PinName antSwitch )
00030 #endif
00031                             : SX1272 ( events, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5 )
00032 #if defined ( TARGET_MOTE_L152RC )
00033                             ,RfSwitchCntr1( rfSwitchCntr1 ),
00034                             RfSwitchCntr2( rfSwitchCntr2 ),
00035                             PwrAmpCntr( PD_2 )               
00036 #elif defined ( TARGET_MTS_MDOT_F411RE )
00037                             ,TxCtl ( txctl ),
00038                             RxCtl ( rxctl )
00039 #elif defined ( TARGET_MBED_LPC1768 )
00040                             // nothing extra needed
00041 #else
00042                             ,AntSwitch( antSwitch ),
00043                         #if( defined ( TARGET_NUCLEO_L152RE ) )
00044                             Fake( D8 )
00045                         #else
00046                             Fake( A3 )
00047                         #endif
00048 #endif
00049 {
00050     this->RadioEvents = events;
00051 
00052     Reset( );
00053 
00054     IoInit( );
00055 
00056     SetOpMode( RF_OPMODE_SLEEP );
00057 
00058     IoIrqInit( dioIrq );
00059 
00060     RadioRegistersInit( );
00061 
00062     SetModem( MODEM_FSK );
00063 
00064     this->settings.State = RF_IDLE ;
00065 }
00066 
00067 SX1272MB2xAS::SX1272MB2xAS( RadioEvents_t *events )
00068                         #if defined ( TARGET_NUCLEO_L152RE )
00069                         :   SX1272 ( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, A3, D9 ), // For NUCLEO L152RE dio4 is on port A3
00070                             AntSwitch( A4 ),
00071                             Fake( D8 )
00072                         #elif defined ( TARGET_MOTE_L152RC )
00073                         :   SX1272 ( events, PB_15, PB_14, PB_13, PB_12, PC_2, PC_6, PC_10, PC_11, PC_8, PC_9, PC_12 ),
00074                             RfSwitchCntr1( PC_4 ),
00075                             RfSwitchCntr2( PC_13 ),
00076                             PwrAmpCntr( PD_2 )
00077                         #elif defined ( TARGET_MTS_MDOT_F411RE )
00078                         :   SX1272 ( events, LORA_MOSI, LORA_MISO, LORA_SCK, LORA_NSS, LORA_RESET, LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5 ),
00079                             TxCtl( LORA_TXCTL ),
00080                             RxCtl( LORA_RXCTL )
00081                         #elif defined ( TARGET_MBED_LPC1768 )
00082                         :   SX1272 ( events, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15 )
00083                         #else
00084                         :   SX1272 ( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
00085                             AntSwitch( A4 ), 
00086                             Fake( A3 )
00087                         #endif
00088 {
00089     this->RadioEvents = events;
00090 
00091     Reset( );
00092 
00093     boardConnected = UNKNOWN;
00094 
00095     DetectBoardType( );
00096 
00097     IoInit( );
00098 
00099     SetOpMode( RF_OPMODE_SLEEP );
00100     IoIrqInit( dioIrq );
00101 
00102     RadioRegistersInit( );
00103 
00104     SetModem( MODEM_FSK );
00105 
00106     this->settings.State = RF_IDLE ;
00107 }
00108 
00109 //-------------------------------------------------------------------------
00110 //                      Board relative functions
00111 //-------------------------------------------------------------------------
00112 uint8_t SX1272MB2xAS::DetectBoardType( void )
00113 {
00114     if( boardConnected == UNKNOWN )
00115     {
00116 #if defined ( TARGET_MOTE_L152RC )
00117         boardConnected = NA_MOTE_72;
00118 #elif defined ( TARGET_MTS_MDOT_F411RE )
00119         boardConnected = MDOT_F411RE;
00120 #elif defined ( TARGET_MBED_LPC1768 )
00121         boardConnected = LPC1768_RFM95;
00122 #else
00123         this->AntSwitch.input( );
00124         wait_ms( 1 );
00125         if( this->AntSwitch == 1 )
00126         {
00127             boardConnected = SX1272MB1DCS;
00128         }
00129         else
00130         {
00131             boardConnected = SX1272MB2XAS;
00132         }
00133         this->AntSwitch.output( );
00134         wait_ms( 1 );
00135 #endif
00136     }
00137     return ( boardConnected );
00138 }
00139 
00140 void SX1272MB2xAS::IoInit( void )
00141 {
00142     AntSwInit( );
00143     SpiInit( );
00144 }
00145 
00146 void SX1272MB2xAS::RadioRegistersInit( )
00147 {
00148     uint8_t i = 0;
00149     for( i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t  ); i++ )
00150     {
00151         SetModem( RadioRegsInit[i].Modem );
00152         Write( RadioRegsInit[i].Addr, RadioRegsInit[i].Value );
00153     }    
00154 }
00155 
00156 void SX1272MB2xAS::SpiInit( void )
00157 {
00158     nss = 1;    
00159     spi .format( 8,0 );   
00160     uint32_t frequencyToSet = 8000000;
00161     #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_MOTE_L152RC ) ||  defined ( TARGET_LPC11U6X ) || defined ( TARGET_MTS_MDOT_F411RE ) || defined ( TARGET_MBED_LPC1768 ) )
00162         spi .frequency( frequencyToSet );
00163     #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
00164         spi .frequency( frequencyToSet * 2 );
00165     #else
00166         #warning "Check the board's SPI frequency"
00167     #endif
00168     wait(0.1); 
00169 }
00170 
00171 void SX1272MB2xAS::IoIrqInit( DioIrqHandler *irqHandlers )
00172 {
00173 #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_MOTE_L152RC ) ||  defined ( TARGET_LPC11U6X ) )
00174     dio0.mode( PullDown );
00175     dio1.mode( PullDown );
00176     dio2.mode( PullDown );
00177     dio3.mode( PullDown );
00178     dio4.mode( PullDown );
00179 #endif
00180     dio0.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[0] ) );
00181     dio1.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[1] ) );
00182     dio2.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[2] ) );
00183     dio3.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[3] ) );
00184     dio4.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[4] ) );
00185 }
00186 
00187 void SX1272MB2xAS::IoDeInit( void )
00188 {
00189     //nothing
00190 }
00191 
00192 uint8_t SX1272MB2xAS::GetPaSelect( uint32_t channel )
00193 {
00194     if( boardConnected == SX1272MB1DCS || boardConnected == MDOT_F411RE || boardConnected == LPC1768_RFM95 )
00195     {
00196         return RF_PACONFIG_PASELECT_PABOOST;
00197     }
00198     else
00199     {
00200         return RF_PACONFIG_PASELECT_RFO;
00201     }
00202 }
00203 
00204 void SX1272MB2xAS::SetAntSwLowPower( bool status )
00205 {
00206     if( isRadioActive != status )
00207     {
00208         isRadioActive = status;
00209     
00210         if( status == false )
00211         {
00212             AntSwInit( );
00213         }
00214         else
00215         {
00216             AntSwDeInit( );
00217         }
00218     }
00219 }
00220 
00221 void SX1272MB2xAS::AntSwInit( void )
00222 {
00223 #if defined ( TARGET_MOTE_L152RC )
00224     this->RfSwitchCntr1  = 0;
00225     this->RfSwitchCntr2 = 0;
00226     this->PwrAmpCntr = 0;
00227 #elif defined ( TARGET_MTS_MDOT_F411RE )
00228     this->TxCtl = 0;
00229     this->RxCtl = 0;
00230 #elif defined ( TARGET_MBED_LPC1768 )
00231     // do nothing
00232 #else
00233     this->AntSwitch = 0;
00234 #endif
00235 }
00236 
00237 void SX1272MB2xAS::AntSwDeInit( void )
00238 {
00239 #if defined ( TARGET_MOTE_L152RC )
00240     this->RfSwitchCntr1  = 0;
00241     this->RfSwitchCntr2 = 0;
00242     this->PwrAmpCntr = 0;
00243 #elif defined ( TARGET_MTS_MDOT_F411RE )
00244     this->TxCtl = 0;
00245     this->RxCtl = 0;    
00246 #elif defined ( TARGET_MBED_LPC1768 )
00247     // do nothing
00248 #else
00249     this->AntSwitch = 0;
00250 #endif
00251 }
00252 
00253 void SX1272MB2xAS::SetAntSw( uint8_t rxTx )
00254 {
00255 #if defined ( TARGET_MOTE_L152RC )
00256     switch( this->currentOpMode )
00257     {
00258     case RFLR_OPMODE_TRANSMITTER:
00259         if( ( Read( REG_PACONFIG ) & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
00260         {
00261             this->RfSwitchCntr1  = 1;
00262             this->RfSwitchCntr2 = 0;
00263         }
00264         else
00265         {
00266             this->RfSwitchCntr1  = 0;
00267             this->RfSwitchCntr2 = 1;
00268         }
00269         break;
00270     case RFLR_OPMODE_RECEIVER:
00271     case RFLR_OPMODE_RECEIVER_SINGLE:
00272     case RFLR_OPMODE_CAD:
00273         this->RfSwitchCntr1  = 1;
00274         this->RfSwitchCntr2 = 1;
00275         break;
00276     default:
00277         this->RfSwitchCntr1  = 0;
00278         this->RfSwitchCntr2 = 0;
00279         this->PwrAmpCntr = 0;
00280         break;
00281     }
00282 #elif defined ( TARGET_MTS_MDOT_F411RE )
00283     /* SKY13350 */
00284     this->rxTx = rxTx ;
00285 
00286     // 1: Tx, 0: Rx
00287     if( rxTx != 0 )
00288     {
00289         this->TxCtl = 1;
00290         this->RxCtl = 0;   
00291     }
00292     else
00293     {
00294         this->TxCtl = 0;
00295         this->RxCtl = 1;   
00296     }    
00297 #elif defined ( TARGET_MBED_LPC1768 )
00298     // do nothing
00299 #else
00300     this->rxTx = rxTx ;
00301 
00302     // 1: Tx, 0: Rx
00303     if( rxTx != 0 )
00304     {
00305         this->AntSwitch = 1;
00306     }
00307     else
00308     {
00309         this->AntSwitch = 0;
00310     }
00311 #endif
00312 }
00313 
00314 bool SX1272MB2xAS::CheckRfFrequency( uint32_t frequency )
00315 {
00316     //TODO: Implement check, currently all frequencies are supported
00317     return true;
00318 }
00319 
00320 
00321 void SX1272MB2xAS::Reset( void )
00322 {
00323     reset.output();
00324     reset = 0;
00325     wait_ms( 1 );
00326     reset.input();
00327     wait_ms( 6 );
00328 }
00329     
00330 void SX1272MB2xAS::Write( uint8_t addr, uint8_t data )
00331 {
00332     Write( addr, &data, 1 );
00333 }
00334 
00335 uint8_t SX1272MB2xAS::Read( uint8_t addr )
00336 {
00337     uint8_t data;
00338     Read( addr, &data, 1 );
00339     return data;
00340 }
00341 
00342 void SX1272MB2xAS::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
00343 {
00344     uint8_t i;
00345 
00346     nss = 0;
00347     spi .write( addr | 0x80 );
00348     for( i = 0; i < size; i++ )
00349     {
00350         spi .write( buffer[i] );
00351     }
00352     nss = 1;
00353 }
00354 
00355 void SX1272MB2xAS::Read( uint8_t addr, uint8_t *buffer, uint8_t size )
00356 {
00357     uint8_t i;
00358 
00359     nss = 0;
00360     spi .write( addr & 0x7F );
00361     for( i = 0; i < size; i++ )
00362     {
00363         buffer[i] = spi .write( 0 );
00364     }
00365     nss = 1;
00366 }
00367 
00368 void SX1272MB2xAS::WriteFifo( uint8_t *buffer, uint8_t size )
00369 {
00370     Write( 0, buffer, size );
00371 }
00372 
00373 void SX1272MB2xAS::ReadFifo( uint8_t *buffer, uint8_t size )
00374 {
00375     Read( 0, buffer, size );
00376 }