Skip to content

Commit 6c49993

Browse files
committed
Add atomics example
1 parent 232e310 commit 6c49993

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

examples/atomic/Makefile

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
NAME=atomic-test
2+
MCU=atmega328p
3+
4+
ifeq ($(STD),)
5+
STD=c++20
6+
endif
7+
8+
BUILD_DIR=./build
9+
LIB_DIR=../..
10+
11+
ifeq ($(INC),)
12+
INCLUDES= -I$(LIB_DIR)/include
13+
else
14+
INCLUDES= -I$(INC)
15+
endif
16+
17+
SOURCES=$(LIB_DIR)/examples/atomic/atomic.cpp $(LIB_DIR)/src/atomic_builtins.cc
18+
# VPATH=.:$(LIB_DIR)/examples/atomic
19+
OBJECTS=$(addprefix $(BUILD_DIR)/,$(notdir $(SOURCES:%=%.o)))
20+
21+
WARNFLAGS=-Wall -Wextra -pedantic
22+
23+
ifeq ($(STD),c++20)
24+
WARNFLAGS+=-Wno-volatile
25+
endif
26+
27+
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
28+
LDFLAGS=
29+
30+
TARGET=$(BUILD_DIR)/$(NAME)
31+
32+
all: hex size
33+
34+
hex: $(TARGET).hex
35+
36+
$(TARGET).hex: $(TARGET).elf
37+
avr-objcopy -O ihex -j .data -j .text $(TARGET).elf $(TARGET).hex
38+
39+
$(TARGET).elf: $(OBJECTS)
40+
avr-g++ $(LDFLAGS) -mmcu=$(MCU) $(OBJECTS) -o $(TARGET).elf
41+
42+
$(BUILD_DIR)/%.cpp.o: %.cpp
43+
@mkdir -p $(BUILD_DIR)
44+
avr-g++ -c $(CXXFLAGS) -mmcu=$(MCU) $(INCLUDES) $< -o $@
45+
46+
$(BUILD_DIR)/%.cc.o: %.cc
47+
@mkdir -p $(BUILD_DIR)
48+
avr-g++ -c $(CXXFLAGS) -mmcu=$(MCU) $(INCLUDES) $< -o $@
49+
50+
size: $(TARGET).elf
51+
avr-objdump -Pmem-usage $(TARGET).elf
52+
53+
program: $(TARGET).hex
54+
avrdude -p$(MCU) $(AVRDUDE_FLAGS) -c$(PROGRAMMER) -Uflash:w:$(TARGET).hex:a
55+
56+
clean:
57+
rm -rf $(BUILD_DIR)/*.o
58+
rm -rf $(BUILD_DIR)/*.elf
59+
rm -rf $(BUILD_DIR)/*.hex

examples/atomic/atomic.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (c) 2022, Christopher Kormanyos
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <chrono>
13+
14+
#include <avr/interrupt.h>
15+
#include <avr/io.h>
16+
#include <avr/wdt.h>
17+
18+
static void app_hw_init();
19+
20+
int main()
21+
{
22+
// Initialize the application hardware. This includes WDT, PORTB.5 and TIMER0.
23+
app_hw_init();
24+
25+
for(;;)
26+
{
27+
// Toggle the LED on portb.5.
28+
PINB = (1U << PORTB5);
29+
30+
31+
}
32+
}
33+
34+
static void app_hw_init()
35+
{
36+
// Initialize the application including WDT, PORTB.5 and TIMER0
37+
38+
// We will now disable the watchdog.
39+
// Service the watchdog just to be sure to avoid pending timeout.
40+
wdt_reset();
41+
42+
// Clear WDRF in MCUSR.
43+
MCUSR &= ~(1U << WDRF);
44+
45+
// Write logical one to WDCE and WDE.
46+
// Keep the old prescaler setting to prevent unintentional time-out.
47+
WDTCSR |= (1U << WDCE) | (1U << WDE);
48+
49+
// Turn off the WDT.
50+
WDTCSR = 0x00;
51+
52+
// We will now initialize PORTB.5 to be used as an LED driver port.
53+
// Set PORTB.5 value to low.
54+
PORTB &= ~(1U << PORTB5);
55+
56+
// Set PORTB.5 direction to output.
57+
DDRB |= (1U << DDB5);
58+
59+
// We will now initialize the TIMER0 clock and interrupt.
60+
// Clear the TIMER0 overflow flag.
61+
TIFR0 = static_cast<std::uint8_t>(1U << TOV0);
62+
63+
// Enable the TIMER0 overflow interrupt.
64+
TIMSK0 = static_cast<std::uint8_t>(1U << TOIE0);
65+
66+
// Set the TIMER0 clock source to f_osc/8 = 2MHz and begin counting.
67+
TCCR0B = static_cast<std::uint8_t>(1U << CS01);
68+
69+
// Enable all interrupts.
70+
sei();
71+
}
72+
73+
74+
75+
ISR(TIMER0_OVF_vect)
76+
{
77+
78+
}

0 commit comments

Comments
 (0)