From 9617d14134e83ac0ff3cfad3a80f2e63d53dd96e Mon Sep 17 00:00:00 2001 From: Gregory Lirent Date: Thu, 2 Nov 2023 21:53:55 +0300 Subject: [PATCH] initial commit --- .gitignore | 2 + CMakeLists.txt | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 24 +++++++++++ 3 files changed, 135 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3036c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/.idea/ +/build/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1869b7b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,109 @@ +cmake_minimum_required(VERSION 3.26) + +project(avr_blink VERSION 0.1.0.0 LANGUAGES C ASM) + +set(CMAKE_C_STANDARD 17) + +if(NOT MCU_TYPE) + set(MCU_TYPE attiny13a) +endif() + +if(NOT MCU_PROGRAMMER_SPEED) + set(MCU_PROGRAMMER_SPEED 256kHz) +endif() + +if(NOT MCU_PROGRAMMER) + set(MCU_PROGRAMMER usbasp) +endif() + +if(NOT MCU_PROGRAMMER_PORT) + set(MCU_PROGRAMMER_PORT /dev/usb/lp0) +endif() + +set(SOURCE_FILES main.c) + +######################################################################################################################## + +find_program(MCU_CC avr-gcc REQUIRED) +find_program(MCU_CXX avr-g++ REQUIRED) +find_program(MCU_UPLOAD_TOOL avrdude REQUIRED) +find_program(MCU_OBJCOPY avr-objcopy REQUIRED) +find_program(MCU_SIZE avr-size REQUIRED) +find_program(MCU_OBJDUMP avr-objdump REQUIRED) + +set(CMAKE_C_COMPILER ${MCU_CC}) +set(CMAKE_CXX_COMPILER ${MCU_CXX}) + +if(DEFINED $ENV{AVR_ROOT_PATH}) + set(AVR_ROOT_PATH $ENV{AVR_ROOT_PATH}) +else() + if(EXISTS "/usr/avr") + set(AVR_ROOT_PATH "/usr/avr") + elseif(EXISTS "/opt/local/avr") + set(AVR_ROOT_PATH "/opt/local/avr") + elseif(EXISTS "/usr/lib/avr") + set(AVR_ROOT_PATH "/usr/lib/avr") + else() + message(FATAL_ERROR "Please set AVR_ROOT_PATH in your environment.") + endif() +endif() + +set(CMAKE_SYSTEM_INCLUDE_PATH "${AVR_ROOT_PATH}/include") +set(CMAKE_SYSTEM_LIBRARY_PATH "${AVR_ROOT_PATH}/lib") + +######################################################################################################################## + +set(MCU_FLAGS -c ${MCU_PROGRAMMER} -p ${MCU_TYPE} -P ${MCU_PROGRAMMER_PORT} -B ${MCU_PROGRAMMER_SPEED}) + +if(NOT CMAKE_BUILD_TYPE OR NOT (CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES Debug)) + set(CMAKE_BUILD_TYPE Debug) +endif() + +if(CMAKE_BUILD_TYPE MATCHES Release) + set(CMAKE_C_FLAGS_RELEASE "-Os") + set(CMAKE_CXX_FLAGS_RELEASE "-Os") +endif() + +if(CMAKE_BUILD_TYPE MATCHES Debug) + set(CMAKE_C_FLAGS_DEBUG "-O0 -save-temps -g -gdwarf-3 -gstrict-dwarf") + set(CMAKE_CXX_FLAGS_DEBUG "-O0 -save-temps -g -gdwarf-3 -gstrict-dwarf") +endif() + +######################################################################################################################## + +function(add_mcu_executable EXECUTABLE_NAME) + if(NOT ARGN) + message(FATAL_ERROR "No source files given for ${EXECUTABLE_NAME}") + endif() + + set(elf_file ${MCU_TYPE}-${EXECUTABLE_NAME}.elf) + set(map_file ${MCU_TYPE}-${EXECUTABLE_NAME}.map) + set(hex_file ${MCU_TYPE}-${EXECUTABLE_NAME}.hex) + set(eeprom_file ${MCU_TYPE}-${EXECUTABLE_NAME}-eeprom.hex) + + add_executable(${elf_file} EXCLUDE_FROM_ALL ${ARGN}) + set_target_properties(${elf_file} PROPERTIES + COMPILE_FLAGS "-mmcu=${MCU_TYPE}" + LINK_FLAGS "-mmcu=${MCU_TYPE} -Wl,--gc-sections -mrelax -Wl,-Map,${map_file}") + + add_custom_command(OUTPUT ${hex_file} + COMMAND ${MCU_OBJCOPY} -j .text -j .data -O ihex ${elf_file} ${hex_file} + DEPENDS ${elf_file}) + + add_custom_command(OUTPUT ${eeprom_file} + COMMAND ${MCU_OBJCOPY} -j .eeprom --set-section-flags=.eeprom=alloc,load + --change-section-lma .eeprom=0 --no-change-warnings + -O ihex ${elf_file} ${eeprom_file} + DEPENDS ${elf_file}) + + add_custom_target(upload_hex + ${MCU_UPLOAD_TOOL} ${MCU_FLAGS} -v -U flash:w:${hex_file} + DEPENDS ${hex_file}) + + add_custom_target(upload_eeprom + ${MCU_UPLOAD_TOOL} ${MCU_FLAGS} -v -U eeprom:w:${eeprom_file} + DEPENDS ${eeprom_file}) + +endfunction() + +add_mcu_executable(avr_blink main.c) diff --git a/main.c b/main.c new file mode 100644 index 0000000..e3d102c --- /dev/null +++ b/main.c @@ -0,0 +1,24 @@ +#include +#include + +int main(void) { +#if defined(DDRC) && defined(DDRD) +#define BITS 8 + DDRC = 0x3f; + DDRD = DDRB = 0xff; +#else +#define BITS 6 + DDRB = 0x3f; +#endif + for (;;) { + for (uint8_t i = 0; i < BITS; ++i) { +#if defined(DDRC) && defined(DDRD) + PORTC = (PORTD = PORTB ^= _BV(i))&0x3f; +#else + PORTB ^= _BV(i); +#endif + _delay_ms(36); + } + } + return 0; +} \ No newline at end of file