From 64aca0c2fa31f2c89f6a999bea440f2e8f7c6f52 Mon Sep 17 00:00:00 2001 From: Michael Mandl Date: Sat, 15 Aug 2020 18:07:30 +0200 Subject: [PATCH] Initial project, blinks led on PC5 (analog input 5) --- .gitignore | 3 ++ CMakeLists.txt | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/blink.cpp | 15 ++++++++++ 3 files changed, 99 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 src/blink.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7a3c8f6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/build/ +/.clangd/ +compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d0d0c21 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,81 @@ +cmake_minimum_required(VERSION 3.15) + +# Project name +project("Blink") + +# Product filename +set(PRODUCT_NAME blink) + +set(F_CPU 16000000UL) +set(MCU atmega328p) +set(BAUD 9600) + +set(PROG_TYPE arduino) +set(PROG_PORT /dev/ttyACM0) + + +set(CMAKE_SYSTEM_NAME Generic) +set(CMAKE_CXX_COMPILER avr-g++) +set(CMAKE_C_COMPILER avr-gcc) +set(CMAKE_ASM_COMPILER avr-gcc) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + + +add_definitions( + -DF_CPU=${F_CPU} + -DBAUD=${BAUD} +) + +include_directories(SYSTEM /usr/avr/include) + +set(CMAKE_EXE_LINKER_FLAGS -mmcu=${MCU}) + +add_compile_options( + -mmcu=${MCU} # MCU + -std=gnu++17 + -Os # optimize + -Wall # enable warnings + -Wno-main + -Wundef + -pedantic + -Werror + -Wfatal-errors + -Wl,--relax,--gc-sections + -g + -gdwarf-2 + -funsigned-char # a few optimizations + -funsigned-bitfields + -fpack-struct + -fshort-enums + -ffunction-sections + -fdata-sections + -fno-split-wide-types + -fno-tree-scev-cprop +) + +# Create one target +add_executable(${PRODUCT_NAME} + src/blink.cpp +) + +# Rename the output to .elf as we will create multiple files +set_target_properties(${PRODUCT_NAME} PROPERTIES OUTPUT_NAME ${PRODUCT_NAME}.elf) + +# Strip binary for upload +add_custom_target(strip ALL avr-strip ${PRODUCT_NAME}.elf DEPENDS ${PRODUCT_NAME}) + +# Transform binary into hex file, we ignore the eeprom segments in the step +add_custom_target(hex ALL avr-objcopy -R .eeprom -O ihex ${PRODUCT_NAME}.elf ${PRODUCT_NAME}.hex DEPENDS strip) +# Transform binary into hex file, this is the eeprom part (empty if you don't +# use eeprom static variables) +add_custom_target(eeprom avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex ${PRODUCT_NAME}.elf ${PRODUCT_NAME}.eep DEPENDS strip) + +# Upload the firmware with avrdude +add_custom_target(upload avrdude -c ${PROG_TYPE} -P ${PROG_PORT} -p ${MCU} -U flash:w:${PRODUCT_NAME}.hex DEPENDS hex) + +# Upload the eeprom with avrdude +add_custom_target(upload_eeprom avrdude -c ${PROG_TYPE} -P ${PROG_PORT} -p ${MCU} -U eeprom:w:${PRODUCT_NAME}.eep DEPENDS eeprom) + +# Clean extra files +set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES "${PRODUCT_NAME}.hex;${PRODUCT_NAME}.eeprom;${PRODUCT_NAME}.lst") diff --git a/src/blink.cpp b/src/blink.cpp new file mode 100644 index 0000000..f1467d7 --- /dev/null +++ b/src/blink.cpp @@ -0,0 +1,15 @@ +#include +#include + +int main() +{ + DDRC |= (1 << PC5); + + while (true) + { + PORTC ^= (1 << PC5); + _delay_ms(250); + } + + return 0; +}