sx1261/2 driver

Dependents:   alarm_slave iq_sx126x sx126x_simple_TX_shield_2020a sx126x_simple_RX_shield_2020a ... more

Driver for SX1261 or SX1262

Committer:
Wayne Roberts
Date:
Tue Oct 15 16:01:02 2019 -0700
Revision:
10:8905722dd5e6
Parent:
8:66d3e344d61c
Child:
13:5703598a88fb
added missing command codes

Who changed what in which revision?

UserRevisionLine numberNew 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 10:8905722dd5e6 64 extern RawSerial pc;
Wayne Roberts 0:c79a1f70c110 65 void SX126x::service()
Wayne Roberts 0:c79a1f70c110 66 {
Wayne Roberts 0:c79a1f70c110 67 IrqFlags_t irqFlags, clearIrqFlags;
Wayne Roberts 0:c79a1f70c110 68 uint8_t buf[4];
Wayne Roberts 0:c79a1f70c110 69
Wayne Roberts 2:e6e159c8ab4d 70 if (busy) {
Wayne Roberts 0:c79a1f70c110 71 return;
Wayne Roberts 2:e6e159c8ab4d 72 }
Wayne Roberts 0:c79a1f70c110 73
Wayne Roberts 0:c79a1f70c110 74 while (dio1) {
Wayne Roberts 2:e6e159c8ab4d 75 xfer(OPCODE_GET_IRQ_STATUS, 0, 3, buf);
Wayne Roberts 0:c79a1f70c110 76 irqFlags.word = buf[1] << 8;
Wayne Roberts 0:c79a1f70c110 77 irqFlags.word |= buf[2];
Wayne Roberts 0:c79a1f70c110 78 clearIrqFlags.word = 0;
Wayne Roberts 0:c79a1f70c110 79 if (irqFlags.bits.TxDone) {
Wayne Roberts 5:8b75387af4e0 80 chipMode = CHIPMODE_NONE;
Wayne Roberts 5:8b75387af4e0 81 if (chipModeChange)
Wayne Roberts 5:8b75387af4e0 82 chipModeChange.call(); // might change to Rx
Wayne Roberts 0:c79a1f70c110 83 if (txDone)
Wayne Roberts 0:c79a1f70c110 84 txDone.call();
Wayne Roberts 0:c79a1f70c110 85 clearIrqFlags.bits.TxDone = 1;
Wayne Roberts 0:c79a1f70c110 86 }
Wayne Roberts 0:c79a1f70c110 87 if (irqFlags.bits.RxDone) {
Wayne Roberts 0:c79a1f70c110 88 if (rxDone) {
Wayne Roberts 0:c79a1f70c110 89 uint8_t len;
Wayne Roberts 0:c79a1f70c110 90 float snr, rssi;
Wayne Roberts 0:c79a1f70c110 91 int8_t s;
Wayne Roberts 2:e6e159c8ab4d 92 xfer(OPCODE_GET_RX_BUFFER_STATUS, 0, 3, buf);
Wayne Roberts 0:c79a1f70c110 93 len = buf[1];
Wayne Roberts 2:e6e159c8ab4d 94 ReadBuffer(len, buf[2]);
Wayne Roberts 2:e6e159c8ab4d 95 xfer(OPCODE_GET_PACKET_STATUS, 0, 4, buf);
Wayne Roberts 0:c79a1f70c110 96 rssi = -buf[1] / 2.0; // TODO FSK
Wayne Roberts 0:c79a1f70c110 97 s = buf[2];
Wayne Roberts 0:c79a1f70c110 98 snr = s / 4.0;
Wayne Roberts 0:c79a1f70c110 99 rxDone(len, rssi, snr);
Wayne Roberts 0:c79a1f70c110 100 }
Wayne Roberts 0:c79a1f70c110 101 clearIrqFlags.bits.RxDone = 1;
Wayne Roberts 0:c79a1f70c110 102 }
Wayne Roberts 0:c79a1f70c110 103 if (irqFlags.bits.Timeout) {
Wayne Roberts 1:497af0bd9e53 104 if (chipMode != CHIPMODE_NONE) {
Wayne Roberts 1:497af0bd9e53 105 if (timeout)
Wayne Roberts 1:497af0bd9e53 106 timeout(chipMode == CHIPMODE_TX);
Wayne Roberts 1:497af0bd9e53 107 }
Wayne Roberts 1:497af0bd9e53 108 chipMode = CHIPMODE_NONE;
Wayne Roberts 4:b941bceb401d 109 if (chipModeChange)
Wayne Roberts 4:b941bceb401d 110 chipModeChange.call();
Wayne Roberts 0:c79a1f70c110 111 clearIrqFlags.bits.Timeout = 1;
Wayne Roberts 0:c79a1f70c110 112 }
Wayne Roberts 8:66d3e344d61c 113 if (irqFlags.bits.CadDone) {
Wayne Roberts 8:66d3e344d61c 114 if (cadDone)
Wayne Roberts 8:66d3e344d61c 115 cadDone(irqFlags.bits.CadDetected);
Wayne Roberts 8:66d3e344d61c 116
Wayne Roberts 8:66d3e344d61c 117 clearIrqFlags.bits.CadDone = 1;
Wayne Roberts 8:66d3e344d61c 118 clearIrqFlags.bits.CadDetected = irqFlags.bits.CadDetected;
Wayne Roberts 8:66d3e344d61c 119 }
Wayne Roberts 10:8905722dd5e6 120 if (irqFlags.bits.PreambleDetected) {
Wayne Roberts 10:8905722dd5e6 121 clearIrqFlags.bits.PreambleDetected = 1;
Wayne Roberts 10:8905722dd5e6 122 if (preambleDetected)
Wayne Roberts 10:8905722dd5e6 123 preambleDetected();
Wayne Roberts 10:8905722dd5e6 124 }
Wayne Roberts 0:c79a1f70c110 125
Wayne Roberts 0:c79a1f70c110 126 if (clearIrqFlags.word != 0) {
Wayne Roberts 0:c79a1f70c110 127 buf[0] = clearIrqFlags.word >> 8;
Wayne Roberts 0:c79a1f70c110 128 buf[1] = (uint8_t)clearIrqFlags.word;
Wayne Roberts 2:e6e159c8ab4d 129 xfer(OPCODE_CLEAR_IRQ_STATUS, 2, 0, buf);
Wayne Roberts 0:c79a1f70c110 130 }
Wayne Roberts 0:c79a1f70c110 131
Wayne Roberts 0:c79a1f70c110 132 } // ...while (dio1)
Wayne Roberts 0:c79a1f70c110 133
Wayne Roberts 0:c79a1f70c110 134 } // ..service()
Wayne Roberts 0:c79a1f70c110 135
Wayne Roberts 2:e6e159c8ab4d 136 void SX126x::xfer(uint8_t opcode, uint8_t wlen, uint8_t rlen, uint8_t* ptr)
Wayne Roberts 0:c79a1f70c110 137 {
Wayne Roberts 2:e6e159c8ab4d 138 const uint8_t* stopPtr;
Wayne Roberts 2:e6e159c8ab4d 139 const uint8_t* wstop;
Wayne Roberts 2:e6e159c8ab4d 140 const uint8_t* rstop;
Wayne Roberts 2:e6e159c8ab4d 141 uint8_t nop = 0;
Wayne Roberts 2:e6e159c8ab4d 142
Wayne Roberts 0:c79a1f70c110 143 if (sleeping) {
Wayne Roberts 0:c79a1f70c110 144 nss = 0;
Wayne Roberts 0:c79a1f70c110 145 while (busy)
Wayne Roberts 0:c79a1f70c110 146 ;
Wayne Roberts 0:c79a1f70c110 147 sleeping = false;
Wayne Roberts 0:c79a1f70c110 148 } else {
Wayne Roberts 2:e6e159c8ab4d 149 while (busy)
Wayne Roberts 2:e6e159c8ab4d 150 ;
Wayne Roberts 0:c79a1f70c110 151 nss = 0;
Wayne Roberts 0:c79a1f70c110 152 }
Wayne Roberts 0:c79a1f70c110 153
Wayne Roberts 0:c79a1f70c110 154 spi.write(opcode);
Wayne Roberts 2:e6e159c8ab4d 155
Wayne Roberts 2:e6e159c8ab4d 156 wstop = ptr + wlen;
Wayne Roberts 2:e6e159c8ab4d 157 rstop = ptr + rlen;
Wayne Roberts 2:e6e159c8ab4d 158 if (rlen > wlen)
Wayne Roberts 2:e6e159c8ab4d 159 stopPtr = rstop;
Wayne Roberts 2:e6e159c8ab4d 160 else
Wayne Roberts 2:e6e159c8ab4d 161 stopPtr = wstop;
Wayne Roberts 2:e6e159c8ab4d 162
Wayne Roberts 2:e6e159c8ab4d 163 for (; ptr < stopPtr; ptr++) {
Wayne Roberts 2:e6e159c8ab4d 164 if (ptr < wstop && ptr < rstop)
Wayne Roberts 2:e6e159c8ab4d 165 *ptr = spi.write(*ptr);
Wayne Roberts 2:e6e159c8ab4d 166 else if (ptr < wstop)
Wayne Roberts 2:e6e159c8ab4d 167 spi.write(*ptr);
Wayne Roberts 2:e6e159c8ab4d 168 else
Wayne Roberts 2:e6e159c8ab4d 169 *ptr = spi.write(nop); // n >= write length: send NOP
Wayne Roberts 0:c79a1f70c110 170 }
Wayne Roberts 0:c79a1f70c110 171
Wayne Roberts 0:c79a1f70c110 172 nss = 1;
Wayne Roberts 0:c79a1f70c110 173
Wayne Roberts 0:c79a1f70c110 174 if (opcode == OPCODE_SET_SLEEP)
Wayne Roberts 0:c79a1f70c110 175 sleeping = true;
Wayne Roberts 0:c79a1f70c110 176 }
Wayne Roberts 0:c79a1f70c110 177
Wayne Roberts 0:c79a1f70c110 178 void SX126x::start_tx(uint8_t pktLen)
Wayne Roberts 0:c79a1f70c110 179 {
Wayne Roberts 0:c79a1f70c110 180 uint8_t buf[8];
Wayne Roberts 0:c79a1f70c110 181
Wayne Roberts 0:c79a1f70c110 182 {
Wayne Roberts 0:c79a1f70c110 183 uint8_t i;
Wayne Roberts 0:c79a1f70c110 184
Wayne Roberts 0:c79a1f70c110 185 while (busy)
Wayne Roberts 0:c79a1f70c110 186 ;
Wayne Roberts 0:c79a1f70c110 187
Wayne Roberts 0:c79a1f70c110 188 nss = 0;
Wayne Roberts 0:c79a1f70c110 189 spi.write(OPCODE_WRITE_BUFFER);
Wayne Roberts 0:c79a1f70c110 190 spi.write(0); // offset
Wayne Roberts 0:c79a1f70c110 191 i = 0;
Wayne Roberts 0:c79a1f70c110 192 for (i = 0; i < pktLen; i++) {
Wayne Roberts 0:c79a1f70c110 193 spi.write(tx_buf[i]);
Wayne Roberts 0:c79a1f70c110 194 }
Wayne Roberts 0:c79a1f70c110 195 nss = 1;
Wayne Roberts 0:c79a1f70c110 196 }
Wayne Roberts 0:c79a1f70c110 197
Wayne Roberts 0:c79a1f70c110 198 buf[0] = 0x40;
Wayne Roberts 0:c79a1f70c110 199 buf[1] = 0x00;
Wayne Roberts 0:c79a1f70c110 200 buf[2] = 0x00;
Wayne Roberts 2:e6e159c8ab4d 201 xfer(OPCODE_SET_TX, 3, 0, buf);
Wayne Roberts 1:497af0bd9e53 202
Wayne Roberts 1:497af0bd9e53 203 chipMode = CHIPMODE_TX;
Wayne Roberts 4:b941bceb401d 204 if (chipModeChange)
Wayne Roberts 4:b941bceb401d 205 chipModeChange.call();
Wayne Roberts 0:c79a1f70c110 206 }
Wayne Roberts 0:c79a1f70c110 207
Wayne Roberts 0:c79a1f70c110 208 void SX126x::start_rx(unsigned timeout)
Wayne Roberts 0:c79a1f70c110 209 {
Wayne Roberts 0:c79a1f70c110 210 uint8_t buf[8];
Wayne Roberts 0:c79a1f70c110 211
Wayne Roberts 0:c79a1f70c110 212 buf[0] = timeout >> 16;
Wayne Roberts 0:c79a1f70c110 213 buf[1] = timeout >> 8;
Wayne Roberts 0:c79a1f70c110 214 buf[2] = timeout;
Wayne Roberts 2:e6e159c8ab4d 215 xfer(OPCODE_SET_RX, 3, 0, buf);
Wayne Roberts 1:497af0bd9e53 216
Wayne Roberts 1:497af0bd9e53 217 chipMode = CHIPMODE_RX;
Wayne Roberts 4:b941bceb401d 218 if (chipModeChange)
Wayne Roberts 4:b941bceb401d 219 chipModeChange.call();
Wayne Roberts 0:c79a1f70c110 220 }
Wayne Roberts 0:c79a1f70c110 221
Wayne Roberts 0:c79a1f70c110 222 uint8_t SX126x::setMHz(float MHz)
Wayne Roberts 0:c79a1f70c110 223 {
Wayne Roberts 0:c79a1f70c110 224 unsigned frf = MHz * MHZ_TO_FRF;
Wayne Roberts 0:c79a1f70c110 225 uint8_t buf[4];
Wayne Roberts 0:c79a1f70c110 226
Wayne Roberts 0:c79a1f70c110 227 buf[0] = frf >> 24;
Wayne Roberts 0:c79a1f70c110 228 buf[1] = frf >> 16;
Wayne Roberts 0:c79a1f70c110 229 buf[2] = frf >> 8;
Wayne Roberts 0:c79a1f70c110 230 buf[3] = frf;
Wayne Roberts 2:e6e159c8ab4d 231 xfer(OPCODE_SET_RF_FREQUENCY, 4, 0, buf);
Wayne Roberts 0:c79a1f70c110 232 return buf[3];
Wayne Roberts 0:c79a1f70c110 233 }
Wayne Roberts 0:c79a1f70c110 234
Wayne Roberts 2:e6e159c8ab4d 235 float SX126x::getMHz()
Wayne Roberts 2:e6e159c8ab4d 236 {
Wayne Roberts 2:e6e159c8ab4d 237 uint32_t frf = readReg(REG_ADDR_RFFREQ, 4);
Wayne Roberts 2:e6e159c8ab4d 238 return frf / (float)MHZ_TO_FRF;
Wayne Roberts 2:e6e159c8ab4d 239 }
Wayne Roberts 2:e6e159c8ab4d 240
Wayne Roberts 0:c79a1f70c110 241 void SX126x::setPacketType(uint8_t type)
Wayne Roberts 0:c79a1f70c110 242 {
Wayne Roberts 2:e6e159c8ab4d 243 xfer(OPCODE_SET_PACKET_TYPE, 1, 0, &type);
Wayne Roberts 0:c79a1f70c110 244 }
Wayne Roberts 0:c79a1f70c110 245
Wayne Roberts 3:f6f2f8adcd22 246 uint8_t SX126x::getPacketType()
Wayne Roberts 3:f6f2f8adcd22 247 {
Wayne Roberts 3:f6f2f8adcd22 248 uint8_t buf[2];
Wayne Roberts 3:f6f2f8adcd22 249 xfer(OPCODE_GET_PACKET_TYPE, 0, 2, buf);
Wayne Roberts 3:f6f2f8adcd22 250 return buf[1];
Wayne Roberts 3:f6f2f8adcd22 251 }
Wayne Roberts 3:f6f2f8adcd22 252
Wayne Roberts 0:c79a1f70c110 253 void SX126x::SetDIO2AsRfSwitchCtrl(uint8_t en)
Wayne Roberts 0:c79a1f70c110 254 {
Wayne Roberts 2:e6e159c8ab4d 255 xfer(OPCODE_SET_DIO2_AS_RFSWITCH, 1, 0, &en);
Wayne Roberts 0:c79a1f70c110 256 }
Wayne Roberts 0:c79a1f70c110 257
Wayne Roberts 2:e6e159c8ab4d 258 void SX126x::ReadBuffer(uint8_t size, uint8_t offset)
Wayne Roberts 0:c79a1f70c110 259 {
Wayne Roberts 0:c79a1f70c110 260 unsigned i;
Wayne Roberts 0:c79a1f70c110 261 while (busy)
Wayne Roberts 0:c79a1f70c110 262 ;
Wayne Roberts 0:c79a1f70c110 263
Wayne Roberts 0:c79a1f70c110 264 nss = 0;
Wayne Roberts 0:c79a1f70c110 265
Wayne Roberts 0:c79a1f70c110 266 spi.write(OPCODE_READ_BUFFER);
Wayne Roberts 2:e6e159c8ab4d 267 spi.write(offset);
Wayne Roberts 0:c79a1f70c110 268 spi.write(0); // NOP
Wayne Roberts 0:c79a1f70c110 269 i = 0;
Wayne Roberts 0:c79a1f70c110 270 for (i = 0; i < size; i++) {
Wayne Roberts 0:c79a1f70c110 271 rx_buf[i] = spi.write(0);
Wayne Roberts 0:c79a1f70c110 272 }
Wayne Roberts 0:c79a1f70c110 273
Wayne Roberts 0:c79a1f70c110 274 nss = 1;
Wayne Roberts 0:c79a1f70c110 275 }
Wayne Roberts 0:c79a1f70c110 276
Wayne Roberts 0:c79a1f70c110 277 void SX126x::set_tx_dbm(bool is1262, int8_t dbm)
Wayne Roberts 0:c79a1f70c110 278 {
Wayne Roberts 0:c79a1f70c110 279 uint8_t buf[4];
Wayne Roberts 0:c79a1f70c110 280 // use OCP default
Wayne Roberts 0:c79a1f70c110 281
Wayne Roberts 0:c79a1f70c110 282 buf[3] = 1;
Wayne Roberts 0:c79a1f70c110 283 if (is1262) {
Wayne Roberts 0:c79a1f70c110 284 buf[0] = 4;
Wayne Roberts 0:c79a1f70c110 285 buf[1] = 7;
Wayne Roberts 0:c79a1f70c110 286 buf[2] = 0;
Wayne Roberts 0:c79a1f70c110 287
Wayne Roberts 0:c79a1f70c110 288 if (dbm > 22)
Wayne Roberts 0:c79a1f70c110 289 dbm = 22;
Wayne Roberts 0:c79a1f70c110 290 else if (dbm < -3)
Wayne Roberts 0:c79a1f70c110 291 dbm = -3;
Wayne Roberts 0:c79a1f70c110 292 } else {
Wayne Roberts 0:c79a1f70c110 293 if (dbm == 15)
Wayne Roberts 0:c79a1f70c110 294 buf[0] = 6;
Wayne Roberts 0:c79a1f70c110 295 else
Wayne Roberts 0:c79a1f70c110 296 buf[0] = 4;
Wayne Roberts 0:c79a1f70c110 297 buf[1] = 0;
Wayne Roberts 0:c79a1f70c110 298 buf[2] = 1;
Wayne Roberts 0:c79a1f70c110 299
Wayne Roberts 0:c79a1f70c110 300 if (dbm > 14)
Wayne Roberts 0:c79a1f70c110 301 dbm = 14;
Wayne Roberts 0:c79a1f70c110 302 else if (dbm < -3)
Wayne Roberts 0:c79a1f70c110 303 dbm = -3;
Wayne Roberts 0:c79a1f70c110 304 }
Wayne Roberts 2:e6e159c8ab4d 305 xfer(OPCODE_SET_PA_CONFIG, 4, 0, buf);
Wayne Roberts 0:c79a1f70c110 306
Wayne Roberts 0:c79a1f70c110 307 if (is1262 && dbm > 18) {
Wayne Roberts 0:c79a1f70c110 308 /* OCP is set by chip whenever SetPaConfig() is called */
Wayne Roberts 0:c79a1f70c110 309 writeReg(REG_ADDR_OCP, 0x38, 1);
Wayne Roberts 0:c79a1f70c110 310 }
Wayne Roberts 0:c79a1f70c110 311
Wayne Roberts 0:c79a1f70c110 312 // SetTxParams
Wayne Roberts 0:c79a1f70c110 313 buf[0] = dbm;
Wayne Roberts 0:c79a1f70c110 314 //if (opt == 0) txco
Wayne Roberts 0:c79a1f70c110 315 buf[1] = SET_RAMP_200U;
Wayne Roberts 2:e6e159c8ab4d 316 xfer(OPCODE_SET_TX_PARAMS, 2, 0, buf);
Wayne Roberts 0:c79a1f70c110 317 }
Wayne Roberts 0:c79a1f70c110 318
Wayne Roberts 0:c79a1f70c110 319 void SX126x::writeReg(uint16_t addr, uint32_t data, uint8_t len)
Wayne Roberts 0:c79a1f70c110 320 {
Wayne Roberts 0:c79a1f70c110 321 uint8_t buf[6];
Wayne Roberts 0:c79a1f70c110 322 uint8_t n;
Wayne Roberts 0:c79a1f70c110 323 buf[0] = addr >> 8;
Wayne Roberts 0:c79a1f70c110 324 buf[1] = (uint8_t)addr;
Wayne Roberts 0:c79a1f70c110 325 for (n = len; n > 0; n--) {
Wayne Roberts 0:c79a1f70c110 326 buf[n+1] = (uint8_t)data;
Wayne Roberts 0:c79a1f70c110 327 data >>= 8;
Wayne Roberts 0:c79a1f70c110 328 }
Wayne Roberts 2:e6e159c8ab4d 329 xfer(OPCODE_WRITE_REGISTER, 2+len, 2+len, buf);
Wayne Roberts 0:c79a1f70c110 330 }
Wayne Roberts 0:c79a1f70c110 331
Wayne Roberts 0:c79a1f70c110 332 void SX126x::setStandby(stby_t stby)
Wayne Roberts 0:c79a1f70c110 333 {
Wayne Roberts 0:c79a1f70c110 334 uint8_t octet = stby;
Wayne Roberts 2:e6e159c8ab4d 335 xfer(OPCODE_SET_STANDBY, 1, 0, &octet);
Wayne Roberts 1:497af0bd9e53 336
Wayne Roberts 1:497af0bd9e53 337 chipMode = CHIPMODE_NONE;
Wayne Roberts 4:b941bceb401d 338 if (chipModeChange)
Wayne Roberts 4:b941bceb401d 339 chipModeChange.call();
Wayne Roberts 4:b941bceb401d 340 }
Wayne Roberts 4:b941bceb401d 341
Wayne Roberts 4:b941bceb401d 342 void SX126x::setFS()
Wayne Roberts 4:b941bceb401d 343 {
Wayne Roberts 4:b941bceb401d 344 xfer(OPCODE_SET_FS, 0, 0, NULL);
Wayne Roberts 4:b941bceb401d 345
Wayne Roberts 4:b941bceb401d 346 chipMode = CHIPMODE_NONE;
Wayne Roberts 4:b941bceb401d 347 if (chipModeChange)
Wayne Roberts 4:b941bceb401d 348 chipModeChange.call();
Wayne Roberts 0:c79a1f70c110 349 }
Wayne Roberts 0:c79a1f70c110 350
Wayne Roberts 8:66d3e344d61c 351 void SX126x::setCAD()
Wayne Roberts 8:66d3e344d61c 352 {
Wayne Roberts 8:66d3e344d61c 353 xfer(OPCODE_SET_CAD, 0, 0, NULL);
Wayne Roberts 8:66d3e344d61c 354
Wayne Roberts 8:66d3e344d61c 355 chipMode = CHIPMODE_RX;
Wayne Roberts 8:66d3e344d61c 356 if (chipModeChange)
Wayne Roberts 8:66d3e344d61c 357 chipModeChange.call();
Wayne Roberts 8:66d3e344d61c 358 }
Wayne Roberts 8:66d3e344d61c 359
Wayne Roberts 0:c79a1f70c110 360 void SX126x::setSleep(bool warmStart, bool rtcWakeup)
Wayne Roberts 0:c79a1f70c110 361 {
Wayne Roberts 0:c79a1f70c110 362 sleepConfig_t sc;
Wayne Roberts 0:c79a1f70c110 363
Wayne Roberts 0:c79a1f70c110 364 sc.octet = 0;
Wayne Roberts 0:c79a1f70c110 365 sc.bits.rtcWakeup = rtcWakeup;
Wayne Roberts 0:c79a1f70c110 366 sc.bits.warmStart = warmStart;
Wayne Roberts 2:e6e159c8ab4d 367 xfer(OPCODE_SET_SLEEP, 1, 0, &sc.octet);
Wayne Roberts 1:497af0bd9e53 368
Wayne Roberts 1:497af0bd9e53 369 chipMode = CHIPMODE_NONE;
Wayne Roberts 4:b941bceb401d 370 if (chipModeChange)
Wayne Roberts 4:b941bceb401d 371 chipModeChange.call();
Wayne Roberts 0:c79a1f70c110 372 }
Wayne Roberts 0:c79a1f70c110 373
Wayne Roberts 0:c79a1f70c110 374 void SX126x::hw_reset(PinName pin)
Wayne Roberts 0:c79a1f70c110 375 {
Wayne Roberts 0:c79a1f70c110 376 DigitalInOut nrst(pin);
Wayne Roberts 0:c79a1f70c110 377 nrst.output();
Wayne Roberts 0:c79a1f70c110 378 nrst = 0;
Wayne Roberts 0:c79a1f70c110 379 wait_us(100);
Wayne Roberts 0:c79a1f70c110 380 nrst = 1;
Wayne Roberts 0:c79a1f70c110 381 nrst.mode(PullUp);
Wayne Roberts 0:c79a1f70c110 382 nrst.input();
Wayne Roberts 0:c79a1f70c110 383
Wayne Roberts 0:c79a1f70c110 384 while (busy)
Wayne Roberts 0:c79a1f70c110 385 ;
Wayne Roberts 0:c79a1f70c110 386 }
Wayne Roberts 0:c79a1f70c110 387
Wayne Roberts 0:c79a1f70c110 388 uint32_t SX126x::readReg(uint16_t addr, uint8_t len)
Wayne Roberts 0:c79a1f70c110 389 {
Wayne Roberts 0:c79a1f70c110 390 uint32_t ret = 0;
Wayne Roberts 0:c79a1f70c110 391 unsigned i;
Wayne Roberts 0:c79a1f70c110 392
Wayne Roberts 0:c79a1f70c110 393 uint8_t buf[7];
Wayne Roberts 0:c79a1f70c110 394 buf[0] = addr >> 8;
Wayne Roberts 0:c79a1f70c110 395 buf[1] = (uint8_t)addr;
Wayne Roberts 2:e6e159c8ab4d 396 xfer(OPCODE_READ_REGISTER, 2, 3+len, buf);
Wayne Roberts 0:c79a1f70c110 397 for (i = 0; i < len; i++) {
Wayne Roberts 0:c79a1f70c110 398 ret <<= 8;
Wayne Roberts 0:c79a1f70c110 399 ret |= buf[i+3];
Wayne Roberts 0:c79a1f70c110 400 }
Wayne Roberts 0:c79a1f70c110 401 return ret;
Wayne Roberts 0:c79a1f70c110 402 }
Wayne Roberts 0:c79a1f70c110 403
Wayne Roberts 5:8b75387af4e0 404 void SX126x::setBufferBase(uint8_t txAddr, uint8_t rxAddr)
Wayne Roberts 5:8b75387af4e0 405 {
Wayne Roberts 5:8b75387af4e0 406 uint8_t buf[2];
Wayne Roberts 5:8b75387af4e0 407
Wayne Roberts 5:8b75387af4e0 408 buf[0] = 0; // TX base address
Wayne Roberts 5:8b75387af4e0 409 buf[1] = 0; // RX base address
Wayne Roberts 5:8b75387af4e0 410 xfer(OPCODE_SET_BUFFER_BASE_ADDR, 2, 0, buf);
Wayne Roberts 5:8b75387af4e0 411 }