avr/morse_tx/main.c

140 lines
3.8 KiB
C
Raw Normal View History

#include <avr/io.h>
#include <util/delay.h>
#include <base.h>
#include <spi.h>
2022-06-26 16:45:27 +00:00
#include <rfm.h>
2022-06-26 16:45:27 +00:00
// typedef struct RFM {
// spi_t spi;
// gpio_t irq;
// gpio_t reset;
// } rfm_t;
//
// void rfm_initialize(rfm_t *rfm) {
// spi_initialize(&rfm->spi);
//
// spi_acquire(&rfm->spi);
// spi_transfer_byte(&rfm->spi, 0x37 | _BV(7));
// spi_transfer_byte(&rfm->spi, 0);
// spi_release(&rfm->spi);
//
// spi_acquire(&rfm->spi);
// spi_transfer_byte(&rfm->spi, 0x38 | _BV(7));
// spi_transfer_byte(&rfm->spi, 1);
// spi_release(&rfm->spi);
// }
//
// void rfm_power_off(rfm_t *rfm) {
// spi_acquire(&rfm->spi);
// spi_transfer_byte(&rfm->spi, 0x11);
// spi_transfer_byte(&rfm->spi, 0x00);
// spi_release(&rfm->spi);
// }
//
// void rfm_send_data(rfm_t *rfm, uint8_t *data, size_t length) {
// /* Go into standby mode */
// spi_acquire(&rfm->spi);
// spi_transfer_byte(&rfm->spi, 0x11);
// spi_transfer_byte(&rfm->spi, 0x01);
// spi_release(&rfm->spi);
//
// /* Write data to FIFO */
// spi_acquire(&rfm->spi);
// spi_transfer_byte(&rfm->spi, 0x10);
// for (size_t i = 0; i < length; i++) {
// spi_transfer_byte(&rfm->spi, data[i]);
// }
// spi_release(&rfm->spi);
//
// /* Go into transmit mode */
// spi_acquire(&rfm->spi);
// spi_transfer_byte(&rfm->spi, 0x11);
// spi_transfer_byte(&rfm->spi, 0x03);
// spi_release(&rfm->spi);
//
// /* Return to standby mode */
// spi_acquire(&rfm->spi);
// spi_transfer_byte(&rfm->spi, 0x11);
// spi_transfer_byte(&rfm->spi, 0x01);
// spi_release(&rfm->spi);
// }
2022-06-26 16:45:27 +00:00
void flash(gpio_t *light, size_t count) {
for (size_t i = 0; i < count; i++) {
set_line(light);
_delay_ms(50);
clear_line(light);
_delay_ms(50);
}
}
int main(void) {
spi_t spi = (spi_t){
.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 },
};
rfm_t rfm = (rfm_t){
.spi = spi,
.irq = { .ddr = &DDRE, .port = &PORTE, .pin = &PINE, .addr = 6 },
.reset = { .ddr = &DDRD, .port = &PORTD, .pin = &PIND, .addr = 4 },
};
gpio_t light = { .ddr = &DDRC, .port = &PORTC, .addr = 7 };
2022-06-26 16:45:27 +00:00
gpio_t fifo_full = { .ddr = &DDRB, .port = &PORTB, .addr = 7 };
gpio_t fifo_empty = { .ddr = &DDRB, .port = &PORTB, .addr = 6 };
set_line_direction(&light, LINE_OUT);
set_line_direction(&fifo_full, LINE_OUT);
set_line_direction(&fifo_empty, LINE_OUT);
flash(&light, 3);
rfm_initialize(&rfm);
2022-06-26 16:45:27 +00:00
set_line(&light);
uint8_t status = rfm_status(&rfm);
status & _BV(7) ? set_line(&fifo_full) : clear_line(&fifo_full);
status & _BV(6) ? set_line(&fifo_empty) : clear_line(&fifo_empty);
/*
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);
}
*/
2022-06-26 16:45:27 +00:00
/*
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);
}
2022-06-26 16:45:27 +00:00
*/
return 0;
}