From 6c49993efc580f0d4d76546bc5f49284bd48cdfd Mon Sep 17 00:00:00 2001 From: Niklas Hauser Date: Mon, 20 May 2024 23:00:50 +0200 Subject: [PATCH] Add atomics example --- examples/atomic/Makefile | 59 ++++++++++++++++++++++++++++ examples/atomic/atomic.cpp | 78 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 examples/atomic/Makefile create mode 100644 examples/atomic/atomic.cpp diff --git a/examples/atomic/Makefile b/examples/atomic/Makefile new file mode 100644 index 0000000..e617a78 --- /dev/null +++ b/examples/atomic/Makefile @@ -0,0 +1,59 @@ +NAME=atomic-test +MCU=atmega328p + +ifeq ($(STD),) +STD=c++20 +endif + +BUILD_DIR=./build +LIB_DIR=../.. + +ifeq ($(INC),) +INCLUDES= -I$(LIB_DIR)/include +else +INCLUDES= -I$(INC) +endif + +SOURCES=$(LIB_DIR)/examples/atomic/atomic.cpp $(LIB_DIR)/src/atomic_builtins.cc +# VPATH=.:$(LIB_DIR)/examples/atomic +OBJECTS=$(addprefix $(BUILD_DIR)/,$(notdir $(SOURCES:%=%.o))) + +WARNFLAGS=-Wall -Wextra -pedantic + +ifeq ($(STD),c++20) +WARNFLAGS+=-Wno-volatile +endif + +CXXFLAGS=-std=$(STD) -O2 $(WARNFLAGS) --param=min-pagesize=0 -fno-exceptions -fno-rtti -fno-unwind-tables -fno-threadsafe-statics -Wshadow -Wcast-qual -Wpointer-arith -Wundef +LDFLAGS= + +TARGET=$(BUILD_DIR)/$(NAME) + +all: hex size + +hex: $(TARGET).hex + +$(TARGET).hex: $(TARGET).elf + avr-objcopy -O ihex -j .data -j .text $(TARGET).elf $(TARGET).hex + +$(TARGET).elf: $(OBJECTS) + avr-g++ $(LDFLAGS) -mmcu=$(MCU) $(OBJECTS) -o $(TARGET).elf + +$(BUILD_DIR)/%.cpp.o: %.cpp + @mkdir -p $(BUILD_DIR) + avr-g++ -c $(CXXFLAGS) -mmcu=$(MCU) $(INCLUDES) $< -o $@ + +$(BUILD_DIR)/%.cc.o: %.cc + @mkdir -p $(BUILD_DIR) + avr-g++ -c $(CXXFLAGS) -mmcu=$(MCU) $(INCLUDES) $< -o $@ + +size: $(TARGET).elf + avr-objdump -Pmem-usage $(TARGET).elf + +program: $(TARGET).hex + avrdude -p$(MCU) $(AVRDUDE_FLAGS) -c$(PROGRAMMER) -Uflash:w:$(TARGET).hex:a + +clean: + rm -rf $(BUILD_DIR)/*.o + rm -rf $(BUILD_DIR)/*.elf + rm -rf $(BUILD_DIR)/*.hex diff --git a/examples/atomic/atomic.cpp b/examples/atomic/atomic.cpp new file mode 100644 index 0000000..597355b --- /dev/null +++ b/examples/atomic/atomic.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022, Christopher Kormanyos + * + * This file is part of the modm project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +// ---------------------------------------------------------------------------- + +#include + +#include +#include +#include + +static void app_hw_init(); + +int main() +{ + // Initialize the application hardware. This includes WDT, PORTB.5 and TIMER0. + app_hw_init(); + + for(;;) + { + // Toggle the LED on portb.5. + PINB = (1U << PORTB5); + + + } +} + +static void app_hw_init() +{ + // Initialize the application including WDT, PORTB.5 and TIMER0 + + // We will now disable the watchdog. + // Service the watchdog just to be sure to avoid pending timeout. + wdt_reset(); + + // Clear WDRF in MCUSR. + MCUSR &= ~(1U << WDRF); + + // Write logical one to WDCE and WDE. + // Keep the old prescaler setting to prevent unintentional time-out. + WDTCSR |= (1U << WDCE) | (1U << WDE); + + // Turn off the WDT. + WDTCSR = 0x00; + + // We will now initialize PORTB.5 to be used as an LED driver port. + // Set PORTB.5 value to low. + PORTB &= ~(1U << PORTB5); + + // Set PORTB.5 direction to output. + DDRB |= (1U << DDB5); + + // We will now initialize the TIMER0 clock and interrupt. + // Clear the TIMER0 overflow flag. + TIFR0 = static_cast(1U << TOV0); + + // Enable the TIMER0 overflow interrupt. + TIMSK0 = static_cast(1U << TOIE0); + + // Set the TIMER0 clock source to f_osc/8 = 2MHz and begin counting. + TCCR0B = static_cast(1U << CS01); + + // Enable all interrupts. + sei(); +} + + + +ISR(TIMER0_OVF_vect) +{ + +}