Driver for the SX1272 RF Transceiver

Dependents:   LoRaWAN_mbed_lmic_agriculture_app

Fork of SX1272Lib by Semtech

Committer:
mluis
Date:
Tue Jan 05 16:43:48 2016 +0000
Revision:
0:45c4f0364ca4
Child:
1:b0372ef620d0
Library creation based on SX1276Lib and https://github.com/Lora-net/LoRaMac-node

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mluis 0:45c4f0364ca4 1 /*
mluis 0:45c4f0364ca4 2 / _____) _ | |
mluis 0:45c4f0364ca4 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:45c4f0364ca4 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:45c4f0364ca4 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:45c4f0364ca4 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 0:45c4f0364ca4 7 (C) 2015 Semtech
mluis 0:45c4f0364ca4 8
mluis 0:45c4f0364ca4 9 Description: -
mluis 0:45c4f0364ca4 10
mluis 0:45c4f0364ca4 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:45c4f0364ca4 12
mluis 0:45c4f0364ca4 13 Maintainers: Miguel Luis, Gregory Cristian and Nicolas Huguenin
mluis 0:45c4f0364ca4 14 */
mluis 0:45c4f0364ca4 15 #include "sx1272-hal.h"
mluis 0:45c4f0364ca4 16
mluis 0:45c4f0364ca4 17 const RadioRegisters_t SX1272MB1DxS::RadioRegsInit[] = RADIO_INIT_REGISTERS_VALUE;
mluis 0:45c4f0364ca4 18
mluis 0:45c4f0364ca4 19 SX1272MB1DxS::SX1272MB1DxS( RadioEvents_t *events,
mluis 0:45c4f0364ca4 20 PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
mluis 0:45c4f0364ca4 21 PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5,
mluis 0:45c4f0364ca4 22 #if defined ( TARGET_MOTE_L152RC )
mluis 0:45c4f0364ca4 23 PinName rfSwitchCntr1, PinName rfSwitchCntr2 )
mluis 0:45c4f0364ca4 24 #else
mluis 0:45c4f0364ca4 25 PinName antSwitch )
mluis 0:45c4f0364ca4 26 #endif
mluis 0:45c4f0364ca4 27 : SX1272( events, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5 ),
mluis 0:45c4f0364ca4 28 #if defined ( TARGET_MOTE_L152RC )
mluis 0:45c4f0364ca4 29 RfSwitchCntr1( rfSwitchCntr1 ),
mluis 0:45c4f0364ca4 30 RfSwitchCntr2( rfSwitchCntr2 ),
mluis 0:45c4f0364ca4 31 PwrAmpCntr( PD_2 )
mluis 0:45c4f0364ca4 32 #else
mluis 0:45c4f0364ca4 33 AntSwitch( antSwitch ),
mluis 0:45c4f0364ca4 34 #if( defined ( TARGET_NUCLEO_L152RE ) )
mluis 0:45c4f0364ca4 35 Fake( D8 )
mluis 0:45c4f0364ca4 36 #else
mluis 0:45c4f0364ca4 37 Fake( A3 )
mluis 0:45c4f0364ca4 38 #endif
mluis 0:45c4f0364ca4 39 #endif
mluis 0:45c4f0364ca4 40 {
mluis 0:45c4f0364ca4 41 this->RadioEvents = events;
mluis 0:45c4f0364ca4 42
mluis 0:45c4f0364ca4 43 Reset( );
mluis 0:45c4f0364ca4 44
mluis 0:45c4f0364ca4 45 IoInit( );
mluis 0:45c4f0364ca4 46
mluis 0:45c4f0364ca4 47 SetOpMode( RF_OPMODE_SLEEP );
mluis 0:45c4f0364ca4 48
mluis 0:45c4f0364ca4 49 IoIrqInit( dioIrq );
mluis 0:45c4f0364ca4 50
mluis 0:45c4f0364ca4 51 RadioRegistersInit( );
mluis 0:45c4f0364ca4 52
mluis 0:45c4f0364ca4 53 SetModem( MODEM_FSK );
mluis 0:45c4f0364ca4 54
mluis 0:45c4f0364ca4 55 this->settings.State = RF_IDLE ;
mluis 0:45c4f0364ca4 56 }
mluis 0:45c4f0364ca4 57
mluis 0:45c4f0364ca4 58 SX1272MB1DxS::SX1272MB1DxS( RadioEvents_t *events )
mluis 0:45c4f0364ca4 59 #if defined ( TARGET_NUCLEO_L152RE )
mluis 0:45c4f0364ca4 60 : SX1272( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, A3, D9 ), // For NUCLEO L152RE dio4 is on port A3
mluis 0:45c4f0364ca4 61 AntSwitch( A4 ),
mluis 0:45c4f0364ca4 62 Fake( D8 )
mluis 0:45c4f0364ca4 63 #elif defined ( TARGET_MOTE_L152RC )
mluis 0:45c4f0364ca4 64 : SX1272( events, PB_15, PB_14, PB_13, PB_12, PC_2, PC_6, PC_10, PC_11, PC_8, PC_9, PC_12 ),
mluis 0:45c4f0364ca4 65 RfSwitchCntr1( PC_4 ),
mluis 0:45c4f0364ca4 66 RfSwitchCntr2( PC_13 ),
mluis 0:45c4f0364ca4 67 PwrAmpCntr( PD_2 )
mluis 0:45c4f0364ca4 68 #else
mluis 0:45c4f0364ca4 69 : SX1272( events, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
mluis 0:45c4f0364ca4 70 AntSwitch( A4 ),
mluis 0:45c4f0364ca4 71 Fake( A3 )
mluis 0:45c4f0364ca4 72 #endif
mluis 0:45c4f0364ca4 73 {
mluis 0:45c4f0364ca4 74 this->RadioEvents = events;
mluis 0:45c4f0364ca4 75
mluis 0:45c4f0364ca4 76 Reset( );
mluis 0:45c4f0364ca4 77
mluis 0:45c4f0364ca4 78 boardConnected = UNKNOWN;
mluis 0:45c4f0364ca4 79
mluis 0:45c4f0364ca4 80 DetectBoardType( );
mluis 0:45c4f0364ca4 81
mluis 0:45c4f0364ca4 82 IoInit( );
mluis 0:45c4f0364ca4 83
mluis 0:45c4f0364ca4 84 SetOpMode( RF_OPMODE_SLEEP );
mluis 0:45c4f0364ca4 85 IoIrqInit( dioIrq );
mluis 0:45c4f0364ca4 86
mluis 0:45c4f0364ca4 87 RadioRegistersInit( );
mluis 0:45c4f0364ca4 88
mluis 0:45c4f0364ca4 89 SetModem( MODEM_FSK );
mluis 0:45c4f0364ca4 90
mluis 0:45c4f0364ca4 91 this->settings.State = RF_IDLE ;
mluis 0:45c4f0364ca4 92 }
mluis 0:45c4f0364ca4 93
mluis 0:45c4f0364ca4 94 //-------------------------------------------------------------------------
mluis 0:45c4f0364ca4 95 // Board relative functions
mluis 0:45c4f0364ca4 96 //-------------------------------------------------------------------------
mluis 0:45c4f0364ca4 97 uint8_t SX1272MB1DxS::DetectBoardType( void )
mluis 0:45c4f0364ca4 98 {
mluis 0:45c4f0364ca4 99 if( boardConnected == UNKNOWN )
mluis 0:45c4f0364ca4 100 {
mluis 0:45c4f0364ca4 101 #if defined ( TARGET_MOTE_L152RC )
mluis 0:45c4f0364ca4 102 boardConnected = NA_MOTE_72;
mluis 0:45c4f0364ca4 103 #else
mluis 0:45c4f0364ca4 104 this->AntSwitch.input( );
mluis 0:45c4f0364ca4 105 wait_ms( 1 );
mluis 0:45c4f0364ca4 106 if( this->AntSwitch == 1 )
mluis 0:45c4f0364ca4 107 {
mluis 0:45c4f0364ca4 108 boardConnected = SX1272MB1DCS;
mluis 0:45c4f0364ca4 109 }
mluis 0:45c4f0364ca4 110 else
mluis 0:45c4f0364ca4 111 {
mluis 0:45c4f0364ca4 112 boardConnected = SX1272MB1DAS;
mluis 0:45c4f0364ca4 113 }
mluis 0:45c4f0364ca4 114 this->AntSwitch.output( );
mluis 0:45c4f0364ca4 115 wait_ms( 1 );
mluis 0:45c4f0364ca4 116 #endif
mluis 0:45c4f0364ca4 117 }
mluis 0:45c4f0364ca4 118 return ( boardConnected );
mluis 0:45c4f0364ca4 119 }
mluis 0:45c4f0364ca4 120
mluis 0:45c4f0364ca4 121 void SX1272MB1DxS::IoInit( void )
mluis 0:45c4f0364ca4 122 {
mluis 0:45c4f0364ca4 123 AntSwInit( );
mluis 0:45c4f0364ca4 124 SpiInit( );
mluis 0:45c4f0364ca4 125 }
mluis 0:45c4f0364ca4 126
mluis 0:45c4f0364ca4 127 void SX1272MB1DxS::RadioRegistersInit( )
mluis 0:45c4f0364ca4 128 {
mluis 0:45c4f0364ca4 129 uint8_t i = 0;
mluis 0:45c4f0364ca4 130 for( i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t ); i++ )
mluis 0:45c4f0364ca4 131 {
mluis 0:45c4f0364ca4 132 SetModem( RadioRegsInit[i].Modem );
mluis 0:45c4f0364ca4 133 Write( RadioRegsInit[i].Addr, RadioRegsInit[i].Value );
mluis 0:45c4f0364ca4 134 }
mluis 0:45c4f0364ca4 135 }
mluis 0:45c4f0364ca4 136
mluis 0:45c4f0364ca4 137 void SX1272MB1DxS::SpiInit( void )
mluis 0:45c4f0364ca4 138 {
mluis 0:45c4f0364ca4 139 nss = 1;
mluis 0:45c4f0364ca4 140 spi.format( 8,0 );
mluis 0:45c4f0364ca4 141 uint32_t frequencyToSet = 8000000;
mluis 0:45c4f0364ca4 142 #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_MOTE_L152RC ) || defined ( TARGET_LPC11U6X ) )
mluis 0:45c4f0364ca4 143 spi.frequency( frequencyToSet );
mluis 0:45c4f0364ca4 144 #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
mluis 0:45c4f0364ca4 145 spi.frequency( frequencyToSet * 2 );
mluis 0:45c4f0364ca4 146 #else
mluis 0:45c4f0364ca4 147 #warning "Check the board's SPI frequency"
mluis 0:45c4f0364ca4 148 #endif
mluis 0:45c4f0364ca4 149 wait(0.1);
mluis 0:45c4f0364ca4 150 }
mluis 0:45c4f0364ca4 151
mluis 0:45c4f0364ca4 152 void SX1272MB1DxS::IoIrqInit( DioIrqHandler *irqHandlers )
mluis 0:45c4f0364ca4 153 {
mluis 0:45c4f0364ca4 154 #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_MOTE_L152RC ) || defined ( TARGET_LPC11U6X ) )
mluis 0:45c4f0364ca4 155 dio0.mode( PullDown );
mluis 0:45c4f0364ca4 156 dio1.mode( PullDown );
mluis 0:45c4f0364ca4 157 dio2.mode( PullDown );
mluis 0:45c4f0364ca4 158 dio3.mode( PullDown );
mluis 0:45c4f0364ca4 159 dio4.mode( PullDown );
mluis 0:45c4f0364ca4 160 #endif
mluis 0:45c4f0364ca4 161 dio0.rise( this, static_cast< TriggerMB1DxS > ( irqHandlers[0] ) );
mluis 0:45c4f0364ca4 162 dio1.rise( this, static_cast< TriggerMB1DxS > ( irqHandlers[1] ) );
mluis 0:45c4f0364ca4 163 dio2.rise( this, static_cast< TriggerMB1DxS > ( irqHandlers[2] ) );
mluis 0:45c4f0364ca4 164 dio3.rise( this, static_cast< TriggerMB1DxS > ( irqHandlers[3] ) );
mluis 0:45c4f0364ca4 165 dio4.rise( this, static_cast< TriggerMB1DxS > ( irqHandlers[4] ) );
mluis 0:45c4f0364ca4 166 }
mluis 0:45c4f0364ca4 167
mluis 0:45c4f0364ca4 168 void SX1272MB1DxS::IoDeInit( void )
mluis 0:45c4f0364ca4 169 {
mluis 0:45c4f0364ca4 170 //nothing
mluis 0:45c4f0364ca4 171 }
mluis 0:45c4f0364ca4 172
mluis 0:45c4f0364ca4 173 uint8_t SX1272MB1DxS::GetPaSelect( uint32_t channel )
mluis 0:45c4f0364ca4 174 {
mluis 0:45c4f0364ca4 175 if( boardConnected == SX1272MB1DCS )
mluis 0:45c4f0364ca4 176 {
mluis 0:45c4f0364ca4 177 return RF_PACONFIG_PASELECT_PABOOST;
mluis 0:45c4f0364ca4 178 }
mluis 0:45c4f0364ca4 179 else
mluis 0:45c4f0364ca4 180 {
mluis 0:45c4f0364ca4 181 return RF_PACONFIG_PASELECT_RFO;
mluis 0:45c4f0364ca4 182 }
mluis 0:45c4f0364ca4 183 }
mluis 0:45c4f0364ca4 184
mluis 0:45c4f0364ca4 185 void SX1272MB1DxS::SetAntSwLowPower( bool status )
mluis 0:45c4f0364ca4 186 {
mluis 0:45c4f0364ca4 187 if( isRadioActive != status )
mluis 0:45c4f0364ca4 188 {
mluis 0:45c4f0364ca4 189 isRadioActive = status;
mluis 0:45c4f0364ca4 190
mluis 0:45c4f0364ca4 191 if( status == false )
mluis 0:45c4f0364ca4 192 {
mluis 0:45c4f0364ca4 193 AntSwInit( );
mluis 0:45c4f0364ca4 194 }
mluis 0:45c4f0364ca4 195 else
mluis 0:45c4f0364ca4 196 {
mluis 0:45c4f0364ca4 197 AntSwDeInit( );
mluis 0:45c4f0364ca4 198 }
mluis 0:45c4f0364ca4 199 }
mluis 0:45c4f0364ca4 200 }
mluis 0:45c4f0364ca4 201
mluis 0:45c4f0364ca4 202 void SX1272MB1DxS::AntSwInit( void )
mluis 0:45c4f0364ca4 203 {
mluis 0:45c4f0364ca4 204 #if defined ( TARGET_MOTE_L152RC )
mluis 0:45c4f0364ca4 205 this->RfSwitchCntr1 = 0;
mluis 0:45c4f0364ca4 206 this->RfSwitchCntr2 = 0;
mluis 0:45c4f0364ca4 207 this->PwrAmpCntr = 0;
mluis 0:45c4f0364ca4 208 #else
mluis 0:45c4f0364ca4 209 this->AntSwitch = 0;
mluis 0:45c4f0364ca4 210 #endif
mluis 0:45c4f0364ca4 211 }
mluis 0:45c4f0364ca4 212
mluis 0:45c4f0364ca4 213 void SX1272MB1DxS::AntSwDeInit( void )
mluis 0:45c4f0364ca4 214 {
mluis 0:45c4f0364ca4 215 #if defined ( TARGET_MOTE_L152RC )
mluis 0:45c4f0364ca4 216 this->RfSwitchCntr1 = 0;
mluis 0:45c4f0364ca4 217 this->RfSwitchCntr2 = 0;
mluis 0:45c4f0364ca4 218 this->PwrAmpCntr = 0;
mluis 0:45c4f0364ca4 219 #else
mluis 0:45c4f0364ca4 220 this->AntSwitch = 0;
mluis 0:45c4f0364ca4 221 #endif
mluis 0:45c4f0364ca4 222 }
mluis 0:45c4f0364ca4 223
mluis 0:45c4f0364ca4 224 void SX1272MB1DxS::SetAntSw( uint8_t rxTx )
mluis 0:45c4f0364ca4 225 {
mluis 0:45c4f0364ca4 226 #if defined ( TARGET_MOTE_L152RC )
mluis 0:45c4f0364ca4 227 switch( this->currentOpMode )
mluis 0:45c4f0364ca4 228 {
mluis 0:45c4f0364ca4 229 case RFLR_OPMODE_TRANSMITTER:
mluis 0:45c4f0364ca4 230 if( ( Read( REG_PACONFIG ) & RF_PACONFIG_PASELECT_PABOOST ) == RF_PACONFIG_PASELECT_PABOOST )
mluis 0:45c4f0364ca4 231 {
mluis 0:45c4f0364ca4 232 this->RfSwitchCntr1 = 1;
mluis 0:45c4f0364ca4 233 this->RfSwitchCntr2 = 0;
mluis 0:45c4f0364ca4 234 }
mluis 0:45c4f0364ca4 235 else
mluis 0:45c4f0364ca4 236 {
mluis 0:45c4f0364ca4 237 this->RfSwitchCntr1 = 0;
mluis 0:45c4f0364ca4 238 this->RfSwitchCntr2 = 1;
mluis 0:45c4f0364ca4 239 }
mluis 0:45c4f0364ca4 240 break;
mluis 0:45c4f0364ca4 241 case RFLR_OPMODE_RECEIVER:
mluis 0:45c4f0364ca4 242 case RFLR_OPMODE_RECEIVER_SINGLE:
mluis 0:45c4f0364ca4 243 case RFLR_OPMODE_CAD:
mluis 0:45c4f0364ca4 244 this->RfSwitchCntr1 = 1;
mluis 0:45c4f0364ca4 245 this->RfSwitchCntr2 = 1;
mluis 0:45c4f0364ca4 246 break;
mluis 0:45c4f0364ca4 247 default:
mluis 0:45c4f0364ca4 248 this->RfSwitchCntr1 = 0;
mluis 0:45c4f0364ca4 249 this->RfSwitchCntr2 = 0;
mluis 0:45c4f0364ca4 250 this->PwrAmpCntr = 0;
mluis 0:45c4f0364ca4 251 break;
mluis 0:45c4f0364ca4 252 }
mluis 0:45c4f0364ca4 253 #else
mluis 0:45c4f0364ca4 254 if( this->rxTx == rxTx )
mluis 0:45c4f0364ca4 255 {
mluis 0:45c4f0364ca4 256 //no need to go further
mluis 0:45c4f0364ca4 257 return;
mluis 0:45c4f0364ca4 258 }
mluis 0:45c4f0364ca4 259
mluis 0:45c4f0364ca4 260 this->rxTx = rxTx;
mluis 0:45c4f0364ca4 261
mluis 0:45c4f0364ca4 262 // 1: Tx, 0: Rx
mluis 0:45c4f0364ca4 263 if( rxTx != 0 )
mluis 0:45c4f0364ca4 264 {
mluis 0:45c4f0364ca4 265 this->AntSwitch = 1;
mluis 0:45c4f0364ca4 266 }
mluis 0:45c4f0364ca4 267 else
mluis 0:45c4f0364ca4 268 {
mluis 0:45c4f0364ca4 269 this->AntSwitch = 0;
mluis 0:45c4f0364ca4 270 }
mluis 0:45c4f0364ca4 271 #endif
mluis 0:45c4f0364ca4 272 }
mluis 0:45c4f0364ca4 273
mluis 0:45c4f0364ca4 274 bool SX1272MB1DxS::CheckRfFrequency( uint32_t frequency )
mluis 0:45c4f0364ca4 275 {
mluis 0:45c4f0364ca4 276 //TODO: Implement check, currently all frequencies are supported
mluis 0:45c4f0364ca4 277 return true;
mluis 0:45c4f0364ca4 278 }
mluis 0:45c4f0364ca4 279
mluis 0:45c4f0364ca4 280
mluis 0:45c4f0364ca4 281 void SX1272MB1DxS::Reset( void )
mluis 0:45c4f0364ca4 282 {
mluis 0:45c4f0364ca4 283 reset.output();
mluis 0:45c4f0364ca4 284 reset = 0;
mluis 0:45c4f0364ca4 285 wait_ms( 1 );
mluis 0:45c4f0364ca4 286 reset.input();
mluis 0:45c4f0364ca4 287 wait_ms( 6 );
mluis 0:45c4f0364ca4 288 }
mluis 0:45c4f0364ca4 289
mluis 0:45c4f0364ca4 290 void SX1272MB1DxS::Write( uint8_t addr, uint8_t data )
mluis 0:45c4f0364ca4 291 {
mluis 0:45c4f0364ca4 292 Write( addr, &data, 1 );
mluis 0:45c4f0364ca4 293 }
mluis 0:45c4f0364ca4 294
mluis 0:45c4f0364ca4 295 uint8_t SX1272MB1DxS::Read( uint8_t addr )
mluis 0:45c4f0364ca4 296 {
mluis 0:45c4f0364ca4 297 uint8_t data;
mluis 0:45c4f0364ca4 298 Read( addr, &data, 1 );
mluis 0:45c4f0364ca4 299 return data;
mluis 0:45c4f0364ca4 300 }
mluis 0:45c4f0364ca4 301
mluis 0:45c4f0364ca4 302 void SX1272MB1DxS::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
mluis 0:45c4f0364ca4 303 {
mluis 0:45c4f0364ca4 304 uint8_t i;
mluis 0:45c4f0364ca4 305
mluis 0:45c4f0364ca4 306 nss = 0;
mluis 0:45c4f0364ca4 307 spi.write( addr | 0x80 );
mluis 0:45c4f0364ca4 308 for( i = 0; i < size; i++ )
mluis 0:45c4f0364ca4 309 {
mluis 0:45c4f0364ca4 310 spi.write( buffer[i] );
mluis 0:45c4f0364ca4 311 }
mluis 0:45c4f0364ca4 312 nss = 1;
mluis 0:45c4f0364ca4 313 }
mluis 0:45c4f0364ca4 314
mluis 0:45c4f0364ca4 315 void SX1272MB1DxS::Read( uint8_t addr, uint8_t *buffer, uint8_t size )
mluis 0:45c4f0364ca4 316 {
mluis 0:45c4f0364ca4 317 uint8_t i;
mluis 0:45c4f0364ca4 318
mluis 0:45c4f0364ca4 319 nss = 0;
mluis 0:45c4f0364ca4 320 spi.write( addr & 0x7F );
mluis 0:45c4f0364ca4 321 for( i = 0; i < size; i++ )
mluis 0:45c4f0364ca4 322 {
mluis 0:45c4f0364ca4 323 buffer[i] = spi.write( 0 );
mluis 0:45c4f0364ca4 324 }
mluis 0:45c4f0364ca4 325 nss = 1;
mluis 0:45c4f0364ca4 326 }
mluis 0:45c4f0364ca4 327
mluis 0:45c4f0364ca4 328 void SX1272MB1DxS::WriteFifo( uint8_t *buffer, uint8_t size )
mluis 0:45c4f0364ca4 329 {
mluis 0:45c4f0364ca4 330 Write( 0, buffer, size );
mluis 0:45c4f0364ca4 331 }
mluis 0:45c4f0364ca4 332
mluis 0:45c4f0364ca4 333 void SX1272MB1DxS::ReadFifo( uint8_t *buffer, uint8_t size )
mluis 0:45c4f0364ca4 334 {
mluis 0:45c4f0364ca4 335 Read( 0, buffer, size );
mluis 0:45c4f0364ca4 336 }