Skip to content

Commit 80f3d39

Browse files
committed
Add atomics example
1 parent d0558ae commit 80f3d39

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed

examples/atomic/Makefile

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
NAME=vector-test
2+
MCU=atmega328p
3+
F_CPU=16000000ul
4+
5+
PROGRAMMER=arduino
6+
AVRDUDE_FLAGS= -P/dev/ttyUSB0 -b57600
7+
8+
ifeq ($(STD),)
9+
STD=c++20
10+
endif
11+
12+
BUILD_DIR=./build
13+
LIB_DIR=../..
14+
COMMON_DIR=../common
15+
16+
INCLUDES=-I$(COMMON_DIR) -I$(LIB_DIR)/include
17+
18+
SOURCES=$(wildcard *.cpp $(LIB_DIR)/src/*.cc)
19+
VPATH=.:$(LIB_DIR)/src:$(COMMON_DIR)
20+
OBJECTS=$(addprefix $(BUILD_DIR)/,$(notdir $(SOURCES:%=%.o)))
21+
22+
CXXFLAGS=-std=$(STD) -Os -Wno-volatile --param=min-pagesize=0 -Wall -Wextra -pedantic -fno-exceptions -fno-rtti -fno-unwind-tables -fno-threadsafe-statics -Wshadow -Wcast-qual -Wpointer-arith -Wundef -DF_CPU=$(F_CPU)
23+
LDFLAGS=
24+
25+
TARGET=$(BUILD_DIR)/$(NAME)
26+
27+
all: hex size
28+
29+
hex: $(TARGET).hex
30+
31+
$(TARGET).hex: $(TARGET).elf
32+
avr-objcopy -O ihex -j .data -j .text $(TARGET).elf $(TARGET).hex
33+
34+
$(TARGET).elf: $(OBJECTS)
35+
avr-g++ $(LDFLAGS) -mmcu=$(MCU) $(OBJECTS) -o $(TARGET).elf
36+
37+
$(BUILD_DIR)/%.cpp.o: %.cpp
38+
@mkdir -p $(BUILD_DIR)
39+
avr-g++ -c $(CXXFLAGS) -mmcu=$(MCU) $(INCLUDES) $< -o $@
40+
41+
$(BUILD_DIR)/%.cc.o: %.cc
42+
@mkdir -p $(BUILD_DIR)
43+
avr-g++ -c $(CXXFLAGS) -mmcu=$(MCU) $(INCLUDES) $< -o $@
44+
45+
size: $(TARGET).elf
46+
avr-objdump -Pmem-usage $(TARGET).elf
47+
48+
program: $(TARGET).hex
49+
avrdude -p$(MCU) $(AVRDUDE_FLAGS) -c$(PROGRAMMER) -Uflash:w:$(TARGET).hex:a
50+
51+
clean:
52+
rm -rf $(BUILD_DIR)/*.o
53+
rm -rf $(BUILD_DIR)/*.elf
54+
rm -rf $(BUILD_DIR)/*.hex

examples/atomic/atomic.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 <atomic>
13+
#include <cstdint>
14+
15+
#include <avr/interrupt.h>
16+
#include <avr/io.h>
17+
#include <avr/wdt.h>
18+
19+
static void app_hw_init();
20+
21+
std::atomic<std::uint16_t> sequence;
22+
23+
int main()
24+
{
25+
// Initialize the application hardware. This includes WDT, PORTB.5 and TIMER0.
26+
app_hw_init();
27+
28+
for(;;)
29+
{
30+
// Toggle the LED on portb.5.
31+
PINB = (1U << PORTB5);
32+
33+
sequence++;
34+
}
35+
}
36+
37+
static void app_hw_init()
38+
{
39+
// Initialize the application including WDT, PORTB.5 and TIMER0
40+
41+
// We will now disable the watchdog.
42+
// Service the watchdog just to be sure to avoid pending timeout.
43+
wdt_reset();
44+
45+
// Clear WDRF in MCUSR.
46+
MCUSR &= ~(1U << WDRF);
47+
48+
// Write logical one to WDCE and WDE.
49+
// Keep the old prescaler setting to prevent unintentional time-out.
50+
WDTCSR |= (1U << WDCE) | (1U << WDE);
51+
52+
// Turn off the WDT.
53+
WDTCSR = 0x00;
54+
55+
// We will now initialize PORTB.5 to be used as an LED driver port.
56+
// Set PORTB.5 value to low.
57+
PORTB &= ~(1U << PORTB5);
58+
59+
// Set PORTB.5 direction to output.
60+
DDRB |= (1U << DDB5);
61+
62+
// We will now initialize the TIMER0 clock and interrupt.
63+
// Clear the TIMER0 overflow flag.
64+
TIFR0 = static_cast<std::uint8_t>(1U << TOV0);
65+
66+
// Enable the TIMER0 overflow interrupt.
67+
TIMSK0 = static_cast<std::uint8_t>(1U << TOIE0);
68+
69+
// Set the TIMER0 clock source to f_osc/8 = 2MHz and begin counting.
70+
TCCR0B = static_cast<std::uint8_t>(1U << CS01);
71+
72+
// Enable all interrupts.
73+
sei();
74+
}
75+
76+
77+
78+
ISR(TIMER0_OVF_vect)
79+
{
80+
sequence++;
81+
}

0 commit comments

Comments
 (0)