James Coleman / SX1272Libjlc

Fork of SX1272Liby by Timothy Mulrooney

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( PTC1 ),
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, PTC6, PTC7, PTC5, PTC4, A0, PTC8, PTC9, PTC10, PTC11, PTD0, PTD1 ),
00085                               AntSwitch( PTC1 ),
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 #else
00121         this->AntSwitch.input( );
00122         wait_ms( 1 );
00123         if( this->AntSwitch == 1 )
00124         {
00125             boardConnected = SX1272MB1DCS;
00126         }
00127         else
00128         {
00129             boardConnected = SX1272MB2XAS;
00130         }
00131         this->AntSwitch.output( );
00132         wait_ms( 1 );
00133 #endif
00134     }
00135     return ( boardConnected );
00136 }
00137 
00138 void SX1272MB2xAS::IoInit( void )
00139 {
00140     AntSwInit( );
00141     SpiInit( );
00142 }
00143 
00144 void SX1272MB2xAS::RadioRegistersInit( )
00145 {
00146     uint8_t i = 0;
00147     for( i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t  ); i++ )
00148     {
00149         SetModem( RadioRegsInit[i].Modem );
00150         Write( RadioRegsInit[i].Addr, RadioRegsInit[i].Value );
00151     }    
00152 }
00153 
00154 void SX1272MB2xAS::SpiInit( void )
00155 {
00156     nss = 1;    
00157     spi .format( 8,0 );   
00158     uint32_t frequencyToSet = 8000000;
00159     #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_MOTE_L152RC ) ||  defined ( TARGET_LPC11U6X ) || defined ( TARGET_MTS_MDOT_F411RE ) )
00160         spi .frequency( frequencyToSet );
00161     #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
00162         spi .frequency( frequencyToSet * 2 );
00163     #else
00164         #warning "Check the board's SPI frequency"
00165     #endif
00166     wait(0.1); 
00167 }
00168 
00169 void SX1272MB2xAS::IoIrqInit( DioIrqHandler *irqHandlers )
00170 {
00171 #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_MOTE_L152RC ) ||  defined ( TARGET_LPC11U6X ) )
00172     dio0.mode( PullDown );
00173     dio1.mode( PullDown );
00174     dio2.mode( PullDown );
00175     dio3.mode( PullDown );
00176     dio4.mode( PullDown );
00177 #endif
00178     dio0.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[0] ) );
00179     dio1.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[1] ) );
00180     dio2.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[2] ) );
00181     dio3.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[3] ) );
00182     dio4.rise( this, static_cast< TriggerMB2xAS > ( irqHandlers[4] ) );
00183 }
00184 
00185 void SX1272MB2xAS::IoDeInit( void )
00186 {
00187     //nothing
00188 }
00189 
00190 uint8_t SX1272MB2xAS::GetPaSelect( uint32_t channel )
00191 {
00192     if( boardConnected == SX1272MB1DCS || boardConnected == MDOT_F411RE )
00193     {
00194         return RF_PACONFIG_PASELECT_PABOOST;
00195     }
00196     else
00197     {
00198         return RF_PACONFIG_PASELECT_RFO;
00199     }
00200 }
00201 
00202 void SX1272MB2xAS::SetAntSwLowPower( bool status )
00203 {
00204     if( isRadioActive != status )
00205     {
00206         isRadioActive = status;
00207     
00208         if( status == false )
00209         {
00210             AntSwInit( );
00211         }
00212         else
00213         {
00214             AntSwDeInit( );
00215         }
00216     }
00217 }
00218 
00219 void SX1272MB2xAS::AntSwInit( void )
00220 {
00221 #if defined ( TARGET_MOTE_L152RC )
00222     this->RfSwitchCntr1  = 0;
00223     this->RfSwitchCntr2 = 0;
00224     this->PwrAmpCntr = 0;
00225 #elif defined ( TARGET_MTS_MDOT_F411RE )
00226     this->TxCtl = 0;
00227     this->RxCtl = 0;
00228 #else
00229     this->AntSwitch = 0;
00230 #endif
00231 }
00232 
00233 void SX1272MB2xAS::AntSwDeInit( void )
00234 {
00235 #if defined ( TARGET_MOTE_L152RC )
00236     this->RfSwitchCntr1  = 0;
00237     this->RfSwitchCntr2 = 0;
00238     this->PwrAmpCntr = 0;
00239 #elif defined ( TARGET_MTS_MDOT_F411RE )
00240     this->TxCtl = 0;
00241     this->RxCtl = 0;    
00242 #else
00243     this->AntSwitch = 0;
00244 #endif
00245 }
00246 
00247 void SX1272MB2xAS::SetAntSw( uint8_t rxTx )
00248 {
00249 #if defined ( TARGET_MOTE_L152RC )
00250     switch( this->currentOpMode )
00251     {
00252     case RFLR_OPMODE_TRANSMITTER:
00253         if( ( Read( REG_PACONFIG ) & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
00254         {
00255             this->RfSwitchCntr1  = 1;
00256             this->RfSwitchCntr2 = 0;
00257         }
00258         else
00259         {
00260             this->RfSwitchCntr1  = 0;
00261             this->RfSwitchCntr2 = 1;
00262         }
00263         break;
00264     case RFLR_OPMODE_RECEIVER:
00265     case RFLR_OPMODE_RECEIVER_SINGLE:
00266     case RFLR_OPMODE_CAD:
00267         this->RfSwitchCntr1  = 1;
00268         this->RfSwitchCntr2 = 1;
00269         break;
00270     default:
00271         this->RfSwitchCntr1  = 0;
00272         this->RfSwitchCntr2 = 0;
00273         this->PwrAmpCntr = 0;
00274         break;
00275     }
00276 #elif defined ( TARGET_MTS_MDOT_F411RE )
00277     if( this->rxTx == rxTx )
00278     {
00279         //no need to go further
00280         return;
00281     }
00282     
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 #else
00298     if( this->rxTx == rxTx )
00299     {
00300         //no need to go further
00301         return;
00302     }
00303 
00304     this->rxTx = rxTx ;
00305 
00306     // 1: Tx, 0: Rx
00307     if( rxTx != 0 )
00308     {
00309         this->AntSwitch = 1;
00310     }
00311     else
00312     {
00313         this->AntSwitch = 0;
00314     }
00315 #endif
00316 }
00317 
00318 bool SX1272MB2xAS::CheckRfFrequency( uint32_t frequency )
00319 {
00320     //TODO: Implement check, currently all frequencies are supported
00321     return true;
00322 }
00323 
00324 
00325 void SX1272MB2xAS::Reset( void )
00326 {
00327     reset.output();
00328     reset = 0;
00329     wait_ms( 1 );
00330     reset.input();
00331     wait_ms( 6 );
00332 }
00333     
00334 void SX1272MB2xAS::Write( uint8_t addr, uint8_t data )
00335 {
00336     Write( addr, &data, 1 );
00337 }
00338 
00339 uint8_t SX1272MB2xAS::Read( uint8_t addr )
00340 {
00341     uint8_t data;
00342     Read( addr, &data, 1 );
00343     return data;
00344 }
00345 
00346 void SX1272MB2xAS::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
00347 {
00348     uint8_t i;
00349 
00350     nss = 0;
00351     spi .write( addr | 0x80 );
00352     for( i = 0; i < size; i++ )
00353     {
00354         spi .write( buffer[i] );
00355     }
00356     nss = 1;
00357 }
00358 
00359 void SX1272MB2xAS::Read( uint8_t addr, uint8_t *buffer, uint8_t size )
00360 {
00361     uint8_t i;
00362 
00363     nss = 0;
00364     spi .write( addr & 0x7F );
00365     for( i = 0; i < size; i++ )
00366     {
00367         buffer[i] = spi .write( 0 );
00368     }
00369     nss = 1;
00370 }
00371 
00372 void SX1272MB2xAS::WriteFifo( uint8_t *buffer, uint8_t size )
00373 {
00374     Write( 0, buffer, size );
00375 }
00376 
00377 void SX1272MB2xAS::ReadFifo( uint8_t *buffer, uint8_t size )
00378 {
00379     Read( 0, buffer, size );
00380 }