Benoit Marchand / iGreenhouse_Node2_gcc_arm_nucleo_l073rz

Dependencies:   mbed Sht31

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