I-O DATA DEV2 / Private_lora_SX1276

Dependents:   Nucleo_Private_LoRa

Embed: (wiki syntax)

« Back to documentation index

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

sx1276-hal.cpp

00001 /*
00002  / _____)             _              | |
00003 ( (____  _____ ____ _| |_ _____  ____| |__
00004  \____ \| ___ |    (_   _) ___ |/ ___)  _ \
00005  _____) ) ____| | | || |_| ____( (___| | | |
00006 (______/|_____)_|_|_| \__)_____)\____)_| |_|
00007     (C) 2014 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 "sx1276-hal.h"
00016 
00017 const RadioRegisters_t  SX1276MB1xAS::RadioRegsInit[] = RADIO_INIT_REGISTERS_VALUE;
00018 
00019 SX1276MB1xAS::SX1276MB1xAS( 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                             PinName antSwitch )
00023                             : SX1276 ( events, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5 ),
00024                             antSwitch( antSwitch ),
00025                         #if( defined ( TARGET_NUCLEO_L152RE ) )
00026                             fake( D8 )
00027                         #else
00028                             fake( A3 )
00029                         #endif
00030 {
00031     this->RadioEvents = events;
00032 
00033     Reset( );
00034 
00035     RxChainCalibration( );
00036 
00037     IoInit( );
00038 
00039     SetOpMode( RF_OPMODE_SLEEP );
00040 
00041     IoIrqInit( dioIrq );
00042 
00043     RadioRegistersInit( );
00044 
00045     SetModem( MODEM_FSK );
00046 
00047     this->settings.State = RF_IDLE ;
00048 }
00049 
00050 SX1276MB1xAS::SX1276MB1xAS( RadioEvents_t *events )
00051                         #if ( defined ( TARGET_NUCLEO_L152RE )|| defined ( TARGET_NUCLEO_L073RZ ) || defined ( TARGET_NUCLEO_L053R8 ) )
00052                         :   SX1276 ( events, D11, D12, D13, D10, D9, D8, D3, D4, D5, A3, A1 ), // For NUCLEO L152RE dio4 is on port A3
00053                             antSwitch( A4 ),
00054                             fake( D8 )
00055                         #elif defined( TARGET_LPC11U6X )
00056                         :   SX1276 ( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
00057                             antSwitch( P0_23 ), 
00058                             fake( A3 )
00059                         #else
00060                         :   SX1276 ( events, D11, D12, D13, D10, D9, D8, D7, D6, D4, D3, D2 ), // For NUCLEO 
00061                             antSwitch( A4 ),
00062                             fake( D8 )
00063 //                        :   SX1276( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
00064 //                            antSwitch( A4 ), 
00065 //                            fake( A3 )
00066                         #endif
00067 {
00068     this->RadioEvents = events;
00069 
00070     Reset( );
00071 
00072     boardConnected = UNKNOWN;
00073 
00074     DetectBoardType( );
00075 
00076     RxChainCalibration( );
00077 
00078     IoInit( );
00079 
00080     SetOpMode( RF_OPMODE_SLEEP );
00081     IoIrqInit( dioIrq );
00082 
00083     RadioRegistersInit( );
00084 
00085     SetModem( MODEM_FSK );
00086 
00087     this->settings.State = RF_IDLE ;
00088 }
00089 
00090 //-------------------------------------------------------------------------
00091 //                      Board relative functions
00092 //-------------------------------------------------------------------------
00093 uint8_t SX1276MB1xAS::DetectBoardType( void )
00094 {
00095     if( boardConnected == UNKNOWN )
00096     {
00097         antSwitch .input( );
00098 //        printf("antSwitch %d\r\n",ant);
00099         wait_ms( 1 );
00100         int ant=1;
00101         if(ant == 1)
00102         {        
00103             printf("SX1276MB1LAS \r\n");
00104             boardConnected = SX1276MB1LAS;
00105         }
00106         else
00107         {
00108             boardConnected = SX1276MB1MAS;
00109         }
00110         antSwitch .output( );
00111         wait_ms( 1 );
00112     }
00113     return ( boardConnected );
00114 }
00115 
00116 void SX1276MB1xAS::IoInit( void )
00117 {
00118     AntSwInit( );
00119     SpiInit( );
00120 }
00121 
00122 void SX1276MB1xAS::RadioRegistersInit( )
00123 {
00124     uint8_t i = 0;
00125     for( i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t  ); i++ )
00126     {
00127         SetModem( RadioRegsInit[i].Modem );
00128         Write( RadioRegsInit[i].Addr, RadioRegsInit[i].Value );
00129     }    
00130 }
00131 
00132 void SX1276MB1xAS::SpiInit( void )
00133 {
00134     nss = 1;    
00135     spi .format( 8,0 );   
00136     uint32_t frequencyToSet = 8000000;
00137     #if( defined ( TARGET_NUCLEO_L152RE ) ||  defined ( TARGET_LPC11U6X ) || defined ( TARGET_NUCLEO_L073RZ )  || defined ( TARGET_NUCLEO_L053R8 ))
00138         spi .frequency( frequencyToSet );
00139     #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
00140         spi .frequency( frequencyToSet * 2 );
00141     #else
00142         spi .frequency( frequencyToSet );
00143     #endif
00144     wait(0.1); 
00145 }
00146 
00147 void SX1276MB1xAS::IoIrqInit( DioIrqHandler *irqHandlers )
00148 {
00149 //#if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) || defined ( TARGET_NUCLEO_L073RZ )  || defined ( TARGET_NUCLEO_L053R8 ) )
00150     dio0 .mode( PullDown );
00151 //    dio1.mode( PullDown );
00152 //    dio2.mode( PullDown );
00153 //    dio3.mode( PullDown );
00154 //    dio4.mode( PullDown );
00155 //#endif
00156     dio0 .rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[0] ) );
00157 //    dio1.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[1] ) );
00158 //    dio2.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[2] ) );
00159 //    dio3.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[3] ) );
00160 //    dio4.rise( this, static_cast< TriggerMB1xAS > ( irqHandlers[4] ) );
00161 }
00162 
00163 void SX1276MB1xAS::IoDeInit( void )
00164 {
00165     //nothing
00166 }
00167 
00168 uint8_t SX1276MB1xAS::GetPaSelect( uint32_t channel )
00169 {
00170     if( 1 /*channel > RF_MID_BAND_THRESH*/ )
00171     {
00172         if( boardConnected == SX1276MB1LAS )
00173         {
00174             return RF_PACONFIG_PASELECT_PABOOST;
00175         }
00176         else
00177         {
00178             return RF_PACONFIG_PASELECT_RFO;
00179         }
00180     }
00181     else
00182     {
00183         return RF_PACONFIG_PASELECT_RFO;
00184     }
00185 }
00186 
00187 void SX1276MB1xAS::SetAntSwLowPower( bool status )
00188 {
00189     if( isRadioActive != status )
00190     {
00191         isRadioActive = status;
00192     
00193         if( status == false )
00194         {
00195             AntSwInit( );
00196         }
00197         else
00198         {
00199             AntSwDeInit( );
00200         }
00201     }
00202 }
00203 
00204 void SX1276MB1xAS::AntSwInit( void )
00205 {
00206     antSwitch  = 0;
00207 }
00208 
00209 void SX1276MB1xAS::AntSwDeInit( void )
00210 {
00211     antSwitch  = 0;
00212 }
00213 
00214 void SX1276MB1xAS::SetAntSw( uint8_t rxTx )
00215 {
00216 
00217     this->rxTx = rxTx ;
00218 
00219     // 1: Tx, 0: Rx
00220     if( rxTx != 0 )
00221     {
00222         antSwitch  = 1;
00223     }
00224     else
00225     {
00226         antSwitch  = 0;
00227     }
00228 }
00229 
00230 bool SX1276MB1xAS::CheckRfFrequency( uint32_t frequency )
00231 {
00232     //TODO: Implement check, currently all frequencies are supported
00233     return true;
00234 }
00235 
00236 
00237 void SX1276MB1xAS::Reset( void )
00238 {
00239     reset .output();
00240     reset  = 0;
00241     wait_ms( 1 );
00242     reset .input();
00243     wait_ms( 6 );
00244 }
00245     
00246 void SX1276MB1xAS::Write( uint8_t addr, uint8_t data )
00247 {
00248     Write( addr, &data, 1 );
00249 }
00250 
00251 uint8_t SX1276MB1xAS::Read( uint8_t addr )
00252 {
00253     uint8_t data;
00254     Read( addr, &data, 1 );
00255     return data;
00256 }
00257 
00258 void SX1276MB1xAS::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
00259 {
00260     uint8_t i;
00261 
00262     nss = 0;
00263     spi .write( addr | 0x80 );
00264     for( i = 0; i < size; i++ )
00265     {
00266         spi .write( buffer[i] );
00267     }
00268     nss = 1;
00269 }
00270 
00271 void SX1276MB1xAS::Read( uint8_t addr, uint8_t *buffer, uint8_t size )
00272 {
00273     uint8_t i;
00274 
00275     nss = 0;
00276     spi .write( addr & 0x7F );
00277     for( i = 0; i < size; i++ )
00278     {
00279         buffer[i] = spi .write( 0 );
00280     }
00281     nss = 1;
00282 }
00283 
00284 void SX1276MB1xAS::WriteFifo( uint8_t *buffer, uint8_t size )
00285 {
00286     Write( 0, buffer, size );
00287 }
00288 
00289 void SX1276MB1xAS::ReadFifo( uint8_t *buffer, uint8_t size )
00290 {
00291     Read( 0, buffer, size );
00292 }