driver for sx1280

Dependents:   alarm_slave_extended_SX1280 alarm_master_extended_Vance_SX1280

Committer:
Wayne Roberts
Date:
Mon Jul 16 09:35:21 2018 -0700
Revision:
2:8a442c3511ae
Parent:
0:abb827c65ff5
Child:
3:54612373bec6
save context at setSleep; reset chip in constructor if stuck busy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Wayne Roberts 0:abb827c65ff5 1 #include "sx12xx.h"
Wayne Roberts 0:abb827c65ff5 2
Wayne Roberts 0:abb827c65ff5 3 Callback<void()> SX128x::diox_topHalf; // low latency ISR context
Wayne Roberts 0:abb827c65ff5 4
Wayne Roberts 0:abb827c65ff5 5 const float SX128x::timeOutStep[] = { 0.015625, 0.0625, 1, 4 };
Wayne Roberts 0:abb827c65ff5 6
Wayne Roberts 0:abb827c65ff5 7 void SX128x::dioxisr()
Wayne Roberts 0:abb827c65ff5 8 {
Wayne Roberts 0:abb827c65ff5 9 if (diox_topHalf)
Wayne Roberts 0:abb827c65ff5 10 diox_topHalf.call();
Wayne Roberts 0:abb827c65ff5 11 }
Wayne Roberts 0:abb827c65ff5 12
Wayne Roberts 2:8a442c3511ae 13 SX128x::SX128x(SPI& _spi, PinName _nss, PinName _busy, PinName _diox, PinName _nrst)
Wayne Roberts 2:8a442c3511ae 14 : spi(_spi), nss(_nss), busy(_busy), diox(_diox), nrst(_nrst)
Wayne Roberts 0:abb827c65ff5 15 {
Wayne Roberts 2:8a442c3511ae 16 unsigned busyCnt = 0;
Wayne Roberts 0:abb827c65ff5 17 nss = 1;
Wayne Roberts 0:abb827c65ff5 18
Wayne Roberts 2:8a442c3511ae 19 while (busy) {
Wayne Roberts 2:8a442c3511ae 20 if (++busyCnt > 0x80000) {
Wayne Roberts 2:8a442c3511ae 21 hw_reset();
Wayne Roberts 2:8a442c3511ae 22 }
Wayne Roberts 0:abb827c65ff5 23 }
Wayne Roberts 0:abb827c65ff5 24
Wayne Roberts 0:abb827c65ff5 25 periodBase = 3;
Wayne Roberts 2:8a442c3511ae 26
Wayne Roberts 2:8a442c3511ae 27 diox.rise(dioxisr);
Wayne Roberts 0:abb827c65ff5 28 }
Wayne Roberts 0:abb827c65ff5 29
Wayne Roberts 0:abb827c65ff5 30 uint8_t SX128x::xfer(uint8_t opcode, uint8_t wlen, uint8_t rlen, uint8_t* ptr)
Wayne Roberts 0:abb827c65ff5 31 {
Wayne Roberts 0:abb827c65ff5 32 const uint8_t* stopPtr;
Wayne Roberts 0:abb827c65ff5 33 const uint8_t* wstop;
Wayne Roberts 0:abb827c65ff5 34 const uint8_t* rstop;
Wayne Roberts 0:abb827c65ff5 35 uint8_t nop = 0;
Wayne Roberts 0:abb827c65ff5 36 uint8_t ret;
Wayne Roberts 0:abb827c65ff5 37
Wayne Roberts 0:abb827c65ff5 38 if (opcode != OPCODE_GET_STATUS) {
Wayne Roberts 0:abb827c65ff5 39 if (sleeping) {
Wayne Roberts 0:abb827c65ff5 40 nss = 0;
Wayne Roberts 0:abb827c65ff5 41 while (busy)
Wayne Roberts 0:abb827c65ff5 42 ;
Wayne Roberts 0:abb827c65ff5 43 sleeping = false;
Wayne Roberts 0:abb827c65ff5 44 } else {
Wayne Roberts 0:abb827c65ff5 45 while (busy)
Wayne Roberts 0:abb827c65ff5 46 ;
Wayne Roberts 0:abb827c65ff5 47
Wayne Roberts 0:abb827c65ff5 48 nss = 0;
Wayne Roberts 0:abb827c65ff5 49 }
Wayne Roberts 0:abb827c65ff5 50 } else
Wayne Roberts 0:abb827c65ff5 51 nss = 0;
Wayne Roberts 0:abb827c65ff5 52
Wayne Roberts 2:8a442c3511ae 53 if (opcode == OPCODE_SET_SLEEP) {
Wayne Roberts 2:8a442c3511ae 54 pc.printf("setSleep-%02x\r\n", *ptr);
Wayne Roberts 2:8a442c3511ae 55 }
Wayne Roberts 2:8a442c3511ae 56
Wayne Roberts 0:abb827c65ff5 57 ret = spi.write(opcode);
Wayne Roberts 0:abb827c65ff5 58
Wayne Roberts 0:abb827c65ff5 59 wstop = ptr + wlen;
Wayne Roberts 0:abb827c65ff5 60 rstop = ptr + rlen;
Wayne Roberts 0:abb827c65ff5 61 if (rlen > wlen)
Wayne Roberts 0:abb827c65ff5 62 stopPtr = rstop;
Wayne Roberts 0:abb827c65ff5 63 else
Wayne Roberts 0:abb827c65ff5 64 stopPtr = wstop;
Wayne Roberts 0:abb827c65ff5 65
Wayne Roberts 0:abb827c65ff5 66 for (; ptr < stopPtr; ptr++) {
Wayne Roberts 0:abb827c65ff5 67 if (ptr < wstop && ptr < rstop)
Wayne Roberts 0:abb827c65ff5 68 *ptr = spi.write(*ptr);
Wayne Roberts 0:abb827c65ff5 69 else if (ptr < wstop)
Wayne Roberts 0:abb827c65ff5 70 spi.write(*ptr);
Wayne Roberts 0:abb827c65ff5 71 else
Wayne Roberts 0:abb827c65ff5 72 *ptr = spi.write(nop); // n >= write length: send NOP
Wayne Roberts 0:abb827c65ff5 73 }
Wayne Roberts 0:abb827c65ff5 74
Wayne Roberts 0:abb827c65ff5 75 nss = 1;
Wayne Roberts 0:abb827c65ff5 76
Wayne Roberts 0:abb827c65ff5 77 if (opcode == OPCODE_SET_SLEEP)
Wayne Roberts 0:abb827c65ff5 78 sleeping = true;
Wayne Roberts 0:abb827c65ff5 79
Wayne Roberts 0:abb827c65ff5 80 return ret;
Wayne Roberts 0:abb827c65ff5 81 }
Wayne Roberts 0:abb827c65ff5 82
Wayne Roberts 0:abb827c65ff5 83 uint32_t SX128x::readReg(uint16_t addr, uint8_t len)
Wayne Roberts 0:abb827c65ff5 84 {
Wayne Roberts 0:abb827c65ff5 85 uint32_t ret = 0;
Wayne Roberts 0:abb827c65ff5 86 unsigned i;
Wayne Roberts 0:abb827c65ff5 87
Wayne Roberts 0:abb827c65ff5 88 uint8_t buf[7];
Wayne Roberts 0:abb827c65ff5 89 buf[0] = addr >> 8;
Wayne Roberts 0:abb827c65ff5 90 buf[1] = (uint8_t)addr;
Wayne Roberts 0:abb827c65ff5 91 xfer(OPCODE_READ_REGISTER, 2, 3+len, buf);
Wayne Roberts 0:abb827c65ff5 92 for (i = 0; i < len; i++) {
Wayne Roberts 0:abb827c65ff5 93 ret <<= 8;
Wayne Roberts 0:abb827c65ff5 94 ret |= buf[i+3];
Wayne Roberts 0:abb827c65ff5 95 }
Wayne Roberts 0:abb827c65ff5 96 return ret;
Wayne Roberts 0:abb827c65ff5 97 }
Wayne Roberts 0:abb827c65ff5 98
Wayne Roberts 0:abb827c65ff5 99 void SX128x::start_tx(uint8_t pktLen, float timeout_ms)
Wayne Roberts 0:abb827c65ff5 100 {
Wayne Roberts 0:abb827c65ff5 101 IrqFlags_t irqEnable;
Wayne Roberts 0:abb827c65ff5 102 uint8_t buf[8];
Wayne Roberts 0:abb827c65ff5 103
Wayne Roberts 0:abb827c65ff5 104 irqEnable.word = 0;
Wayne Roberts 0:abb827c65ff5 105 irqEnable.bits.TxDone = 1;
Wayne Roberts 0:abb827c65ff5 106 irqEnable.bits.RxTxTimeout = 1;
Wayne Roberts 0:abb827c65ff5 107
Wayne Roberts 0:abb827c65ff5 108 buf[0] = irqEnable.word >> 8; // enable bits
Wayne Roberts 0:abb827c65ff5 109 buf[1] = irqEnable.word; // enable bits
Wayne Roberts 0:abb827c65ff5 110 buf[2] = irqEnable.word >> 8; // dio1
Wayne Roberts 0:abb827c65ff5 111 buf[3] = irqEnable.word; // dio1
Wayne Roberts 0:abb827c65ff5 112 buf[4] = 0; // dio2
Wayne Roberts 0:abb827c65ff5 113 buf[5] = 0; // dio2
Wayne Roberts 0:abb827c65ff5 114 buf[6] = 0; // dio3
Wayne Roberts 0:abb827c65ff5 115 buf[7] = 0; // dio3
Wayne Roberts 0:abb827c65ff5 116 xfer(OPCODE_SET_DIO_IRQ_PARAMS, 8, 0, buf);
Wayne Roberts 0:abb827c65ff5 117
Wayne Roberts 0:abb827c65ff5 118 {
Wayne Roberts 0:abb827c65ff5 119 uint8_t i;
Wayne Roberts 0:abb827c65ff5 120
Wayne Roberts 0:abb827c65ff5 121 while (busy)
Wayne Roberts 0:abb827c65ff5 122 ;
Wayne Roberts 0:abb827c65ff5 123
Wayne Roberts 0:abb827c65ff5 124 nss = 0;
Wayne Roberts 0:abb827c65ff5 125 spi.write(OPCODE_WRITE_BUFFER);
Wayne Roberts 0:abb827c65ff5 126 spi.write(0); // offset
Wayne Roberts 0:abb827c65ff5 127 i = 0;
Wayne Roberts 0:abb827c65ff5 128 for (i = 0; i < pktLen; i++) {
Wayne Roberts 0:abb827c65ff5 129 spi.write(tx_buf[i]);
Wayne Roberts 0:abb827c65ff5 130 }
Wayne Roberts 0:abb827c65ff5 131 nss = 1;
Wayne Roberts 0:abb827c65ff5 132 }
Wayne Roberts 0:abb827c65ff5 133
Wayne Roberts 2:8a442c3511ae 134 buf[0] = periodBase;
Wayne Roberts 0:abb827c65ff5 135 if (timeout_ms > 0) {
Wayne Roberts 0:abb827c65ff5 136 unsigned t_o = timeout_ms / timeOutStep[periodBase];
Wayne Roberts 0:abb827c65ff5 137 buf[1] = t_o >> 8;
Wayne Roberts 0:abb827c65ff5 138 buf[2] = t_o;
Wayne Roberts 0:abb827c65ff5 139 } else {
Wayne Roberts 0:abb827c65ff5 140 /* no timeout */
Wayne Roberts 0:abb827c65ff5 141 buf[1] = 0;
Wayne Roberts 0:abb827c65ff5 142 buf[2] = 0;
Wayne Roberts 0:abb827c65ff5 143 }
Wayne Roberts 0:abb827c65ff5 144 xfer(OPCODE_SET_TX, 3, 0, buf);
Wayne Roberts 0:abb827c65ff5 145
Wayne Roberts 0:abb827c65ff5 146 chipMode = CHIPMODE_TX;
Wayne Roberts 0:abb827c65ff5 147 if (chipModeChange)
Wayne Roberts 0:abb827c65ff5 148 chipModeChange.call();
Wayne Roberts 0:abb827c65ff5 149 }
Wayne Roberts 0:abb827c65ff5 150
Wayne Roberts 2:8a442c3511ae 151 void SX128x::hw_reset()
Wayne Roberts 0:abb827c65ff5 152 {
Wayne Roberts 0:abb827c65ff5 153 nrst.output();
Wayne Roberts 0:abb827c65ff5 154 nrst = 0;
Wayne Roberts 0:abb827c65ff5 155 wait_us(100);
Wayne Roberts 0:abb827c65ff5 156 nrst = 1;
Wayne Roberts 0:abb827c65ff5 157 nrst.mode(PullUp);
Wayne Roberts 0:abb827c65ff5 158 nrst.input();
Wayne Roberts 0:abb827c65ff5 159
Wayne Roberts 0:abb827c65ff5 160 while (busy)
Wayne Roberts 0:abb827c65ff5 161 ;
Wayne Roberts 0:abb827c65ff5 162 }
Wayne Roberts 0:abb827c65ff5 163
Wayne Roberts 0:abb827c65ff5 164 uint64_t SX128x::getSyncAddr(uint8_t num)
Wayne Roberts 0:abb827c65ff5 165 {
Wayne Roberts 0:abb827c65ff5 166 uint64_t ret;
Wayne Roberts 0:abb827c65ff5 167 unsigned regAdr = REG_ADDR_PKT_SYNC_ADRS_1 + ((num-1) * 5);
Wayne Roberts 0:abb827c65ff5 168 ret = readReg(regAdr, 1);
Wayne Roberts 0:abb827c65ff5 169 ret <<= 32;
Wayne Roberts 0:abb827c65ff5 170 ret |= readReg(regAdr+1, 4);
Wayne Roberts 0:abb827c65ff5 171 return ret;
Wayne Roberts 0:abb827c65ff5 172 }
Wayne Roberts 0:abb827c65ff5 173
Wayne Roberts 0:abb827c65ff5 174 void SX128x::setSyncAddr(uint8_t num, uint64_t sa)
Wayne Roberts 0:abb827c65ff5 175 {
Wayne Roberts 0:abb827c65ff5 176 unsigned regAdr = REG_ADDR_PKT_SYNC_ADRS_1 + ((num-1) * 5);
Wayne Roberts 0:abb827c65ff5 177 writeReg(regAdr+1, sa & 0xffffffff, 4);
Wayne Roberts 0:abb827c65ff5 178 sa >>= 32;
Wayne Roberts 0:abb827c65ff5 179 writeReg(regAdr, sa & 0xff, 1);
Wayne Roberts 0:abb827c65ff5 180 }
Wayne Roberts 0:abb827c65ff5 181
Wayne Roberts 0:abb827c65ff5 182 void SX128x::set_tx_dbm(int8_t dbm)
Wayne Roberts 0:abb827c65ff5 183 {
Wayne Roberts 0:abb827c65ff5 184 uint8_t buf[2];
Wayne Roberts 0:abb827c65ff5 185
Wayne Roberts 0:abb827c65ff5 186 buf[0] = dbm;
Wayne Roberts 0:abb827c65ff5 187 buf[1] = RADIO_RAMP_20_US;
Wayne Roberts 0:abb827c65ff5 188 xfer(OPCODE_SET_TX_PARAMS, 2, 0, buf);
Wayne Roberts 0:abb827c65ff5 189 }
Wayne Roberts 0:abb827c65ff5 190
Wayne Roberts 0:abb827c65ff5 191 void SX128x::setMHz(float MHz)
Wayne Roberts 0:abb827c65ff5 192 {
Wayne Roberts 0:abb827c65ff5 193 unsigned frf = MHz / PLL_STEP_MHZ;
Wayne Roberts 0:abb827c65ff5 194 uint8_t buf[3];
Wayne Roberts 0:abb827c65ff5 195
Wayne Roberts 0:abb827c65ff5 196 buf[0] = frf >> 16;
Wayne Roberts 0:abb827c65ff5 197 buf[1] = frf >> 8;
Wayne Roberts 0:abb827c65ff5 198 buf[2] = frf;
Wayne Roberts 0:abb827c65ff5 199 xfer(OPCODE_SET_RF_FREQUENCY, 3, 0, buf);
Wayne Roberts 0:abb827c65ff5 200 }
Wayne Roberts 0:abb827c65ff5 201
Wayne Roberts 0:abb827c65ff5 202 float SX128x::getMHz()
Wayne Roberts 0:abb827c65ff5 203 {
Wayne Roberts 0:abb827c65ff5 204 uint32_t frf = readReg(REG_ADDR_RFFREQ, 3);
Wayne Roberts 0:abb827c65ff5 205 return frf * PLL_STEP_MHZ;
Wayne Roberts 0:abb827c65ff5 206 }
Wayne Roberts 0:abb827c65ff5 207
Wayne Roberts 0:abb827c65ff5 208 void SX128x::setStandby(stby_t stby)
Wayne Roberts 0:abb827c65ff5 209 {
Wayne Roberts 0:abb827c65ff5 210 uint8_t octet = stby;
Wayne Roberts 0:abb827c65ff5 211 xfer(OPCODE_SET_STANDBY, 1, 0, &octet);
Wayne Roberts 0:abb827c65ff5 212
Wayne Roberts 0:abb827c65ff5 213 chipMode = CHIPMODE_NONE;
Wayne Roberts 0:abb827c65ff5 214 if (chipModeChange)
Wayne Roberts 0:abb827c65ff5 215 chipModeChange.call();
Wayne Roberts 0:abb827c65ff5 216 }
Wayne Roberts 0:abb827c65ff5 217
Wayne Roberts 0:abb827c65ff5 218 void SX128x::setFS()
Wayne Roberts 0:abb827c65ff5 219 {
Wayne Roberts 0:abb827c65ff5 220 xfer(OPCODE_SET_FS, 0, 0, NULL);
Wayne Roberts 0:abb827c65ff5 221
Wayne Roberts 0:abb827c65ff5 222 chipMode = CHIPMODE_NONE;
Wayne Roberts 0:abb827c65ff5 223 if (chipModeChange)
Wayne Roberts 0:abb827c65ff5 224 chipModeChange.call();
Wayne Roberts 0:abb827c65ff5 225 }
Wayne Roberts 0:abb827c65ff5 226
Wayne Roberts 2:8a442c3511ae 227 void SX128x::setSleep(bool warm)
Wayne Roberts 0:abb827c65ff5 228 {
Wayne Roberts 0:abb827c65ff5 229 sleepConfig_t sc;
Wayne Roberts 0:abb827c65ff5 230
Wayne Roberts 2:8a442c3511ae 231 if (warm) {
Wayne Roberts 2:8a442c3511ae 232 xfer(OPCODE_SAVE_CONTEXT, 0, 0, NULL);
Wayne Roberts 2:8a442c3511ae 233 }
Wayne Roberts 0:abb827c65ff5 234
Wayne Roberts 0:abb827c65ff5 235 chipMode = CHIPMODE_NONE;
Wayne Roberts 0:abb827c65ff5 236 if (chipModeChange)
Wayne Roberts 0:abb827c65ff5 237 chipModeChange.call();
Wayne Roberts 2:8a442c3511ae 238
Wayne Roberts 2:8a442c3511ae 239 sc.octet = 0;
Wayne Roberts 2:8a442c3511ae 240 sc.retentionBits.dataRAM = warm;
Wayne Roberts 2:8a442c3511ae 241 sc.retentionBits.dataBuffer = warm;
Wayne Roberts 2:8a442c3511ae 242 sc.retentionBits.instructionRAM = warm;
Wayne Roberts 2:8a442c3511ae 243 xfer(OPCODE_SET_SLEEP, 1, 0, &sc.octet);
Wayne Roberts 0:abb827c65ff5 244 }
Wayne Roberts 0:abb827c65ff5 245
Wayne Roberts 0:abb827c65ff5 246 uint8_t SX128x::getPacketType()
Wayne Roberts 0:abb827c65ff5 247 {
Wayne Roberts 0:abb827c65ff5 248 uint8_t buf[2];
Wayne Roberts 0:abb827c65ff5 249
Wayne Roberts 0:abb827c65ff5 250 xfer(OPCODE_GET_PACKET_TYPE, 0, 2, buf);
Wayne Roberts 2:8a442c3511ae 251 pktType = buf[1];
Wayne Roberts 0:abb827c65ff5 252
Wayne Roberts 0:abb827c65ff5 253 return buf[1];
Wayne Roberts 0:abb827c65ff5 254 }
Wayne Roberts 0:abb827c65ff5 255
Wayne Roberts 0:abb827c65ff5 256 void SX128x::setPacketType(uint8_t type)
Wayne Roberts 0:abb827c65ff5 257 {
Wayne Roberts 0:abb827c65ff5 258 xfer(OPCODE_SET_PACKET_TYPE, 1, 0, &type);
Wayne Roberts 2:8a442c3511ae 259 pktType = type;
Wayne Roberts 0:abb827c65ff5 260 }
Wayne Roberts 0:abb827c65ff5 261
Wayne Roberts 0:abb827c65ff5 262 void SX128x::setBufferBase(uint8_t txAddr, uint8_t rxAddr)
Wayne Roberts 0:abb827c65ff5 263 {
Wayne Roberts 0:abb827c65ff5 264 uint8_t buf[2];
Wayne Roberts 0:abb827c65ff5 265
Wayne Roberts 0:abb827c65ff5 266 buf[0] = txAddr; // TX base address
Wayne Roberts 0:abb827c65ff5 267 buf[1] = rxAddr; // RX base address
Wayne Roberts 0:abb827c65ff5 268 xfer(OPCODE_SET_BUFFER_BASE_ADDR, 2, 0, buf);
Wayne Roberts 0:abb827c65ff5 269 }
Wayne Roberts 0:abb827c65ff5 270
Wayne Roberts 0:abb827c65ff5 271 void SX128x::setRegulator(uint8_t rmp)
Wayne Roberts 0:abb827c65ff5 272 {
Wayne Roberts 0:abb827c65ff5 273 xfer(OPCODE_SET_REGULATOR_MODE, 1, 0, &rmp);
Wayne Roberts 0:abb827c65ff5 274 }
Wayne Roberts 0:abb827c65ff5 275
Wayne Roberts 0:abb827c65ff5 276 void SX128x::start_rx(float timeout_ms)
Wayne Roberts 0:abb827c65ff5 277 {
Wayne Roberts 0:abb827c65ff5 278 IrqFlags_t irqEnable;
Wayne Roberts 0:abb827c65ff5 279 uint8_t buf[8];
Wayne Roberts 0:abb827c65ff5 280 unsigned t_o;
Wayne Roberts 0:abb827c65ff5 281
Wayne Roberts 0:abb827c65ff5 282 irqEnable.word = 0;
Wayne Roberts 0:abb827c65ff5 283 irqEnable.bits.RxDone = 1;
Wayne Roberts 0:abb827c65ff5 284 irqEnable.bits.RxTxTimeout = 1;
Wayne Roberts 0:abb827c65ff5 285
Wayne Roberts 0:abb827c65ff5 286 buf[0] = irqEnable.word >> 8; // enable bits
Wayne Roberts 0:abb827c65ff5 287 buf[1] = irqEnable.word; // enable bits
Wayne Roberts 0:abb827c65ff5 288 buf[2] = irqEnable.word >> 8; // dio1
Wayne Roberts 0:abb827c65ff5 289 buf[3] = irqEnable.word; // dio1
Wayne Roberts 0:abb827c65ff5 290 buf[4] = 0; // dio2
Wayne Roberts 0:abb827c65ff5 291 buf[5] = 0; // dio2
Wayne Roberts 0:abb827c65ff5 292 buf[6] = 0; // dio3
Wayne Roberts 0:abb827c65ff5 293 buf[7] = 0; // dio3
Wayne Roberts 0:abb827c65ff5 294 xfer(OPCODE_SET_DIO_IRQ_PARAMS, 8, 0, buf);
Wayne Roberts 0:abb827c65ff5 295
Wayne Roberts 0:abb827c65ff5 296 buf[0] = periodBase;
Wayne Roberts 0:abb827c65ff5 297 if (timeout_ms > 0) {
Wayne Roberts 0:abb827c65ff5 298 t_o = timeout_ms / timeOutStep[periodBase];
Wayne Roberts 0:abb827c65ff5 299 buf[1] = t_o >> 8;
Wayne Roberts 0:abb827c65ff5 300 buf[2] = t_o;
Wayne Roberts 0:abb827c65ff5 301 } else {
Wayne Roberts 0:abb827c65ff5 302 /* receive packets forever */
Wayne Roberts 0:abb827c65ff5 303 buf[1] = 0xff;
Wayne Roberts 0:abb827c65ff5 304 buf[2] = 0xff;
Wayne Roberts 0:abb827c65ff5 305 }
Wayne Roberts 0:abb827c65ff5 306 xfer(OPCODE_SET_RX, 3, 0, buf);
Wayne Roberts 0:abb827c65ff5 307
Wayne Roberts 0:abb827c65ff5 308 chipMode = CHIPMODE_RX;
Wayne Roberts 0:abb827c65ff5 309 if (chipModeChange) {
Wayne Roberts 0:abb827c65ff5 310 chipModeChange.call();
Wayne Roberts 0:abb827c65ff5 311 }
Wayne Roberts 0:abb827c65ff5 312 }
Wayne Roberts 0:abb827c65ff5 313
Wayne Roberts 0:abb827c65ff5 314 void SX128x::service()
Wayne Roberts 0:abb827c65ff5 315 {
Wayne Roberts 0:abb827c65ff5 316 IrqFlags_t irqFlags, clearIrqFlags;
Wayne Roberts 0:abb827c65ff5 317 uint8_t buf[6];
Wayne Roberts 0:abb827c65ff5 318 pktStatus_t pktStatus;
Wayne Roberts 2:8a442c3511ae 319 //status_t st;
Wayne Roberts 0:abb827c65ff5 320
Wayne Roberts 0:abb827c65ff5 321 if (busy) {
Wayne Roberts 0:abb827c65ff5 322 return;
Wayne Roberts 0:abb827c65ff5 323 }
Wayne Roberts 0:abb827c65ff5 324
Wayne Roberts 0:abb827c65ff5 325 while (diox) {
Wayne Roberts 0:abb827c65ff5 326 xfer(OPCODE_GET_IRQ_STATUS, 0, 3, buf);
Wayne Roberts 2:8a442c3511ae 327 /*st.octet = buf[0]; */
Wayne Roberts 0:abb827c65ff5 328 irqFlags.word = buf[1] << 8;
Wayne Roberts 0:abb827c65ff5 329 irqFlags.word |= buf[2];
Wayne Roberts 0:abb827c65ff5 330 clearIrqFlags.word = 0;
Wayne Roberts 0:abb827c65ff5 331 if (irqFlags.bits.TxDone) {
Wayne Roberts 0:abb827c65ff5 332 chipMode = CHIPMODE_NONE;
Wayne Roberts 0:abb827c65ff5 333 if (chipModeChange)
Wayne Roberts 0:abb827c65ff5 334 chipModeChange.call();
Wayne Roberts 2:8a442c3511ae 335 if (txDone)
Wayne Roberts 2:8a442c3511ae 336 txDone.call(); // might change to Rx
Wayne Roberts 2:8a442c3511ae 337 clearIrqFlags.bits.TxDone = 1;
Wayne Roberts 0:abb827c65ff5 338 }
Wayne Roberts 0:abb827c65ff5 339 if (irqFlags.bits.RxDone) {
Wayne Roberts 0:abb827c65ff5 340 if (rxDone) {
Wayne Roberts 2:8a442c3511ae 341 uint8_t len, slen;
Wayne Roberts 0:abb827c65ff5 342 xfer(OPCODE_GET_RX_BUFFER_STATUS, 0, 3, buf);
Wayne Roberts 2:8a442c3511ae 343 /*st.octet = buf[0]; */
Wayne Roberts 0:abb827c65ff5 344 if (buf[1] == 0)
Wayne Roberts 0:abb827c65ff5 345 len = readReg(REG_ADDR_LORA_TX_PAYLOAD_LENGTH, 1); // lora implicit
Wayne Roberts 0:abb827c65ff5 346 else
Wayne Roberts 0:abb827c65ff5 347 len = buf[1];
Wayne Roberts 0:abb827c65ff5 348
Wayne Roberts 0:abb827c65ff5 349 ReadBuffer(len, buf[2]);
Wayne Roberts 2:8a442c3511ae 350
Wayne Roberts 2:8a442c3511ae 351 if (pktType == PACKET_TYPE_LORA || pktType == PACKET_TYPE_RANGING)
Wayne Roberts 2:8a442c3511ae 352 slen = 3;
Wayne Roberts 2:8a442c3511ae 353 else
Wayne Roberts 2:8a442c3511ae 354 slen = 6;
Wayne Roberts 2:8a442c3511ae 355
Wayne Roberts 2:8a442c3511ae 356 xfer(OPCODE_GET_PACKET_STATUS, 0, slen, pktStatus.buf);
Wayne Roberts 0:abb827c65ff5 357 rxDone(len, &pktStatus);
Wayne Roberts 0:abb827c65ff5 358 }
Wayne Roberts 2:8a442c3511ae 359
Wayne Roberts 0:abb827c65ff5 360 clearIrqFlags.bits.RxDone = 1;
Wayne Roberts 0:abb827c65ff5 361 }
Wayne Roberts 0:abb827c65ff5 362 if (irqFlags.bits.RxTxTimeout) {
Wayne Roberts 0:abb827c65ff5 363 if (chipMode != CHIPMODE_NONE) {
Wayne Roberts 0:abb827c65ff5 364 if (timeout)
Wayne Roberts 0:abb827c65ff5 365 timeout(chipMode == CHIPMODE_TX);
Wayne Roberts 0:abb827c65ff5 366 }
Wayne Roberts 0:abb827c65ff5 367 chipMode = CHIPMODE_NONE;
Wayne Roberts 0:abb827c65ff5 368 if (chipModeChange)
Wayne Roberts 0:abb827c65ff5 369 chipModeChange.call();
Wayne Roberts 0:abb827c65ff5 370 clearIrqFlags.bits.RxTxTimeout = 1;
Wayne Roberts 0:abb827c65ff5 371 }
Wayne Roberts 0:abb827c65ff5 372
Wayne Roberts 0:abb827c65ff5 373 if (clearIrqFlags.word != 0) {
Wayne Roberts 0:abb827c65ff5 374 buf[0] = clearIrqFlags.word >> 8;
Wayne Roberts 0:abb827c65ff5 375 buf[1] = (uint8_t)clearIrqFlags.word;
Wayne Roberts 0:abb827c65ff5 376 xfer(OPCODE_CLEAR_IRQ_STATUS, 2, 0, buf);
Wayne Roberts 0:abb827c65ff5 377 }
Wayne Roberts 0:abb827c65ff5 378
Wayne Roberts 0:abb827c65ff5 379 } // ...while (diox)
Wayne Roberts 0:abb827c65ff5 380
Wayne Roberts 0:abb827c65ff5 381 } // ..service()
Wayne Roberts 0:abb827c65ff5 382
Wayne Roberts 0:abb827c65ff5 383 void SX128x::writeReg(uint16_t addr, uint32_t data, uint8_t len)
Wayne Roberts 0:abb827c65ff5 384 {
Wayne Roberts 0:abb827c65ff5 385 uint8_t buf[6];
Wayne Roberts 0:abb827c65ff5 386 uint8_t n;
Wayne Roberts 0:abb827c65ff5 387 buf[0] = addr >> 8;
Wayne Roberts 0:abb827c65ff5 388 buf[1] = (uint8_t)addr;
Wayne Roberts 0:abb827c65ff5 389 for (n = len; n > 0; n--) {
Wayne Roberts 0:abb827c65ff5 390 buf[n+1] = (uint8_t)data;
Wayne Roberts 0:abb827c65ff5 391 data >>= 8;
Wayne Roberts 0:abb827c65ff5 392 }
Wayne Roberts 0:abb827c65ff5 393 xfer(OPCODE_WRITE_REGISTER, 2+len, 2+len, buf);
Wayne Roberts 0:abb827c65ff5 394 }
Wayne Roberts 0:abb827c65ff5 395
Wayne Roberts 0:abb827c65ff5 396 void SX128x::ReadBuffer(uint8_t size, uint8_t offset)
Wayne Roberts 0:abb827c65ff5 397 {
Wayne Roberts 2:8a442c3511ae 398 //status_t st;
Wayne Roberts 0:abb827c65ff5 399 unsigned i;
Wayne Roberts 2:8a442c3511ae 400
Wayne Roberts 0:abb827c65ff5 401 while (busy)
Wayne Roberts 0:abb827c65ff5 402 ;
Wayne Roberts 0:abb827c65ff5 403
Wayne Roberts 0:abb827c65ff5 404 nss = 0;
Wayne Roberts 0:abb827c65ff5 405
Wayne Roberts 2:8a442c3511ae 406 /*st.octet =*/ spi.write(OPCODE_READ_BUFFER);
Wayne Roberts 2:8a442c3511ae 407 /*st.octet =*/ spi.write(offset);
Wayne Roberts 2:8a442c3511ae 408 /*st.octet =*/ spi.write(0); // NOP
Wayne Roberts 0:abb827c65ff5 409 i = 0;
Wayne Roberts 0:abb827c65ff5 410 for (i = 0; i < size; i++) {
Wayne Roberts 0:abb827c65ff5 411 rx_buf[i] = spi.write(0);
Wayne Roberts 0:abb827c65ff5 412 }
Wayne Roberts 0:abb827c65ff5 413
Wayne Roberts 0:abb827c65ff5 414 nss = 1;
Wayne Roberts 0:abb827c65ff5 415 }
Wayne Roberts 0:abb827c65ff5 416