summaryrefslogtreecommitdiffstats
path: root/main.c
blob: 082d9d555b8e5729fdb757c9c3ab0cef49cf779a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdlib.h>
#include <avr/io.h>
#include <util/delay.h>

#include "uart.h"

#define SPI_SS     PB2
#define SPI_SCK    PB5
#define SPI_MISO   PB4
#define SPI_MOSI   PB3
#define SPI_DDR    DDRB
#define SPI_PORT   PORTB

static inline uint8_t read_reg(uint8_t reg)
{
	SPI_PORT &= ~(1 << SPI_SS);

	SPDR = reg & 0x1F;
	while (!(SPSR & (1 << SPIF)))
		;

	SPDR = 0xFF;
	while (!(SPSR & (1 << SPIF)))
		;

	SPI_PORT |= (1 << SPI_SS);
	return SPDR;
}

void radio_init(void)
{
	SPI_DDR |= (1 << SPI_SS) | (1 << SPI_SCK) | (1 << SPI_MOSI);
	SPI_PORT |= (1 << SPI_SS);
	SPCR |= (1 << SPE) | (1 << MSTR);
}

int main(void)
{
	uart_init();
	radio_init();

	for (int i = 0; i < 10; i++) {
		uint8_t val = read_reg(0x00);

		uart_write("CONFIG: ");
		uart_write_char(val);
		uart_write_char('\r');
		uart_write_char('\n');
	}

	return 0;
}