summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-03-08 15:26:31 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-03-08 15:26:31 +0800
commita807f4849155262efb243c574dd5d77f35755ebc (patch)
tree3813576fdcad1b78191edd498d9c331c9a52df62
parentd30783770f2230ffa3ce9b3b1d652d4dda28e166 (diff)
downloadavr-nrf24l01-driver-a807f4849155262efb243c574dd5d77f35755ebc.tar.gz
wip: spi interface.
-rw-r--r--main.c42
-rw-r--r--uart.c14
-rw-r--r--uart.h3
3 files changed, 53 insertions, 6 deletions
diff --git a/main.c b/main.c
index f63f5ee..082d9d5 100644
--- a/main.c
+++ b/main.c
@@ -1,10 +1,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;
}
diff --git a/uart.c b/uart.c
index b2a1b59..d3012af 100644
--- a/uart.c
+++ b/uart.c
@@ -16,18 +16,22 @@ void uart_init(void)
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
}
-void uart_write(char c)
+void uart_write_char(char c)
{
while (!(UCSR0A & (1 << UDRE0)))
;
UDR0 = c;
}
-void uart_write_line(const char *s)
+void uart_write(const char *s)
{
for (; *s; s++)
- uart_write(*s);
+ uart_write_char(*s);
+}
- uart_write('\r');
- uart_write('\n');
+void uart_write_line(const char *s)
+{
+ uart_write(s);
+ uart_write_char('\r');
+ uart_write_char('\n');
}
diff --git a/uart.h b/uart.h
index 81db74b..854cd14 100644
--- a/uart.h
+++ b/uart.h
@@ -2,7 +2,8 @@
#define UART_H
void uart_init(void);
-void uart_write(char c);
+void uart_write_char(char c);
+void uart_write(const char *s);
void uart_write_line(const char *s);
#endif /* UART_H */