summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2025-03-08 14:53:48 +0800
committerSadeep Madurange <sadeep@asciimx.com>2025-03-08 14:53:48 +0800
commit8598bb180745eed4f5b9ca8969bd55bb06d4da5e (patch)
treefb7083c19a8811c2217bd7dfa5e760b602b1ee8b
parentc90f6941d7be5205a809ee51c529f35d28db6222 (diff)
downloadavr-nrf24l01-driver-8598bb180745eed4f5b9ca8969bd55bb06d4da5e.tar.gz
Makefile and UART code.
-rw-r--r--Makefile44
-rw-r--r--uart.c33
-rw-r--r--uart.h8
3 files changed, 85 insertions, 0 deletions
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..8762701
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,44 @@
+CC = avr-gcc
+MCU = atmega328p
+TARGET = app
+
+SRC = main.c uart.c
+OBJ = $(SRC:.c=.o)
+
+CFLAGS = -std=gnu99
+CFLAGS += -Os
+CFLAGS += -Wall
+CFLAGS += -mmcu=$(MCU)
+CFLAGS += -DBAUD=115200
+CFLAGS += -DF_CPU=16000000UL
+CFLAGS += -ffunction-sections -fdata-sections
+
+LDFLAGS = -mmcu=$(MCU)
+LDFLAGS += -Wl,--gc-sections
+
+HEX_FLAGS = -O ihex
+HEX_FLAGS += -j .text -j .data
+
+AVRDUDE_FLAGS = -p $(MCU)
+AVRDUDE_FLAGS += -c arduino
+AVRDUDE_FLAGS += -P /dev/cuaU0
+AVRDUDE_FLAGS += -D -U
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+elf: $(OBJ)
+ $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf
+
+hex: elf
+ avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex
+
+upload: hex
+ avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i
+
+.PHONY: clean
+
+clean:
+ rm *.o *.elf *.hex
+
+
diff --git a/uart.c b/uart.c
new file mode 100644
index 0000000..b2a1b59
--- /dev/null
+++ b/uart.c
@@ -0,0 +1,33 @@
+#include <avr/io.h>
+#include <util/setbaud.h>
+
+#include "uart.h"
+
+void uart_init(void)
+{
+ UBRR0H = UBRRH_VALUE;
+ UBRR0L = UBRRL_VALUE;
+#if USE_2X
+ UCSR0A |= (1 << U2X0);
+#else
+ UCSR0A &= ~(1 << U2X0);
+#endif
+ UCSR0B = (1 << TXEN0) | (1 << RXEN0);
+ UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
+}
+
+void uart_write(char c)
+{
+ while (!(UCSR0A & (1 << UDRE0)))
+ ;
+ UDR0 = c;
+}
+
+void uart_write_line(const char *s)
+{
+ for (; *s; s++)
+ uart_write(*s);
+
+ uart_write('\r');
+ uart_write('\n');
+}
diff --git a/uart.h b/uart.h
new file mode 100644
index 0000000..81db74b
--- /dev/null
+++ b/uart.h
@@ -0,0 +1,8 @@
+#ifndef UART_H
+#define UART_H
+
+void uart_init(void);
+void uart_write(char c);
+void uart_write_line(const char *s);
+
+#endif /* UART_H */