SX1276 library for modtronix inair9. Edited for use with NRF51DK board.

Dependents:   InAir9_PingPong

Fork of SX1276Lib_modtronix by modtronix H

Committer:
modtronix
Date:
Tue Mar 03 10:42:25 2015 +0000
Revision:
17:6ae745bb937f
Parent:
16:0927c093fd82
Child:
18:cdb08d710838
Added FRDM-K64F

Who changed what in which revision?

UserRevisionLine numberNew contents of line
modtronix 16:0927c093fd82 1 /*
modtronix 16:0927c093fd82 2
modtronix 16:0927c093fd82 3 Description: -
modtronix 16:0927c093fd82 4
modtronix 16:0927c093fd82 5 License: Revised BSD License, see LICENSE.TXT file include in the project
modtronix 16:0927c093fd82 6
modtronix 16:0927c093fd82 7 Maintainers: Miguel Luis, Gregory Cristian and Nicolas Huguenin
modtronix 16:0927c093fd82 8 */
modtronix 17:6ae745bb937f 9 #include "myDebug.h"
modtronix 16:0927c093fd82 10 #include "sx1276-inAir.h"
modtronix 16:0927c093fd82 11
modtronix 16:0927c093fd82 12 const RadioRegisters_t SX1276inAir9::RadioRegsInit[] =
modtronix 16:0927c093fd82 13 {
modtronix 16:0927c093fd82 14 { MODEM_FSK , REG_LNA , 0x23 },
modtronix 16:0927c093fd82 15 { MODEM_FSK , REG_RXCONFIG , 0x1E },
modtronix 16:0927c093fd82 16 { MODEM_FSK , REG_RSSICONFIG , 0xD2 },
modtronix 16:0927c093fd82 17 { MODEM_FSK , REG_PREAMBLEDETECT , 0xAA },
modtronix 16:0927c093fd82 18 { MODEM_FSK , REG_OSC , 0x07 },
modtronix 16:0927c093fd82 19 { MODEM_FSK , REG_SYNCCONFIG , 0x12 },
modtronix 16:0927c093fd82 20 { MODEM_FSK , REG_SYNCVALUE1 , 0xC1 },
modtronix 16:0927c093fd82 21 { MODEM_FSK , REG_SYNCVALUE2 , 0x94 },
modtronix 16:0927c093fd82 22 { MODEM_FSK , REG_SYNCVALUE3 , 0xC1 },
modtronix 16:0927c093fd82 23 { MODEM_FSK , REG_PACKETCONFIG1 , 0xD8 },
modtronix 16:0927c093fd82 24 { MODEM_FSK , REG_FIFOTHRESH , 0x8F },
modtronix 16:0927c093fd82 25 { MODEM_FSK , REG_IMAGECAL , 0x02 },
modtronix 16:0927c093fd82 26 { MODEM_FSK , REG_DIOMAPPING1 , 0x00 },
modtronix 16:0927c093fd82 27 { MODEM_FSK , REG_DIOMAPPING2 , 0x30 },
modtronix 16:0927c093fd82 28 { MODEM_LORA, REG_LR_PAYLOADMAXLENGTH, 0x40 },
modtronix 16:0927c093fd82 29 };
modtronix 16:0927c093fd82 30
modtronix 16:0927c093fd82 31 SX1276inAir9::SX1276inAir9( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ),
modtronix 16:0927c093fd82 32 void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool ChannelActivityDetected ),
modtronix 16:0927c093fd82 33 PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset,
modtronix 16:0927c093fd82 34 PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5,
modtronix 16:0927c093fd82 35 PinName antSwitch )
modtronix 16:0927c093fd82 36 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5),
modtronix 16:0927c093fd82 37 antSwitch( NC ),
modtronix 16:0927c093fd82 38 fake( NC )
modtronix 16:0927c093fd82 39 {
modtronix 16:0927c093fd82 40 Reset( );
modtronix 16:0927c093fd82 41
modtronix 16:0927c093fd82 42 boardConnected = UNKNOWN;
modtronix 16:0927c093fd82 43
modtronix 16:0927c093fd82 44 RxChainCalibration( );
modtronix 16:0927c093fd82 45
modtronix 16:0927c093fd82 46 IoInit( );
modtronix 16:0927c093fd82 47
modtronix 16:0927c093fd82 48 SetOpMode( RF_OPMODE_SLEEP );
modtronix 16:0927c093fd82 49
modtronix 16:0927c093fd82 50 IoIrqInit( dioIrq );
modtronix 16:0927c093fd82 51
modtronix 16:0927c093fd82 52 RadioRegistersInit( );
modtronix 16:0927c093fd82 53
modtronix 16:0927c093fd82 54 SetModem( MODEM_FSK );
modtronix 16:0927c093fd82 55
modtronix 16:0927c093fd82 56 this->settings.State = IDLE ;
modtronix 16:0927c093fd82 57 }
modtronix 16:0927c093fd82 58
modtronix 16:0927c093fd82 59 SX1276inAir9::SX1276inAir9( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ),
modtronix 16:0927c093fd82 60 void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool ChannelActivityDetected ) )
modtronix 17:6ae745bb937f 61 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE))
modtronix 16:0927c093fd82 62 //For SHD3I with inAir9 in imod3, on Nucleo board
modtronix 16:0927c093fd82 63 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone,
modtronix 16:0927c093fd82 64 D11/*MOSI*/, D12/*MISO*/, D13/*SCLK*/, D7/*CS*/, A5/*RST*/, D2/*DIO0*/, D8, D4, A4, PC_3/*FAKE!!!*/, D3/*DIO5*/ ),
modtronix 17:6ae745bb937f 65 #elif( defined (TARGET_K64F) )
modtronix 17:6ae745bb937f 66 //For SHD3I with inAir9 in imod3, on Nucleo board
modtronix 17:6ae745bb937f 67 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone,
modtronix 17:6ae745bb937f 68 D11/*MOSI*/, D12/*MISO*/, D13/*SCLK*/, D7/*CS*/, A5/*RST*/, D2/*DIO0*/, D8, D4, A4, PTC0/*FAKE!!!*/, D3/*DIO5*/ ),
modtronix 16:0927c093fd82 69 #elif ( defined ( TARGET_NZ32ST1L ) )
modtronix 16:0927c093fd82 70 //For NZ32ST1L board with inAir9 in imod2
modtronix 16:0927c093fd82 71 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone,
modtronix 16:0927c093fd82 72 PB_5/*MOSI*/, PB_4/*MISO*/, PB_3/*SCLK*/, PC_8/*CS*/, PA_9/*RST*/, PB_0/*DIO0*/, PB_1, PC_6, PA_10, PC_3/*FAKE!!!*/, PC_13/*DIO5*/ ),
modtronix 16:0927c093fd82 73 #else
modtronix 16:0927c093fd82 74 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
modtronix 16:0927c093fd82 75 #endif
modtronix 16:0927c093fd82 76 antSwitch( NC ),
modtronix 16:0927c093fd82 77 fake( NC )
modtronix 16:0927c093fd82 78
modtronix 16:0927c093fd82 79 {
modtronix 16:0927c093fd82 80 Reset( );
modtronix 16:0927c093fd82 81
modtronix 16:0927c093fd82 82 boardConnected = UNKNOWN;
modtronix 16:0927c093fd82 83
modtronix 16:0927c093fd82 84 DetectBoardType( );
modtronix 16:0927c093fd82 85
modtronix 16:0927c093fd82 86 RxChainCalibration( );
modtronix 16:0927c093fd82 87
modtronix 16:0927c093fd82 88 IoInit( );
modtronix 16:0927c093fd82 89
modtronix 16:0927c093fd82 90 SetOpMode( RF_OPMODE_SLEEP );
modtronix 16:0927c093fd82 91 IoIrqInit( dioIrq );
modtronix 16:0927c093fd82 92
modtronix 16:0927c093fd82 93 RadioRegistersInit( );
modtronix 16:0927c093fd82 94
modtronix 16:0927c093fd82 95 SetModem( MODEM_FSK );
modtronix 16:0927c093fd82 96
modtronix 16:0927c093fd82 97 this->settings.State = IDLE ;
modtronix 16:0927c093fd82 98 }
modtronix 16:0927c093fd82 99
modtronix 16:0927c093fd82 100 //-------------------------------------------------------------------------
modtronix 16:0927c093fd82 101 // Board relative functions
modtronix 16:0927c093fd82 102 //-------------------------------------------------------------------------
modtronix 16:0927c093fd82 103 uint8_t SX1276inAir9::DetectBoardType( void )
modtronix 16:0927c093fd82 104 {
modtronix 16:0927c093fd82 105 // if( boardConnected == UNKNOWN )
modtronix 16:0927c093fd82 106 // {
modtronix 16:0927c093fd82 107 // antSwitch.input( );
modtronix 16:0927c093fd82 108 // wait_ms( 1 );
modtronix 16:0927c093fd82 109 // if( antSwitch == 1 )
modtronix 16:0927c093fd82 110 // {
modtronix 16:0927c093fd82 111 // boardConnected = SX1276MB1LAS;
modtronix 16:0927c093fd82 112 // }
modtronix 16:0927c093fd82 113 // else
modtronix 16:0927c093fd82 114 // {
modtronix 16:0927c093fd82 115 // boardConnected = SX1276MB1MAS;
modtronix 16:0927c093fd82 116 // }
modtronix 16:0927c093fd82 117 // antSwitch.output( );
modtronix 16:0927c093fd82 118 // wait_ms( 1 );
modtronix 16:0927c093fd82 119 // }
modtronix 16:0927c093fd82 120 boardConnected = SX1276MB1MAS; //Use SX1276MB1MAS, it is a 14dBm output board
modtronix 16:0927c093fd82 121
modtronix 16:0927c093fd82 122 return ( boardConnected );
modtronix 16:0927c093fd82 123 }
modtronix 16:0927c093fd82 124
modtronix 16:0927c093fd82 125 void SX1276inAir9::IoInit( void )
modtronix 16:0927c093fd82 126 {
modtronix 16:0927c093fd82 127 AntSwInit( );
modtronix 16:0927c093fd82 128 SpiInit( );
modtronix 16:0927c093fd82 129 }
modtronix 16:0927c093fd82 130
modtronix 16:0927c093fd82 131 void SX1276inAir9::RadioRegistersInit( ){
modtronix 16:0927c093fd82 132 uint8_t i = 0;
modtronix 16:0927c093fd82 133 for( i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t ); i++ )
modtronix 16:0927c093fd82 134 {
modtronix 16:0927c093fd82 135 SetModem( RadioRegsInit[i].Modem );
modtronix 16:0927c093fd82 136 Write( RadioRegsInit[i].Addr, RadioRegsInit[i].Value );
modtronix 16:0927c093fd82 137 }
modtronix 16:0927c093fd82 138 }
modtronix 16:0927c093fd82 139
modtronix 16:0927c093fd82 140 void SX1276inAir9::SpiInit( void )
modtronix 16:0927c093fd82 141 {
modtronix 16:0927c093fd82 142 nss = 1;
modtronix 16:0927c093fd82 143 spi.format( 8,0 );
modtronix 16:0927c093fd82 144 //uint32_t frequencyToSet = 8000000;
modtronix 16:0927c093fd82 145 uint32_t frequencyToSet = 1000000; //DJH - Reduced speed to 1MHz
modtronix 17:6ae745bb937f 146 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE) || defined ( TARGET_LPC11U6X ) || defined (TARGET_K64F) || defined ( TARGET_NZ32ST1L ) )
modtronix 16:0927c093fd82 147 debug( "SpiInit!\n\r", NULL );
modtronix 17:6ae745bb937f 148 //spi.frequency( frequencyToSet );
modtronix 16:0927c093fd82 149 #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
modtronix 16:0927c093fd82 150 spi.frequency( frequencyToSet * 2 );
modtronix 16:0927c093fd82 151 #else
modtronix 16:0927c093fd82 152 #warning "Check the board's SPI frequency"
modtronix 16:0927c093fd82 153 #endif
modtronix 16:0927c093fd82 154 wait(0.1);
modtronix 16:0927c093fd82 155 }
modtronix 16:0927c093fd82 156
modtronix 16:0927c093fd82 157 void SX1276inAir9::IoIrqInit( DioIrqHandler *irqHandlers )
modtronix 16:0927c093fd82 158 {
modtronix 17:6ae745bb937f 159 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE) || defined ( TARGET_LPC11U6X ) || defined (TARGET_K64F) || defined ( TARGET_NZ32ST1L ))
modtronix 16:0927c093fd82 160 dio0.mode(PullDown);
modtronix 16:0927c093fd82 161 dio1.mode(PullDown);
modtronix 16:0927c093fd82 162 dio2.mode(PullDown);
modtronix 16:0927c093fd82 163 dio3.mode(PullDown);
modtronix 16:0927c093fd82 164 dio4.mode(PullDown);
modtronix 16:0927c093fd82 165 #endif
modtronix 16:0927c093fd82 166 dio0.rise( this, static_cast< TriggerInAir > ( irqHandlers[0] ) );
modtronix 16:0927c093fd82 167 dio1.rise( this, static_cast< TriggerInAir > ( irqHandlers[1] ) );
modtronix 16:0927c093fd82 168 dio2.rise( this, static_cast< TriggerInAir > ( irqHandlers[2] ) );
modtronix 16:0927c093fd82 169 dio3.rise( this, static_cast< TriggerInAir > ( irqHandlers[3] ) );
modtronix 16:0927c093fd82 170 dio4.rise( this, static_cast< TriggerInAir > ( irqHandlers[4] ) );
modtronix 16:0927c093fd82 171 }
modtronix 16:0927c093fd82 172
modtronix 16:0927c093fd82 173 void SX1276inAir9::IoDeInit( void )
modtronix 16:0927c093fd82 174 {
modtronix 16:0927c093fd82 175 //nothing
modtronix 16:0927c093fd82 176 }
modtronix 16:0927c093fd82 177
modtronix 16:0927c093fd82 178 uint8_t SX1276inAir9::GetPaSelect( uint32_t channel )
modtronix 16:0927c093fd82 179 {
modtronix 16:0927c093fd82 180 if( channel > RF_MID_BAND_THRESH )
modtronix 16:0927c093fd82 181 {
modtronix 16:0927c093fd82 182 if( boardConnected == SX1276MB1LAS )
modtronix 16:0927c093fd82 183 {
modtronix 16:0927c093fd82 184 return RF_PACONFIG_PASELECT_PABOOST;
modtronix 16:0927c093fd82 185 }
modtronix 16:0927c093fd82 186 else
modtronix 16:0927c093fd82 187 {
modtronix 16:0927c093fd82 188 return RF_PACONFIG_PASELECT_RFO;
modtronix 16:0927c093fd82 189 }
modtronix 16:0927c093fd82 190 }
modtronix 16:0927c093fd82 191 else
modtronix 16:0927c093fd82 192 {
modtronix 16:0927c093fd82 193 return RF_PACONFIG_PASELECT_RFO;
modtronix 16:0927c093fd82 194 }
modtronix 16:0927c093fd82 195 }
modtronix 16:0927c093fd82 196
modtronix 16:0927c093fd82 197 void SX1276inAir9::SetAntSwLowPower( bool status )
modtronix 16:0927c093fd82 198 {
modtronix 16:0927c093fd82 199 if( isRadioActive != status )
modtronix 16:0927c093fd82 200 {
modtronix 16:0927c093fd82 201 isRadioActive = status;
modtronix 16:0927c093fd82 202
modtronix 16:0927c093fd82 203 if( status == false )
modtronix 16:0927c093fd82 204 {
modtronix 16:0927c093fd82 205 AntSwInit( );
modtronix 16:0927c093fd82 206 }
modtronix 16:0927c093fd82 207 else
modtronix 16:0927c093fd82 208 {
modtronix 16:0927c093fd82 209 AntSwDeInit( );
modtronix 16:0927c093fd82 210 }
modtronix 16:0927c093fd82 211 }
modtronix 16:0927c093fd82 212 }
modtronix 16:0927c093fd82 213
modtronix 16:0927c093fd82 214 void SX1276inAir9::AntSwInit( void )
modtronix 16:0927c093fd82 215 {
modtronix 16:0927c093fd82 216 //antSwitch = 0;
modtronix 16:0927c093fd82 217 }
modtronix 16:0927c093fd82 218
modtronix 16:0927c093fd82 219 void SX1276inAir9::AntSwDeInit( void )
modtronix 16:0927c093fd82 220 {
modtronix 16:0927c093fd82 221 //antSwitch = 0;
modtronix 16:0927c093fd82 222 }
modtronix 16:0927c093fd82 223
modtronix 16:0927c093fd82 224 void SX1276inAir9::SetAntSw( uint8_t rxTx )
modtronix 16:0927c093fd82 225 {
modtronix 16:0927c093fd82 226 if( this->rxTx == rxTx )
modtronix 16:0927c093fd82 227 {
modtronix 16:0927c093fd82 228 //no need to go further
modtronix 16:0927c093fd82 229 return;
modtronix 16:0927c093fd82 230 }
modtronix 16:0927c093fd82 231
modtronix 16:0927c093fd82 232 this->rxTx = rxTx;
modtronix 16:0927c093fd82 233
modtronix 16:0927c093fd82 234 // if( rxTx != 0 )
modtronix 16:0927c093fd82 235 // {
modtronix 16:0927c093fd82 236 // antSwitch = 1;
modtronix 16:0927c093fd82 237 // }
modtronix 16:0927c093fd82 238 // else
modtronix 16:0927c093fd82 239 // {
modtronix 16:0927c093fd82 240 // antSwitch = 0;
modtronix 16:0927c093fd82 241 // }
modtronix 16:0927c093fd82 242 }
modtronix 16:0927c093fd82 243
modtronix 16:0927c093fd82 244 bool SX1276inAir9::CheckRfFrequency( uint32_t frequency )
modtronix 16:0927c093fd82 245 {
modtronix 16:0927c093fd82 246 //TODO: Implement check, currently all frequencies are supported
modtronix 16:0927c093fd82 247 return true;
modtronix 16:0927c093fd82 248 }
modtronix 16:0927c093fd82 249
modtronix 16:0927c093fd82 250
modtronix 16:0927c093fd82 251 void SX1276inAir9::Reset( void )
modtronix 16:0927c093fd82 252 {
modtronix 16:0927c093fd82 253 reset.output();
modtronix 16:0927c093fd82 254 reset = 0;
modtronix 16:0927c093fd82 255 wait_ms( 1 );
modtronix 16:0927c093fd82 256 reset.input();
modtronix 16:0927c093fd82 257 wait_ms( 6 );
modtronix 16:0927c093fd82 258 }
modtronix 16:0927c093fd82 259
modtronix 16:0927c093fd82 260 void SX1276inAir9::Write( uint8_t addr, uint8_t data )
modtronix 16:0927c093fd82 261 {
modtronix 16:0927c093fd82 262 Write( addr, &data, 1 );
modtronix 16:0927c093fd82 263 }
modtronix 16:0927c093fd82 264
modtronix 16:0927c093fd82 265 uint8_t SX1276inAir9::Read( uint8_t addr )
modtronix 16:0927c093fd82 266 {
modtronix 16:0927c093fd82 267 uint8_t data;
modtronix 16:0927c093fd82 268 Read( addr, &data, 1 );
modtronix 16:0927c093fd82 269 return data;
modtronix 16:0927c093fd82 270 }
modtronix 16:0927c093fd82 271
modtronix 16:0927c093fd82 272 void SX1276inAir9::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 273 {
modtronix 16:0927c093fd82 274 uint8_t i;
modtronix 16:0927c093fd82 275
modtronix 16:0927c093fd82 276 nss = 0;
modtronix 16:0927c093fd82 277 spi.write( addr | 0x80 );
modtronix 16:0927c093fd82 278 for( i = 0; i < size; i++ )
modtronix 16:0927c093fd82 279 {
modtronix 16:0927c093fd82 280 spi.write( buffer[i] );
modtronix 16:0927c093fd82 281 }
modtronix 16:0927c093fd82 282 nss = 1;
modtronix 16:0927c093fd82 283 }
modtronix 16:0927c093fd82 284
modtronix 16:0927c093fd82 285 void SX1276inAir9::Read( uint8_t addr, uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 286 {
modtronix 16:0927c093fd82 287 uint8_t i;
modtronix 16:0927c093fd82 288
modtronix 16:0927c093fd82 289 nss = 0;
modtronix 16:0927c093fd82 290 spi.write( addr & 0x7F );
modtronix 16:0927c093fd82 291 for( i = 0; i < size; i++ )
modtronix 16:0927c093fd82 292 {
modtronix 16:0927c093fd82 293 buffer[i] = spi.write( 0 );
modtronix 16:0927c093fd82 294 }
modtronix 16:0927c093fd82 295 nss = 1;
modtronix 16:0927c093fd82 296 }
modtronix 16:0927c093fd82 297
modtronix 16:0927c093fd82 298 void SX1276inAir9::WriteFifo( uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 299 {
modtronix 16:0927c093fd82 300 Write( 0, buffer, size );
modtronix 16:0927c093fd82 301 }
modtronix 16:0927c093fd82 302
modtronix 16:0927c093fd82 303 void SX1276inAir9::ReadFifo( uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 304 {
modtronix 16:0927c093fd82 305 Read( 0, buffer, size );
modtronix 16:0927c093fd82 306 }