Rewrite my init function, carefully following the RadioHead library

;
This commit is contained in:
Savanni D'Gerinel 2022-07-10 15:57:50 -04:00
parent b8494551a4
commit 7846fa175f
3 changed files with 79 additions and 25 deletions

View File

@ -9,15 +9,13 @@
void status(display_t *display, rfm_t *radio, int clock) { void status(display_t *display, rfm_t *radio, int clock) {
op_mode_t mode = rfm_mode(radio); op_mode_t mode = rfm_mode(radio);
interrupt_flags_t flags = rfm_interrupts(radio); interrupt_flags_t flags = rfm_interrupts(radio);
uint8_t rssi = rfm_rssi(radio); // uint8_t rssi = rfm_rssi(radio);
display_clear(display); display_clear(display);
/*
char clock_str[16]; char clock_str[16];
sprintf(clock_str, "%4d ", clock); sprintf(clock_str, "%4d ", clock);
display_write_message(display, clock_str); display_write_message(display, clock_str);
*/
if (mode.listen_on) display_write_message(display, "Listen "); if (mode.listen_on) display_write_message(display, "Listen ");
if (mode.mode == sleep) display_write_message(display, "Sleep"); if (mode.mode == sleep) display_write_message(display, "Sleep");
@ -28,9 +26,11 @@ void status(display_t *display, rfm_t *radio, int clock) {
display_set_location(display, 1, 0); display_set_location(display, 1, 0);
/*
char msg[16]; char msg[16];
snprintf(msg, 15, "[-%d] ", rssi / 2); snprintf(msg, 15, "[-%d] ", rssi / 2);
display_write_message(display, msg); display_write_message(display, msg);
*/
if (flags.mode_ready) display_write_message(display, "R "); if (flags.mode_ready) display_write_message(display, "R ");
if (flags.rx_ready) display_write_message(display, "RX "); if (flags.rx_ready) display_write_message(display, "RX ");
@ -70,16 +70,19 @@ int main(void) {
_delay_ms(15); _delay_ms(15);
display_init(&display); display_init(&display);
display_clear(&display); display_clear(&display);
display_write_message(&display, "waking up");
rfm_error_e error; rfm_error_e error;
rfm_init(&radio, (uint8_t [4]){ 0xde, 0xca, 0xfb, 0xad }, 4, &error); rfm_init(&radio, (uint8_t [4]){ 0xde, 0xca, 0xfb, 0xad }, 4, &error);
rfm_listen(&radio); rfm_listen(&radio);
/*
if (!error) { if (!error) {
display_write_message(&display, "error detected"); display_write_message(&display, "error detected");
return 0; return 0;
} }
*/
/* /*
uint8_t word[8]; uint8_t word[8];
@ -99,12 +102,15 @@ int main(void) {
_delay_ms(5000); _delay_ms(5000);
*/ */
/*
char msg[16]; char msg[16];
uint32_t frequency = rfm_frequency(&radio); uint32_t frequency = rfm_frequency(&radio);
snprintf(msg, 15, "F: %ld", frequency); snprintf(msg, 15, "F: %ld", frequency);
display_write_message(&display, msg); display_write_message(&display, msg);
_delay_ms(5000); _delay_ms(5000);
*/
int clock = 0;
while(1) { while(1) {
// char msg[16]; // char msg[16];
// uint8_t packet[66]; // uint8_t packet[66];
@ -116,7 +122,23 @@ int main(void) {
// display_write_message(&display, msg); // display_write_message(&display, msg);
// _delay_ms(1000); // _delay_ms(1000);
// } // }
status(&display, &radio, 0); interrupt_flags_t flags = rfm_interrupts(&radio);
if (flags.fifo_not_empty) {
uint8_t packet[66];
uint8_t length;
rfm_receive(&radio, packet, &length);
char msg[16];
snprintf(msg, 15, "[%d]: %d", length, packet[0]);
display_clear(&display);
display_write_message(&display, msg);
_delay_ms(1000); _delay_ms(1000);
} }
status(&display, &radio, clock);
_delay_ms(1000);
clock++;
}
} }

View File

@ -17,19 +17,29 @@ You should have received a copy of the GNU General Public License along with Lum
#define REG_FIFO 0x00 #define REG_FIFO 0x00
#define REG_OP_MODE 0x01 #define REG_OP_MODE 0x01
#define REG_DATA_MODUL 0x02 #define REG_DATA_MODUL 0x02
#define REG_BITRATE_MSB 0x03
#define REG_BITRATE_LSB 0x04
#define REG_FDEV_MSB 0x05
#define REG_FDEV_LSB 0x06
#define REG_VERSION 0x10 #define REG_VERSION 0x10
#define REG_PA_LEVEL 0x11
#define REG_RXBW 0x19
#define REG_AFCBW 0x1a
#define REG_RSSI_CONFIG 0x23 #define REG_RSSI_CONFIG 0x23
#define REG_RSSI_VALUE 0x24 #define REG_RSSI_VALUE 0x24
#define REG_DIO_MAPPING1 0x25 #define REG_DIO_MAPPING1 0x25
#define REG_IRQ_FLAGS1 0x27 #define REG_IRQ_FLAGS1 0x27
#define REG_IRQ_FLAGS2 0x28 #define REG_IRQ_FLAGS2 0x28
#define REG_RSSI_THRESH 0x29 #define REG_RSSI_THRESH 0x29
#define REG_PREAMBLE_MSB 0x2c
#define REG_PREAMBLE_LSB 0x2d
#define REG_SYNC_CONFIG 0x2e #define REG_SYNC_CONFIG 0x2e
#define REG_SYNC_VALUE1 0x2f #define REG_SYNC_VALUE1 0x2f
#define REG_PACKET_CONFIG1 0x37 #define REG_PACKET_CONFIG1 0x37
#define REG_FIFO_THRESH 0x3c #define REG_FIFO_THRESH 0x3c
#define REG_TEST_PA1 0x5a #define REG_TEST_PA1 0x5a
#define REG_TEST_PA2 0x5c #define REG_TEST_PA2 0x5c
#define REG_TEST_DAGC 0x6f
#define PA1_LOW_POWER 0x55 #define PA1_LOW_POWER 0x55
#define PA1_HIGH_POWER 0x5D #define PA1_HIGH_POWER 0x5D
@ -81,27 +91,56 @@ void rfm_init(rfm_t *rfm, uint8_t *sync_word, size_t length, rfm_error_e *error)
return; return;
} }
rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = standby }); // RadioHead packet format
// 4 bytes preamble (done automatically)
_rfm_write(rfm, REG_DATA_MODUL, (uint8_t [1]){ _BV(3) }, 1); // 2 Sync words 2d, d4 (configured here, but done automatically)
// 2 CRC CCITT octets computed on the header, length, and data (wat?)
// 0 to 60 bytes of data
// RSSI Threshhold -114dbm
// No RFM69HCW adress filtering. Payload has custom headers.
_rfm_write(rfm, REG_FIFO_THRESH, (uint8_t [1]){ 0x8f }, 1); _rfm_write(rfm, REG_FIFO_THRESH, (uint8_t [1]){ 0x8f }, 1);
_rfm_write(rfm, REG_RSSI_THRESH, (uint8_t [1]){ 0xe4 }, 1); _rfm_write(rfm, REG_RSSI_THRESH, (uint8_t [1]){ 0xe4 }, 1);
// Fading margin improvement (3.4.4). Improved Margin, LowBeta off
_rfm_write(rfm, REG_TEST_DAGC, (uint8_t [1]){ 0x30 }, 1);
_rfm_set_low_power(rfm); _rfm_set_low_power(rfm);
_rfm_write(rfm, REG_PACKET_CONFIG1, uint8_t syncconfig;
(uint8_t [1]){ _rfm_read(rfm, REG_SYNC_CONFIG, &syncconfig, 1);
_BV(7) /* variable length packets */
| _BV(6) /* DC whitening */
| _BV(4) /* CRC on */
| _BV(0) /* packet filtering does not require node address */
}, 1);
_rfm_write(rfm, REG_SYNC_CONFIG, (uint8_t [1]){ _BV(7) | (length << 2) }, 1);
uint8_t word_base = REG_SYNC_VALUE1; uint8_t word_base = REG_SYNC_VALUE1;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
_rfm_write(rfm, word_base + i, &sync_word[i], 1); _rfm_write(rfm, word_base + i, &sync_word[i], 1);
} }
syncconfig &= ~0b00111000;
syncconfig |= (length - 1) << 3;
_rfm_write(rfm, REG_SYNC_CONFIG, &syncconfig, 1);
_rfm_write(rfm, REG_PREAMBLE_MSB, (uint8_t [1]){ 0 }, 1);
_rfm_write(rfm, REG_PREAMBLE_LSB, (uint8_t [1]){ 4 }, 1);
// Datamodul: CONFIG_FSK (Packet mode, FSK, no shaping: 0b00000000
// BitRateMSB: 0x00
// BitRateLSB: 0x80 (oscillator 32,000,000 / bitrate 250kbps = 128 (0x80))
// FDEVMSB: 0x10
// FDEVLSB: 0x00
// RXBW: 0xe0
// AFCBW: 0xe0
// PacketConfig1: CONFIG_WHITE (variable packet format, whitening on, crc on, no address filtering)
_rfm_write(rfm, REG_DATA_MODUL, (uint8_t [1]){ 0 }, 1);
_rfm_write(rfm, REG_BITRATE_MSB, (uint8_t [1]){ 0 }, 1);
_rfm_write(rfm, REG_BITRATE_LSB, (uint8_t [1]){ 0x80 }, 1);
_rfm_write(rfm, REG_FDEV_MSB, (uint8_t [1]){ 0x10 }, 1);
_rfm_write(rfm, REG_FDEV_LSB, (uint8_t [1]){ 0x00 }, 1);
_rfm_write(rfm, REG_RXBW, (uint8_t [1]){ 0xe0 }, 1);
_rfm_write(rfm, REG_AFCBW, (uint8_t [1]){ 0xe0 }, 1);
_rfm_write(rfm, REG_PACKET_CONFIG1, (uint8_t [1]){ 0b10110000 }, 1);
// set power level 13dbm
// This is super complicated. Dig through the RadioHead library and the documentation and make things better.
_rfm_write(rfm, REG_PA_LEVEL, (uint8_t [1]){ 0b11000000 | 31 }, 1);
} }
uint8_t sync_word(rfm_t *rfm, uint8_t sync_word[8]) { uint8_t sync_word(rfm_t *rfm, uint8_t sync_word[8]) {
@ -167,18 +206,11 @@ void rfm_receive_mode(rfm_t *rfm) {
rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = rx }); rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = rx });
} }
void rfm_receive(display_t *display, rfm_t *rfm, uint8_t data[66], uint8_t *length) { void rfm_receive(rfm_t *rfm, uint8_t data[66], uint8_t *length) {
display_clear(display);
_rfm_write(rfm, REG_DIO_MAPPING1, (uint8_t [1]){ _BV(6) }, 1); _rfm_write(rfm, REG_DIO_MAPPING1, (uint8_t [1]){ _BV(6) }, 1);
rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = rx }); rfm_set_mode(rfm, (op_mode_t){ .listen_on = false, .mode = rx });
_rfm_read(rfm, REG_FIFO, length, 1); _rfm_read(rfm, REG_FIFO, length, 1);
char msg[16];
sprintf(msg, "length: %d", *length);
display_write_message(display, msg);
_delay_ms(1000);
if (*length > 0) { if (*length > 0) {
_rfm_read(rfm, 0x00, data, *length); _rfm_read(rfm, 0x00, data, *length);
} }

View File

@ -77,7 +77,7 @@ void rfm_sleep(rfm_t *);
void rfm_standby(rfm_t *); void rfm_standby(rfm_t *);
void rfm_receive_mode(rfm_t *); void rfm_receive_mode(rfm_t *);
void rfm_transmit(rfm_t *rfm, uint8_t *data, uint8_t length); void rfm_transmit(rfm_t *rfm, uint8_t *data, uint8_t length);
void rfm_receive(display_t *display, rfm_t *rfm, uint8_t data[66], uint8_t *length); void rfm_receive(rfm_t *rfm, uint8_t data[66], uint8_t *length);
op_mode_t rfm_mode(rfm_t *rfm); op_mode_t rfm_mode(rfm_t *rfm);
void rfm_set_mode(rfm_t *rfm, op_mode_t mode); void rfm_set_mode(rfm_t *rfm, op_mode_t mode);