sx1261/2 driver
Dependents: alarm_slave iq_sx126x sx126x_simple_TX_shield_2020a sx126x_simple_RX_shield_2020a ... more
Driver for SX1261 or SX1262
sx126x.cpp@13:5703598a88fb, 2020-07-10 (annotated)
- Committer:
- Wayne Roberts
- Date:
- Fri Jul 10 09:44:30 2020 -0700
- Revision:
- 13:5703598a88fb
- Parent:
- 10:8905722dd5e6
run on mbed-6
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wayne Roberts |
1:497af0bd9e53 | 1 | #include "sx12xx.h" |
Wayne Roberts |
0:c79a1f70c110 | 2 | |
Wayne Roberts |
0:c79a1f70c110 | 3 | Callback<void()> SX126x::dio1_topHalf; // low latency ISR context |
Wayne Roberts |
0:c79a1f70c110 | 4 | |
Wayne Roberts |
0:c79a1f70c110 | 5 | void SX126x::dio1isr() |
Wayne Roberts |
0:c79a1f70c110 | 6 | { |
Wayne Roberts |
0:c79a1f70c110 | 7 | if (dio1_topHalf) |
Wayne Roberts |
0:c79a1f70c110 | 8 | dio1_topHalf.call(); |
Wayne Roberts |
0:c79a1f70c110 | 9 | } |
Wayne Roberts |
0:c79a1f70c110 | 10 | |
Wayne Roberts |
0:c79a1f70c110 | 11 | SX126x::SX126x(SPI& _spi, PinName _nss, PinName _busy, PinName _dio1) |
Wayne Roberts |
0:c79a1f70c110 | 12 | : spi(_spi), nss(_nss), busy(_busy), dio1(_dio1) |
Wayne Roberts |
0:c79a1f70c110 | 13 | { |
Wayne Roberts |
0:c79a1f70c110 | 14 | uint8_t buf[8]; |
Wayne Roberts |
0:c79a1f70c110 | 15 | IrqFlags_t irqEnable; |
Wayne Roberts |
0:c79a1f70c110 | 16 | |
Wayne Roberts |
1:497af0bd9e53 | 17 | nss = 1; |
Wayne Roberts |
1:497af0bd9e53 | 18 | |
Wayne Roberts |
1:497af0bd9e53 | 19 | dio1.rise(dio1isr); |
Wayne Roberts |
1:497af0bd9e53 | 20 | |
Wayne Roberts |
1:497af0bd9e53 | 21 | |
Wayne Roberts |
0:c79a1f70c110 | 22 | irqEnable.word = 0; |
Wayne Roberts |
0:c79a1f70c110 | 23 | irqEnable.bits.TxDone = 1; |
Wayne Roberts |
0:c79a1f70c110 | 24 | irqEnable.bits.RxDone = 1; |
Wayne Roberts |
0:c79a1f70c110 | 25 | irqEnable.bits.Timeout = 1; |
Wayne Roberts |
0:c79a1f70c110 | 26 | |
Wayne Roberts |
0:c79a1f70c110 | 27 | buf[0] = irqEnable.word >> 8; // enable bits |
Wayne Roberts |
0:c79a1f70c110 | 28 | buf[1] = irqEnable.word; // enable bits |
Wayne Roberts |
0:c79a1f70c110 | 29 | buf[2] = irqEnable.word >> 8; // dio1 |
Wayne Roberts |
0:c79a1f70c110 | 30 | buf[3] = irqEnable.word; // dio1 |
Wayne Roberts |
0:c79a1f70c110 | 31 | buf[4] = 0; // dio2 |
Wayne Roberts |
0:c79a1f70c110 | 32 | buf[5] = 0; // dio2 |
Wayne Roberts |
0:c79a1f70c110 | 33 | buf[6] = 0; // dio3 |
Wayne Roberts |
0:c79a1f70c110 | 34 | buf[7] = 0; // dio3 |
Wayne Roberts |
2:e6e159c8ab4d | 35 | xfer(OPCODE_SET_DIO_IRQ_PARAMS, 8, 0, buf); |
Wayne Roberts |
0:c79a1f70c110 | 36 | |
Wayne Roberts |
0:c79a1f70c110 | 37 | } |
Wayne Roberts |
0:c79a1f70c110 | 38 | |
Wayne Roberts |
0:c79a1f70c110 | 39 | void SX126x::PrintChipStatus(status_t status) |
Wayne Roberts |
0:c79a1f70c110 | 40 | { |
Wayne Roberts |
0:c79a1f70c110 | 41 | printf("%02x cmdStatus:", status.octet); |
Wayne Roberts |
0:c79a1f70c110 | 42 | switch (status.bits.cmdStatus) { |
Wayne Roberts |
0:c79a1f70c110 | 43 | case 0: printf("Reserved"); break; |
Wayne Roberts |
0:c79a1f70c110 | 44 | case 1: printf("RFU"); break; |
Wayne Roberts |
0:c79a1f70c110 | 45 | case 2: printf("dataAvail"); break; |
Wayne Roberts |
0:c79a1f70c110 | 46 | case 3: printf("cmdTimeout"); break; |
Wayne Roberts |
0:c79a1f70c110 | 47 | case 4: printf("cmdError"); break; |
Wayne Roberts |
0:c79a1f70c110 | 48 | case 5: printf("execFail"); break; |
Wayne Roberts |
0:c79a1f70c110 | 49 | case 6: printf("cmdTxDone"); break; |
Wayne Roberts |
0:c79a1f70c110 | 50 | } |
Wayne Roberts |
0:c79a1f70c110 | 51 | printf(" chipMode:"); |
Wayne Roberts |
0:c79a1f70c110 | 52 | switch (status.bits.chipMode) { |
Wayne Roberts |
0:c79a1f70c110 | 53 | case 0: printf("Unused"); break; |
Wayne Roberts |
0:c79a1f70c110 | 54 | case 1: printf("RFU"); break; |
Wayne Roberts |
0:c79a1f70c110 | 55 | case 2: printf("STBY_RC"); break; |
Wayne Roberts |
0:c79a1f70c110 | 56 | case 3: printf("STBY_XOSC"); break; |
Wayne Roberts |
0:c79a1f70c110 | 57 | case 4: printf("FS"); break; |
Wayne Roberts |
0:c79a1f70c110 | 58 | case 5: printf("RX"); break; |
Wayne Roberts |
0:c79a1f70c110 | 59 | case 6: printf("TX"); break; |
Wayne Roberts |
0:c79a1f70c110 | 60 | } |
Wayne Roberts |
0:c79a1f70c110 | 61 | printf("\r\n"); |
Wayne Roberts |
0:c79a1f70c110 | 62 | } |
Wayne Roberts |
0:c79a1f70c110 | 63 | |
Wayne Roberts |
0:c79a1f70c110 | 64 | void SX126x::service() |
Wayne Roberts |
0:c79a1f70c110 | 65 | { |
Wayne Roberts |
0:c79a1f70c110 | 66 | IrqFlags_t irqFlags, clearIrqFlags; |
Wayne Roberts |
0:c79a1f70c110 | 67 | uint8_t buf[4]; |
Wayne Roberts |
0:c79a1f70c110 | 68 | |
Wayne Roberts |
2:e6e159c8ab4d | 69 | if (busy) { |
Wayne Roberts |
0:c79a1f70c110 | 70 | return; |
Wayne Roberts |
2:e6e159c8ab4d | 71 | } |
Wayne Roberts |
0:c79a1f70c110 | 72 | |
Wayne Roberts |
0:c79a1f70c110 | 73 | while (dio1) { |
Wayne Roberts |
2:e6e159c8ab4d | 74 | xfer(OPCODE_GET_IRQ_STATUS, 0, 3, buf); |
Wayne Roberts |
0:c79a1f70c110 | 75 | irqFlags.word = buf[1] << 8; |
Wayne Roberts |
0:c79a1f70c110 | 76 | irqFlags.word |= buf[2]; |
Wayne Roberts |
0:c79a1f70c110 | 77 | clearIrqFlags.word = 0; |
Wayne Roberts |
0:c79a1f70c110 | 78 | if (irqFlags.bits.TxDone) { |
Wayne Roberts |
5:8b75387af4e0 | 79 | chipMode = CHIPMODE_NONE; |
Wayne Roberts |
5:8b75387af4e0 | 80 | if (chipModeChange) |
Wayne Roberts |
5:8b75387af4e0 | 81 | chipModeChange.call(); // might change to Rx |
Wayne Roberts |
0:c79a1f70c110 | 82 | if (txDone) |
Wayne Roberts |
0:c79a1f70c110 | 83 | txDone.call(); |
Wayne Roberts |
0:c79a1f70c110 | 84 | clearIrqFlags.bits.TxDone = 1; |
Wayne Roberts |
0:c79a1f70c110 | 85 | } |
Wayne Roberts |
0:c79a1f70c110 | 86 | if (irqFlags.bits.RxDone) { |
Wayne Roberts |
0:c79a1f70c110 | 87 | if (rxDone) { |
Wayne Roberts |
0:c79a1f70c110 | 88 | uint8_t len; |
Wayne Roberts |
0:c79a1f70c110 | 89 | float snr, rssi; |
Wayne Roberts |
0:c79a1f70c110 | 90 | int8_t s; |
Wayne Roberts |
2:e6e159c8ab4d | 91 | xfer(OPCODE_GET_RX_BUFFER_STATUS, 0, 3, buf); |
Wayne Roberts |
0:c79a1f70c110 | 92 | len = buf[1]; |
Wayne Roberts |
2:e6e159c8ab4d | 93 | ReadBuffer(len, buf[2]); |
Wayne Roberts |
2:e6e159c8ab4d | 94 | xfer(OPCODE_GET_PACKET_STATUS, 0, 4, buf); |
Wayne Roberts |
0:c79a1f70c110 | 95 | rssi = -buf[1] / 2.0; // TODO FSK |
Wayne Roberts |
0:c79a1f70c110 | 96 | s = buf[2]; |
Wayne Roberts |
0:c79a1f70c110 | 97 | snr = s / 4.0; |
Wayne Roberts |
0:c79a1f70c110 | 98 | rxDone(len, rssi, snr); |
Wayne Roberts |
0:c79a1f70c110 | 99 | } |
Wayne Roberts |
0:c79a1f70c110 | 100 | clearIrqFlags.bits.RxDone = 1; |
Wayne Roberts |
0:c79a1f70c110 | 101 | } |
Wayne Roberts |
0:c79a1f70c110 | 102 | if (irqFlags.bits.Timeout) { |
Wayne Roberts |
1:497af0bd9e53 | 103 | if (chipMode != CHIPMODE_NONE) { |
Wayne Roberts |
1:497af0bd9e53 | 104 | if (timeout) |
Wayne Roberts |
1:497af0bd9e53 | 105 | timeout(chipMode == CHIPMODE_TX); |
Wayne Roberts |
1:497af0bd9e53 | 106 | } |
Wayne Roberts |
1:497af0bd9e53 | 107 | chipMode = CHIPMODE_NONE; |
Wayne Roberts |
4:b941bceb401d | 108 | if (chipModeChange) |
Wayne Roberts |
4:b941bceb401d | 109 | chipModeChange.call(); |
Wayne Roberts |
0:c79a1f70c110 | 110 | clearIrqFlags.bits.Timeout = 1; |
Wayne Roberts |
0:c79a1f70c110 | 111 | } |
Wayne Roberts |
8:66d3e344d61c | 112 | if (irqFlags.bits.CadDone) { |
Wayne Roberts |
8:66d3e344d61c | 113 | if (cadDone) |
Wayne Roberts |
8:66d3e344d61c | 114 | cadDone(irqFlags.bits.CadDetected); |
Wayne Roberts |
8:66d3e344d61c | 115 | |
Wayne Roberts |
8:66d3e344d61c | 116 | clearIrqFlags.bits.CadDone = 1; |
Wayne Roberts |
8:66d3e344d61c | 117 | clearIrqFlags.bits.CadDetected = irqFlags.bits.CadDetected; |
Wayne Roberts |
8:66d3e344d61c | 118 | } |
Wayne Roberts |
10:8905722dd5e6 | 119 | if (irqFlags.bits.PreambleDetected) { |
Wayne Roberts |
10:8905722dd5e6 | 120 | clearIrqFlags.bits.PreambleDetected = 1; |
Wayne Roberts |
10:8905722dd5e6 | 121 | if (preambleDetected) |
Wayne Roberts |
10:8905722dd5e6 | 122 | preambleDetected(); |
Wayne Roberts |
10:8905722dd5e6 | 123 | } |
Wayne Roberts |
0:c79a1f70c110 | 124 | |
Wayne Roberts |
0:c79a1f70c110 | 125 | if (clearIrqFlags.word != 0) { |
Wayne Roberts |
0:c79a1f70c110 | 126 | buf[0] = clearIrqFlags.word >> 8; |
Wayne Roberts |
0:c79a1f70c110 | 127 | buf[1] = (uint8_t)clearIrqFlags.word; |
Wayne Roberts |
2:e6e159c8ab4d | 128 | xfer(OPCODE_CLEAR_IRQ_STATUS, 2, 0, buf); |
Wayne Roberts |
0:c79a1f70c110 | 129 | } |
Wayne Roberts |
0:c79a1f70c110 | 130 | |
Wayne Roberts |
0:c79a1f70c110 | 131 | } // ...while (dio1) |
Wayne Roberts |
0:c79a1f70c110 | 132 | |
Wayne Roberts |
0:c79a1f70c110 | 133 | } // ..service() |
Wayne Roberts |
0:c79a1f70c110 | 134 | |
Wayne Roberts |
2:e6e159c8ab4d | 135 | void SX126x::xfer(uint8_t opcode, uint8_t wlen, uint8_t rlen, uint8_t* ptr) |
Wayne Roberts |
0:c79a1f70c110 | 136 | { |
Wayne Roberts |
2:e6e159c8ab4d | 137 | const uint8_t* stopPtr; |
Wayne Roberts |
2:e6e159c8ab4d | 138 | const uint8_t* wstop; |
Wayne Roberts |
2:e6e159c8ab4d | 139 | const uint8_t* rstop; |
Wayne Roberts |
2:e6e159c8ab4d | 140 | uint8_t nop = 0; |
Wayne Roberts |
2:e6e159c8ab4d | 141 | |
Wayne Roberts |
0:c79a1f70c110 | 142 | if (sleeping) { |
Wayne Roberts |
0:c79a1f70c110 | 143 | nss = 0; |
Wayne Roberts |
0:c79a1f70c110 | 144 | while (busy) |
Wayne Roberts |
0:c79a1f70c110 | 145 | ; |
Wayne Roberts |
0:c79a1f70c110 | 146 | sleeping = false; |
Wayne Roberts |
0:c79a1f70c110 | 147 | } else { |
Wayne Roberts |
2:e6e159c8ab4d | 148 | while (busy) |
Wayne Roberts |
2:e6e159c8ab4d | 149 | ; |
Wayne Roberts |
0:c79a1f70c110 | 150 | nss = 0; |
Wayne Roberts |
0:c79a1f70c110 | 151 | } |
Wayne Roberts |
0:c79a1f70c110 | 152 | |
Wayne Roberts |
0:c79a1f70c110 | 153 | spi.write(opcode); |
Wayne Roberts |
2:e6e159c8ab4d | 154 | |
Wayne Roberts |
2:e6e159c8ab4d | 155 | wstop = ptr + wlen; |
Wayne Roberts |
2:e6e159c8ab4d | 156 | rstop = ptr + rlen; |
Wayne Roberts |
2:e6e159c8ab4d | 157 | if (rlen > wlen) |
Wayne Roberts |
2:e6e159c8ab4d | 158 | stopPtr = rstop; |
Wayne Roberts |
2:e6e159c8ab4d | 159 | else |
Wayne Roberts |
2:e6e159c8ab4d | 160 | stopPtr = wstop; |
Wayne Roberts |
2:e6e159c8ab4d | 161 | |
Wayne Roberts |
2:e6e159c8ab4d | 162 | for (; ptr < stopPtr; ptr++) { |
Wayne Roberts |
2:e6e159c8ab4d | 163 | if (ptr < wstop && ptr < rstop) |
Wayne Roberts |
2:e6e159c8ab4d | 164 | *ptr = spi.write(*ptr); |
Wayne Roberts |
2:e6e159c8ab4d | 165 | else if (ptr < wstop) |
Wayne Roberts |
2:e6e159c8ab4d | 166 | spi.write(*ptr); |
Wayne Roberts |
2:e6e159c8ab4d | 167 | else |
Wayne Roberts |
2:e6e159c8ab4d | 168 | *ptr = spi.write(nop); // n >= write length: send NOP |
Wayne Roberts |
0:c79a1f70c110 | 169 | } |
Wayne Roberts |
0:c79a1f70c110 | 170 | |
Wayne Roberts |
0:c79a1f70c110 | 171 | nss = 1; |
Wayne Roberts |
0:c79a1f70c110 | 172 | |
Wayne Roberts |
0:c79a1f70c110 | 173 | if (opcode == OPCODE_SET_SLEEP) |
Wayne Roberts |
0:c79a1f70c110 | 174 | sleeping = true; |
Wayne Roberts |
0:c79a1f70c110 | 175 | } |
Wayne Roberts |
0:c79a1f70c110 | 176 | |
Wayne Roberts |
0:c79a1f70c110 | 177 | void SX126x::start_tx(uint8_t pktLen) |
Wayne Roberts |
0:c79a1f70c110 | 178 | { |
Wayne Roberts |
0:c79a1f70c110 | 179 | uint8_t buf[8]; |
Wayne Roberts |
0:c79a1f70c110 | 180 | |
Wayne Roberts |
0:c79a1f70c110 | 181 | { |
Wayne Roberts |
0:c79a1f70c110 | 182 | uint8_t i; |
Wayne Roberts |
0:c79a1f70c110 | 183 | |
Wayne Roberts |
0:c79a1f70c110 | 184 | while (busy) |
Wayne Roberts |
0:c79a1f70c110 | 185 | ; |
Wayne Roberts |
0:c79a1f70c110 | 186 | |
Wayne Roberts |
0:c79a1f70c110 | 187 | nss = 0; |
Wayne Roberts |
0:c79a1f70c110 | 188 | spi.write(OPCODE_WRITE_BUFFER); |
Wayne Roberts |
0:c79a1f70c110 | 189 | spi.write(0); // offset |
Wayne Roberts |
0:c79a1f70c110 | 190 | i = 0; |
Wayne Roberts |
0:c79a1f70c110 | 191 | for (i = 0; i < pktLen; i++) { |
Wayne Roberts |
0:c79a1f70c110 | 192 | spi.write(tx_buf[i]); |
Wayne Roberts |
0:c79a1f70c110 | 193 | } |
Wayne Roberts |
0:c79a1f70c110 | 194 | nss = 1; |
Wayne Roberts |
0:c79a1f70c110 | 195 | } |
Wayne Roberts |
0:c79a1f70c110 | 196 | |
Wayne Roberts |
0:c79a1f70c110 | 197 | buf[0] = 0x40; |
Wayne Roberts |
0:c79a1f70c110 | 198 | buf[1] = 0x00; |
Wayne Roberts |
0:c79a1f70c110 | 199 | buf[2] = 0x00; |
Wayne Roberts |
2:e6e159c8ab4d | 200 | xfer(OPCODE_SET_TX, 3, 0, buf); |
Wayne Roberts |
1:497af0bd9e53 | 201 | |
Wayne Roberts |
1:497af0bd9e53 | 202 | chipMode = CHIPMODE_TX; |
Wayne Roberts |
4:b941bceb401d | 203 | if (chipModeChange) |
Wayne Roberts |
4:b941bceb401d | 204 | chipModeChange.call(); |
Wayne Roberts |
0:c79a1f70c110 | 205 | } |
Wayne Roberts |
0:c79a1f70c110 | 206 | |
Wayne Roberts |
0:c79a1f70c110 | 207 | void SX126x::start_rx(unsigned timeout) |
Wayne Roberts |
0:c79a1f70c110 | 208 | { |
Wayne Roberts |
0:c79a1f70c110 | 209 | uint8_t buf[8]; |
Wayne Roberts |
0:c79a1f70c110 | 210 | |
Wayne Roberts |
0:c79a1f70c110 | 211 | buf[0] = timeout >> 16; |
Wayne Roberts |
0:c79a1f70c110 | 212 | buf[1] = timeout >> 8; |
Wayne Roberts |
0:c79a1f70c110 | 213 | buf[2] = timeout; |
Wayne Roberts |
2:e6e159c8ab4d | 214 | xfer(OPCODE_SET_RX, 3, 0, buf); |
Wayne Roberts |
1:497af0bd9e53 | 215 | |
Wayne Roberts |
1:497af0bd9e53 | 216 | chipMode = CHIPMODE_RX; |
Wayne Roberts |
4:b941bceb401d | 217 | if (chipModeChange) |
Wayne Roberts |
4:b941bceb401d | 218 | chipModeChange.call(); |
Wayne Roberts |
0:c79a1f70c110 | 219 | } |
Wayne Roberts |
0:c79a1f70c110 | 220 | |
Wayne Roberts |
0:c79a1f70c110 | 221 | uint8_t SX126x::setMHz(float MHz) |
Wayne Roberts |
0:c79a1f70c110 | 222 | { |
Wayne Roberts |
0:c79a1f70c110 | 223 | unsigned frf = MHz * MHZ_TO_FRF; |
Wayne Roberts |
0:c79a1f70c110 | 224 | uint8_t buf[4]; |
Wayne Roberts |
0:c79a1f70c110 | 225 | |
Wayne Roberts |
0:c79a1f70c110 | 226 | buf[0] = frf >> 24; |
Wayne Roberts |
0:c79a1f70c110 | 227 | buf[1] = frf >> 16; |
Wayne Roberts |
0:c79a1f70c110 | 228 | buf[2] = frf >> 8; |
Wayne Roberts |
0:c79a1f70c110 | 229 | buf[3] = frf; |
Wayne Roberts |
2:e6e159c8ab4d | 230 | xfer(OPCODE_SET_RF_FREQUENCY, 4, 0, buf); |
Wayne Roberts |
0:c79a1f70c110 | 231 | return buf[3]; |
Wayne Roberts |
0:c79a1f70c110 | 232 | } |
Wayne Roberts |
0:c79a1f70c110 | 233 | |
Wayne Roberts |
2:e6e159c8ab4d | 234 | float SX126x::getMHz() |
Wayne Roberts |
2:e6e159c8ab4d | 235 | { |
Wayne Roberts |
2:e6e159c8ab4d | 236 | uint32_t frf = readReg(REG_ADDR_RFFREQ, 4); |
Wayne Roberts |
2:e6e159c8ab4d | 237 | return frf / (float)MHZ_TO_FRF; |
Wayne Roberts |
2:e6e159c8ab4d | 238 | } |
Wayne Roberts |
2:e6e159c8ab4d | 239 | |
Wayne Roberts |
0:c79a1f70c110 | 240 | void SX126x::setPacketType(uint8_t type) |
Wayne Roberts |
0:c79a1f70c110 | 241 | { |
Wayne Roberts |
2:e6e159c8ab4d | 242 | xfer(OPCODE_SET_PACKET_TYPE, 1, 0, &type); |
Wayne Roberts |
0:c79a1f70c110 | 243 | } |
Wayne Roberts |
0:c79a1f70c110 | 244 | |
Wayne Roberts |
3:f6f2f8adcd22 | 245 | uint8_t SX126x::getPacketType() |
Wayne Roberts |
3:f6f2f8adcd22 | 246 | { |
Wayne Roberts |
3:f6f2f8adcd22 | 247 | uint8_t buf[2]; |
Wayne Roberts |
3:f6f2f8adcd22 | 248 | xfer(OPCODE_GET_PACKET_TYPE, 0, 2, buf); |
Wayne Roberts |
3:f6f2f8adcd22 | 249 | return buf[1]; |
Wayne Roberts |
3:f6f2f8adcd22 | 250 | } |
Wayne Roberts |
3:f6f2f8adcd22 | 251 | |
Wayne Roberts |
0:c79a1f70c110 | 252 | void SX126x::SetDIO2AsRfSwitchCtrl(uint8_t en) |
Wayne Roberts |
0:c79a1f70c110 | 253 | { |
Wayne Roberts |
2:e6e159c8ab4d | 254 | xfer(OPCODE_SET_DIO2_AS_RFSWITCH, 1, 0, &en); |
Wayne Roberts |
0:c79a1f70c110 | 255 | } |
Wayne Roberts |
0:c79a1f70c110 | 256 | |
Wayne Roberts |
2:e6e159c8ab4d | 257 | void SX126x::ReadBuffer(uint8_t size, uint8_t offset) |
Wayne Roberts |
0:c79a1f70c110 | 258 | { |
Wayne Roberts |
0:c79a1f70c110 | 259 | unsigned i; |
Wayne Roberts |
0:c79a1f70c110 | 260 | while (busy) |
Wayne Roberts |
0:c79a1f70c110 | 261 | ; |
Wayne Roberts |
0:c79a1f70c110 | 262 | |
Wayne Roberts |
0:c79a1f70c110 | 263 | nss = 0; |
Wayne Roberts |
0:c79a1f70c110 | 264 | |
Wayne Roberts |
0:c79a1f70c110 | 265 | spi.write(OPCODE_READ_BUFFER); |
Wayne Roberts |
2:e6e159c8ab4d | 266 | spi.write(offset); |
Wayne Roberts |
0:c79a1f70c110 | 267 | spi.write(0); // NOP |
Wayne Roberts |
0:c79a1f70c110 | 268 | i = 0; |
Wayne Roberts |
0:c79a1f70c110 | 269 | for (i = 0; i < size; i++) { |
Wayne Roberts |
0:c79a1f70c110 | 270 | rx_buf[i] = spi.write(0); |
Wayne Roberts |
0:c79a1f70c110 | 271 | } |
Wayne Roberts |
0:c79a1f70c110 | 272 | |
Wayne Roberts |
0:c79a1f70c110 | 273 | nss = 1; |
Wayne Roberts |
0:c79a1f70c110 | 274 | } |
Wayne Roberts |
0:c79a1f70c110 | 275 | |
Wayne Roberts |
0:c79a1f70c110 | 276 | void SX126x::set_tx_dbm(bool is1262, int8_t dbm) |
Wayne Roberts |
0:c79a1f70c110 | 277 | { |
Wayne Roberts |
0:c79a1f70c110 | 278 | uint8_t buf[4]; |
Wayne Roberts |
0:c79a1f70c110 | 279 | // use OCP default |
Wayne Roberts |
0:c79a1f70c110 | 280 | |
Wayne Roberts |
0:c79a1f70c110 | 281 | buf[3] = 1; |
Wayne Roberts |
0:c79a1f70c110 | 282 | if (is1262) { |
Wayne Roberts |
0:c79a1f70c110 | 283 | buf[0] = 4; |
Wayne Roberts |
0:c79a1f70c110 | 284 | buf[1] = 7; |
Wayne Roberts |
0:c79a1f70c110 | 285 | buf[2] = 0; |
Wayne Roberts |
0:c79a1f70c110 | 286 | |
Wayne Roberts |
0:c79a1f70c110 | 287 | if (dbm > 22) |
Wayne Roberts |
0:c79a1f70c110 | 288 | dbm = 22; |
Wayne Roberts |
0:c79a1f70c110 | 289 | else if (dbm < -3) |
Wayne Roberts |
0:c79a1f70c110 | 290 | dbm = -3; |
Wayne Roberts |
0:c79a1f70c110 | 291 | } else { |
Wayne Roberts |
0:c79a1f70c110 | 292 | if (dbm == 15) |
Wayne Roberts |
0:c79a1f70c110 | 293 | buf[0] = 6; |
Wayne Roberts |
0:c79a1f70c110 | 294 | else |
Wayne Roberts |
0:c79a1f70c110 | 295 | buf[0] = 4; |
Wayne Roberts |
0:c79a1f70c110 | 296 | buf[1] = 0; |
Wayne Roberts |
0:c79a1f70c110 | 297 | buf[2] = 1; |
Wayne Roberts |
0:c79a1f70c110 | 298 | |
Wayne Roberts |
0:c79a1f70c110 | 299 | if (dbm > 14) |
Wayne Roberts |
0:c79a1f70c110 | 300 | dbm = 14; |
Wayne Roberts |
0:c79a1f70c110 | 301 | else if (dbm < -3) |
Wayne Roberts |
0:c79a1f70c110 | 302 | dbm = -3; |
Wayne Roberts |
0:c79a1f70c110 | 303 | } |
Wayne Roberts |
2:e6e159c8ab4d | 304 | xfer(OPCODE_SET_PA_CONFIG, 4, 0, buf); |
Wayne Roberts |
0:c79a1f70c110 | 305 | |
Wayne Roberts |
0:c79a1f70c110 | 306 | if (is1262 && dbm > 18) { |
Wayne Roberts |
0:c79a1f70c110 | 307 | /* OCP is set by chip whenever SetPaConfig() is called */ |
Wayne Roberts |
0:c79a1f70c110 | 308 | writeReg(REG_ADDR_OCP, 0x38, 1); |
Wayne Roberts |
0:c79a1f70c110 | 309 | } |
Wayne Roberts |
0:c79a1f70c110 | 310 | |
Wayne Roberts |
0:c79a1f70c110 | 311 | // SetTxParams |
Wayne Roberts |
0:c79a1f70c110 | 312 | buf[0] = dbm; |
Wayne Roberts |
0:c79a1f70c110 | 313 | //if (opt == 0) txco |
Wayne Roberts |
0:c79a1f70c110 | 314 | buf[1] = SET_RAMP_200U; |
Wayne Roberts |
2:e6e159c8ab4d | 315 | xfer(OPCODE_SET_TX_PARAMS, 2, 0, buf); |
Wayne Roberts |
0:c79a1f70c110 | 316 | } |
Wayne Roberts |
0:c79a1f70c110 | 317 | |
Wayne Roberts |
0:c79a1f70c110 | 318 | void SX126x::writeReg(uint16_t addr, uint32_t data, uint8_t len) |
Wayne Roberts |
0:c79a1f70c110 | 319 | { |
Wayne Roberts |
0:c79a1f70c110 | 320 | uint8_t buf[6]; |
Wayne Roberts |
0:c79a1f70c110 | 321 | uint8_t n; |
Wayne Roberts |
0:c79a1f70c110 | 322 | buf[0] = addr >> 8; |
Wayne Roberts |
0:c79a1f70c110 | 323 | buf[1] = (uint8_t)addr; |
Wayne Roberts |
0:c79a1f70c110 | 324 | for (n = len; n > 0; n--) { |
Wayne Roberts |
0:c79a1f70c110 | 325 | buf[n+1] = (uint8_t)data; |
Wayne Roberts |
0:c79a1f70c110 | 326 | data >>= 8; |
Wayne Roberts |
0:c79a1f70c110 | 327 | } |
Wayne Roberts |
2:e6e159c8ab4d | 328 | xfer(OPCODE_WRITE_REGISTER, 2+len, 2+len, buf); |
Wayne Roberts |
0:c79a1f70c110 | 329 | } |
Wayne Roberts |
0:c79a1f70c110 | 330 | |
Wayne Roberts |
0:c79a1f70c110 | 331 | void SX126x::setStandby(stby_t stby) |
Wayne Roberts |
0:c79a1f70c110 | 332 | { |
Wayne Roberts |
0:c79a1f70c110 | 333 | uint8_t octet = stby; |
Wayne Roberts |
2:e6e159c8ab4d | 334 | xfer(OPCODE_SET_STANDBY, 1, 0, &octet); |
Wayne Roberts |
1:497af0bd9e53 | 335 | |
Wayne Roberts |
1:497af0bd9e53 | 336 | chipMode = CHIPMODE_NONE; |
Wayne Roberts |
4:b941bceb401d | 337 | if (chipModeChange) |
Wayne Roberts |
4:b941bceb401d | 338 | chipModeChange.call(); |
Wayne Roberts |
4:b941bceb401d | 339 | } |
Wayne Roberts |
4:b941bceb401d | 340 | |
Wayne Roberts |
4:b941bceb401d | 341 | void SX126x::setFS() |
Wayne Roberts |
4:b941bceb401d | 342 | { |
Wayne Roberts |
4:b941bceb401d | 343 | xfer(OPCODE_SET_FS, 0, 0, NULL); |
Wayne Roberts |
4:b941bceb401d | 344 | |
Wayne Roberts |
4:b941bceb401d | 345 | chipMode = CHIPMODE_NONE; |
Wayne Roberts |
4:b941bceb401d | 346 | if (chipModeChange) |
Wayne Roberts |
4:b941bceb401d | 347 | chipModeChange.call(); |
Wayne Roberts |
0:c79a1f70c110 | 348 | } |
Wayne Roberts |
0:c79a1f70c110 | 349 | |
Wayne Roberts |
8:66d3e344d61c | 350 | void SX126x::setCAD() |
Wayne Roberts |
8:66d3e344d61c | 351 | { |
Wayne Roberts |
8:66d3e344d61c | 352 | xfer(OPCODE_SET_CAD, 0, 0, NULL); |
Wayne Roberts |
8:66d3e344d61c | 353 | |
Wayne Roberts |
8:66d3e344d61c | 354 | chipMode = CHIPMODE_RX; |
Wayne Roberts |
8:66d3e344d61c | 355 | if (chipModeChange) |
Wayne Roberts |
8:66d3e344d61c | 356 | chipModeChange.call(); |
Wayne Roberts |
8:66d3e344d61c | 357 | } |
Wayne Roberts |
8:66d3e344d61c | 358 | |
Wayne Roberts |
0:c79a1f70c110 | 359 | void SX126x::setSleep(bool warmStart, bool rtcWakeup) |
Wayne Roberts |
0:c79a1f70c110 | 360 | { |
Wayne Roberts |
0:c79a1f70c110 | 361 | sleepConfig_t sc; |
Wayne Roberts |
0:c79a1f70c110 | 362 | |
Wayne Roberts |
0:c79a1f70c110 | 363 | sc.octet = 0; |
Wayne Roberts |
0:c79a1f70c110 | 364 | sc.bits.rtcWakeup = rtcWakeup; |
Wayne Roberts |
0:c79a1f70c110 | 365 | sc.bits.warmStart = warmStart; |
Wayne Roberts |
2:e6e159c8ab4d | 366 | xfer(OPCODE_SET_SLEEP, 1, 0, &sc.octet); |
Wayne Roberts |
1:497af0bd9e53 | 367 | |
Wayne Roberts |
1:497af0bd9e53 | 368 | chipMode = CHIPMODE_NONE; |
Wayne Roberts |
4:b941bceb401d | 369 | if (chipModeChange) |
Wayne Roberts |
4:b941bceb401d | 370 | chipModeChange.call(); |
Wayne Roberts |
0:c79a1f70c110 | 371 | } |
Wayne Roberts |
0:c79a1f70c110 | 372 | |
Wayne Roberts |
0:c79a1f70c110 | 373 | void SX126x::hw_reset(PinName pin) |
Wayne Roberts |
0:c79a1f70c110 | 374 | { |
Wayne Roberts |
0:c79a1f70c110 | 375 | DigitalInOut nrst(pin); |
Wayne Roberts |
0:c79a1f70c110 | 376 | nrst.output(); |
Wayne Roberts |
0:c79a1f70c110 | 377 | nrst = 0; |
Wayne Roberts |
13:5703598a88fb | 378 | #if (MBED_MAJOR_VERSION < 6) |
Wayne Roberts |
13:5703598a88fb | 379 | ThisThread::sleep_for(1); |
Wayne Roberts |
13:5703598a88fb | 380 | #else |
Wayne Roberts |
13:5703598a88fb | 381 | ThisThread::sleep_for(1ms); |
Wayne Roberts |
13:5703598a88fb | 382 | #endif |
Wayne Roberts |
0:c79a1f70c110 | 383 | nrst = 1; |
Wayne Roberts |
0:c79a1f70c110 | 384 | nrst.mode(PullUp); |
Wayne Roberts |
0:c79a1f70c110 | 385 | nrst.input(); |
Wayne Roberts |
0:c79a1f70c110 | 386 | |
Wayne Roberts |
0:c79a1f70c110 | 387 | while (busy) |
Wayne Roberts |
0:c79a1f70c110 | 388 | ; |
Wayne Roberts |
0:c79a1f70c110 | 389 | } |
Wayne Roberts |
0:c79a1f70c110 | 390 | |
Wayne Roberts |
0:c79a1f70c110 | 391 | uint32_t SX126x::readReg(uint16_t addr, uint8_t len) |
Wayne Roberts |
0:c79a1f70c110 | 392 | { |
Wayne Roberts |
0:c79a1f70c110 | 393 | uint32_t ret = 0; |
Wayne Roberts |
0:c79a1f70c110 | 394 | unsigned i; |
Wayne Roberts |
0:c79a1f70c110 | 395 | |
Wayne Roberts |
0:c79a1f70c110 | 396 | uint8_t buf[7]; |
Wayne Roberts |
0:c79a1f70c110 | 397 | buf[0] = addr >> 8; |
Wayne Roberts |
0:c79a1f70c110 | 398 | buf[1] = (uint8_t)addr; |
Wayne Roberts |
2:e6e159c8ab4d | 399 | xfer(OPCODE_READ_REGISTER, 2, 3+len, buf); |
Wayne Roberts |
0:c79a1f70c110 | 400 | for (i = 0; i < len; i++) { |
Wayne Roberts |
0:c79a1f70c110 | 401 | ret <<= 8; |
Wayne Roberts |
0:c79a1f70c110 | 402 | ret |= buf[i+3]; |
Wayne Roberts |
0:c79a1f70c110 | 403 | } |
Wayne Roberts |
0:c79a1f70c110 | 404 | return ret; |
Wayne Roberts |
0:c79a1f70c110 | 405 | } |
Wayne Roberts |
0:c79a1f70c110 | 406 | |
Wayne Roberts |
5:8b75387af4e0 | 407 | void SX126x::setBufferBase(uint8_t txAddr, uint8_t rxAddr) |
Wayne Roberts |
5:8b75387af4e0 | 408 | { |
Wayne Roberts |
5:8b75387af4e0 | 409 | uint8_t buf[2]; |
Wayne Roberts |
5:8b75387af4e0 | 410 | |
Wayne Roberts |
5:8b75387af4e0 | 411 | buf[0] = 0; // TX base address |
Wayne Roberts |
5:8b75387af4e0 | 412 | buf[1] = 0; // RX base address |
Wayne Roberts |
5:8b75387af4e0 | 413 | xfer(OPCODE_SET_BUFFER_BASE_ADDR, 2, 0, buf); |
Wayne Roberts |
5:8b75387af4e0 | 414 | } |