driver for sx1280
Dependents: alarm_slave_extended_SX1280 alarm_master_extended_Vance_SX1280
Diff: sx1280.cpp
- Revision:
- 2:8a442c3511ae
- Parent:
- 0:abb827c65ff5
- Child:
- 3:54612373bec6
--- a/sx1280.cpp Fri Jun 29 15:04:59 2018 -0700 +++ b/sx1280.cpp Mon Jul 16 09:35:21 2018 -0700 @@ -10,21 +10,21 @@ diox_topHalf.call(); } -SX128x::SX128x(SPI& _spi, PinName _nss, PinName _busy, PinName _diox) - : spi(_spi), nss(_nss), busy(_busy), diox(_diox) +SX128x::SX128x(SPI& _spi, PinName _nss, PinName _busy, PinName _diox, PinName _nrst) + : spi(_spi), nss(_nss), busy(_busy), diox(_diox), nrst(_nrst) { + unsigned busyCnt = 0; nss = 1; - diox.rise(dioxisr); - - if (busy) { - /*status_t status; - status.octet =*/ xfer(OPCODE_GET_STATUS, 0, 0, NULL); - while (busy) - ; + while (busy) { + if (++busyCnt > 0x80000) { + hw_reset(); + } } periodBase = 3; + + diox.rise(dioxisr); } uint8_t SX128x::xfer(uint8_t opcode, uint8_t wlen, uint8_t rlen, uint8_t* ptr) @@ -50,6 +50,10 @@ } else nss = 0; + if (opcode == OPCODE_SET_SLEEP) { + pc.printf("setSleep-%02x\r\n", *ptr); + } + ret = spi.write(opcode); wstop = ptr + wlen; @@ -127,7 +131,7 @@ nss = 1; } - buf[0] = 3; + buf[0] = periodBase; if (timeout_ms > 0) { unsigned t_o = timeout_ms / timeOutStep[periodBase]; buf[1] = t_o >> 8; @@ -144,9 +148,8 @@ chipModeChange.call(); } -void SX128x::hw_reset(PinName pin) +void SX128x::hw_reset() { - DigitalInOut nrst(pin); nrst.output(); nrst = 0; wait_us(100); @@ -221,19 +224,23 @@ chipModeChange.call(); } -void SX128x::setSleep(bool warmStart) +void SX128x::setSleep(bool warm) { sleepConfig_t sc; - sc.octet = 0; - sc.retentionBits.dataRAM = warmStart; - sc.retentionBits.dataBuffer = warmStart; - sc.retentionBits.instructionRAM = warmStart; - xfer(OPCODE_SET_SLEEP, 1, 0, &sc.octet); + if (warm) { + xfer(OPCODE_SAVE_CONTEXT, 0, 0, NULL); + } chipMode = CHIPMODE_NONE; if (chipModeChange) chipModeChange.call(); + + sc.octet = 0; + sc.retentionBits.dataRAM = warm; + sc.retentionBits.dataBuffer = warm; + sc.retentionBits.instructionRAM = warm; + xfer(OPCODE_SET_SLEEP, 1, 0, &sc.octet); } uint8_t SX128x::getPacketType() @@ -241,6 +248,7 @@ uint8_t buf[2]; xfer(OPCODE_GET_PACKET_TYPE, 0, 2, buf); + pktType = buf[1]; return buf[1]; } @@ -248,6 +256,7 @@ void SX128x::setPacketType(uint8_t type) { xfer(OPCODE_SET_PACKET_TYPE, 1, 0, &type); + pktType = type; } void SX128x::setBufferBase(uint8_t txAddr, uint8_t rxAddr) @@ -307,6 +316,7 @@ IrqFlags_t irqFlags, clearIrqFlags; uint8_t buf[6]; pktStatus_t pktStatus; + //status_t st; if (busy) { return; @@ -314,30 +324,39 @@ while (diox) { xfer(OPCODE_GET_IRQ_STATUS, 0, 3, buf); + /*st.octet = buf[0]; */ irqFlags.word = buf[1] << 8; irqFlags.word |= buf[2]; clearIrqFlags.word = 0; if (irqFlags.bits.TxDone) { - if (txDone) - txDone.call(); - clearIrqFlags.bits.TxDone = 1; chipMode = CHIPMODE_NONE; if (chipModeChange) chipModeChange.call(); + if (txDone) + txDone.call(); // might change to Rx + clearIrqFlags.bits.TxDone = 1; } if (irqFlags.bits.RxDone) { if (rxDone) { - uint8_t len; + uint8_t len, slen; xfer(OPCODE_GET_RX_BUFFER_STATUS, 0, 3, buf); + /*st.octet = buf[0]; */ if (buf[1] == 0) len = readReg(REG_ADDR_LORA_TX_PAYLOAD_LENGTH, 1); // lora implicit else len = buf[1]; ReadBuffer(len, buf[2]); - xfer(OPCODE_GET_PACKET_STATUS, 0, 6, pktStatus.buf); + + if (pktType == PACKET_TYPE_LORA || pktType == PACKET_TYPE_RANGING) + slen = 3; + else + slen = 6; + + xfer(OPCODE_GET_PACKET_STATUS, 0, slen, pktStatus.buf); rxDone(len, &pktStatus); } + clearIrqFlags.bits.RxDone = 1; } if (irqFlags.bits.RxTxTimeout) { @@ -376,15 +395,17 @@ void SX128x::ReadBuffer(uint8_t size, uint8_t offset) { + //status_t st; unsigned i; + while (busy) ; nss = 0; - spi.write(OPCODE_READ_BUFFER); - spi.write(offset); - spi.write(0); // NOP + /*st.octet =*/ spi.write(OPCODE_READ_BUFFER); + /*st.octet =*/ spi.write(offset); + /*st.octet =*/ spi.write(0); // NOP i = 0; for (i = 0; i < size; i++) { rx_buf[i] = spi.write(0);