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 Jul 29 12:07:40 2015 +1000
Revision:
25:72381be1b0ce
Parent:
24:1346d1c71c40
Child:
26:ad32782125eb
Removed DIO4 and DIO5 from code. Not available on inAir Modules.

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 25:72381be1b0ce 34 PinName dio0, PinName dio1, PinName dio2, PinName dio3, /*PinName dio4, PinName dio5,*/
modtronix 16:0927c093fd82 35 PinName antSwitch )
modtronix 25:72381be1b0ce 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 25:72381be1b0ce 65 D11/*MOSI*/, D12/*MISO*/, D13/*SCLK*/, D7/*CS*/, A5/*RST*/, D2/*DIO0*/, D8, D4, A4),
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 25:72381be1b0ce 69 D11/*MOSI*/, D12/*MISO*/, D13/*SCLK*/, D7/*CS*/, A5/*RST*/, D2/*DIO0*/, D8, D4, A4),
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 25:72381be1b0ce 73 D11/*MOSI*/, D12/*MISO*/, D13/*SCLK*/, D7/*CS*/, A5/*RST*/, D2/*DIO0*/, D8, D4, A4),
modtronix 24:1346d1c71c40 74 #elif ( defined(TARGET_NZ32ST1L) || defined(TARGET_NZ32SC151) )
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 25:72381be1b0ce 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),
modtronix 16:0927c093fd82 78 #else
modtronix 25:72381be1b0ce 79 : SX1276( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, D11, D12, D13, D10, A0, D2, D3, D4, D5),
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 25:72381be1b0ce 133 uint32_t frequencyToSet = 8000000;
modtronix 24:1346d1c71c40 134 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE) || defined ( TARGET_LPC11U6X ) || defined (TARGET_K64F) || defined ( TARGET_NZ32ST1L ) || defined(TARGET_NZ32SC151) )
modtronix 18:cdb08d710838 135 spi.frequency( frequencyToSet );
modtronix 16:0927c093fd82 136 #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate
modtronix 16:0927c093fd82 137 spi.frequency( frequencyToSet * 2 );
modtronix 16:0927c093fd82 138 #else
modtronix 16:0927c093fd82 139 #warning "Check the board's SPI frequency"
modtronix 16:0927c093fd82 140 #endif
modtronix 16:0927c093fd82 141 wait(0.1);
modtronix 16:0927c093fd82 142 }
modtronix 16:0927c093fd82 143
modtronix 22:20db480143c9 144 void SX1276inAir::IoIrqInit( DioIrqHandler *irqHandlers )
modtronix 16:0927c093fd82 145 {
modtronix 18:cdb08d710838 146 //TARGET_KL25Z board does not have pulldown resistors, seems like TARGET_K64F does have them
modtronix 24:1346d1c71c40 147 #if( defined ( TARGET_NUCLEO_L152RE ) || defined (TARGET_NUCLEO_F401RE) || defined ( TARGET_LPC11U6X ) || defined (TARGET_K64F) || defined ( TARGET_NZ32ST1L ) || defined(TARGET_NZ32SC151))
modtronix 16:0927c093fd82 148 dio0.mode(PullDown);
modtronix 16:0927c093fd82 149 dio1.mode(PullDown);
modtronix 16:0927c093fd82 150 dio2.mode(PullDown);
modtronix 16:0927c093fd82 151 dio3.mode(PullDown);
modtronix 25:72381be1b0ce 152 //dio4.mode(PullDown);
modtronix 16:0927c093fd82 153 #endif
modtronix 16:0927c093fd82 154 dio0.rise( this, static_cast< TriggerInAir > ( irqHandlers[0] ) );
modtronix 16:0927c093fd82 155 dio1.rise( this, static_cast< TriggerInAir > ( irqHandlers[1] ) );
modtronix 16:0927c093fd82 156 dio2.rise( this, static_cast< TriggerInAir > ( irqHandlers[2] ) );
modtronix 22:20db480143c9 157 //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 158 #if( defined ( TARGET_KL25Z ) && defined(SHIELD_SHD3I_INAIR9) )
modtronix 18:cdb08d710838 159 //Nothing to be done
modtronix 18:cdb08d710838 160 #else
modtronix 16:0927c093fd82 161 dio3.rise( this, static_cast< TriggerInAir > ( irqHandlers[3] ) );
modtronix 18:cdb08d710838 162 #endif
modtronix 25:72381be1b0ce 163 //dio4.rise( this, static_cast< TriggerInAir > ( irqHandlers[4] ) );
modtronix 16:0927c093fd82 164 }
modtronix 16:0927c093fd82 165
modtronix 22:20db480143c9 166 void SX1276inAir::IoDeInit( void )
modtronix 16:0927c093fd82 167 {
modtronix 16:0927c093fd82 168 //nothing
modtronix 16:0927c093fd82 169 }
modtronix 16:0927c093fd82 170
modtronix 22:20db480143c9 171 uint8_t SX1276inAir::GetPaSelect( uint32_t channel )
modtronix 16:0927c093fd82 172 {
modtronix 22:20db480143c9 173 if(boardConnected == BOARD_INAIR9B) {
modtronix 22:20db480143c9 174 return RF_PACONFIG_PASELECT_PABOOST;
modtronix 22:20db480143c9 175 }
modtronix 22:20db480143c9 176 else {
modtronix 22:20db480143c9 177 return RF_PACONFIG_PASELECT_RFO;
modtronix 22:20db480143c9 178 }
modtronix 16:0927c093fd82 179 }
modtronix 16:0927c093fd82 180
modtronix 22:20db480143c9 181 void SX1276inAir::SetAntSwLowPower( bool status )
modtronix 16:0927c093fd82 182 {
modtronix 16:0927c093fd82 183 if( isRadioActive != status )
modtronix 16:0927c093fd82 184 {
modtronix 16:0927c093fd82 185 isRadioActive = status;
modtronix 16:0927c093fd82 186
modtronix 16:0927c093fd82 187 if( status == false )
modtronix 16:0927c093fd82 188 {
modtronix 16:0927c093fd82 189 AntSwInit( );
modtronix 16:0927c093fd82 190 }
modtronix 16:0927c093fd82 191 else
modtronix 16:0927c093fd82 192 {
modtronix 16:0927c093fd82 193 AntSwDeInit( );
modtronix 16:0927c093fd82 194 }
modtronix 16:0927c093fd82 195 }
modtronix 16:0927c093fd82 196 }
modtronix 16:0927c093fd82 197
modtronix 22:20db480143c9 198 void SX1276inAir::AntSwInit( void )
modtronix 16:0927c093fd82 199 {
modtronix 16:0927c093fd82 200 //antSwitch = 0;
modtronix 16:0927c093fd82 201 }
modtronix 16:0927c093fd82 202
modtronix 22:20db480143c9 203 void SX1276inAir::AntSwDeInit( void )
modtronix 16:0927c093fd82 204 {
modtronix 16:0927c093fd82 205 //antSwitch = 0;
modtronix 16:0927c093fd82 206 }
modtronix 16:0927c093fd82 207
modtronix 22:20db480143c9 208 void SX1276inAir::SetAntSw( uint8_t rxTx )
modtronix 16:0927c093fd82 209 {
modtronix 16:0927c093fd82 210 if( this->rxTx == rxTx )
modtronix 16:0927c093fd82 211 {
modtronix 16:0927c093fd82 212 //no need to go further
modtronix 16:0927c093fd82 213 return;
modtronix 16:0927c093fd82 214 }
modtronix 16:0927c093fd82 215
modtronix 16:0927c093fd82 216 this->rxTx = rxTx;
modtronix 16:0927c093fd82 217
modtronix 16:0927c093fd82 218 // if( rxTx != 0 )
modtronix 16:0927c093fd82 219 // {
modtronix 16:0927c093fd82 220 // antSwitch = 1;
modtronix 16:0927c093fd82 221 // }
modtronix 16:0927c093fd82 222 // else
modtronix 16:0927c093fd82 223 // {
modtronix 16:0927c093fd82 224 // antSwitch = 0;
modtronix 16:0927c093fd82 225 // }
modtronix 16:0927c093fd82 226 }
modtronix 16:0927c093fd82 227
modtronix 22:20db480143c9 228 bool SX1276inAir::CheckRfFrequency( uint32_t frequency )
modtronix 16:0927c093fd82 229 {
modtronix 16:0927c093fd82 230 //TODO: Implement check, currently all frequencies are supported
modtronix 16:0927c093fd82 231 return true;
modtronix 16:0927c093fd82 232 }
modtronix 16:0927c093fd82 233
modtronix 16:0927c093fd82 234
modtronix 22:20db480143c9 235 void SX1276inAir::Reset( void )
modtronix 16:0927c093fd82 236 {
modtronix 16:0927c093fd82 237 reset.output();
modtronix 16:0927c093fd82 238 reset = 0;
modtronix 16:0927c093fd82 239 wait_ms( 1 );
modtronix 16:0927c093fd82 240 reset.input();
modtronix 16:0927c093fd82 241 wait_ms( 6 );
modtronix 16:0927c093fd82 242 }
modtronix 16:0927c093fd82 243
modtronix 22:20db480143c9 244 void SX1276inAir::Write( uint8_t addr, uint8_t data )
modtronix 16:0927c093fd82 245 {
modtronix 16:0927c093fd82 246 Write( addr, &data, 1 );
modtronix 16:0927c093fd82 247 }
modtronix 16:0927c093fd82 248
modtronix 22:20db480143c9 249 uint8_t SX1276inAir::Read( uint8_t addr )
modtronix 16:0927c093fd82 250 {
modtronix 16:0927c093fd82 251 uint8_t data;
modtronix 16:0927c093fd82 252 Read( addr, &data, 1 );
modtronix 16:0927c093fd82 253 return data;
modtronix 16:0927c093fd82 254 }
modtronix 16:0927c093fd82 255
modtronix 22:20db480143c9 256 void SX1276inAir::Write( uint8_t addr, uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 257 {
modtronix 16:0927c093fd82 258 uint8_t i;
modtronix 16:0927c093fd82 259
modtronix 16:0927c093fd82 260 nss = 0;
modtronix 16:0927c093fd82 261 spi.write( addr | 0x80 );
modtronix 16:0927c093fd82 262 for( i = 0; i < size; i++ )
modtronix 16:0927c093fd82 263 {
modtronix 16:0927c093fd82 264 spi.write( buffer[i] );
modtronix 16:0927c093fd82 265 }
modtronix 16:0927c093fd82 266 nss = 1;
modtronix 16:0927c093fd82 267 }
modtronix 16:0927c093fd82 268
modtronix 22:20db480143c9 269 void SX1276inAir::Read( 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 & 0x7F );
modtronix 16:0927c093fd82 275 for( i = 0; i < size; i++ )
modtronix 16:0927c093fd82 276 {
modtronix 16:0927c093fd82 277 buffer[i] = spi.write( 0 );
modtronix 16:0927c093fd82 278 }
modtronix 16:0927c093fd82 279 nss = 1;
modtronix 16:0927c093fd82 280 }
modtronix 16:0927c093fd82 281
modtronix 22:20db480143c9 282 void SX1276inAir::WriteFifo( uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 283 {
modtronix 16:0927c093fd82 284 Write( 0, buffer, size );
modtronix 16:0927c093fd82 285 }
modtronix 16:0927c093fd82 286
modtronix 22:20db480143c9 287 void SX1276inAir::ReadFifo( uint8_t *buffer, uint8_t size )
modtronix 16:0927c093fd82 288 {
modtronix 16:0927c093fd82 289 Read( 0, buffer, size );
modtronix 16:0927c093fd82 290 }