first draft

Dependents:   LoRaWAN-demo-72_tjm frdm_LoRa_Connect_Woodstream_Demo_tjm frdm_LoRa_Connect_Woodstream_Demo_jlc

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 #else
00027                             PinName antSwitch )
00028 #endif
00029 //                            : SX1272( events, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5 ),
00030 //                            : SX1272( events, PTD6, PTD7, PTD5, PTD4, reset, PTC2, PTB1, PTC3, PTB0, PTC4, PTC1 ),
00031                            : SX1272 ( events, PTC6, PTC7, PTC5, PTC4, reset, PTC8, PTC9, PTC10, PTC11, PTD0, PTD1 ),
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 #else
00040 //                            AntSwitch( antSwitch ),
00041 //                            AntSwitch( PTC6 ),
00042                             AntSwitch( PTC0),
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                         #else
00082 //                        :   SX1272( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
00083 //                            AntSwitch( A4 ), 
00084 //                        :   SX1272( events, PTD6, PTD7, PTD5, PTD4, A0, PTC2, PTB1, PTC3, PTB0, PTC4, PTC1 ),
00085                         :   SX1272 ( events, PTC6, PTC7, PTC5, PTC4, A0, PTC8, PTC9, PTC10, PTC11, PTD0, PTD1 ),
00086 //                            AntSwitch( PTC6 ), 
00087                             AntSwitch( PTC0 ), 
00088                             Fake( A3 )
00089                         #endif
00090 {
00091     this->RadioEvents = events;
00092 
00093     Reset( );
00094     
00095     boardConnected = UNKNOWN;
00096     
00097     DetectBoardType( );
00098     
00099     IoInit( );
00100     
00101     SetOpMode( RF_OPMODE_SLEEP );
00102     IoIrqInit( dioIrq );
00103     
00104     RadioRegistersInit( );
00105 
00106     SetModem( MODEM_FSK );
00107 
00108     this->settings.State = RF_IDLE ;
00109 }
00110 
00111 //-------------------------------------------------------------------------
00112 //                      Board relative functions
00113 //-------------------------------------------------------------------------
00114 uint8_t SX1272MB2xAS::DetectBoardType( void )
00115 {
00116     if( boardConnected == UNKNOWN )
00117     {
00118 #if defined ( TARGET_MOTE_L152RC )
00119         boardConnected = NA_MOTE_72;
00120 #elif defined ( TARGET_MTS_MDOT_F411RE )
00121         boardConnected = MDOT_F411RE;
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     uint32_t frequencyToSet = (8000000 * 2)/3;  /* 8 MHz/12 Mhz */
00162 //    #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_MOTE_L152RC ) ||  defined ( TARGET_LPC11U6X ) || defined ( TARGET_MTS_MDOT_F411RE ) )
00163         spi .frequency( frequencyToSet );
00164 //    #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
00165 //        spi.frequency( frequencyToSet * 2 );
00166 //    #else
00167 //        #warning "Check the board's SPI frequency"x
00168 //    #endif
00169     wait(0.1); 
00170 }
00171 
00172 void SX1272MB2xAS::IoIrqInit( DioIrqHandler *irqHandlers )
00173 {
00174 #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_MOTE_L152RC ) ||  defined ( TARGET_LPC11U6X ) )
00175     dio0.mode( PullDown );
00176     dio1.mode( PullDown );
00177     dio2.mode( PullDown );
00178     dio3.mode( PullDown );
00179     dio4.mode( PullDown );
00180 #endif
00181     dio0.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[0] ) );
00182     dio1.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[1] ) );
00183     dio2.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[2] ) );
00184     dio3.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[3] ) );
00185     dio4.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[4] ) );
00186 }
00187 
00188 void SX1272MB2xAS::IoDeInit( void )
00189 {
00190     //nothing
00191 }
00192 
00193 uint8_t SX1272MB2xAS::GetPaSelect( uint32_t channel )
00194 {
00195     if( boardConnected == SX1272MB1DCS || boardConnected == MDOT_F411RE )
00196     {
00197         return RF_PACONFIG_PASELECT_PABOOST;
00198     }
00199     else
00200     {
00201         return RF_PACONFIG_PASELECT_RFO;
00202     }
00203 }
00204 
00205 void SX1272MB2xAS::SetAntSwLowPower( bool status )
00206 {
00207     if( isRadioActive != status )
00208     {
00209         isRadioActive = status;
00210     
00211         if( status == false )
00212         {
00213             AntSwInit( );
00214         }
00215         else
00216         {
00217             AntSwDeInit( );
00218         }
00219     }
00220 }
00221 
00222 void SX1272MB2xAS::AntSwInit( void )
00223 {
00224 #if defined ( TARGET_MOTE_L152RC )
00225     this->RfSwitchCntr1  = 0;
00226     this->RfSwitchCntr2 = 0;
00227     this->PwrAmpCntr = 0;
00228 #elif defined ( TARGET_MTS_MDOT_F411RE )
00229     this->TxCtl = 0;
00230     this->RxCtl = 0;
00231 #else
00232     this->AntSwitch = 0;
00233 #endif
00234 }
00235 
00236 void SX1272MB2xAS::AntSwDeInit( void )
00237 {
00238 #if defined ( TARGET_MOTE_L152RC )
00239     this->RfSwitchCntr1  = 0;
00240     this->RfSwitchCntr2 = 0;
00241     this->PwrAmpCntr = 0;
00242 #elif defined ( TARGET_MTS_MDOT_F411RE )
00243     this->TxCtl = 0;
00244     this->RxCtl = 0;    
00245 #else
00246     this->AntSwitch = 0;
00247 #endif
00248 }
00249 
00250 void SX1272MB2xAS::SetAntSw( uint8_t rxTx )
00251 {
00252 #if defined ( TARGET_MOTE_L152RC )
00253     switch( this->currentOpMode )
00254     {
00255     case RFLR_OPMODE_TRANSMITTER:
00256         if( ( Read( REG_PACONFIG ) & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
00257         {
00258             this->RfSwitchCntr1  = 1;
00259             this->RfSwitchCntr2 = 0;
00260         }
00261         else
00262         {
00263             this->RfSwitchCntr1  = 0;
00264             this->RfSwitchCntr2 = 1;
00265         }
00266         break;
00267     case RFLR_OPMODE_RECEIVER:
00268     case RFLR_OPMODE_RECEIVER_SINGLE:
00269     case RFLR_OPMODE_CAD:
00270         this->RfSwitchCntr1  = 1;
00271         this->RfSwitchCntr2 = 1;
00272         break;
00273     default:
00274         this->RfSwitchCntr1  = 0;
00275         this->RfSwitchCntr2 = 0;
00276         this->PwrAmpCntr = 0;
00277         break;
00278     }
00279 #elif defined ( TARGET_MTS_MDOT_F411RE )
00280     if( this->rxTx == rxTx )
00281     {
00282         //no need to go further
00283         return;
00284     }
00285     
00286     /* SKY13350 */
00287     this->rxTx = rxTx ;
00288 
00289     // 1: Tx, 0: Rx
00290     if( rxTx != 0 )
00291     {
00292         this->TxCtl = 1;
00293         this->RxCtl = 0;   
00294     }
00295     else
00296     {
00297         this->TxCtl = 0;
00298         this->RxCtl = 1;   
00299     }    
00300 #else
00301     if( this->rxTx == rxTx )
00302     {
00303         //no need to go further
00304         return;
00305     }
00306 
00307     this->rxTx = rxTx ;
00308 
00309     // 1: Tx, 0: Rx
00310     if( rxTx != 0 )
00311     {
00312         this->AntSwitch = 1;
00313     }
00314     else
00315     {
00316         this->AntSwitch = 0;
00317     }
00318 #endif
00319 }
00320 
00321 bool SX1272MB2xAS::CheckRfFrequency( uint32_t frequency )
00322 {
00323     //TODO: Implement check, currently all frequencies are supported
00324     return true;
00325 }
00326 
00327 
00328 void SX1272MB2xAS::Reset( void )
00329 {
00330     reset.output();
00331     reset = 0;
00332     wait_ms( 1 );
00333     reset.input();
00334     wait_ms( 6 );
00335 }
00336     
00337 void SX1272MB2xAS::Write( uint8_t addr, uint8_t data )
00338 {
00339     Write( addr, &data, 1 );
00340 }
00341 
00342 uint8_t SX1272MB2xAS::Read( uint8_t addr )
00343 {
00344     uint8_t data;
00345     Read( addr, &data, 1 );
00346     return data;
00347 }
00348 
00349 void SX1272MB2xAS::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
00350 {
00351     uint8_t i;
00352 
00353     nss = 0;
00354     spi .write( addr | 0x80 );
00355     for( i = 0; i < size; i++ )
00356     {
00357         spi .write( buffer[i] );
00358     }
00359     nss = 1;
00360 }
00361 
00362 void SX1272MB2xAS::Read( uint8_t addr, uint8_t *buffer, uint8_t size )
00363 {
00364     uint8_t i;
00365 
00366     nss = 0;
00367     spi .write( addr & 0x7F );
00368     for( i = 0; i < size; i++ )
00369     {
00370         buffer[i] = spi .write( 0 );
00371     }
00372     nss = 1;
00373 }
00374 
00375 void SX1272MB2xAS::WriteFifo( uint8_t *buffer, uint8_t size )
00376 {
00377     Write( 0, buffer, size );
00378 }
00379 
00380 void SX1272MB2xAS::ReadFifo( uint8_t *buffer, uint8_t size )
00381 {
00382     Read( 0, buffer, size );
00383 }