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

Dependents:   InAir9_PingPong

Fork of SX1276Lib_modtronix by modtronix H

Committer:
modtronix
Date:
Mon Mar 02 23:54:25 2015 +0000
Revision:
16:0927c093fd82
Child:
17:6ae745bb937f
Enabled all bandwidths, added Modtronix inAir9 module

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 16:0927c093fd82 9 #include "debug.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
modtronix 16:0927c093fd82 60 SX1276inAir9::SX1276inAir9( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ),
modtronix 16:0927c093fd82 61 void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool ChannelActivityDetected ) )
modtronix 16:0927c093fd82 62 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE) )
modtronix 16:0927c093fd82 63 //For SHD3I with inAir9 in imod3, on Nucleo board
modtronix 16:0927c093fd82 64 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone,
modtronix 16:0927c093fd82 65 D11/*MOSI*/, D12/*MISO*/, D13/*SCLK*/, D7/*CS*/, A5/*RST*/, D2/*DIO0*/, D8, D4, A4, PC_3/*FAKE!!!*/, D3/*DIO5*/ ),
modtronix 16:0927c093fd82 66 #elif ( defined ( TARGET_NZ32ST1L ) )
modtronix 16:0927c093fd82 67 //For NZ32ST1L board with inAir9 in imod2
modtronix 16:0927c093fd82 68 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone,
modtronix 16:0927c093fd82 69 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 70 #else
modtronix 16:0927c093fd82 71 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
modtronix 16:0927c093fd82 72 #endif
modtronix 16:0927c093fd82 73 antSwitch( NC ),
modtronix 16:0927c093fd82 74 fake( NC )
modtronix 16:0927c093fd82 75
modtronix 16:0927c093fd82 76 {
modtronix 16:0927c093fd82 77 Reset( );
modtronix 16:0927c093fd82 78
modtronix 16:0927c093fd82 79 boardConnected = UNKNOWN;
modtronix 16:0927c093fd82 80
modtronix 16:0927c093fd82 81 DetectBoardType( );
modtronix 16:0927c093fd82 82
modtronix 16:0927c093fd82 83 RxChainCalibration( );
modtronix 16:0927c093fd82 84
modtronix 16:0927c093fd82 85 IoInit( );
modtronix 16:0927c093fd82 86
modtronix 16:0927c093fd82 87 SetOpMode( RF_OPMODE_SLEEP );
modtronix 16:0927c093fd82 88 IoIrqInit( dioIrq );
modtronix 16:0927c093fd82 89
modtronix 16:0927c093fd82 90 RadioRegistersInit( );
modtronix 16:0927c093fd82 91
modtronix 16:0927c093fd82 92 SetModem( MODEM_FSK );
modtronix 16:0927c093fd82 93
modtronix 16:0927c093fd82 94 this->settings.State = IDLE ;
modtronix 16:0927c093fd82 95 }
modtronix 16:0927c093fd82 96
modtronix 16:0927c093fd82 97 //-------------------------------------------------------------------------
modtronix 16:0927c093fd82 98 // Board relative functions
modtronix 16:0927c093fd82 99 //-------------------------------------------------------------------------
modtronix 16:0927c093fd82 100 uint8_t SX1276inAir9::DetectBoardType( void )
modtronix 16:0927c093fd82 101 {
modtronix 16:0927c093fd82 102 // if( boardConnected == UNKNOWN )
modtronix 16:0927c093fd82 103 // {
modtronix 16:0927c093fd82 104 // antSwitch.input( );
modtronix 16:0927c093fd82 105 // wait_ms( 1 );
modtronix 16:0927c093fd82 106 // if( antSwitch == 1 )
modtronix 16:0927c093fd82 107 // {
modtronix 16:0927c093fd82 108 // boardConnected = SX1276MB1LAS;
modtronix 16:0927c093fd82 109 // }
modtronix 16:0927c093fd82 110 // else
modtronix 16:0927c093fd82 111 // {
modtronix 16:0927c093fd82 112 // boardConnected = SX1276MB1MAS;
modtronix 16:0927c093fd82 113 // }
modtronix 16:0927c093fd82 114 // antSwitch.output( );
modtronix 16:0927c093fd82 115 // wait_ms( 1 );
modtronix 16:0927c093fd82 116 // }
modtronix 16:0927c093fd82 117 boardConnected = SX1276MB1MAS; //Use SX1276MB1MAS, it is a 14dBm output board
modtronix 16:0927c093fd82 118
modtronix 16:0927c093fd82 119 return ( boardConnected );
modtronix 16:0927c093fd82 120 }
modtronix 16:0927c093fd82 121
modtronix 16:0927c093fd82 122 void SX1276inAir9::IoInit( void )
modtronix 16:0927c093fd82 123 {
modtronix 16:0927c093fd82 124 AntSwInit( );
modtronix 16:0927c093fd82 125 SpiInit( );
modtronix 16:0927c093fd82 126 }
modtronix 16:0927c093fd82 127
modtronix 16:0927c093fd82 128 void SX1276inAir9::RadioRegistersInit( ){
modtronix 16:0927c093fd82 129 uint8_t i = 0;
modtronix 16:0927c093fd82 130 for( i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t ); i++ )
modtronix 16:0927c093fd82 131 {
modtronix 16:0927c093fd82 132 SetModem( RadioRegsInit[i].Modem );
modtronix 16:0927c093fd82 133 Write( RadioRegsInit[i].Addr, RadioRegsInit[i].Value );
modtronix 16:0927c093fd82 134 }
modtronix 16:0927c093fd82 135 }
modtronix 16:0927c093fd82 136
modtronix 16:0927c093fd82 137 void SX1276inAir9::SpiInit( void )
modtronix 16:0927c093fd82 138 {
modtronix 16:0927c093fd82 139 nss = 1;
modtronix 16:0927c093fd82 140 spi.format( 8,0 );
modtronix 16:0927c093fd82 141 //uint32_t frequencyToSet = 8000000;
modtronix 16:0927c093fd82 142 uint32_t frequencyToSet = 1000000; //DJH - Reduced speed to 1MHz
modtronix 16:0927c093fd82 143 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE) || defined ( TARGET_LPC11U6X ) || defined ( TARGET_NZ32ST1L ) )
modtronix 16:0927c093fd82 144 debug( "SpiInit!\n\r", NULL );
modtronix 16:0927c093fd82 145 spi.frequency( frequencyToSet );
modtronix 16:0927c093fd82 146 #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
modtronix 16:0927c093fd82 147 spi.frequency( frequencyToSet * 2 );
modtronix 16:0927c093fd82 148 #else
modtronix 16:0927c093fd82 149 #warning "Check the board's SPI frequency"
modtronix 16:0927c093fd82 150 #endif
modtronix 16:0927c093fd82 151 wait(0.1);
modtronix 16:0927c093fd82 152 }
modtronix 16:0927c093fd82 153
modtronix 16:0927c093fd82 154 void SX1276inAir9::IoIrqInit( DioIrqHandler *irqHandlers )
modtronix 16:0927c093fd82 155 {
modtronix 16:0927c093fd82 156 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE) || defined ( TARGET_LPC11U6X ) || defined ( TARGET_NZ32ST1L ))
modtronix 16:0927c093fd82 157 dio0.mode(PullDown);
modtronix 16:0927c093fd82 158 dio1.mode(PullDown);
modtronix 16:0927c093fd82 159 dio2.mode(PullDown);
modtronix 16:0927c093fd82 160 dio3.mode(PullDown);
modtronix 16:0927c093fd82 161 dio4.mode(PullDown);
modtronix 16:0927c093fd82 162 #endif
modtronix 16:0927c093fd82 163 dio0.rise( this, static_cast< TriggerInAir > ( irqHandlers[0] ) );
modtronix 16:0927c093fd82 164 dio1.rise( this, static_cast< TriggerInAir > ( irqHandlers[1] ) );
modtronix 16:0927c093fd82 165 dio2.rise( this, static_cast< TriggerInAir > ( irqHandlers[2] ) );
modtronix 16:0927c093fd82 166 dio3.rise( this, static_cast< TriggerInAir > ( irqHandlers[3] ) );
modtronix 16:0927c093fd82 167 dio4.rise( this, static_cast< TriggerInAir > ( irqHandlers[4] ) );
modtronix 16:0927c093fd82 168 }
modtronix 16:0927c093fd82 169
modtronix 16:0927c093fd82 170 void SX1276inAir9::IoDeInit( void )
modtronix 16:0927c093fd82 171 {
modtronix 16:0927c093fd82 172 //nothing
modtronix 16:0927c093fd82 173 }
modtronix 16:0927c093fd82 174
modtronix 16:0927c093fd82 175 uint8_t SX1276inAir9::GetPaSelect( uint32_t channel )
modtronix 16:0927c093fd82 176 {
modtronix 16:0927c093fd82 177 if( channel > RF_MID_BAND_THRESH )
modtronix 16:0927c093fd82 178 {
modtronix 16:0927c093fd82 179 if( boardConnected == SX1276MB1LAS )
modtronix 16:0927c093fd82 180 {
modtronix 16:0927c093fd82 181 return RF_PACONFIG_PASELECT_PABOOST;
modtronix 16:0927c093fd82 182 }
modtronix 16:0927c093fd82 183 else
modtronix 16:0927c093fd82 184 {
modtronix 16:0927c093fd82 185 return RF_PACONFIG_PASELECT_RFO;
modtronix 16:0927c093fd82 186 }
modtronix 16:0927c093fd82 187 }
modtronix 16:0927c093fd82 188 else
modtronix 16:0927c093fd82 189 {
modtronix 16:0927c093fd82 190 return RF_PACONFIG_PASELECT_RFO;
modtronix 16:0927c093fd82 191 }
modtronix 16:0927c093fd82 192 }
modtronix 16:0927c093fd82 193
modtronix 16:0927c093fd82 194 void SX1276inAir9::SetAntSwLowPower( bool status )
modtronix 16:0927c093fd82 195 {
modtronix 16:0927c093fd82 196 if( isRadioActive != status )
modtronix 16:0927c093fd82 197 {
modtronix 16:0927c093fd82 198 isRadioActive = status;
modtronix 16:0927c093fd82 199
modtronix 16:0927c093fd82 200 if( status == false )
modtronix 16:0927c093fd82 201 {
modtronix 16:0927c093fd82 202 AntSwInit( );
modtronix 16:0927c093fd82 203 }
modtronix 16:0927c093fd82 204 else
modtronix 16:0927c093fd82 205 {
modtronix 16:0927c093fd82 206 AntSwDeInit( );
modtronix 16:0927c093fd82 207 }
modtronix 16:0927c093fd82 208 }
modtronix 16:0927c093fd82 209 }
modtronix 16:0927c093fd82 210
modtronix 16:0927c093fd82 211 void SX1276inAir9::AntSwInit( void )
modtronix 16:0927c093fd82 212 {
modtronix 16:0927c093fd82 213 //antSwitch = 0;
modtronix 16:0927c093fd82 214 }
modtronix 16:0927c093fd82 215
modtronix 16:0927c093fd82 216 void SX1276inAir9::AntSwDeInit( void )
modtronix 16:0927c093fd82 217 {
modtronix 16:0927c093fd82 218 //antSwitch = 0;
modtronix 16:0927c093fd82 219 }
modtronix 16:0927c093fd82 220
modtronix 16:0927c093fd82 221 void SX1276inAir9::SetAntSw( uint8_t rxTx )
modtronix 16:0927c093fd82 222 {
modtronix 16:0927c093fd82 223 if( this->rxTx == rxTx )
modtronix 16:0927c093fd82 224 {
modtronix 16:0927c093fd82 225 //no need to go further
modtronix 16:0927c093fd82 226 return;
modtronix 16:0927c093fd82 227 }
modtronix 16:0927c093fd82 228
modtronix 16:0927c093fd82 229 this->rxTx = rxTx;
modtronix 16:0927c093fd82 230
modtronix 16:0927c093fd82 231 // if( rxTx != 0 )
modtronix 16:0927c093fd82 232 // {
modtronix 16:0927c093fd82 233 // antSwitch = 1;
modtronix 16:0927c093fd82 234 // }
modtronix 16:0927c093fd82 235 // else
modtronix 16:0927c093fd82 236 // {
modtronix 16:0927c093fd82 237 // antSwitch = 0;
modtronix 16:0927c093fd82 238 // }
modtronix 16:0927c093fd82 239 }
modtronix 16:0927c093fd82 240
modtronix 16:0927c093fd82 241 bool SX1276inAir9::CheckRfFrequency( uint32_t frequency )
modtronix 16:0927c093fd82 242 {
modtronix 16:0927c093fd82 243 //TODO: Implement check, currently all frequencies are supported
modtronix 16:0927c093fd82 244 return true;
modtronix 16:0927c093fd82 245 }
modtronix 16:0927c093fd82 246
modtronix 16:0927c093fd82 247
modtronix 16:0927c093fd82 248 void SX1276inAir9::Reset( void )
modtronix 16:0927c093fd82 249 {
modtronix 16:0927c093fd82 250 reset.output();
modtronix 16:0927c093fd82 251 reset = 0;
modtronix 16:0927c093fd82 252 wait_ms( 1 );
modtronix 16:0927c093fd82 253 reset.input();
modtronix 16:0927c093fd82 254 wait_ms( 6 );
modtronix 16:0927c093fd82 255 }
modtronix 16:0927c093fd82 256
modtronix 16:0927c093fd82 257 void SX1276inAir9::Write( uint8_t addr, uint8_t data )
modtronix 16:0927c093fd82 258 {
modtronix 16:0927c093fd82 259 Write( addr, &data, 1 );
modtronix 16:0927c093fd82 260 }
modtronix 16:0927c093fd82 261
modtronix 16:0927c093fd82 262 uint8_t SX1276inAir9::Read( uint8_t addr )
modtronix 16:0927c093fd82 263 {
modtronix 16:0927c093fd82 264 uint8_t data;
modtronix 16:0927c093fd82 265 Read( addr, &data, 1 );
modtronix 16:0927c093fd82 266 return data;
modtronix 16:0927c093fd82 267 }
modtronix 16:0927c093fd82 268
modtronix 16:0927c093fd82 269 void SX1276inAir9::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 270 {
modtronix 16:0927c093fd82 271 uint8_t i;
modtronix 16:0927c093fd82 272
modtronix 16:0927c093fd82 273 nss = 0;
modtronix 16:0927c093fd82 274 spi.write( addr | 0x80 );
modtronix 16:0927c093fd82 275 for( i = 0; i < size; i++ )
modtronix 16:0927c093fd82 276 {
modtronix 16:0927c093fd82 277 spi.write( buffer[i] );
modtronix 16:0927c093fd82 278 }
modtronix 16:0927c093fd82 279 nss = 1;
modtronix 16:0927c093fd82 280 }
modtronix 16:0927c093fd82 281
modtronix 16:0927c093fd82 282 void SX1276inAir9::Read( uint8_t addr, uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 283 {
modtronix 16:0927c093fd82 284 uint8_t i;
modtronix 16:0927c093fd82 285
modtronix 16:0927c093fd82 286 nss = 0;
modtronix 16:0927c093fd82 287 spi.write( addr & 0x7F );
modtronix 16:0927c093fd82 288 for( i = 0; i < size; i++ )
modtronix 16:0927c093fd82 289 {
modtronix 16:0927c093fd82 290 buffer[i] = spi.write( 0 );
modtronix 16:0927c093fd82 291 }
modtronix 16:0927c093fd82 292 nss = 1;
modtronix 16:0927c093fd82 293 }
modtronix 16:0927c093fd82 294
modtronix 16:0927c093fd82 295 void SX1276inAir9::WriteFifo( uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 296 {
modtronix 16:0927c093fd82 297 Write( 0, buffer, size );
modtronix 16:0927c093fd82 298 }
modtronix 16:0927c093fd82 299
modtronix 16:0927c093fd82 300 void SX1276inAir9::ReadFifo( uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 301 {
modtronix 16:0927c093fd82 302 Read( 0, buffer, size );
modtronix 16:0927c093fd82 303 }