diff --git a/avrrfm.c b/avrrfm.c index bb9b95d..5555801 100644 --- a/avrrfm.c +++ b/avrrfm.c @@ -36,6 +36,10 @@ #define MEASURE_INTS 4 #define LABEL_OFFSET 10 +#define BLACK 0x0000 +#define RED 0xf800 +#define WHITE 0xffff + #ifndef RECEIVER #define RECEIVER 1 #endif @@ -154,26 +158,26 @@ * * @param raw temperature */ -static void displayTemp(uint8_t rssi, uint16_t raw) { +static void displayTemp(uint8_t rssi, bool crc, uint16_t raw) { uint8_t _rssi = divRoundNearest(rssi, 2); int16_t tempx10 = convertTSens(raw); div_t temp = div(tempx10, 10); - static char buf[32]; + static char buf[42]; - snprintf(buf, sizeof (buf), "RSSI: -%d dBm, %d.%d°C\r\n", - _rssi, temp.quot, abs(temp.rem)); + snprintf(buf, sizeof (buf), "RSSI: -%d dBm, CRC: %d, %d.%d°C\r\n", + _rssi, crc, temp.quot, abs(temp.rem)); printString(buf); - snprintf(buf, sizeof (buf), "RSSI: -%d dBm", _rssi); + snprintf(buf, sizeof (buf), "RSSI: -%d dBm, CRC: %d", _rssi, crc); const __flash Font *unifont = &unifontFont; - writeString(0, 0, unifont, buf, 0xffff, 0x0000); + writeString(0, 0, unifont, buf, WHITE, BLACK); snprintf(buf, sizeof (buf), "%4d.%d°", temp.quot, abs(temp.rem)); const __flash Font *dejaVu = &dejaVuFont; - if (width > 0) fillArea(xo, yo, width, dejaVu->height, 0xffff); + if (width > 0) fillArea(xo, yo, width, dejaVu->height, WHITE); if (yl == 0) yl = unifont->height; - width = writeString(xl, yl, dejaVu, buf, 0xffff, 0x0000); + width = writeString(xl, yl, dejaVu, buf, WHITE, crc ? BLACK : RED); xo = xl; yo = yl; xl += LABEL_OFFSET; @@ -183,22 +187,6 @@ } /** - * Blocks until the raw temperature is received, then returns it. - * - * @return raw temp - */ -static uint16_t receiveTemp(void) { - // printString("Receiving... "); - uint8_t payload[2]; - receivePayload(payload, sizeof (payload)); - uint16_t raw = 0; - raw |= payload[0] << 8; - raw |= payload[1]; - - return raw; -} - -/** * Reads and returns the raw temperature. * * @return raw temp @@ -242,7 +230,7 @@ while (true) { // do something else besides tx/rx // printString("Running...\r\n"); - // _delay_ms(10); + // _delay_ms(1000); if (!RECEIVER) { if (ints % MEASURE_INTS == 0) { @@ -257,10 +245,11 @@ disableSPI(); } } else { - if (payloadReady()) { + PayloadFlags flags = payloadReady(); + if (flags.ready) { uint8_t rssi = readRssi(); uint16_t raw = readTemp(); - displayTemp(rssi, raw); + displayTemp(rssi, flags.crc, raw); startReceive(); } } diff --git a/rfm69.c b/rfm69.c index e3cbbb7..43a1605 100644 --- a/rfm69.c +++ b/rfm69.c @@ -6,6 +6,7 @@ */ #include "rfm69.h" +#include "types.h" static volatile uint8_t irqFlags1 = 0; static volatile uint8_t irqFlags2 = 0; @@ -93,9 +94,9 @@ // regWrite(BITRATE_MSB, 0x0d); // regWrite(BITRATE_LSB, 0x05); - // frequency deviation 100 kHz (default 5 kHz) - regWrite(FDEV_MSB, 0x17); - regWrite(FDEV_LSB, 0xd4); + // frequency deviation 5 kHz (default) + // regWrite(FDEV_MSB, 0x00); + // regWrite(FDEV_LSB, 0x52); // RC calibration, automatically done at device power-up // regWrite(OSC1, 0x80); @@ -123,10 +124,13 @@ // RX_BW during AFC (default) regWrite(AFC_BW, 0x8b); + + // AFC auto on + // regWrite(AFC_FEI, 0x04); // RSSI threshold (default, POR 0xff) regWrite(RSSI_THRESH, 0xe4); - + // Preamble size regWrite(PREAMB_MSB, 0x00); regWrite(PREAMB_LSB, 0x0f); @@ -141,8 +145,8 @@ regWrite(FRF_LSB, freq >> 0); // enable sync word generation and detection, FIFO fill on sync address, - // size of sync word 3 - regWrite(SYNC_CONF, 0x98); + // 4 bytes sync word, tolerate 3 bit errors + regWrite(SYNC_CONF, 0x9b); // just set all sync word values to some really creative value regWrite(SYNC_VAL1, 0x2f); @@ -157,7 +161,12 @@ // variable payload length, crc on, no address matching // regWrite(PCK_CFG1, 0x90); // match broadcast or node address - regWrite(PCK_CFG1, 0x94); + // regWrite(PCK_CFG1, 0x94); + // + CrcAutoClearOff + regWrite(PCK_CFG1, 0x9c); + + // disable automatic RX restart + regWrite(PCK_CFG2, 0x00); // node and broadcast address regWrite(NODE_ADDR, NODE_ADDRESS); @@ -193,15 +202,16 @@ return regRead(RSSI_VALUE); } -bool payloadReady(void) { +PayloadFlags payloadReady(void) { + PayloadFlags flags = {.ready = false, .crc = false}; if (irqFlags2 & (1 << 2)) { + flags.ready = true; + flags.crc = irqFlags2 & (1 << 1); clearIrqFlags(); setMode(MODE_STDBY); - - return true; } - return false; + return flags; } size_t readPayload(uint8_t *payload, size_t size) { diff --git a/rfm69.h b/rfm69.h index 18b578a..21f3bb4 100644 --- a/rfm69.h +++ b/rfm69.h @@ -13,6 +13,7 @@ #include "rfm69.h" #include "pins.h" +#include "types.h" #include "spi.h" #include "usart.h" @@ -30,6 +31,7 @@ #define PA_LEVEL 0x11 #define LNA 0x18 #define RX_BW 0x19 +#define AFC_FEI 0x1e #define AFC_BW 0x20 #define RSSI_CONFIG 0x23 #define RSSI_VALUE 0x24 @@ -37,6 +39,8 @@ #define DIO_MAP2 0x26 #define IRQ_FLAGS1 0x27 #define RSSI_THRESH 0x29 +#define RX_TIMEOUT1 0x2a +#define RX_TIMEOUT2 0x2b #define PREAMB_MSB 0x2c #define PREAMB_LSB 0x2d #define IRQ_FLAGS2 0x28 @@ -54,6 +58,7 @@ #define CAST_ADDR 0x3a #define AUTO_MODES 0x3b #define FIFO_THRESH 0x3c +#define PCK_CFG2 0x3d #define TEST_LNA 0x58 #define TEST_PA1 0x5a #define TEST_PA2 0x5c @@ -105,7 +110,7 @@ * * @return true if "PayloadReady" */ -bool payloadReady(void); +PayloadFlags payloadReady(void); /** * Sets the radio in standby mode, puts the payload into the given array diff --git a/types.h b/types.h index af5ebfd..c21419f 100644 --- a/types.h +++ b/types.h @@ -38,6 +38,14 @@ } Point; /** + * Flags for "payload ready" event. + */ +typedef struct { + bool ready; + bool crc; +} PayloadFlags; + +/** * Pointer to a function that takes an array of bytes * and returns a boolean. */