Here you can find documentation of the Embedded Programming group assignment which maps to the following Fab Academy assignment weeks.


Team


TODO


Initial Responsibilities


Build


ESP32 Programming: Jasmine


Arduino Programming: Ranjit


Wan-Ting: AVR Programming: ATtiny 412 + avr-gcc + pyupdi + Terminal

Step 1. Set up Programming Environment

I followed this avr-gcc & avrdude makefile tutorial | Compile C & upload to AVR board (mac) and adjusted some parameters to generate hex code from a elf file (which is generated from a c file) via avr-gcc and then upload the hex file to the ATtiny 412 board via avrdude.

Follow the order below to install avr-gcc properly:

  1. /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)": install Homebrew

  2. xcode-select --install: install Xcode

  3. brew tap osx-cross/avr

  4. brew install avr-gcc: install avr-gcc & install AVR Libc

  5. brew install avrdude: install AVR Downloader/Uploader

Step 2. Start Programming C File

hello.412.blink.c
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    PORTA.DIRSET = PIN3_bm;// or PORTA.DIR = 0b01000000; // Use LED: PA3 as Output
    PORTA.DIRCLR = PIN6_bm;// Use Button: PA6 as Input

    while (1) {
        PORTA.OUT |= LED_AVR;
        _delay_ms(500);
        PORTA.OUT &= ~LED_AVR;
        _delay_ms(500);
    }
}
Step 3. Create & Edit a Makefile
Makefile
FILENAME      = hello.412.blink
PORT 	      = /dev/tty.usbserial-D3072T1T
DEVICE 	      = attiny412
PYUPDI_DEVICE = tiny412
# PROGRAMMER   = jtag2updi
BAUD 	      = 115200
# For adding device packs of the latest chips (e.g. ATtiny412, 1614...)
INCLUDE       = "/Users/USER_NAME/Documents/AVR/Atmel.ATtiny_DFP.1.8.332/include"
PACK	      = "/Users/USER_NAME/Documents/AVR/Atmel.ATtiny_DFP.1.8.332/gcc/dev/attiny412"
COMPILE       = avr-gcc -Wall -Os -DF_CPU=20000000 -mmcu=${DEVICE} -I ${INCLUDE} -B ${PACK}

default: compile upload clean

compile:
	${COMPILE} -c ${FILENAME}.c -o ${FILENAME}.o
    //highlight-next-line
	${COMPILE} -o ${FILENAME}.elf ${FILENAME}.o  # Problem: Can't generate .elf from .o file.
	avr-objcopy -j .text -j .data -O ihex ${FILENAME}.elf ${FILENAME}.hex
	avr-size --format=avr --mcu=${DEVICE} ${FILENAME}.elf

upload:
	# avrdude -v -p ${DEVICE} -c ${PROGRAMMER} -P ${PORT} -b ${BAUD} -U flash:w:${FILENAME}.hex:i
	pyupdi -d ${PYUPDI_DEVICE} -b ${BAUD} -c ${PORT} -f ${FILENAME}.hex -v

clean:
	rm ${FILENAME}.o
	rm ${FILENAME}.elf
	rm ${FILENAME}.hex

arduino_avr_gcc.png

elf.png

Step 4. Upload the code via Makefile

Since the command for uploading the hex file to the ATtiny 412 board through pyupdi has been defined in the upload section of the Makefile, we just need to type the following commands in the terminal with the ATtiny 412 board connected to the UPDI programmer introduced here.

cd path/to/hex/and/Makefile/file
make

avr-gcc_folder.png