avr/radio-rx/main.c

123 lines
4.1 KiB
C
Raw Normal View History

#include <avr/io.h>
#include <base.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 = {
.output = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 7 },
.shift_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
.latch_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .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);
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);
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);
// }
status(&display, &radio, 0);
_delay_ms(1000);
}
}