commit 49164a0c183d8bf95498bab056b3489b7a89a197 Author: Maix0 <39835848+Maix0@users.noreply.github.com> Date: Tue Apr 14 11:53:58 2026 +0200 feat(ex00): ex00 now works ! diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..beb2561 --- /dev/null +++ b/.clang-format @@ -0,0 +1,57 @@ +BasedOnStyle: Chromium +AccessModifierOffset: 0 +AlignConsecutiveAssignments: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true + AlignCompound: true + AlignFunctionPointers: true + PadOperators: false +AlignConsecutiveBitFields: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true + AlignCompound: true + AlignFunctionPointers: true + PadOperators: true +AlignConsecutiveDeclarations: + Enabled: true + AcrossComments: true + AlignCompound: true + AlignFunctionPointers: true + PadOperators: true +AlignConsecutiveMacros: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true + AlignCompound: true + AlignFunctionPointers: true + PadOperators: true +AlignConsecutiveShortCaseStatements: + Enabled: true + AcrossEmptyLines: true + AcrossComments: true + AlignCaseArrows: true + AlignCaseColons: true +ColumnLimit: 100 +NamespaceIndentation: All +CommentPragmas: "" +IncludeCategories: + - Regex: ^<.*\.h> + Priority: 1 + SortPriority: 0 + CaseSensitive: false + - Regex: ^<.* + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: .* + Priority: 3 + SortPriority: 0 + CaseSensitive: false +IndentPPDirectives: AfterHash +IndentWidth: 4 +TabWidth: 4 +UseTab: Always +Language: Cpp +IndentAccessModifiers: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3efd3b1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.o +*.hex +*.bin +*.elf +build/ +/avr-libc +compile_commands.json +.cache/ diff --git a/ex00/Makefile b/ex00/Makefile new file mode 100644 index 0000000..793c19a --- /dev/null +++ b/ex00/Makefile @@ -0,0 +1,43 @@ +# Makefile +MCU=atmega328p +F_CPU=16000000 +CC=avr-gcc +OBJCOPY=avr-objcopy +CFLAGS=-std=c99 -Wall -Wextra -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -Iinclude +TARGET=main +SERIAL=-P /dev/ttyUSB0 -b 115200 + + +SRC_DIR=. +OBJ_DIR=build + +SRC_FILES=main.c utils.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).elf: $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + @mkdir -p $(shell dirname $@) + $(CC) $(CFLAGS) -c $< -o $@ + + +$(TARGET).hex: $(OBJ_DIR)/$(TARGET).elf + $(OBJCOPY) -j .text -j .data -O ihex $< $@ diff --git a/ex00/include/mystd.h b/ex00/include/mystd.h new file mode 100644 index 0000000..5d880c5 --- /dev/null +++ b/ex00/include/mystd.h @@ -0,0 +1,15 @@ +#ifndef MYSTDINT_H +#define MYSTDINT_H + +typedef unsigned int uint16_t; +typedef signed int int16_t; + +typedef unsigned char uint8_t; +typedef signed char int8_t; + +typedef uint8_t bool; + +#define true (1) +#define false (0) + +#endif /* MYSTDINT_H */ diff --git a/ex00/include/utils.h b/ex00/include/utils.h new file mode 100644 index 0000000..5565161 --- /dev/null +++ b/ex00/include/utils.h @@ -0,0 +1,8 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "mystd.h" + +void delay_ms(uint16_t ms); + +#endif /* UTILS_H */ diff --git a/ex00/main.c b/ex00/main.c new file mode 100644 index 0000000..65207e8 --- /dev/null +++ b/ex00/main.c @@ -0,0 +1,18 @@ +#include + +#include "mystd.h" +#include "utils.h" + +#define D1 PORTB0 +#define D2 PORTB1 +#define D3 PORTB2 +#define D4 PORTB4 + +int main(void) { + DDRB |= _BV(D1); + + while (true) { + PORTB ^= _BV(D1); + delay_ms(500); + }; +} diff --git a/ex00/utils.c b/ex00/utils.c new file mode 100644 index 0000000..29ae993 --- /dev/null +++ b/ex00/utils.c @@ -0,0 +1,21 @@ +#include "utils.h" +#include "mystd.h" + + +// this just burns cycles. +// the volatile is important, it means that the cpu can't optimize any +// read/writes for the value +static inline void spin_loop(volatile uint16_t counts) { + while (counts) + counts--; +} + +void delay_ms(uint16_t ms) { + while (ms) { + // this value was taken using a delay of 500ms, and just recording the led + // blinking. it seems to be high enough such that each loop of delay_loop + // takes 1ms :D + spin_loop((F_CPU) / 6000); + ms--; + } +}