Attiny202事始め (1)環境設定からLチカまで

# 秋月電子で@70円
https://akizukidenshi.com/catalog/g/gI-15731/
# 開発環境はWindows11でavr-gccとGNU Make
## avr-gccのバージョンはavr-gcc-12.1.0-x64-windows
https://github.com/ZakKemble/avr-gcc-build
のリリースを利用させて頂いています。
## GNU MakeはMinGW-w64
https://www.mingw-w64.org/
Makefile例（実はよく分かっていない）
```Makefile
####
CROSSCOMPILE ?= avr-

CC := $(CROSSCOMPILE)gcc
CPP := $(CROSSCOMPILE)gcc
CXX := $(CROSSCOMPILE)g++
AS := $(CROSSCOMPILE)as
AR := $(CROSSCOMPILE)ar
LD := $(CROSSCOMPILE)gcc
GDB := $(CROSSCOMPILE)gdb
NM := $(CROSSCOMPILE)nm
OBJCOPY := $(CROSSCOMPILE)objcopy
OBJDUMP := $(CROSSCOMPILE)objdump
READELF := $(CROSSCOMPILE)readelf
SIZE := $(CROSSCOMPILE)size

###
PARTNO := t202
MMCU := attiny202
CLOCK := 20000000L

MCU_FLAGS += -mmcu=${MMCU} -DF_CPU=${CLOCK} -I.
DEBUG_OPTIMIZE_FLAGS += -g -gdwarf-4 -Os

# only one of these makes sense: either use LTO
#DEBUG_OPTIMIZE_FLAGS += -flto
# or generate listings. listings wont make any sense when using LTO, as LTO does not
# generate actual assembler code
CXXC_FLAGS += -Wa,-adhlns=$(<:%=%.lst)

CXXC_FLAGS += -Wall -Werror -Wshadow --pedantic
CXXC_FLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -fstack-usage
CXXC_FLAGS += $(MCU_FLAGS) $(DEBUG_OPTIMIZE_FLAGS)
CXXFLAGS += $(CXXC_FLAGS)
CFLAGS += $(CXXC_FLAGS) -std=c99

LDFLAGS += $(MCU_FLAGS) $(DEBUG_OPTIMIZE_FLAGS)
LDFLAGS += -Wl,--cref,-Map=$(@:%.elf=%.map)

###

# AVR0 UPDI
PROGRAMMER_ID ?= serialupdi
PORT ?= COM17
AVRDUDE_OPTS := -c ${PROGRAMMER_ID} -P ${PORT} -p ${PARTNO}

###
.PHONY: all main readfuses flash erase clean

all:	main

main:	main.asm main.hex main.eep main.bin

readfuses:
	avrdude ${AVRDUDE_OPTS} -Ulfuse:r:fuse_low.hex:h -U hfuse:r:fuse_high.hex:h


flash: main.hex
	avrdude ${AVRDUDE_OPTS} -U flash:w:main.hex

erase:
	avrdude ${AVRDUDE_OPTS} -e

clean:
	rm -f *.asm *.lst *.o *.map *.hex *.eep *.bin *.elf *.su

###
main.o: main.cpp

###
%.elf: %.o
	$(CC) $(LDFLAGS) $^ -o $@

%.hex: %.elf
	$(OBJCOPY) -j .text -j .data -O ihex $< $@
	$(SIZE) $@

%.eep: %.elf
	$(OBJCOPY) -j .eeprom -O ihex $< $@
	$(SIZE) $@

%.bin: %.elf
	$(OBJCOPY) -j .text -j .data -O binary $< $@

%.asm: %.elf
	@# for private builds:
	$(OBJDUMP) -dgCxwsSh --show-raw-insn $< > $@
	@# for public builds which should not leak data:
	@#$(OBJDUMP) -dCwSh --show-raw-insn $< > $@
```
## Flashの書き込みはavrdudeでserialupdi
https://github.com/avrdudes/avrdude/releases/tag/v7.1
Makefileで.PHONY: flashを定義して
```
PARTNO := t202
PROGRAMMER_ID ?= serialupdi
PORT ?= COM17
AVRDUDE_OPTS := -c ${PROGRAMMER_ID} -P ${PORT} -p ${PARTNO}
flash: main.hex
    avrdude ${AVRDUDE_OPTS} -U flash:w:main.hex
```
## USBシリアルアダプタにショットキーバリアダイオードと470Ω抵抗を接続したUPDI書き込み機
http://nemuisan.blog.bai.ne.jp/?eid=239957

