avr/rfm_diagnostic/main.c

210 lines
6.2 KiB
C

/*
Copyright 2022, Savanni D'Gerinel <savanni@luminescent-dreams.com>
This file is part of Savanni's AVR library.
Lumeto is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Lumeto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Lumeto. If not, see <https://www.gnu.org/licenses/>.
*/
#include <avr/io.h>
#include <util/delay.h>
#include <base.h>
#include <spi.h>
#include <rfm.h>
#include <display.h>
#include <stdio.h>
void display_current_mode(display_t *display, rfm_t *rfm) {
display_clear(display);
uint8_t mode = rfm_mode(rfm);
char msg[40];
sprintf(msg, "Mode: 0x%02x", mode);
display_write_message(display, msg);
display_set_location(display, 1, 0);
display_write_message(display, ": ");
if (mode & _BV(7)) {
display_write_message(display, "SO ");
}
if (mode & _BV(6)) {
display_write_message(display, "LO ");
}
if (mode & _BV(5)) {
display_write_message(display, "LA ");
}
uint8_t tr_op_mode = (mode & 0b00011100) >> 2;
switch (tr_op_mode) {
case 0: display_write_message(display, "Sleep");
break;
case 1: display_write_message(display, "Standby");
break;
case 2: display_write_message(display, "FS");
break;
case 3: display_write_message(display, "TX");
break;
case 4: display_write_message(display, "RS");
}
}
void display_temperature(display_t *display, rfm_t *rfm) {
display_clear(display);
uint8_t temperature = rfm_temperature(rfm);
display_write_message(display, "Temperature:");
display_set_location(display, 1, 0);
char temp_str[8];
sprintf(temp_str, ": %d", temperature);
display_write_message(display, temp_str);
}
void display_status_flags(display_t *display, rfm_t *rfm) {
display_clear(display);
display_write_message(display, "Status:");
display_set_location(display, 1, 0);
display_write_message(display, ": ");
uint8_t status = rfm_status(rfm);
if (status & _BV(7)) {
display_write_message(display, "FF ");
}
if (status & _BV(6)) {
display_write_message(display, "FNE ");
}
if (status & _BV(5)) {
display_write_message(display, "FL ");
}
if (status & _BV(4)) {
display_write_message(display, "FO ");
}
if (status & _BV(3)) {
display_write_message(display, "PS ");
}
if (status & _BV(2)) {
display_write_message(display, "PR ");
}
if (status & _BV(1)) {
display_write_message(display, "Ok ");
}
}
void display_irq_flags(display_t *display, rfm_t *rfm) {
display_clear(display);
display_write_message(display, "IRQ:");
display_set_location(display, 1, 0);
display_write_message(display, ": ");
uint8_t flags = rfm_irq_1(rfm);
if (flags & _BV(7)) {
display_write_message(display, "MR ");
}
if (flags & _BV(6)) {
display_write_message(display, "RR ");
}
if (flags & _BV(5)) {
display_write_message(display, "TR ");
}
if (flags & _BV(4)) {
display_write_message(display, "PL ");
}
if (flags & _BV(3)) {
display_write_message(display, "RS ");
}
if (flags & _BV(2)) {
display_write_message(display, "TO ");
}
if (flags & _BV(1)) {
display_write_message(display, "AO ");
}
if (flags & _BV(0)) {
display_write_message(display, "SM ");
}
}
void display_frequency(display_t *display, rfm_t *rfm) {
display_clear(display);
display_write_message(display, "Frequency: ");
display_set_location(display, 1, 0);
uint32_t frequency = rfm_frequency(rfm);
char temp_str[32];
sprintf(temp_str, ": %lu", frequency);
display_write_message(display, temp_str);
}
void display_bits(display_t *display, uint8_t bits) {
display_clear(display);
char value[8];
sprintf(value, "0x%02x", bits);
display_write_message(display, value);
display_set_location(display, 1, 0);
for (int i = 7; i >= 0; i--) {
if (bits & _BV(i)) {
display_write_message(display, "1");
} else {
display_write_message(display, "0");
}
}
}
int main(void) {
display_t display = {
.reg = {
.output = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 5 },
.latch_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 6 },
.shift_clock = { .ddr = &DDRB, .port = &PORTB, .pin = &PINB, .addr = 7 },
},
.register_select = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 1 },
.enable = { .ddr = &DDRF, .port = &PORTF, .pin = &PINF, .addr = 0 },
};
display_init(&display);
rfm_t rfm = {
.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 },
};
rfm_initialize(&rfm);
_delay_ms(15);
display_enable(&display);
display_clear(&display);
display_write_message(&display, "Waking up");
_delay_ms(1000);
/*
rfm_set_mode(&rfm, standby);
*/
rfm_listen(&rfm);
while(1) {
display_status_flags(&display, &rfm);
_delay_ms(5000);
display_current_mode(&display, &rfm);
_delay_ms(5000);
display_irq_flags(&display, &rfm);
_delay_ms(5000);
display_frequency(&display, &rfm);
_delay_ms(5000);
}
}