diff --git a/README.md b/README.md index 58756e5..fdb0c63 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# librfm +# librfm95 ## About diff --git a/librfm.c b/librfm.c index eec770c..eed555a 100644 --- a/librfm.c +++ b/librfm.c @@ -8,8 +8,9 @@ #include "librfm.h" #include "utils.h" -static volatile uint8_t irqFlags1 = 0; -static volatile uint8_t irqFlags2 = 0; +static volatile bool packetSent = false; +static volatile bool payloadReady = false; +static volatile bool timeout = false; /** * Writes the given value to the given register. @@ -47,14 +48,6 @@ } /** - * Clears the IRQ flags read from the module. - */ -static void clearIrqFlags(void) { - irqFlags1 = 0; - irqFlags2 = 0; -} - -/** * Enables or disables timeouts. * * @param enable @@ -63,6 +56,7 @@ if (enable) { // get "Timeout" on DIO4 regWrite(DIO_MAP2, (regRead(DIO_MAP2) | 0x80) & ~0x40); + timeout = false; // TODO calculate - seems to be about 50, 75, 100ms regWrite(RX_TO_RSSI, 0x1f); regWrite(RX_TO_PREA, 0x2f); @@ -70,7 +64,7 @@ } else { regWrite(RX_TO_RSSI, 0x00); regWrite(RX_TO_PREA, 0x00); - regWrite(RX_TO_SYNC, 0x0f); + regWrite(RX_TO_SYNC, 0x00); } } @@ -102,7 +96,7 @@ regWrite(FDEV_MSB, 0x00); regWrite(FDEV_LSB, 0xa4); - // set the carrier frequency + // set the carrier frequency uint32_t frf = freq * 1000000ULL / F_STEP; regWrite(FRF_MSB, frf >> 16); regWrite(FRF_MID, frf >> 8); @@ -183,11 +177,20 @@ } void rfmIrq(void) { - irqFlags1 = regRead(IRQ_FLAGS1); - irqFlags2 = regRead(IRQ_FLAGS2); + uint8_t irqFlags1 = regRead(IRQ_FLAGS1); + uint8_t irqFlags2 = regRead(IRQ_FLAGS2); + + if (irqFlags1 & (1 << 2)) timeout = true; + if (irqFlags2 & (1 << 3)) packetSent = true; + if (irqFlags2 & (1 << 2)) payloadReady = true; +} + +void rfmTimeout(void) { + timeout = true; } void rfmSleep(void) { + _rfmDelay5(); setMode(MODE_SLEEP); } @@ -217,15 +220,14 @@ // get "PayloadReady" on DIO0 regWrite(DIO_MAP1, regRead(DIO_MAP1) & ~0xc0); + payloadReady = false; setMode(MODE_RX); } PayloadFlags rfmPayloadReady(void) { PayloadFlags flags = {.ready = false, .rssi = 255, .crc = false}; - if (irqFlags2 & (1 << 2)) { - clearIrqFlags(); - + if (payloadReady) { flags.ready = true; flags.rssi = regRead(RSSI_VALUE); flags.crc = regRead(IRQ_FLAGS2) & (1 << 1); @@ -252,24 +254,21 @@ return len; } -size_t rfmReceivePayload(uint8_t *payload, size_t size, bool timeout) { - rfmStartReceive(timeout); +size_t rfmReceivePayload(uint8_t *payload, size_t size, bool enable) { + rfmStartReceive(enable); - // wait until "PayloadReady" or "Timeout" - do {} while (!(irqFlags2 & (1 << 2)) && !(irqFlags1 & (1 << 2))); - bool ready = irqFlags2 & (1 << 2); - bool timedout = irqFlags1 & (1 << 2); - clearIrqFlags(); + // wait until "PayloadReady" or (forced) "Timeout" + do {} while (!payloadReady && !timeout); - if (ready) { + if (payloadReady) { timeoutEnable(false); } setMode(MODE_STDBY); - if (timedout) { + if (timeout) { // full power as last resort, indicate timeout - regWrite(PA_CONFIG, 0xcf); + regWrite(PA_CONFIG, 0xff); return 0; } @@ -292,12 +291,12 @@ // get "PacketSent" on DIO0 (default) regWrite(DIO_MAP1, regRead(DIO_MAP1) & ~0xc0); + packetSent = false; setMode(MODE_TX); // wait until "PacketSent" - do {} while (!(irqFlags2 & (1 << 3))); - clearIrqFlags(); + do {} while (!packetSent); setMode(MODE_STDBY); diff --git a/librfm.h b/librfm.h index 625e333..5d80df9 100644 --- a/librfm.h +++ b/librfm.h @@ -142,6 +142,12 @@ void rfmIrq(void); /** + * Sets the "Timeout" interrupt flag, allowing to "unlock" a possibly hanging + * wait for either "PayloadReady" or "Timeout" by the radio. + */ +void rfmTimeout(void); + +/** * Shuts down the radio. */ void rfmSleep(void);