fholin fholin / Mbed 2 deprecated MiniMousetest

Dependencies:   mbed

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