Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AasmundN committed Dec 4, 2024
0 parents commit cf2ef4e
Show file tree
Hide file tree
Showing 13 changed files with 736 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .clangd
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CompileFlags:
Add: [-Wno-unknown-warning-option, -Wno-c23-extensions, -mmcu=avrxmega3]
Remove: [-m*, -f*]
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store
compile_commands.json
build
device_pack
cmake_install.cmake
*.hex
*.out
*.elf
*.map
CMakeCache.txt
.cache
35 changes: 35 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
cmake_minimum_required(VERSION 3.20)

# ---------------------------- Target Configuration ----------------------------

# Specify the target/project name
set(TARGET attiny-isp)

set(MCU attiny1607)
set(F_CPU 3300000)

set(TOOLCHAIN_USE_PYMCUPROG true)

# Specify the desired language standard
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)

#Include the toolchain. If a device pack is needed, keep this value to true.
# The device pack should be in a folder name 'device_pack'
set(TOOLCHAIN_NEED_DEVICE_PACK true)
include(cmake/Toolchain.cmake)

# ----------------------------------- Target -----------------------------------

project(lab4)
file(GLOB SOURCES src/*.c)

configure_target(${TARGET} ${SOURCES})

# Specify own definitions, include directories and libraries here. The
# AVRToolChain.cmake file already specifies a bunch of standard compile options
# and definitions

# target_compile_definitions(${TARGET} ...)
target_include_directories(${TARGET} PRIVATE include src)
target_link_libraries(${TARGET} PRIVATE m)
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# ATtiny In-circuit Serial Programmer (ISP)

In-circuit Serial Programmer for the first generation of (ATtiny microchontrollers)[https://en.wikipedia.org/wiki/ATtiny_microcontroller_comparison_chart]. These devices are very simple and can only be programmed in assembler; perfect for learning! The ISP programmer is implemented on an ATtiny1607.

## How to...

Compile the source files for ATtiny1607 using the GNU toolchain for AVR (avr-gcc etc) and CMake. You must also install the device family pack (DFP) for the ATtiny1607 and place it in a folder named _device_pack_.

The example folder includes a simple led blinker program in assembler for the ATtiny15L. Commands for assembling and linking the program can be found in the same folder.

Will add circuit diagrams later...

## Further work

- Add a cool serial interface where you can paste programs to be written to the ATtiny program memory
- Document code
- Write a cool assembler program
- PCB?

## Resources

- (ATtiny15L datasheet)[https://ww1.microchip.com/downloads/en/DeviceDoc/doc1187.pdf]
- (ATtiny1607 datasheet)[https://ww1.microchip.com/downloads/en/DeviceDoc/ATtiny807_1607-Data-Sheet-40002030A.pdf]
249 changes: 249 additions & 0 deletions cmake/Toolchain.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
set(DEFAULT_BUILD_TYPE "Release")

if(NOT CMAKE_BUILD_TYPE)
message(
STATUS
"Setting build type to '${DEFAULT_BUILD_TYPE}' as none was specified.")
set(CMAKE_BUILD_TYPE "${DEFAULT_BUILD_TYPE}")
endif()

# Find the binaries for the toolchain
find_program(AVR_CC avr-gcc REQUIRED)
find_program(AVR_CXX avr-g++ REQUIRED)
find_program(AVR_OBJCOPY avr-objcopy REQUIRED)
find_program(AVR_OBJDUMP avr-objdump REQUIRED)

if(TOOLCHAIN_USE_PYMCUPROG)
find_program(PROGRAMMER pymcuprog REQUIRED)
else()
find_program(PROGRAMMER avrdude REQUIRED)
endif()

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR avr)

set(CMAKE_C_STANDARD_REQUIRED TRUE)
set(CMAKE_C_COMPILER ${AVR_CC})

set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_COMPILER ${AVR_CXX})
set(CMAKE_ASM_COMPILER ${AVR_CC})

# Export compile commands for use with LSPs
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

enable_language(C CXX ASM)

# ------------------------------- Device pack --------------------------------

# Find the device pack if specified (this is needed for newer devices which
# don't have native support in avr-gcc)
if(TOOLCHAIN_NEED_DEVICE_PACK)

set(DEVICE_PACK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/device_pack)

if(NOT EXISTS ${DEVICE_PACK_DIR})
message(
FATAL_ERROR
"Device pack not found in root of project directory. The device pack (ATPACK/DFP) should be in a folder named 'device_pack'. Download it for your device at http://packs.download.atmel.com."
)
endif()

# Helper variables for the device related directories in the ATPACK and the
# toolchain
set(MCU_DEVICE_DIRECTORY ${DEVICE_PACK_DIR}/gcc/dev/${MCU})
set(MCU_INCLUDE_DIRECTORY ${DEVICE_PACK_DIR}/include)

if(NOT EXISTS ${MCU_DEVICE_DIRECTORY}/device-specs/specs-${MCU})
message(
FATAL_ERROR
"Could not find device pack for ${MCU}, tried looking in directory ${MCU_DEVICE_DIRECTORY}/device-specs/specs-${MCU}. The device name might be misspelled or the device pack might be incorrect for this device."
)
endif()

# Find the device library name by going through the device specs for the
# device
file(READ ${MCU_DEVICE_DIRECTORY}/device-specs/specs-${MCU}
SPECS_FILE_CONTENT)
string(
REGEX MATCH
"-D__AVR_DEVICE_NAME__=${MCU} -D__AVR_DEV_LIB_NAME__=([a-zA-Z0-9]*)"
_ ${SPECS_FILE_CONTENT})
set(MCU_DEV_LIB_NAME ${CMAKE_MATCH_1})

endif()

# ---------------------------- Definitions & flags -----------------------------

if(NOT DEFINED F_CPU)
message(WARNING "F_CPU not defined, has to be defined in code")
else()
set(TOOLCHAIN_COMPILE_DEFINITIONS F_CPU=${F_CPU})
endif()

set(TOOLCHAIN_COMPILE_DEFINITIONS
${TOOLCHAIN_COMPILE_DEFINITIONS} $<$<CONFIG:Debug>:DEBUG>
$<$<CONFIG:Release>:NDEBUG>)

set(TOOLCHAIN_COMPILE_OPTIONS
-mmcu=${MCU}
-Werror
-Wall
-Wextra
-Wshadow
-Wno-array-bounds
-Wno-vla
# Optimisations
-funsigned-char
-funsigned-bitfields
-fpack-struct
-fshort-enums
-ffunction-sections
-fdata-sections
-fno-split-wide-types
-fno-tree-scev-cprop
$<$<CONFIG:Debug>:-Og>
$<$<CONFIG:Debug>:-Wno-unused-function>
$<$<CONFIG:Release>:-Os>
$<$<COMPILE_LANGUAGE:CXX>:-fno-rtti>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-volatile>
$<$<COMPILE_LANGUAGE:CXX>:-Wno-register>)

set(TOOLCHAIN_LINK_OPTIONS
-mmcu=${MCU}
-Wl,-Map=${TARGET}.map
-Wl,--print-memory-usage
-Wl,--gc-section
-Wl,--sort-section=alignment
-Wl,--cref
$<$<CONFIG:Release>:-Os>
$<$<CONFIG:Debug>:-Og>)

if(TOOLCHAIN_NEED_DEVICE_PACK)
set(TOOLCHAIN_COMPILE_DEFINITIONS ${TOOLCHAIN_COMPILE_DEFINITIONS}
__AVR_DEV_LIB_NAME__=${MCU_DEV_LIB_NAME})

set(TOOLCHAIN_COMPILE_OPTIONS
${TOOLCHAIN_COMPILE_OPTIONS}
# Include the AVR header files from the ATPACK
-I${MCU_INCLUDE_DIRECTORY}
# Notify the compiler about the device specs
-B${MCU_DEVICE_DIRECTORY})

set(TOOLCHAIN_LINK_OPTIONS
${TOOLCHAIN_LINK_OPTIONS}
# Notify the compiler about the device specs
-B${MCU_DEVICE_DIRECTORY})

endif()

add_compile_options(${TOOLCHAIN_COMPILE_OPTIONS})
add_compile_definitions(${TOOLCHAIN_COMPILE_DEFINITIONS})
add_link_options(${TOOLCHAIN_LINK_OPTIONS})

function(configure_target TARGET)

if(NOT ARGN)
message(FATAL_ERROR "No source files given for ${TARGET}.")
endif(NOT ARGN)

add_executable(${TARGET} ${SOURCES})
set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME ${TARGET}.elf)

# STRIP
add_custom_target(
strip ALL
avr-strip ${TARGET}.elf
DEPENDS ${TARGET}
COMMENT "Stripping ${TARGET}.elf")

# HEX
add_custom_target(
hex ALL
${AVR_OBJCOPY} -O ihex ${TARGET}.elf ${TARGET}.hex
DEPENDS strip
COMMENT "Creating ${TARGET}.hex")

# EEPROM
add_custom_target(
eeprom ALL
${AVR_OBJCOPY}
-j
.eeprom
--set-section-flags=.eeprom=alloc,load
--change-section-lma
.eeprom=0
--no-change-warnings
-O
ihex
${TARGET}.elf
${TARGET}-eeprom.hex
DEPENDS strip
COMMENT "Creating ${TARGET}-eeprom.hex")

if(TOOLCHAIN_USE_PYMCUPROG)
add_custom_target(
upload
${PROGRAMMER} write -f ${TARGET}.hex --erase --verify
DEPENDS hex
COMMENT "Uploading ${TARGET}.hex to ${MCU}")

add_custom_target(
reset
${PROGRAMMER} reset
COMMENT "Resetting device...")
else()
add_custom_target(
upload
${PROGRAMMER}
-c
${AVRDUDE_PROGRAMMER_ID}
-p
${AVRDUDE_MCU}
-U
flash:w:${TARGET}.hex:i
DEPENDS hex
COMMENT "Uploading ${TARGET}.hex to ${MCU}")

add_custom_target(
upload_eeprom
${PROGRAMMER}
-c
${AVRDUDE_PROGRAMMER_ID}
-p
${AVRDUDE_MCU}
-U
eeprom:w:${TARGET}-eeprom.hex:i
DEPENDS eeprm
COMMENT "Uploading ${TARGET}-eeprom.hex to ${MCU}")
endif()

# Disassemble
add_custom_target(
disassemble
${AVR_OBJDUMP} -h -S ${TARGET}.elf > ${TARGET}.lst
DEPENDS strip
COMMENT "Disassembling ${TARGET}.elf")

# Remove .hex, .map, eeprom .hex and .lst on clean
set_property(
TARGET ${TARGET}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES ${TARGET}.hex)

set_property(
TARGET ${TARGET}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES ${TARGET}.map)

set_property(
TARGET ${TARGET}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES ${TARGET}-eeprom.hex)

set_property(
TARGET ${TARGET}
APPEND
PROPERTY ADDITIONAL_CLEAN_FILES ${TARGET}.lst)

endfunction(configure_target)
62 changes: 62 additions & 0 deletions example/main.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
;
; main.S
;
; Author : AasmundN
;
; Simple LED blinker. Toggles all pins in PORTB
;
; Assemble and link using:
; avr-gcc -nostdlib -mmcu=attiny15 main.S -o main.out
; avr-objcopy -O binary main.out main.hex
;
; WARN file ending must be .S
;

#define __SFR_OFFSET 0
#include <avr/io.h>

.global main

main:
rcall gpio_init
main_loop: ; main program loop
in r16, PORTB ; read PORTB
ldi r17, 0xff

eor r16, r17 ; toggle bits in PORTB
out PORTB, r16 ; wirte PORTB

rcall delay
rcall main_loop


gpio_init: ; initialize PORTB as output
ldi r16, 0xff
out DDRB, r16 ; direction output
out PORTB, r16 ; drive all pins high
ret


delay: ; delay routine
ldi r17, 0xff
ldi r20, 0x00
delay_loop_outer:
cp r17, r20
breq end
dec r17
ldi 18, 0xff
delay_loop_inner:
nop
nop
nop
nop
nop
dec r18
cp r18, r20
brne delay_loop_inner
rjmp delay_loop_outer
end:
ret


.end
8 changes: 8 additions & 0 deletions include/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef __GPIO__
#define __GPIO__

void GPIO_init(void);

void GPIO_toggle_led(void);

#endif // __GPIO__
Loading

0 comments on commit cf2ef4e

Please sign in to comment.