avr/prime-tx/main.c

189 lines
5.4 KiB
C

#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) {
op_mode_t mode = rfm_mode(radio);
interrupt_flags_t flags = rfm_interrupts(radio);
display_clear(display);
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) {
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);
_delay_ms(1000);
rfm_transmit(&radio, (uint8_t [7]){ 1, 2, 3, 5, 7, 11, 13 }, 7);
status(&display, &radio);
while (1) {
_delay_ms(1000);
status(&display, &radio);
interrupt_flags_t flags = rfm_interrupts(&radio);
if (flags.packet_sent) {
rfm_standby(&radio);
}
}
/*
*/
/*
if (!error) {
display_clear(&display);
display_write_message(&display, "Error detected");
} else {
while (1) {
status(&display, &radio);
_delay_ms(1000);
rfm_receive_mode(&radio);
status(&display, &radio);
_delay_ms(1000);
rfm_standby(&radio);
}
}
*/
/*
status(&display, &radio);
_delay_ms(1000);
rfm_set_mode(&display, &radio, (op_mode_t){ .listen_on = true, .mode = standby });
_delay_ms(1000);
rfm_set_mode(&display, &radio, (op_mode_t){ .listen_on = true, .mode = standby });
_delay_ms(1000);
*/
/*
rfm_packet_format(&radio, fixed, 8);
rfm_set_mode(&display, &radio, (op_mode_t){ .listen_on = false, .mode = rx });
_delay_ms(1000);
status(&display, &radio);
*/
/*
while (1) {
status(&display, &radio);
_delay_ms(1000);
uint8_t data;
rfm_receive(rfm, &data, 1);
*/
/*
uint8_t b[8];
char msg[32];
status(&display, &radio);
_delay_ms(1000);
interrupt_flags_t flags = rfm_interrupts(&radio);
if (flags.fifo_not_empty) {
rfm_receive(&radio, b, 8);
display_clear(&display);
for (int i = 0; i < 8; i++) {
sprintf(msg, "%x", b[i]);
}
display_write_message(&display, msg);
_delay_ms(1000);
}
*/
// }
/*
spi_acquire(&rfm.spi);
spi_transfer_byte(&rfm.spi, 0x11);
spi_transfer_byte(&rfm.spi, 0x01);
spi_release(&rfm.spi);
spi_acquire(&rfm.spi);
spi_transfer_byte(&rfm.spi, 0x10);
spi_transfer_byte(&rfm.spi, 1);
spi_transfer_byte(&rfm.spi, 2);
spi_transfer_byte(&rfm.spi, 3);
spi_transfer_byte(&rfm.spi, 5);
spi_transfer_byte(&rfm.spi, 7);
spi_transfer_byte(&rfm.spi, 11);
spi_transfer_byte(&rfm.spi, 13);
spi_release(&rfm.spi);
status = rfm_status(&rfm);
if (status & _BV(7)) {
clear_line(&light);
} else {
set_line(&light);
}
*/
/*
uint8_t count[1] = { 0 };
while(1) {
rfm_send_data(&rfm, count, 1);
count[0]++;
set_line(&light);
_delay_ms(50);
clear_line(&light);
_delay_ms(50);
}
*/
return 0;
}