This library update contains changes according to the HW-modification.
Dependents: LoRaWAN_Serial_port_driven_and_configurable_ELMO_based_on_TxRx_Template
Fork of SX1272lib by
Changes compared to original SX1272lib:
HW modification was made to remove RFO-output and replaced with PABOOST-output. PASELECT changed accordingly.
sx1272/sx1272-hal.cpp@1:51fd7a5843e5, 2015-09-17 (annotated)
- Committer:
- WGorniak
- Date:
- Thu Sep 17 15:03:32 2015 +0200
- Revision:
- 1:51fd7a5843e5
- Parent:
- 0:669f3b0e91c8
- Child:
- 2:b0a793d995ad
renamed class implementing driver for sx1272 boards
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WGorniak | 0:669f3b0e91c8 | 1 | /* |
WGorniak | 0:669f3b0e91c8 | 2 | / _____) _ | | |
WGorniak | 0:669f3b0e91c8 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
WGorniak | 0:669f3b0e91c8 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
WGorniak | 0:669f3b0e91c8 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
WGorniak | 0:669f3b0e91c8 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
WGorniak | 0:669f3b0e91c8 | 7 | ( C ) Semtech |
WGorniak | 0:669f3b0e91c8 | 8 | |
WGorniak | 0:669f3b0e91c8 | 9 | Description: Port of Semtech SX1276 C++ library to support SX1272 chipset performed by Espotel |
WGorniak | 0:669f3b0e91c8 | 10 | |
WGorniak | 0:669f3b0e91c8 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
WGorniak | 0:669f3b0e91c8 | 12 | |
WGorniak | 0:669f3b0e91c8 | 13 | Maintainers: www.espotel.com |
WGorniak | 0:669f3b0e91c8 | 14 | |
WGorniak | 0:669f3b0e91c8 | 15 | */ |
WGorniak | 0:669f3b0e91c8 | 16 | #include "sx1272-hal.h" |
WGorniak | 0:669f3b0e91c8 | 17 | |
WGorniak | 1:51fd7a5843e5 | 18 | const RadioRegisters_t SX1272BRD::RadioRegsInit[] = |
WGorniak | 0:669f3b0e91c8 | 19 | { |
WGorniak | 0:669f3b0e91c8 | 20 | { MODEM_FSK , REG_LNA , 0x23 }, |
WGorniak | 0:669f3b0e91c8 | 21 | { MODEM_FSK , REG_RXCONFIG , 0x1E }, |
WGorniak | 0:669f3b0e91c8 | 22 | { MODEM_FSK , REG_RSSICONFIG , 0xD2 }, |
WGorniak | 0:669f3b0e91c8 | 23 | { MODEM_FSK , REG_PREAMBLEDETECT , 0xAA }, |
WGorniak | 0:669f3b0e91c8 | 24 | { MODEM_FSK , REG_OSC , 0x07 }, |
WGorniak | 0:669f3b0e91c8 | 25 | { MODEM_FSK , REG_SYNCCONFIG , 0x12 }, |
WGorniak | 0:669f3b0e91c8 | 26 | { MODEM_FSK , REG_SYNCVALUE1 , 0xC1 }, |
WGorniak | 0:669f3b0e91c8 | 27 | { MODEM_FSK , REG_SYNCVALUE2 , 0x94 }, |
WGorniak | 0:669f3b0e91c8 | 28 | { MODEM_FSK , REG_SYNCVALUE3 , 0xC1 }, |
WGorniak | 0:669f3b0e91c8 | 29 | { MODEM_FSK , REG_PACKETCONFIG1 , 0x98 }, |
WGorniak | 0:669f3b0e91c8 | 30 | { MODEM_FSK , REG_FIFOTHRESH , 0x8F }, |
WGorniak | 0:669f3b0e91c8 | 31 | { MODEM_FSK , REG_IMAGECAL , 0x02 }, |
WGorniak | 0:669f3b0e91c8 | 32 | { MODEM_FSK , REG_DIOMAPPING1 , 0x00 }, |
WGorniak | 0:669f3b0e91c8 | 33 | { MODEM_FSK , REG_DIOMAPPING2 , 0x30 }, |
WGorniak | 0:669f3b0e91c8 | 34 | { MODEM_LORA, REG_LR_PAYLOADMAXLENGTH, 0x40 }, |
WGorniak | 0:669f3b0e91c8 | 35 | }; |
WGorniak | 0:669f3b0e91c8 | 36 | |
WGorniak | 1:51fd7a5843e5 | 37 | SX1272BRD::SX1272BRD( void ( *txDone )( ), void ( *txTimeout ) ( ), void ( *rxDone ) ( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr ), |
WGorniak | 0:669f3b0e91c8 | 38 | void ( *rxTimeout ) ( ), void ( *rxError ) ( ), void ( *fhssChangeChannel ) ( uint8_t channelIndex ), void ( *cadDone ) ( bool ChannelActivityDetected ), |
WGorniak | 0:669f3b0e91c8 | 39 | PinName mosi, PinName miso, PinName sclk, PinName nss, PinName reset, |
WGorniak | 0:669f3b0e91c8 | 40 | PinName dio0, PinName dio1, PinName dio2, PinName dio3, PinName dio4, PinName dio5, |
WGorniak | 0:669f3b0e91c8 | 41 | PinName antSwitch ) |
WGorniak | 0:669f3b0e91c8 | 42 | : SX1272( txDone, txTimeout, rxDone, rxTimeout, rxError, fhssChangeChannel, cadDone, mosi, miso, sclk, nss, reset, dio0, dio1, dio2, dio3, dio4, dio5), |
WGorniak | 0:669f3b0e91c8 | 43 | antSwitch( antSwitch ) |
WGorniak | 0:669f3b0e91c8 | 44 | { |
WGorniak | 0:669f3b0e91c8 | 45 | Reset( ); |
WGorniak | 0:669f3b0e91c8 | 46 | |
WGorniak | 0:669f3b0e91c8 | 47 | RxChainCalibration( ); |
WGorniak | 0:669f3b0e91c8 | 48 | |
WGorniak | 0:669f3b0e91c8 | 49 | IoInit( ); |
WGorniak | 0:669f3b0e91c8 | 50 | |
WGorniak | 0:669f3b0e91c8 | 51 | SetOpMode( RF_OPMODE_SLEEP ); |
WGorniak | 0:669f3b0e91c8 | 52 | |
WGorniak | 0:669f3b0e91c8 | 53 | IoIrqInit( dioIrq ); |
WGorniak | 0:669f3b0e91c8 | 54 | |
WGorniak | 0:669f3b0e91c8 | 55 | RadioRegistersInit( ); |
WGorniak | 0:669f3b0e91c8 | 56 | |
WGorniak | 0:669f3b0e91c8 | 57 | SetModem( MODEM_FSK ); |
WGorniak | 0:669f3b0e91c8 | 58 | |
WGorniak | 0:669f3b0e91c8 | 59 | this->settings.State = IDLE ; |
WGorniak | 0:669f3b0e91c8 | 60 | } |
WGorniak | 0:669f3b0e91c8 | 61 | |
WGorniak | 0:669f3b0e91c8 | 62 | |
WGorniak | 1:51fd7a5843e5 | 63 | SX1272BRD::~SX1272BRD ( ) |
WGorniak | 0:669f3b0e91c8 | 64 | { |
WGorniak | 0:669f3b0e91c8 | 65 | IoIrqDeInit( ); |
WGorniak | 0:669f3b0e91c8 | 66 | }; |
WGorniak | 0:669f3b0e91c8 | 67 | |
WGorniak | 0:669f3b0e91c8 | 68 | //------------------------------------------------------------------------- |
WGorniak | 0:669f3b0e91c8 | 69 | // Board relative functions |
WGorniak | 0:669f3b0e91c8 | 70 | //------------------------------------------------------------------------- |
WGorniak | 1:51fd7a5843e5 | 71 | void SX1272BRD::IoInit( void ) |
WGorniak | 0:669f3b0e91c8 | 72 | { |
WGorniak | 0:669f3b0e91c8 | 73 | AntSwInit( ); |
WGorniak | 0:669f3b0e91c8 | 74 | SpiInit( ); |
WGorniak | 0:669f3b0e91c8 | 75 | } |
WGorniak | 0:669f3b0e91c8 | 76 | |
WGorniak | 1:51fd7a5843e5 | 77 | void SX1272BRD::RadioRegistersInit( ){ |
WGorniak | 0:669f3b0e91c8 | 78 | uint8_t i = 0; |
WGorniak | 0:669f3b0e91c8 | 79 | for( i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t ); i++ ) |
WGorniak | 0:669f3b0e91c8 | 80 | { |
WGorniak | 0:669f3b0e91c8 | 81 | SetModem( RadioRegsInit[i].Modem ); |
WGorniak | 0:669f3b0e91c8 | 82 | Write( RadioRegsInit[i].Addr, RadioRegsInit[i].Value ); |
WGorniak | 0:669f3b0e91c8 | 83 | } |
WGorniak | 0:669f3b0e91c8 | 84 | } |
WGorniak | 0:669f3b0e91c8 | 85 | |
WGorniak | 1:51fd7a5843e5 | 86 | void SX1272BRD::SpiInit( void ) |
WGorniak | 0:669f3b0e91c8 | 87 | { |
WGorniak | 0:669f3b0e91c8 | 88 | nss = 1; |
WGorniak | 0:669f3b0e91c8 | 89 | spi.format( 8,0 ); |
WGorniak | 0:669f3b0e91c8 | 90 | uint32_t frequencyToSet = 8000000; |
WGorniak | 0:669f3b0e91c8 | 91 | #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) ) |
WGorniak | 0:669f3b0e91c8 | 92 | spi.frequency( frequencyToSet ); |
WGorniak | 0:669f3b0e91c8 | 93 | #elif( defined ( TARGET_KL25Z ) ) //busclock frequency is halved -> double the spi frequency to compensate |
WGorniak | 0:669f3b0e91c8 | 94 | spi.frequency( frequencyToSet * 2 ); |
WGorniak | 0:669f3b0e91c8 | 95 | #else |
WGorniak | 0:669f3b0e91c8 | 96 | #warning "Check the board's SPI frequency" |
WGorniak | 0:669f3b0e91c8 | 97 | #endif |
WGorniak | 0:669f3b0e91c8 | 98 | wait(0.1); |
WGorniak | 0:669f3b0e91c8 | 99 | } |
WGorniak | 0:669f3b0e91c8 | 100 | |
WGorniak | 1:51fd7a5843e5 | 101 | void SX1272BRD::IoIrqInit( DioIrqHandler *irqHandlers ) |
WGorniak | 0:669f3b0e91c8 | 102 | { |
WGorniak | 0:669f3b0e91c8 | 103 | #if( defined ( TARGET_NUCLEO_L152RE ) || defined ( TARGET_LPC11U6X ) ) |
WGorniak | 0:669f3b0e91c8 | 104 | dio0.mode(PullDown); |
WGorniak | 0:669f3b0e91c8 | 105 | dio1.mode(PullDown); |
WGorniak | 0:669f3b0e91c8 | 106 | dio2.mode(PullDown); |
WGorniak | 0:669f3b0e91c8 | 107 | dio3.mode(PullDown); |
WGorniak | 0:669f3b0e91c8 | 108 | dio4.mode(PullDown); |
WGorniak | 0:669f3b0e91c8 | 109 | #endif |
WGorniak | 0:669f3b0e91c8 | 110 | dio0.rise( this, static_cast< TriggerMB1xASSX1272 > ( irqHandlers[0] ) ); |
WGorniak | 0:669f3b0e91c8 | 111 | dio1.rise( this, static_cast< TriggerMB1xASSX1272 > ( irqHandlers[1] ) ); |
WGorniak | 0:669f3b0e91c8 | 112 | dio2.rise( this, static_cast< TriggerMB1xASSX1272 > ( irqHandlers[2] ) ); |
WGorniak | 0:669f3b0e91c8 | 113 | dio3.rise( this, static_cast< TriggerMB1xASSX1272 > ( irqHandlers[3] ) ); |
WGorniak | 0:669f3b0e91c8 | 114 | dio4.rise( this, static_cast< TriggerMB1xASSX1272 > ( irqHandlers[4] ) ); |
WGorniak | 0:669f3b0e91c8 | 115 | } |
WGorniak | 0:669f3b0e91c8 | 116 | |
WGorniak | 1:51fd7a5843e5 | 117 | void SX1272BRD::IoIrqDeInit( ) |
WGorniak | 0:669f3b0e91c8 | 118 | { |
WGorniak | 0:669f3b0e91c8 | 119 | dio0.disable_irq(); |
WGorniak | 0:669f3b0e91c8 | 120 | dio1.disable_irq(); |
WGorniak | 0:669f3b0e91c8 | 121 | dio2.disable_irq(); |
WGorniak | 0:669f3b0e91c8 | 122 | dio3.disable_irq(); |
WGorniak | 0:669f3b0e91c8 | 123 | dio4.disable_irq(); |
WGorniak | 0:669f3b0e91c8 | 124 | } |
WGorniak | 0:669f3b0e91c8 | 125 | |
WGorniak | 1:51fd7a5843e5 | 126 | void SX1272BRD::IoDeInit( void ) |
WGorniak | 0:669f3b0e91c8 | 127 | { |
WGorniak | 0:669f3b0e91c8 | 128 | //nothing |
WGorniak | 0:669f3b0e91c8 | 129 | } |
WGorniak | 0:669f3b0e91c8 | 130 | |
WGorniak | 1:51fd7a5843e5 | 131 | uint8_t SX1272BRD::GetPaSelect( uint32_t channel ) |
WGorniak | 0:669f3b0e91c8 | 132 | { |
WGorniak | 0:669f3b0e91c8 | 133 | return RF_PACONFIG_PASELECT_RFO; |
WGorniak | 0:669f3b0e91c8 | 134 | } |
WGorniak | 0:669f3b0e91c8 | 135 | |
WGorniak | 1:51fd7a5843e5 | 136 | void SX1272BRD::SetAntSwLowPower( bool status ) |
WGorniak | 0:669f3b0e91c8 | 137 | { |
WGorniak | 0:669f3b0e91c8 | 138 | if( isRadioActive != status ) |
WGorniak | 0:669f3b0e91c8 | 139 | { |
WGorniak | 0:669f3b0e91c8 | 140 | isRadioActive = status; |
WGorniak | 0:669f3b0e91c8 | 141 | |
WGorniak | 0:669f3b0e91c8 | 142 | if( status == false ) |
WGorniak | 0:669f3b0e91c8 | 143 | { |
WGorniak | 0:669f3b0e91c8 | 144 | AntSwInit( ); |
WGorniak | 0:669f3b0e91c8 | 145 | } |
WGorniak | 0:669f3b0e91c8 | 146 | else |
WGorniak | 0:669f3b0e91c8 | 147 | { |
WGorniak | 0:669f3b0e91c8 | 148 | AntSwDeInit( ); |
WGorniak | 0:669f3b0e91c8 | 149 | } |
WGorniak | 0:669f3b0e91c8 | 150 | } |
WGorniak | 0:669f3b0e91c8 | 151 | } |
WGorniak | 0:669f3b0e91c8 | 152 | |
WGorniak | 1:51fd7a5843e5 | 153 | void SX1272BRD::AntSwInit( void ) |
WGorniak | 0:669f3b0e91c8 | 154 | { |
WGorniak | 0:669f3b0e91c8 | 155 | antSwitch = 0; |
WGorniak | 0:669f3b0e91c8 | 156 | } |
WGorniak | 0:669f3b0e91c8 | 157 | |
WGorniak | 1:51fd7a5843e5 | 158 | void SX1272BRD::AntSwDeInit( void ) |
WGorniak | 0:669f3b0e91c8 | 159 | { |
WGorniak | 0:669f3b0e91c8 | 160 | antSwitch = 0; |
WGorniak | 0:669f3b0e91c8 | 161 | } |
WGorniak | 0:669f3b0e91c8 | 162 | |
WGorniak | 1:51fd7a5843e5 | 163 | void SX1272BRD::SetAntSw( uint8_t rxTx ) |
WGorniak | 0:669f3b0e91c8 | 164 | { |
WGorniak | 0:669f3b0e91c8 | 165 | if( this->rxTx == rxTx ) |
WGorniak | 0:669f3b0e91c8 | 166 | { |
WGorniak | 0:669f3b0e91c8 | 167 | //no need to go further |
WGorniak | 0:669f3b0e91c8 | 168 | return; |
WGorniak | 0:669f3b0e91c8 | 169 | } |
WGorniak | 0:669f3b0e91c8 | 170 | |
WGorniak | 0:669f3b0e91c8 | 171 | this->rxTx = rxTx; |
WGorniak | 0:669f3b0e91c8 | 172 | |
WGorniak | 0:669f3b0e91c8 | 173 | if( rxTx != 0 ) |
WGorniak | 0:669f3b0e91c8 | 174 | { |
WGorniak | 0:669f3b0e91c8 | 175 | antSwitch = 1; |
WGorniak | 0:669f3b0e91c8 | 176 | } |
WGorniak | 0:669f3b0e91c8 | 177 | else |
WGorniak | 0:669f3b0e91c8 | 178 | { |
WGorniak | 0:669f3b0e91c8 | 179 | antSwitch = 0; |
WGorniak | 0:669f3b0e91c8 | 180 | } |
WGorniak | 0:669f3b0e91c8 | 181 | } |
WGorniak | 0:669f3b0e91c8 | 182 | |
WGorniak | 1:51fd7a5843e5 | 183 | bool SX1272BRD::CheckRfFrequency( uint32_t frequency ) |
WGorniak | 0:669f3b0e91c8 | 184 | { |
WGorniak | 0:669f3b0e91c8 | 185 | //TODO: Implement check, currently all frequencies are supported |
WGorniak | 0:669f3b0e91c8 | 186 | return true; |
WGorniak | 0:669f3b0e91c8 | 187 | } |
WGorniak | 0:669f3b0e91c8 | 188 | |
WGorniak | 0:669f3b0e91c8 | 189 | |
WGorniak | 1:51fd7a5843e5 | 190 | void SX1272BRD::Reset( void ) |
WGorniak | 0:669f3b0e91c8 | 191 | { |
WGorniak | 0:669f3b0e91c8 | 192 | reset.output(); |
WGorniak | 0:669f3b0e91c8 | 193 | reset = 1; |
WGorniak | 0:669f3b0e91c8 | 194 | wait_ms( 1 ); |
WGorniak | 0:669f3b0e91c8 | 195 | reset.input(); |
WGorniak | 0:669f3b0e91c8 | 196 | wait_ms( 6 ); |
WGorniak | 0:669f3b0e91c8 | 197 | } |
WGorniak | 0:669f3b0e91c8 | 198 | |
WGorniak | 1:51fd7a5843e5 | 199 | void SX1272BRD::Write( uint8_t addr, uint8_t data ) |
WGorniak | 0:669f3b0e91c8 | 200 | { |
WGorniak | 0:669f3b0e91c8 | 201 | Write( addr, &data, 1 ); |
WGorniak | 0:669f3b0e91c8 | 202 | } |
WGorniak | 0:669f3b0e91c8 | 203 | |
WGorniak | 1:51fd7a5843e5 | 204 | uint8_t SX1272BRD::Read( uint8_t addr ) |
WGorniak | 0:669f3b0e91c8 | 205 | { |
WGorniak | 0:669f3b0e91c8 | 206 | uint8_t data; |
WGorniak | 0:669f3b0e91c8 | 207 | Read( addr, &data, 1 ); |
WGorniak | 0:669f3b0e91c8 | 208 | return data; |
WGorniak | 0:669f3b0e91c8 | 209 | } |
WGorniak | 0:669f3b0e91c8 | 210 | |
WGorniak | 1:51fd7a5843e5 | 211 | void SX1272BRD::Write( uint8_t addr, uint8_t *buffer, uint8_t size ) |
WGorniak | 0:669f3b0e91c8 | 212 | { |
WGorniak | 0:669f3b0e91c8 | 213 | uint8_t i; |
WGorniak | 0:669f3b0e91c8 | 214 | |
WGorniak | 0:669f3b0e91c8 | 215 | nss = 0; |
WGorniak | 0:669f3b0e91c8 | 216 | spi.write( addr | 0x80 ); |
WGorniak | 0:669f3b0e91c8 | 217 | for( i = 0; i < size; i++ ) |
WGorniak | 0:669f3b0e91c8 | 218 | { |
WGorniak | 0:669f3b0e91c8 | 219 | spi.write( buffer[i] ); |
WGorniak | 0:669f3b0e91c8 | 220 | } |
WGorniak | 0:669f3b0e91c8 | 221 | nss = 1; |
WGorniak | 0:669f3b0e91c8 | 222 | } |
WGorniak | 0:669f3b0e91c8 | 223 | |
WGorniak | 1:51fd7a5843e5 | 224 | void SX1272BRD::Read( uint8_t addr, uint8_t *buffer, uint8_t size ) |
WGorniak | 0:669f3b0e91c8 | 225 | { |
WGorniak | 0:669f3b0e91c8 | 226 | uint8_t i; |
WGorniak | 0:669f3b0e91c8 | 227 | |
WGorniak | 0:669f3b0e91c8 | 228 | nss = 0; |
WGorniak | 0:669f3b0e91c8 | 229 | spi.write( addr & 0x7F ); |
WGorniak | 0:669f3b0e91c8 | 230 | for( i = 0; i < size; i++ ) |
WGorniak | 0:669f3b0e91c8 | 231 | { |
WGorniak | 0:669f3b0e91c8 | 232 | buffer[i] = spi.write( 0 ); |
WGorniak | 0:669f3b0e91c8 | 233 | } |
WGorniak | 0:669f3b0e91c8 | 234 | nss = 1; |
WGorniak | 0:669f3b0e91c8 | 235 | } |
WGorniak | 0:669f3b0e91c8 | 236 | |
WGorniak | 1:51fd7a5843e5 | 237 | void SX1272BRD::WriteFifo( uint8_t *buffer, uint8_t size ) |
WGorniak | 0:669f3b0e91c8 | 238 | { |
WGorniak | 0:669f3b0e91c8 | 239 | Write( 0, buffer, size ); |
WGorniak | 0:669f3b0e91c8 | 240 | } |
WGorniak | 0:669f3b0e91c8 | 241 | |
WGorniak | 1:51fd7a5843e5 | 242 | void SX1272BRD::ReadFifo( uint8_t *buffer, uint8_t size ) |
WGorniak | 0:669f3b0e91c8 | 243 | { |
WGorniak | 0:669f3b0e91c8 | 244 | Read( 0, buffer, size ); |
WGorniak | 0:669f3b0e91c8 | 245 | } |