123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- /*
- * THE FOLLOWING FIRMWARE IS PROVIDED: (1) "AS IS" WITH NO WARRANTY; AND
- * (2)TO ENABLE ACCESS TO CODING INFORMATION TO GUIDE AND FACILITATE CUSTOMER.
- * CONSEQUENTLY, SEMTECH SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR
- * CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
- * OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION
- * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *
- * Copyright (C) SEMTECH S.A.
- */
- /*!
- * \file sx1276.c
- * \brief SX1276 RF chip driver
- *
- * \version 2.0.0
- * \date May 6 2013
- * \author Gregory Cristian
- *
- * Last modified by Miguel Luis on Jun 19 2013
- */
- #include "platform.h"
- #include "radio.h"
- #if defined( USE_SX1276_RADIO )
- #include "sx1276.h"
- #include "sx1276-Hal.h"
- #include "sx1276-Fsk.h"
- #include "sx1276-LoRa.h"
- #include "delay.h"
- #include "stdio.h"
- /*!
- * SX1276 registers variable
- */
- uint8_t SX1276Regs[0x70];
- static bool LoRaOn = false;
- static bool LoRaOnState = false;
- void SX1276Init( void )
- {
- // Initialize FSK and LoRa registers structure
- SX1276 = ( tSX1276* )SX1276Regs;
- SX1276LR = ( tSX1276LR* )SX1276Regs;
- SX1276InitIo( );
-
- SX1276Reset( );
- uint8_t RegVersion = 0;
- SX1276Read( REG_LR_VERSION, &RegVersion );
-
- if(RegVersion != 0x12)
- {
- printf("LoRa read Error!\r\n");
- printf("LoRa RegVersion = %d!\r\n",RegVersion);
-
- }
- else
- {
- printf("LoRa read Ok!\r\n");
- printf("LoRa RegVersion = %d!\r\n",RegVersion);
- }
- // REMARK: After radio reset the default modem is FSK
- #if ( LORA == 0 )
- LoRaOn = false;
- SX1276SetLoRaOn( LoRaOn );
- // Initialize FSK modem
- SX1276FskInit( );
- #else
- LoRaOn = true;
- SX1276SetLoRaOn( LoRaOn );
- // Initialize LoRa modem
- SX1276LoRaInit( );
-
- #endif
- }
- void SX1276Reset( void )
- {
- SX1276SetReset( RADIO_RESET_OFF );
-
- // Wait 1ms
- delay_ms(1);
- SX1276SetReset( RADIO_RESET_ON );
-
- // Wait 6ms
- delay_ms(6);
- }
- void SX1276SetLoRaOn( bool enable )
- {
- if( LoRaOnState == enable )
- {
- return;
- }
- LoRaOnState = enable;
- LoRaOn = enable;
- if( LoRaOn == true )
- {
- SX1276LoRaSetOpMode( RFLR_OPMODE_SLEEP );
-
- SX1276LR->RegOpMode = ( SX1276LR->RegOpMode & RFLR_OPMODE_LONGRANGEMODE_MASK ) | RFLR_OPMODE_LONGRANGEMODE_ON;
- SX1276Write( REG_LR_OPMODE, SX1276LR->RegOpMode );
-
- SX1276LoRaSetOpMode( RFLR_OPMODE_STANDBY );
- // RxDone RxTimeout FhssChangeChannel CadDone
- SX1276LR->RegDioMapping1 = RFLR_DIOMAPPING1_DIO0_00 | RFLR_DIOMAPPING1_DIO1_00 | RFLR_DIOMAPPING1_DIO2_00 | RFLR_DIOMAPPING1_DIO3_00;
- // CadDetected ModeReady
- SX1276LR->RegDioMapping2 = RFLR_DIOMAPPING2_DIO4_00 | RFLR_DIOMAPPING2_DIO5_00;
- SX1276WriteBuffer( REG_LR_DIOMAPPING1, &SX1276LR->RegDioMapping1, 2 );
-
- SX1276ReadBuffer( REG_LR_OPMODE, SX1276Regs + 1, 0x70 - 1 );
- }
- else
- {
- SX1276LoRaSetOpMode( RFLR_OPMODE_SLEEP );
-
- SX1276LR->RegOpMode = ( SX1276LR->RegOpMode & RFLR_OPMODE_LONGRANGEMODE_MASK ) | RFLR_OPMODE_LONGRANGEMODE_OFF;
- SX1276Write( REG_LR_OPMODE, SX1276LR->RegOpMode );
-
- SX1276LoRaSetOpMode( RFLR_OPMODE_STANDBY );
-
- SX1276ReadBuffer( REG_OPMODE, SX1276Regs + 1, 0x70 - 1 );
- }
- }
- bool SX1276GetLoRaOn( void )
- {
- return LoRaOn;
- }
- void SX1276SetOpMode( uint8_t opMode )
- {
- if( LoRaOn == false )
- {
- SX1276FskSetOpMode( opMode );
- }
- else
- {
- SX1276LoRaSetOpMode( opMode );
- }
- }
- uint8_t SX1276GetOpMode( void )
- {
- if( LoRaOn == false )
- {
- return SX1276FskGetOpMode( );
- }
- else
- {
- return SX1276LoRaGetOpMode( );
- }
- }
- double SX1276ReadRssi( void )
- {
- if( LoRaOn == false )
- {
- return SX1276FskReadRssi( );
- }
- else
- {
- return SX1276LoRaReadRssi( );
- }
- }
- uint8_t SX1276ReadRxGain( void )
- {
- if( LoRaOn == false )
- {
- return SX1276FskReadRxGain( );
- }
- else
- {
- return SX1276LoRaReadRxGain( );
- }
- }
- uint8_t SX1276GetPacketRxGain( void )
- {
- if( LoRaOn == false )
- {
- return SX1276FskGetPacketRxGain( );
- }
- else
- {
- return SX1276LoRaGetPacketRxGain( );
- }
- }
- int8_t SX1276GetPacketSnr( void )
- {
- if( LoRaOn == false )
- {
- while( 1 )
- {
- // Useless in FSK mode
- // Block program here
- }
- }
- else
- {
- return SX1276LoRaGetPacketSnr( );
- }
- }
- double SX1276GetPacketRssi( void )
- {
- if( LoRaOn == false )
- {
- return SX1276FskGetPacketRssi( );
- }
- else
- {
- return SX1276LoRaGetPacketRssi( );
- }
- }
- uint32_t SX1276GetPacketAfc( void )
- {
- if( LoRaOn == false )
- {
- return SX1276FskGetPacketAfc( );
- }
- else
- {
- while( 1 )
- {
- // Useless in LoRa mode
- // Block program here
- }
- }
- }
- void SX1276StartRx( void )
- {
- if( LoRaOn == false )
- {
- SX1276FskSetRFState( RF_STATE_RX_INIT );
- }
- else
- {
- SX1276LoRaSetRFState( RFLR_STATE_RX_INIT );
- }
- }
- void SX1276GetRxPacket( void *buffer, uint16_t *size )
- {
- if( LoRaOn == false )
- {
- SX1276FskGetRxPacket( buffer, size );
- }
- else
- {
- SX1276LoRaGetRxPacket( buffer, size );
- }
- }
- void SX1276SetTxPacket( const void *buffer, uint16_t size )
- {
- if( LoRaOn == false )
- {
- SX1276FskSetTxPacket( buffer, size );
- }
- else
- {
- SX1276LoRaSetTxPacket( buffer, size );
- }
- }
- uint8_t SX1276GetRFState( void )
- {
- if( LoRaOn == false )
- {
- return SX1276FskGetRFState( );
- }
- else
- {
- return SX1276LoRaGetRFState( );
- }
- }
- void SX1276SetRFState( uint8_t state )
- {
- if( LoRaOn == false )
- {
- SX1276FskSetRFState( state );
- }
- else
- {
- SX1276LoRaSetRFState( state );
- }
- }
- uint32_t SX1276Process( void )
- {
- if( LoRaOn == false )
- {
- return SX1276FskProcess( );
- }
- else
- {
- return SX1276LoRaProcess( );
- }
- }
- #endif // USE_SX1276_RADIO
|