diff --git a/.gitignore b/.gitignore index 06776cb..8a66dc6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ *.o *.hex *.bin +/avr-libc +*/compile_commands.json +.cache/ diff --git a/ex01/Makefile b/ex01/Makefile new file mode 100644 index 0000000..d0f7e1f --- /dev/null +++ b/ex01/Makefile @@ -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 diff --git a/ex01/main.c b/ex01/main.c new file mode 100644 index 0000000..c1bbb10 --- /dev/null +++ b/ex01/main.c @@ -0,0 +1,13 @@ +#include + +#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) + ; +}