avr/prime-tx/main.c

96 lines
3.5 KiB
C

#include <avr/io.h>
#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);
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);
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) {
int clock = 0;
uint8_t primes[] = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 0 };
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);
rfm_error_e error;
rfm_init(&radio, (uint8_t [4]){ 0xde, 0xca, 0xfb, 0xad }, 4, &error);
status(&display, &radio, clock);
int prime = 0;
while (1) {
status(&display, &radio, clock);
op_mode_t mode = rfm_mode(&radio);
interrupt_flags_t flags = rfm_interrupts(&radio);
if (mode.mode == standby) {
rfm_transmit(&radio, &primes[prime], 1);
if (primes[prime] == 0) {
prime = 0;
} else {
prime++;
}
} else if (mode.mode == tx && flags.packet_sent) {
rfm_standby(&radio);
}
clock++;
_delay_ms(1000);
}
return 0;
}