initial commit

This commit is contained in:
Gregory Lirent 2023-11-02 21:53:55 +03:00
commit 9617d14134
3 changed files with 135 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.idea/
/build/

109
CMakeLists.txt Normal file
View File

@ -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)

24
main.c Normal file
View File

@ -0,0 +1,24 @@
#include <avr/io.h>
#include <util/delay.h>
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;
}