feat(ex01): full ex01

This will also compile the avr-libc from source into /tmp/avr-libc
This commit is contained in:
Maix0 2026-04-13 12:40:12 +02:00
parent 39e535df0e
commit 90c358409a
3 changed files with 69 additions and 0 deletions

53
ex01/Makefile Normal file
View file

@ -0,0 +1,53 @@
AVR_LIBC_DIR=/tmp/avr-libc
# Makefile
MCU=atmega328p
F_CPU=16000000
CC=avr-gcc
OBJCOPY=avr-objcopy
CFLAGS=-nostdlib -std=c99 -Wall -Wextra -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -I. -I$(AVR_LIBC_DIR)/include
TARGET=main
SERIAL=-P /dev/ttyUSB0 -b 115200
SRC_DIR=.
OBJ_DIR=build
SRC_FILES=main.c
OBJ_FILES=$(patsubst %.c,%.o,$(SRC_FILES))
SRC=$(addprefix $(SRC_DIR)/,$(SRC_FILES))
OBJ=$(addprefix $(OBJ_DIR)/,$(OBJ_FILES))
all: flash
re: fclean all
fclean: clean
clean:
rm -rf $(OBJ_DIR)
rm -f $(TARGET).hex
hex: $(TARGET).hex
flash: hex
avrdude -p $(MCU) -c arduino -U flash:w:$(TARGET).hex:i $(SERIAL)
$(OBJ_DIR)/$(TARGET).bin: $(OBJ)
$(CC) $(CFLAGS) $(OBJ) -o $@
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(AVR_LIBC_DIR)/.CACHETAG
mkdir -p $(shell dirname $@)
$(CC) $(CFLAGS) -c $< -o $@
$(TARGET).hex: $(OBJ_DIR)/$(TARGET).bin
$(OBJCOPY) -j .text -j .data -O ihex $< $@
$(AVR_LIBC_DIR)/.CACHETAG:
rm -rf $(AVR_LIBC_DIR)
git clone --depth=1 https://github.com/avrdudes/avr-libc/ $(AVR_LIBC_DIR)
cd $(AVR_LIBC_DIR) && ./bootstrap
cd $(AVR_LIBC_DIR) && ./configure --build=`./config.guess` --host=avr
cd $(AVR_LIBC_DIR) && make -j
touch $(AVR_LIBC_DIR)/.CACHETAG

13
ex01/main.c Normal file
View file

@ -0,0 +1,13 @@
#include <avr/io.h>
#define LED_PORT PORTB0
int main(void) {
// set pin to output
DDRB |= (_BV(LED_PORT));
// say to pull high the pin
PORTB |= (_BV(LED_PORT));
while (1)
;
}