From d70042cf934950348f962162a33ab61c9a9eee46 Mon Sep 17 00:00:00 2001 From: Maix0 <39835848+Maix0@users.noreply.github.com> Date: Wed, 15 Apr 2026 13:15:55 +0200 Subject: [PATCH] feat(ex02): should be done now too ! --- ex02/.clang-format | 57 ++++++++++++++++++++++++++++++++++++++++++++ ex02/.gitignore | 8 +++++++ ex02/Makefile | 43 +++++++++++++++++++++++++++++++++ ex02/include/mystd.h | 15 ++++++++++++ ex02/include/timer.h | 8 +++++++ ex02/include/uart.h | 13 ++++++++++ ex02/include/utils.h | 8 +++++++ ex02/main.c | 15 ++++++++++++ ex02/timer.c | 38 +++++++++++++++++++++++++++++ ex02/uart.c | 49 +++++++++++++++++++++++++++++++++++++ ex02/utils.c | 19 +++++++++++++++ 11 files changed, 273 insertions(+) create mode 100644 ex02/.clang-format create mode 100644 ex02/.gitignore create mode 100644 ex02/Makefile create mode 100644 ex02/include/mystd.h create mode 100644 ex02/include/timer.h create mode 100644 ex02/include/uart.h create mode 100644 ex02/include/utils.h create mode 100644 ex02/main.c create mode 100644 ex02/timer.c create mode 100644 ex02/uart.c create mode 100644 ex02/utils.c diff --git a/ex02/.clang-format b/ex02/.clang-format new file mode 100644 index 0000000..beb2561 --- /dev/null +++ b/ex02/.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/ex02/.gitignore b/ex02/.gitignore new file mode 100644 index 0000000..3efd3b1 --- /dev/null +++ b/ex02/.gitignore @@ -0,0 +1,8 @@ +*.o +*.hex +*.bin +*.elf +build/ +/avr-libc +compile_commands.json +.cache/ diff --git a/ex02/Makefile b/ex02/Makefile new file mode 100644 index 0000000..5258199 --- /dev/null +++ b/ex02/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 uart.c timer.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 + mkdir -p $(shell dirname $@) + $(CC) $(CFLAGS) -c $< -o $@ + + +$(TARGET).hex: $(OBJ_DIR)/$(TARGET).bin + $(OBJCOPY) -j .text -j .data -O ihex $< $@ diff --git a/ex02/include/mystd.h b/ex02/include/mystd.h new file mode 100644 index 0000000..5d880c5 --- /dev/null +++ b/ex02/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/ex02/include/timer.h b/ex02/include/timer.h new file mode 100644 index 0000000..9d97cd8 --- /dev/null +++ b/ex02/include/timer.h @@ -0,0 +1,8 @@ +#ifndef TIMER_H +#define TIMER_H + +#include "mystd.h" + +void timer1_init(void); + +#endif /* TIMER_H */ diff --git a/ex02/include/uart.h b/ex02/include/uart.h new file mode 100644 index 0000000..24c1081 --- /dev/null +++ b/ex02/include/uart.h @@ -0,0 +1,13 @@ +#ifndef UART_H +#define UART_H + +#include "mystd.h" + +void uart_init(void); + +void uart_tx(char data); +void uart_sendstring(const char* str); + +char uart_rx(void); + +#endif /* UART_H */ diff --git a/ex02/include/utils.h b/ex02/include/utils.h new file mode 100644 index 0000000..a9ab05f --- /dev/null +++ b/ex02/include/utils.h @@ -0,0 +1,8 @@ +#ifndef UTILS_H +#define UTILS_H + +#include "mystd.h" + +void delay_ms(uint16_t count); + +#endif /* UTILS_H */ diff --git a/ex02/main.c b/ex02/main.c new file mode 100644 index 0000000..21d7d42 --- /dev/null +++ b/ex02/main.c @@ -0,0 +1,15 @@ +#include + +#include "mystd.h" +#include "timer.h" +#include "uart.h" +#include "utils.h" + +int main(void) { + uart_init(); + + while (true) { + char data = uart_rx(); + uart_tx(data); + } +} diff --git a/ex02/timer.c b/ex02/timer.c new file mode 100644 index 0000000..bc35f18 --- /dev/null +++ b/ex02/timer.c @@ -0,0 +1,38 @@ + +#include +#include +#include "mystd.h" +#include "utils.h" + +#define PRESCALER 256 +#define TIMER_FREQ (F_CPU / PRESCALER) + +// at a high level: +// Set the OC1B (PB2) pin as output +// set the TIMER1 mode to COMPARE (CTC) +// say to compare against OC1A +// set the value to be compated at X count +// say the presacler for the timer is 512 +// +// all these information are on page ~140 +void timer1_init(void) { + // Set PB1 (OC1A) as output + DDRB |= _BV(PB1); + + // CTC mode (WGM12 = 1) + TCCR1B |= _BV(WGM12); + + // Toggle OC1B on compare match (COM1B0 = 1) + // TCCR1A |= _BV(COM1A0); + + // Set compare values + OCR1A = TIMER_FREQ / 2; + + // Start timer with prescaler 256 (CS12) + TCCR1B |= _BV(CS12); + + // set OCR1A interrupt + TIMSK1 = _BV(1); + + sei(); +} diff --git a/ex02/uart.c b/ex02/uart.c new file mode 100644 index 0000000..ecf9315 --- /dev/null +++ b/ex02/uart.c @@ -0,0 +1,49 @@ +#include "uart.h" +#include +#include "mystd.h" +#include "utils.h" + +#define BAUD_RATE 115200 + +#define UBRR_VALUE ((F_CPU / (8UL * BAUD_RATE)) - 1) + +// uart is 115200 baud rate, 8 bits per word, no parrity and 1 stop bit +// 115200 8N1 +void uart_init(void) { + // Set baud rate + UBRR0H = (uint8_t)(UBRR_VALUE >> 8); + UBRR0L = (uint8_t)(UBRR_VALUE); + + UCSR0A |= _BV(U2X0); + // Enable transmitter + UCSR0B = _BV(TXEN0) | _BV(RXEN0); + + // Set frame format: 8 data bits, no parity, 1 stop bit + UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); + + // Set TX (PD1) as output + DDRD |= _BV(PD1); +} + +void uart_tx(char data) { + // wait for transmit buffer to be empty + while (!(UCSR0A & _BV(UDRE0))) + ; + // load data into transmit register + UDR0 = data; +} + +char uart_rx(void) { + while (!(UCSR0A & _BV(RXC0))) + ; + return UDR0; +} + +void uart_sendstring(const char* str) { + if (!str) + return; + while (*str) { + uart_tx(*str); + str++; + } +} diff --git a/ex02/utils.c b/ex02/utils.c new file mode 100644 index 0000000..96efa11 --- /dev/null +++ b/ex02/utils.c @@ -0,0 +1,19 @@ +#include "utils.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) / 5000); + ms--; + } +}