commit b7004a017145a30969f9e0523ee69d3f798bd34d Author: giomba Date: Sat Jun 26 19:15:17 2021 +0200 first commit vertical line at about 1/3 of the horizontal line diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..72e306b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/ +datasheet/ +output/ + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2c4ef34 --- /dev/null +++ b/Makefile @@ -0,0 +1,217 @@ +# Name of the project +PROJ_NAME = video + +## Directories ## +# This is where the source files are located, +# which are not in the current directory +SRC_DIR = ./src + +# The header files we use are located here +INC_DIR = ./inc +INC_DIR += ./src +INC_DIR += . + +BUILD_DIR = build +OUTPUT_DIR = output + +###################################################################### +# SOURCES # +###################################################################### + +## FILES ## +# c files +# SRCS = +SRCS += main.c + +# asm files +# S maiuscola! Invoca prima il compilatore gcc che interpreta macro e altro +# ASRC = main.S +ASRC += main.S +#ASRC += video.S +#ASRC += sleep.S +# ASRC += variables.S +#ASRC += lines/line_white.S +#ASRC += lines/line_black.S +#ASRC += lines/line_mid.S +#ASRC += lines/line_syncs.S +#ASRC += lines/line_calc.S +# ASRC += lines/line_digits.S + +# header files +# Specify here libraries! Makefile will check existance before launching +# DEPS = $(notdir $(wildcard $(INC_DIR)/*.h)) +# DEPS += Makefile +# DEPS += $(ENUMSTRING) + +# Object files +# Automatically declares object file names +OBJS = $(patsubst %.c, $(BUILD_DIR)/%.c.o, $(filter %.c,$(SRCS)) ) +OBJS += $(patsubst %.cpp, $(BUILD_DIR)/%.cpp.o,$(filter %.cpp,$(SRCS)) ) + +# Dependencies from .h/.hpp files, but exclude asm files +DEPS := $(patsubst %.o, %.d, $(OBJS)) + +OBJS += $(patsubst %.s, $(BUILD_DIR)/%.s.o, $(filter %.s,$(ASRC)) ) +OBJS += $(patsubst %.S, $(BUILD_DIR)/%.S.o, $(filter %.S,$(ASRC)) ) + + +# Virtual Paths +# Tell make to look in that folder if it cannot find a source +# in the current directory +vpath %.c $(SRC_DIR) +vpath %.cpp $(SRC_DIR) +vpath %.S $(SRC_DIR) +vpath %.h $(INC_DIR) + +###################################################################### +# SETUP TOOLS # +###################################################################### +ECHO := /bin/echo + +# GCC/programming Tools +CC = avr-gcc +CXX = avr-g++ +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +GDB = avr-gdb +AS = avr-as +SIZE = avr-size +AVRDUDE = avrdude +# Custom script for enum parsing +# PACK2STRING = python3 bin/enum2string.py + +# Microcontroller +MCU = atmega328p +F_CPU = 16000000 +# Fuses -- www.engbedded.com/fusecalc/ +LFUSE = 0xfd +HFUSE = 0xde + +### GCC options ### + +## Compiler flags ## +# Do not run the linker +CFLAGS += -c +# Debug informations +CFLAGS += -g +# Auto optimisation +CFLAGS += -Os +# All warning messages +CFLAGS += -Wall -Wextra +# Puts functions and data into its own section - remove thread-safe things +CFLAGS += -fno-exceptions -fstack-usage -fdump-tree-optimized -ffunction-sections -fdata-sections -fno-threadsafe-statics +# Use smallest size for enums +CFLAGS += -fshort-enums +# Microcontroller +CFLAGS += -mmcu=$(MCU) +# Clock speed +CFLAGS += -DF_CPU=$(F_CPU)L +# Header files +CFLAGS += $(addprefix -I,$(INC_DIR)) +CFLAGS += -flto +# CFLAGS += -DAVR +# Debug Flag +# CFLAGS += -DDEBUG + +## CXX flags are the same as CC ones here! +CXXFLAGS = $(CFLAGS) +CXXFLAGS += -std=c++11 + +# Linker flags +LFLAGS = -mmcu=$(MCU) +LFLAGS += $(addprefix -I,$(INC_DIR)) + +###################################################################### +# PROGRAMMING TOOLS # +###################################################################### +# To match MCU with BOARD, see link +# http://www.nongnu.org/avr-libc/user-manual/using_tools.html +#PROGRAMMER = avrispmkii +PROGRAMMER = usbasp +# verbose +# PROGRAM_FLAGS = -v +# erase +# PROGRAM_FLAGS = -e +# choose programmer +PROGRAM_FLAGS += -c $(PROGRAMMER) +# target cpu +PROGRAM_FLAGS += -p $(MCU) +# USB port and baudrate +# PROGRAM_FLAGS += -P /dev/ttyACM0 + +###################################################################### +# TARGETS # +###################################################################### +.PHONY: clean + +all: $(OUTPUT_DIR)/$(PROJ_NAME).hex + +-include $(DEPS) + +# invokes CC compiler before assemblying +$(BUILD_DIR)/%.S.o : %.S + @echo -e "\033[1;33m[Assembling ]\033[0m AS $<" + @mkdir -p `dirname $@` + $(CC) $(CFLAGS) $< -o $@ + +# pure asm +$(BUILD_DIR)/%.s.o : %.s + @echo -e "\033[1;33m[Assembling ]\033[0m AS $<" + @mkdir -p `dirname $@` + $(CC) $(CFLAGS) $< -o $@ + +# .cxx files +$(BUILD_DIR)/%.cpp.o: %.cpp + @echo -e "\033[1;33m[Compiling ]\033[0m CX $<" + @mkdir -p `dirname $@` + $(CXX) $(CXXFLAGS) $< -o $@ + +# .c files +$(BUILD_DIR)/%.c.o: %.c + @echo -e "\033[1;33m[Compiling ]\033[0m CC $<" + @mkdir -p `dirname $@` + $(CC) $(CFLAGS) $< -o $@ + +.DELETE_ON_ERROR: +$(BUILD_DIR)/%.c.d: %.c + @mkdir -p `dirname $@` + @$(ECHO) -n "$@ " > $@ + @$(CC) -MM -MT '$(@D)/$(> $@ + +.DELETE_ON_ERROR: +$(BUILD_DIR)/%.cpp.d: %.cpp + @mkdir -p `dirname $@` + @$(ECHO) -n "$@ " > $@ + @$(CC) -MM -MT '$(@D)/$(> $@ + +# $(ENUMSTRING): $(STRINGFILES) +# $(PACK2STRING) $^ > $(ENUMSTRING) + +$(OUTPUT_DIR)/$(PROJ_NAME).elf: $(OBJS) + @echo -e "\033[1;34m[Linking ]\033[0m $@" + @mkdir -p ${OUTPUT_DIR} + $(CC) $(LFLAGS) -o $@ $(foreach file, $^, $(file)) -lm + @echo -e "\033[1;35m[Disasm... ]\033[0m $^" + $(OBJDUMP) -h -S $@ > $(OUTPUT_DIR)/$(PROJ_NAME).lss + +$(OUTPUT_DIR)/$(PROJ_NAME).hex: $(OUTPUT_DIR)/$(PROJ_NAME).elf + @echo -e "\033[1;36m[Binary ]\033[0m $^" + $(OBJCOPY) -O ihex -R .eeprom $^ $@ + +size: $(OUTPUT_DIR)/$(PROJ_NAME).elf + $(SIZE) -C --mcu=$(MCU) $(OUTPUT_DIR)/$(PROJ_NAME).elf + +flash: $(OUTPUT_DIR)/$(PROJ_NAME).hex + $(AVRDUDE) $(PROGRAM_FLAGS) -U flash:w:$(OUTPUT_DIR)/$(PROJ_NAME).hex + +fuse: $(OUTPUT_DIR)/$(PROJ_NAME).hex + $(AVRDUDE) $(PROGRAM_FLAGS) -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m + +test: + $(AVRDUDE) $(PROGRAM_FLAGS) + +clean: + @echo -e "\033[1;33m[Cleaning ]\033[0m" + @rm -rf $(BUILD_DIR)/* + @rm -rf $(OUTPUT_DIR)/* + diff --git a/macro.h b/macro.h new file mode 100644 index 0000000..2dc8ee9 --- /dev/null +++ b/macro.h @@ -0,0 +1 @@ +#define IO(x) x - 0x20 diff --git a/main.S b/main.S new file mode 100644 index 0000000..c696261 --- /dev/null +++ b/main.S @@ -0,0 +1,89 @@ +#include +#include "macro.h" + +.data +status: +.byte 0x0 + +.text + +.global main_s +main_s: + ldi r16, 0x30 + sts DDRB, r16 + + ldi r16, (1 << IVCE) ; set vector at address 0x0 + ldi r17, 0 + out IO(MCUCR), r16 + out IO(MCUCR), r17 + + ldi r16, 0x3 ; enable falling edge interrupt 0 + sts EICRA, r16 + + ldi r16, 0x1 ; external interrupt mask enable + sts EIMSK, r16 + + sei + +again: + sbi IO(PORTB), 5 + ldi r16, 0x1 + sts status, r16 + +wait1: + ldi r16, 0xff + ldi r17, 0xff + ldi r18, 0xff + +1: + dec r16 + brne 1b + dec r17 + brne 1b + dec r18 + brne 1b + + cbi IO(PORTB), 5 + ldi r16, 0x0 + sts status, r16 + +wait2: + ldi r16, 0xff + ldi r17, 0xff + ldi r18, 0xff + +1: + dec r16 + brne 1b + dec r17 + brne 1b + dec r18 + brne 1b + + jmp again + +.global int0_handler +int0_handler: + ldi r31, 80 +loop: + dec r31 + brne loop + + lds r31, status + cpi r31, 1 + brne 1f + sbi IO(PORTB), 4 + nop + nop + nop + nop + nop + nop + nop + nop + nop + nop + cbi IO(PORTB), 4 +1: + + ret diff --git a/main.c b/main.c new file mode 100644 index 0000000..31732f8 --- /dev/null +++ b/main.c @@ -0,0 +1,13 @@ +#include +#include + +void main_s(void); +void int0_handler(void); + +ISR(INT0_vect) { + int0_handler(); +} + +int main() { + main_s(); +}