avr/radio-rx/main.c

150 lines
4.8 KiB
C
Raw Permalink Normal View History

#include <avr/io.h>
2022-07-30 04:33:46 +00:00
#include <dio.h>
#include <display.h>
#include <rfm.h>
#include <spi.h>
#include <stdio.h>
#include <util/delay.h>
void status(display_t *display, rfm_t *radio, int clock) {
op_mode_t mode = rfm_mode(radio);
interrupt_flags_t flags = rfm_interrupts(radio);
// uint8_t rssi = rfm_rssi(radio);
display_clear(display);
char clock_str[16];
sprintf(clock_str, "%4d ", clock);
display_write_message(display, clock_str);
if (mode.listen_on) display_write_message(display, "Listen ");
if (mode.mode == sleep) display_write_message(display, "Sleep");
if (mode.mode == standby) display_write_message(display, "Standby");
if (mode.mode == fs) display_write_message(display, "FS");
if (mode.mode == tx) display_write_message(display, "TX");
if (mode.mode == rx) display_write_message(display, "RX");
display_set_location(display, 1, 0);
/*
char msg[16];
snprintf(msg, 15, "[-%d] ", rssi / 2);
display_write_message(display, msg);
*/
if (flags.mode_ready) display_write_message(display, "R ");
if (flags.rx_ready) display_write_message(display, "RX ");
if (flags.tx_ready) display_write_message(display, "TX ");
if (flags.timeout) display_write_message(display, "TO ");
if (flags.auto_mode) display_write_message(display, "AM ");
if (flags.sync_addr_match) display_write_message(display, "SM ");
if (flags.fifo_full) display_write_message(display, "F ");
if (flags.fifo_not_empty) display_write_message(display, "NE ");
if (flags.fifo_level) display_write_message(display, "LE ");
if (flags.fifo_overrun) display_write_message(display, "OR ");
if (flags.packet_sent) display_write_message(display, "PS ");
if (flags.payload_ready) display_write_message(display, "PR");
if (flags.crc_ok) display_write_message(display, "CRC");
}
int main(void) {
display_t display = {
.reg = {
2022-07-27 01:12:11 +00:00
.output = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 7 },
.shift_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 6 },
.latch_clock = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 5 },
},
};
rfm_t radio = (rfm_t){
.spi = {
.clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 1 },
.data_out = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 2 },
.data_in = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 3 },
.chip_select = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 4 },
},
.irq = { .ddr = &DDRE, .port = &PORTE, .pin = &PINE, .addr = 6 },
.reset = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 4 },
};
_delay_ms(15);
display_init(&display);
display_clear(&display);
display_write_message(&display, "waking up");
rfm_error_e error;
rfm_init(&radio, (uint8_t [4]){ 0xde, 0xca, 0xfb, 0xad }, 4, &error);
rfm_listen(&radio);
/*
if (!error) {
display_write_message(&display, "error detected");
return 0;
}
*/
/*
uint8_t word[8];
uint8_t sync_length = sync_word(&radio, word);
char msg[16];
snprintf(msg, 15, "sync: %d", sync_length);
display_write_message(&display, msg);
display_set_location(&display, 1, 0);
if (sync_length == 4) {
snprintf(msg, 15, ": %x %x %x %x", word[0], word[1], word[2], word[3]);
} else {
snprintf(msg, 15, "word err");
}
display_write_message(&display, msg);
_delay_ms(5000);
*/
/*
char msg[16];
uint32_t frequency = rfm_frequency(&radio);
snprintf(msg, 15, "F: %ld", frequency);
display_write_message(&display, msg);
_delay_ms(5000);
*/
int clock = 0;
while(1) {
// char msg[16];
// uint8_t packet[66];
// uint8_t length;
// display_clear(&display);
// if (length > 0) {
// snprintf(msg, 15, "%d ", packet[0]);
// display_write_message(&display, msg);
// _delay_ms(1000);
// }
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]);
2022-07-27 01:12:11 +00:00
char msg_2[16];
snprintf(msg_2, 15, "RSSI: %d", rfm_rssi(&radio));
display_clear(&display);
display_write_message(&display, msg);
2022-07-27 01:12:11 +00:00
display_set_location(&display, 1, 0);
display_write_message(&display, msg_2);
_delay_ms(1000);
}
status(&display, &radio, clock);
_delay_ms(1000);
clock++;
}
}