## Lチカサンプルコード
PA2（ピン#5）にLEDを接続
デフォルトはプリスケーラーが有効になっているようなので最初にクロック設定を行っている
CCPのプロテクトを解除後にプリスケーラーを無効化する
```main.cpp
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    // クロック設定 20MHz
    CPU_CCP = 0xD8;             // Configuration Change Protection (CCP) pass code
    CLKCTRL.MCLKCTRLB = 0x0;    // no-prescaler
    // PA2を出力モードに設定
    VPORTA.DIR |= 0x4;

    while (1) {
        // PA2をHIGHに設定してLED点灯
        VPORTA.OUT |= 0x4;
        _delay_ms(1000);

        // PA2をLOWに設定してLED消灯
        VPORTA.OUT &= 0xFB;
        _delay_ms(1000);
    }

    return 0;
}
```
## ひとまずLチカ成功！
```log.txt
PS D:\Work\Attiny202\VSCode\TEST00> make
D:/Work/AVR/avr-gcc-12.1.0-x64-windows/bin/avr-g++ -Wa,-adhlns=main.cpp.lst -Wall -Werror -Wshadow --pedantic -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -fstack-usage -mmcu=attiny202 -DF_CPU=20000000L -I. -g -gdwarf-4 -Os   -c -o main.o main.cpp
D:/Work/AVR/avr-gcc-12.1.0-x64-windows/bin/avr-gcc -mmcu=attiny202 -DF_CPU=20000000L -I. -g -gdwarf-4 -Os -Wl,--cref,-Map=main.map main.o -o main.elf
D:/Work/AVR/avr-gcc-12.1.0-x64-windows/bin/avr-objdump -dgCxwsSh --show-raw-insn main.elf > main.asm
D:/Work/AVR/avr-gcc-12.1.0-x64-windows/bin/avr-objcopy -j .text -j .data -O ihex main.elf main.hex
D:/Work/AVR/avr-gcc-12.1.0-x64-windows/bin/avr-size main.hex
   text    data     bss     dec     hex filename
      0     126       0     126      7e main.hex
D:/Work/AVR/avr-gcc-12.1.0-x64-windows/bin/avr-objcopy -j .eeprom -O ihex main.elf main.eep
D:/Work/AVR/avr-gcc-12.1.0-x64-windows/bin/avr-size main.eep
   text    data     bss     dec     hex filename
      0       0       0       0       0 main.eep
D:/Work/AVR/avr-gcc-12.1.0-x64-windows/bin/avr-objcopy -j .text -j .data -O binary main.elf main.bin
rm main.elf
PS D:\Work\Attiny202\VSCode\TEST00> make flash
avrdude -c serialupdi -P COM17 -p t202 -U flash:w:main.hex

avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e9123 (probably t202)
avrdude: Note: flash memory has been specified, an erase cycle will be performed.
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file main.hex for flash
         with 126 bytes in 1 section within [0, 0x7d]
         using 2 pages and 2 pad bytes
avrdude: writing 126 bytes flash ...

Writing | ################################################## | 100% 0.06 s 

avrdude: 126 bytes of flash written
avrdude: verifying flash memory against main.hex

Reading | ################################################## | 100% 0.02 s 

avrdude: 126 bytes of flash verified

avrdude done.  Thank you.
```
![attiny202_001.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/121242/598c1daa-84fc-a160-6a49-1e2410aaeed8.jpeg)

### 参考にさせて頂いたみなさん、ありがとうございました。