diff --git a/avrrfm.c b/avrrfm.c index bd90c6b..ad8216f 100644 --- a/avrrfm.c +++ b/avrrfm.c @@ -256,9 +256,8 @@ * Waits for a response from the receiver with timeout. */ static void waitResponse(void) { - timeoutEnable(true); uint8_t response[1]; - int8_t len = receivePayload(response, sizeof (response)); + int8_t len = receivePayload(response, sizeof (response), true); if (len > 0) { // receiver RSSI int8_t rssi = divRoundNearest(response[0], 2); diff --git a/rfm69.c b/rfm69.c index 2481426..cbf0757 100644 --- a/rfm69.c +++ b/rfm69.c @@ -11,8 +11,8 @@ static volatile uint8_t irqFlags1 = 0; static volatile uint8_t irqFlags2 = 0; -static volatile bool toena = false; -static volatile uint8_t toints = 0; +static volatile bool timeoutEnabled = false; +static volatile uint8_t timeoutInts = 0; /** * Selects the radio to talk to via SPI. @@ -72,6 +72,23 @@ } /** + * Enables timeout (sets timeout interrupt flag on expiration). + * + * @param enable + */ +static void timeoutEnable(bool enable) { + timeoutEnabled = enable; + if (!enable) timeoutInts = 0; +} + +/** + * Times out the current operation. + */ +static void timeout(void) { + irqFlags1 |= (1 << 2); +} + +/** * Reads interrupt flags when a radio interrupt occurs. */ ISR(INT0_vect) { @@ -201,21 +218,12 @@ } void timeRadio(void) { - if (toena && toints++ >= TIMEOUT_INTS) { + if (timeoutEnabled && timeoutInts++ >= TIMEOUT_INTS) { timeoutEnable(false); - timeoutRadio(); + timeout(); } } -void timeoutEnable(bool enable) { - toena = enable; - if (!enable) toints = 0; -} - -void timeoutRadio(void) { - irqFlags1 |= (1 << 2); -} - void sleepRadio(void) { setMode(MODE_SLEEP); } @@ -267,19 +275,20 @@ return len; } -size_t receivePayload(uint8_t *payload, size_t size) { +size_t receivePayload(uint8_t *payload, size_t size, bool timeout) { + timeoutEnable(timeout); startReceive(); // wait until "PayloadReady" or timeout do {} while (!(irqFlags2 & (1 << 2)) && !(irqFlags1 & (1 << 2))); bool ready = irqFlags2 & (1 << 2); - bool timeout = irqFlags1 & (1 << 2); + bool timedout = irqFlags1 & (1 << 2); clearIrqFlags(); if (ready) timeoutEnable(false); setMode(MODE_STDBY); - if (timeout) { + if (timedout) { regWrite(PA_LEVEL, 0x5f); return 0; diff --git a/rfm69.h b/rfm69.h index c54f2ef..113953e 100644 --- a/rfm69.h +++ b/rfm69.h @@ -108,18 +108,6 @@ void timeRadio(void); /** - * Enables or disables the radio timeout. - * - * @param enable - */ -void timeoutEnable(bool enable); - -/** - * Indicates a timeout. - */ -void timeoutRadio(void); - -/** * Shuts down the radio. */ void sleepRadio(void); @@ -166,9 +154,10 @@ * * @param payload buffer for payload * @param size of payload buffer + * @param timeout enable timeout * @return payload bytes actually received */ -size_t receivePayload(uint8_t *payload, size_t size); +size_t receivePayload(uint8_t *payload, size_t size, bool timeout); /** * Transmits up to 64 bytes of the given payload with the given node address.