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

Dependents:   InAir9_PingPong

Fork of SX1276Lib_modtronix by modtronix H

Committer:
modtronix
Date:
Wed May 13 10:24:04 2015 +1000
Revision:
22:20db480143c9
Parent:
21:d3f64a075f0c
Child:
24:1346d1c71c40
Changed SX1276inAir9 to SX1276inAir. Added support for inAir4, inAir9 and
inAir9B boards.

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 22:20db480143c9 12 const RadioRegisters_t SX1276inAir::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 22:20db480143c9 31 SX1276inAir::SX1276inAir( 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 22:20db480143c9 42 boardConnected = BOARD_UNKNOWN;
modtronix 22:20db480143c9 43 DetectBoardType( );
modtronix 16:0927c093fd82 44
modtronix 16:0927c093fd82 45 RxChainCalibration( );
modtronix 16:0927c093fd82 46
modtronix 16:0927c093fd82 47 IoInit( );
modtronix 16:0927c093fd82 48
modtronix 16:0927c093fd82 49 SetOpMode( RF_OPMODE_SLEEP );
modtronix 16:0927c093fd82 50
modtronix 16:0927c093fd82 51 IoIrqInit( dioIrq );
modtronix 16:0927c093fd82 52
modtronix 16:0927c093fd82 53 RadioRegistersInit( );
modtronix 16:0927c093fd82 54
modtronix 16:0927c093fd82 55 SetModem( MODEM_FSK );
modtronix 16:0927c093fd82 56
modtronix 16:0927c093fd82 57 this->settings.State = IDLE ;
modtronix 16:0927c093fd82 58 }
modtronix 16:0927c093fd82 59
modtronix 22:20db480143c9 60 SX1276inAir::SX1276inAir( 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 18:cdb08d710838 62 #if( (defined (TARGET_NUCLEO_L152RE) || defined (TARGET_NUCLEO_F401RE)) && defined(SHIELD_SHD3I_INAIR9))
modtronix 22:20db480143c9 63 //For SHD3I with BOARD_INAIR4 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 18:cdb08d710838 66 #elif( defined (TARGET_K64F) && defined(SHIELD_SHD3I_INAIR9) ) /* Use pin PTC0 as fake pin */
modtronix 22:20db480143c9 67 //For SHD3I with BOARD_INAIR4 in imod3, on FRDM-K64F board
modtronix 17:6ae745bb937f 68 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone,
modtronix 17:6ae745bb937f 69 D11/*MOSI*/, D12/*MISO*/, D13/*SCLK*/, D7/*CS*/, A5/*RST*/, D2/*DIO0*/, D8, D4, A4, PTC0/*FAKE!!!*/, D3/*DIO5*/ ),
modtronix 18:cdb08d710838 70 #elif( defined (TARGET_KL25Z) && defined(SHIELD_SHD3I_INAIR9) ) /* Use pin PTD7 as fake pin - only A & D pins have interrupts */
modtronix 22:20db480143c9 71 //For SHD3I with BOARD_INAIR4 in imod3, on FRDM-KL25Z board
modtronix 18:cdb08d710838 72 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone,
modtronix 18:cdb08d710838 73 D11/*MOSI*/, D12/*MISO*/, D13/*SCLK*/, D7/*CS*/, A5/*RST*/, D2/*DIO0*/, D8, D4, A4, PTD7/*FAKE!!!*/, D3/*DIO5*/ ),
modtronix 16:0927c093fd82 74 #elif ( defined ( TARGET_NZ32ST1L ) )
modtronix 22:20db480143c9 75 //For NZ32ST1L board with BOARD_INAIR4 in imod2
modtronix 16:0927c093fd82 76 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone,
modtronix 16:0927c093fd82 77 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 78 #else
modtronix 16:0927c093fd82 79 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, D11, D12, D13, D10, A0, D2, D3, D4, D5, D8, D9 ),
modtronix 16:0927c093fd82 80 #endif
modtronix 16:0927c093fd82 81 antSwitch( NC ),
modtronix 16:0927c093fd82 82 fake( NC )
modtronix 16:0927c093fd82 83
modtronix 16:0927c093fd82 84 {
modtronix 16:0927c093fd82 85 Reset( );
modtronix 16:0927c093fd82 86
modtronix 22:20db480143c9 87 boardConnected = BOARD_UNKNOWN;
modtronix 16:0927c093fd82 88 DetectBoardType( );
modtronix 16:0927c093fd82 89
modtronix 16:0927c093fd82 90 RxChainCalibration( );
modtronix 16:0927c093fd82 91
modtronix 16:0927c093fd82 92 IoInit( );
modtronix 16:0927c093fd82 93
modtronix 16:0927c093fd82 94 SetOpMode( RF_OPMODE_SLEEP );
modtronix 16:0927c093fd82 95 IoIrqInit( dioIrq );
modtronix 16:0927c093fd82 96
modtronix 16:0927c093fd82 97 RadioRegistersInit( );
modtronix 16:0927c093fd82 98
modtronix 16:0927c093fd82 99 SetModem( MODEM_FSK );
modtronix 16:0927c093fd82 100
modtronix 16:0927c093fd82 101 this->settings.State = IDLE ;
modtronix 16:0927c093fd82 102 }
modtronix 16:0927c093fd82 103
modtronix 16:0927c093fd82 104 //-------------------------------------------------------------------------
modtronix 16:0927c093fd82 105 // Board relative functions
modtronix 16:0927c093fd82 106 //-------------------------------------------------------------------------
modtronix 22:20db480143c9 107 uint8_t SX1276inAir::DetectBoardType( void )
modtronix 16:0927c093fd82 108 {
modtronix 22:20db480143c9 109 //Detect board type NOT possible with inAir4, inAir9 and inAir9B boards. User has to call
modtronix 22:20db480143c9 110 //the SetBoardType() function to set the board type!
modtronix 22:20db480143c9 111 return ( boardConnected );
modtronix 16:0927c093fd82 112 }
modtronix 16:0927c093fd82 113
modtronix 22:20db480143c9 114 void SX1276inAir::IoInit( void )
modtronix 16:0927c093fd82 115 {
modtronix 16:0927c093fd82 116 AntSwInit( );
modtronix 16:0927c093fd82 117 SpiInit( );
modtronix 16:0927c093fd82 118 }
modtronix 16:0927c093fd82 119
modtronix 22:20db480143c9 120 void SX1276inAir::RadioRegistersInit( ){
modtronix 16:0927c093fd82 121 uint8_t i = 0;
modtronix 16:0927c093fd82 122 for( i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t ); i++ )
modtronix 16:0927c093fd82 123 {
modtronix 16:0927c093fd82 124 SetModem( RadioRegsInit[i].Modem );
modtronix 16:0927c093fd82 125 Write( RadioRegsInit[i].Addr, RadioRegsInit[i].Value );
modtronix 16:0927c093fd82 126 }
modtronix 16:0927c093fd82 127 }
modtronix 16:0927c093fd82 128
modtronix 22:20db480143c9 129 void SX1276inAir::SpiInit( void )
modtronix 16:0927c093fd82 130 {
modtronix 16:0927c093fd82 131 nss = 1;
modtronix 16:0927c093fd82 132 spi.format( 8,0 );
modtronix 16:0927c093fd82 133 //uint32_t frequencyToSet = 8000000;
modtronix 22:20db480143c9 134 // TEST TEST
modtronix 16:0927c093fd82 135 uint32_t frequencyToSet = 1000000; //DJH - Reduced speed to 1MHz
modtronix 17:6ae745bb937f 136 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE) || defined ( TARGET_LPC11U6X ) || defined (TARGET_K64F) || defined ( TARGET_NZ32ST1L ) )
modtronix 18:cdb08d710838 137 spi.frequency( frequencyToSet );
modtronix 16:0927c093fd82 138 #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
modtronix 16:0927c093fd82 139 spi.frequency( frequencyToSet * 2 );
modtronix 16:0927c093fd82 140 #else
modtronix 16:0927c093fd82 141 #warning "Check the board's SPI frequency"
modtronix 16:0927c093fd82 142 #endif
modtronix 16:0927c093fd82 143 wait(0.1);
modtronix 16:0927c093fd82 144 }
modtronix 16:0927c093fd82 145
modtronix 22:20db480143c9 146 void SX1276inAir::IoIrqInit( DioIrqHandler *irqHandlers )
modtronix 16:0927c093fd82 147 {
modtronix 18:cdb08d710838 148 //TARGET_KL25Z board does not have pulldown resistors, seems like TARGET_K64F does have them
modtronix 17:6ae745bb937f 149 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE) || defined ( TARGET_LPC11U6X ) || defined (TARGET_K64F) || defined ( TARGET_NZ32ST1L ))
modtronix 16:0927c093fd82 150 dio0.mode(PullDown);
modtronix 16:0927c093fd82 151 dio1.mode(PullDown);
modtronix 16:0927c093fd82 152 dio2.mode(PullDown);
modtronix 16:0927c093fd82 153 dio3.mode(PullDown);
modtronix 16:0927c093fd82 154 dio4.mode(PullDown);
modtronix 16:0927c093fd82 155 #endif
modtronix 16:0927c093fd82 156 dio0.rise( this, static_cast< TriggerInAir > ( irqHandlers[0] ) );
modtronix 16:0927c093fd82 157 dio1.rise( this, static_cast< TriggerInAir > ( irqHandlers[1] ) );
modtronix 16:0927c093fd82 158 dio2.rise( this, static_cast< TriggerInAir > ( irqHandlers[2] ) );
modtronix 22:20db480143c9 159 //For SHD3I with BOARD_INAIR4 in imod3, on FRDM-KL25Z board. It uses A4 on FRDM-KL25Z board, which does not have interrupt
modtronix 18:cdb08d710838 160 #if( defined ( TARGET_KL25Z ) && defined(SHIELD_SHD3I_INAIR9) )
modtronix 18:cdb08d710838 161 //Nothing to be done
modtronix 18:cdb08d710838 162 #else
modtronix 16:0927c093fd82 163 dio3.rise( this, static_cast< TriggerInAir > ( irqHandlers[3] ) );
modtronix 18:cdb08d710838 164 #endif
modtronix 16:0927c093fd82 165 dio4.rise( this, static_cast< TriggerInAir > ( irqHandlers[4] ) );
modtronix 16:0927c093fd82 166 }
modtronix 16:0927c093fd82 167
modtronix 22:20db480143c9 168 void SX1276inAir::IoDeInit( void )
modtronix 16:0927c093fd82 169 {
modtronix 16:0927c093fd82 170 //nothing
modtronix 16:0927c093fd82 171 }
modtronix 16:0927c093fd82 172
modtronix 22:20db480143c9 173 uint8_t SX1276inAir::GetPaSelect( uint32_t channel )
modtronix 16:0927c093fd82 174 {
modtronix 22:20db480143c9 175 if(boardConnected == BOARD_INAIR9B) {
modtronix 22:20db480143c9 176 return RF_PACONFIG_PASELECT_PABOOST;
modtronix 22:20db480143c9 177 }
modtronix 22:20db480143c9 178 else {
modtronix 22:20db480143c9 179 return RF_PACONFIG_PASELECT_RFO;
modtronix 22:20db480143c9 180 }
modtronix 16:0927c093fd82 181 }
modtronix 16:0927c093fd82 182
modtronix 22:20db480143c9 183 void SX1276inAir::SetAntSwLowPower( bool status )
modtronix 16:0927c093fd82 184 {
modtronix 16:0927c093fd82 185 if( isRadioActive != status )
modtronix 16:0927c093fd82 186 {
modtronix 16:0927c093fd82 187 isRadioActive = status;
modtronix 16:0927c093fd82 188
modtronix 16:0927c093fd82 189 if( status == false )
modtronix 16:0927c093fd82 190 {
modtronix 16:0927c093fd82 191 AntSwInit( );
modtronix 16:0927c093fd82 192 }
modtronix 16:0927c093fd82 193 else
modtronix 16:0927c093fd82 194 {
modtronix 16:0927c093fd82 195 AntSwDeInit( );
modtronix 16:0927c093fd82 196 }
modtronix 16:0927c093fd82 197 }
modtronix 16:0927c093fd82 198 }
modtronix 16:0927c093fd82 199
modtronix 22:20db480143c9 200 void SX1276inAir::AntSwInit( void )
modtronix 16:0927c093fd82 201 {
modtronix 16:0927c093fd82 202 //antSwitch = 0;
modtronix 16:0927c093fd82 203 }
modtronix 16:0927c093fd82 204
modtronix 22:20db480143c9 205 void SX1276inAir::AntSwDeInit( void )
modtronix 16:0927c093fd82 206 {
modtronix 16:0927c093fd82 207 //antSwitch = 0;
modtronix 16:0927c093fd82 208 }
modtronix 16:0927c093fd82 209
modtronix 22:20db480143c9 210 void SX1276inAir::SetAntSw( uint8_t rxTx )
modtronix 16:0927c093fd82 211 {
modtronix 16:0927c093fd82 212 if( this->rxTx == rxTx )
modtronix 16:0927c093fd82 213 {
modtronix 16:0927c093fd82 214 //no need to go further
modtronix 16:0927c093fd82 215 return;
modtronix 16:0927c093fd82 216 }
modtronix 16:0927c093fd82 217
modtronix 16:0927c093fd82 218 this->rxTx = rxTx;
modtronix 16:0927c093fd82 219
modtronix 16:0927c093fd82 220 // if( rxTx != 0 )
modtronix 16:0927c093fd82 221 // {
modtronix 16:0927c093fd82 222 // antSwitch = 1;
modtronix 16:0927c093fd82 223 // }
modtronix 16:0927c093fd82 224 // else
modtronix 16:0927c093fd82 225 // {
modtronix 16:0927c093fd82 226 // antSwitch = 0;
modtronix 16:0927c093fd82 227 // }
modtronix 16:0927c093fd82 228 }
modtronix 16:0927c093fd82 229
modtronix 22:20db480143c9 230 bool SX1276inAir::CheckRfFrequency( uint32_t frequency )
modtronix 16:0927c093fd82 231 {
modtronix 16:0927c093fd82 232 //TODO: Implement check, currently all frequencies are supported
modtronix 16:0927c093fd82 233 return true;
modtronix 16:0927c093fd82 234 }
modtronix 16:0927c093fd82 235
modtronix 16:0927c093fd82 236
modtronix 22:20db480143c9 237 void SX1276inAir::Reset( void )
modtronix 16:0927c093fd82 238 {
modtronix 16:0927c093fd82 239 reset.output();
modtronix 16:0927c093fd82 240 reset = 0;
modtronix 16:0927c093fd82 241 wait_ms( 1 );
modtronix 16:0927c093fd82 242 reset.input();
modtronix 16:0927c093fd82 243 wait_ms( 6 );
modtronix 16:0927c093fd82 244 }
modtronix 16:0927c093fd82 245
modtronix 22:20db480143c9 246 void SX1276inAir::Write( uint8_t addr, uint8_t data )
modtronix 16:0927c093fd82 247 {
modtronix 16:0927c093fd82 248 Write( addr, &data, 1 );
modtronix 16:0927c093fd82 249 }
modtronix 16:0927c093fd82 250
modtronix 22:20db480143c9 251 uint8_t SX1276inAir::Read( uint8_t addr )
modtronix 16:0927c093fd82 252 {
modtronix 16:0927c093fd82 253 uint8_t data;
modtronix 16:0927c093fd82 254 Read( addr, &data, 1 );
modtronix 16:0927c093fd82 255 return data;
modtronix 16:0927c093fd82 256 }
modtronix 16:0927c093fd82 257
modtronix 22:20db480143c9 258 void SX1276inAir::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 259 {
modtronix 16:0927c093fd82 260 uint8_t i;
modtronix 16:0927c093fd82 261
modtronix 16:0927c093fd82 262 nss = 0;
modtronix 16:0927c093fd82 263 spi.write( addr | 0x80 );
modtronix 16:0927c093fd82 264 for( i = 0; i < size; i++ )
modtronix 16:0927c093fd82 265 {
modtronix 16:0927c093fd82 266 spi.write( buffer[i] );
modtronix 16:0927c093fd82 267 }
modtronix 16:0927c093fd82 268 nss = 1;
modtronix 16:0927c093fd82 269 }
modtronix 16:0927c093fd82 270
modtronix 22:20db480143c9 271 void SX1276inAir::Read( uint8_t addr, uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 272 {
modtronix 16:0927c093fd82 273 uint8_t i;
modtronix 16:0927c093fd82 274
modtronix 16:0927c093fd82 275 nss = 0;
modtronix 16:0927c093fd82 276 spi.write( addr & 0x7F );
modtronix 16:0927c093fd82 277 for( i = 0; i < size; i++ )
modtronix 16:0927c093fd82 278 {
modtronix 16:0927c093fd82 279 buffer[i] = spi.write( 0 );
modtronix 16:0927c093fd82 280 }
modtronix 16:0927c093fd82 281 nss = 1;
modtronix 16:0927c093fd82 282 }
modtronix 16:0927c093fd82 283
modtronix 22:20db480143c9 284 void SX1276inAir::WriteFifo( uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 285 {
modtronix 16:0927c093fd82 286 Write( 0, buffer, size );
modtronix 16:0927c093fd82 287 }
modtronix 16:0927c093fd82 288
modtronix 22:20db480143c9 289 void SX1276inAir::ReadFifo( uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 290 {
modtronix 16:0927c093fd82 291 Read( 0, buffer, size );
modtronix 16:0927c093fd82 292 }