Arduino-CLIでATTinyの開発環境を整える

# はじめに

前回、esp32の開発環境をarduino-cliで整備しました。
attinyについて同じことをします。

https://qiita.com/curtain6935/items/c95479b53954e1388eee

# 想定環境

https://github.com/SpenceKonde/ATTinyCore

- macos arm64
- x86_64? rosetta? 知らない子ですね...

platformioのattinyでは、arm64 macosでもx86_64のtoolchainが入ります。
本記事の方法ではplatformioではなくarduino-cliとなりますが、arm64 nativeとなります。

https://community.platformio.org/t/cant-build-with-toolchain-atmelavr-on-darwin-arm64/26688

# インストールと初期設定

## Arduino-CLIのインストール

homebrewなどで適当にインストールします。

```shell-session
$ brew install arduino-cli
```

## クロスコンパイラgcc、書き込みツールのインストール

`arduino-cli core install`で入るホスト用バイナリはx86_64ですので、rosettaless環境では動作しません。
arm64バイナリをhomebrewで入れます。

```shell-session
$ brew install osx-cross/avr/avr-gcc@11 osx-cross/avr/avr-binutils avrdude
```

## arduino用ctagsのインストール

ctagsもx86_64なのでarm64でビルドします。
esp32の環境と同じです。
(homebrewに入っているctagsはarduino-cliでは使えません)

https://github.com/arduino/arduino-ide/issues/2337

https://github.com/arduino/ctags/issues/20

```shell-session
$ git clone https://github.com/arduino/ctags.git
$ cd ctags
$ ./configure
$ vi general.h
%% 60行目付近のifをelseのみ有効にする
$ make
$ install ctags /usr/local/bin/
```

## yamlの用意

```~/src/arduino/cli-config/attiny.yaml
board_manager:
  additional_urls:
    - http://drazzy.com/package_drazzy.com_index.json
directories:
  data: /home/sweethome/src/arduino/core/attiny/
  downloads: /home/sweethome/src/arduino/core/download/
  user: /home/sweethome/src/arduino
```

## ボードのインストール

```shell-session
$ arduino-cli --config-file ~/src/arduino/cli-config/attiny.yaml \
  core install ATTinyCore:avr
```

## platform.txtの書き換え

`platform.txt`を書き換えて、homebrewで入れたものを使うように指定します。

```diff
--- src/arduino/core/attiny/packages/ATTinyCore/hardware/avr/1.5.2/platform.txt.orig	2024-01-01 00:00:00 +0000
+++ src/arduino/core/attiny/packages/ATTinyCore/hardware/avr/1.5.2/platform.txt	2024-00-01 00:00:00 +0000
@@ -17,14 +17,15 @@
 compiler.warning_flags.all=-Wall -Wextra
 
 # Default "compiler.path" is correct, change only if you want to overidde the initial value
-compiler.path={runtime.tools.avr-gcc.path}/bin/
+compiler.path=/opt/homebrew/opt/avr-gcc@11/bin/
+binutils.path=/opt/homebrew/opt/avr-binutils/bin/
 compiler.c.cmd=avr-gcc
-compiler.c.flags=-c -g -Os {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -MMD {ltocflags}
+compiler.c.flags=-c -g -Os {compiler.warning_flags} -std=gnu11 -ffunction-sections -fdata-sections -MMD {ltocflags} -I/opt/homebrew/opt/avr-gcc@11/avr/include
 compiler.c.elf.flags={compiler.warning_flags} -Os {ltoelfflags} -Wl,--gc-sections
 compiler.c.elf.cmd=avr-gcc
 compiler.S.flags=-c -g -x assembler-with-cpp {ltocppflags}
 compiler.cpp.cmd=avr-g++
-compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD {ltocppflags}
+compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -fpermissive -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -MMD {ltocppflags} -I/opt/homebrew/opt/avr-gcc@11/avr/include
 compiler.ar.cmd=avr-{ltoarcmd}ar
 compiler.ar.flags=rcs
 compiler.objcopy.cmd=avr-objcopy
@@ -68,13 +69,13 @@
 recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {compiler.c.elf.flags} -mmcu={build.mcu} {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" {object_files} "{build.path}/{archive_file}" "-L{build.path}" -lm
 
 ## Create eeprom
-recipe.objcopy.eep.pattern="{compiler.path}{compiler.objcopy.cmd}" {compiler.objcopy.eep.flags} {compiler.objcopy.eep.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.eep"
+recipe.objcopy.eep.pattern="{binutils.path}{compiler.objcopy.cmd}" {compiler.objcopy.eep.flags} {compiler.objcopy.eep.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.eep"
 
 ## Create hex
-recipe.objcopy.hex.pattern="{compiler.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.hex"
+recipe.objcopy.hex.pattern="{binutils.path}{compiler.elf2hex.cmd}" {compiler.elf2hex.flags} {compiler.elf2hex.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.hex"
 
 ## Compute size
-recipe.size.pattern="{compiler.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
+recipe.size.pattern="{binutils.path}{compiler.size.cmd}" -A "{build.path}/{build.project_name}.elf"
 recipe.size.regex=^(?:\.text|\.data|\.bootloader)\s+([0-9]+).*
 recipe.size.regex.data=^(?:\.data|\.bss|\.noinit)\s+([0-9]+).*
 recipe.size.regex.eeprom=^(?:\.eeprom)\s+([0-9]+).*
@@ -109,9 +110,8 @@
 
 # AVR Uploader/Programmers tools
 # ------------------------------
-tools.avrdude.path={runtime.tools.avrdude.path}
-tools.avrdude.cmd.path={path}/bin/avrdude
-tools.avrdude.config.path={runtime.platform.path}/avrdude.conf
+tools.avrdude.cmd.path=/opt/homebrew/opt/avrdude/bin/avrdude
+tools.avrdude.config.path=/opt/homebrew/etc/avrdude.conf
 
 
 tools.avrdude.upload.params.verbose=-v
```

# Makefile

以下、attiny85にArdiunoISPで書き込む場合です。

```makefile:Makefile

PORTWRITE    = $(shell echo /dev/cu.usbmodem*)
PORT         = $(shell echo /dev/cu.usbserial*)
FQBN         = ATTinyCore:avr:attinyx5opti
OPTIONS      = --programmer arduinoasisp
CLI_YAML     = $(HOME)/src/arduino/cli-config/attiny.yaml
BUILD_DIR    = $(CURDIR)/build-attiny
MONITORSPEED = 9600

.PHONY: build

clean:
	rm -rf $(BUILD_DIR)

build:
	@ mkdir -p $(BUILD_DIR)
	arduino-cli compile \
		--config-file $(CLI_YAML) \
		--fqbn $(FQBN) \
		--build-path $(BUILD_DIR) \
		$(CURDIR)

upload:
	arduino-cli upload \
		--config-file $(CLI_YAML) \
		--fqbn $(FQBN) \
		$(OPTIONS) \
		--input-dir $(BUILD_DIR) \
		--port $(PORTWRITE)

monitor:
	arduino-cli monitor \
		--config-file $(CLI_YAML) \
		--fqbn $(FQBN) \
		--port $(PORT) \
		--config baudrate=$(MONITORSPEED)

burn-bootloader:
	arduino-cli burn-bootloader -v \
		--config-file $(CLI_YAML) \
		--fqbn $(FQBN) \
		$(OPTIONS) \
		--port $(PORTWRITE)

print-size:
	avr-nm -t d --size-sort --print-size $(BUILD_DIR)/$(SKETCH).elf

put: clean build upload
```

# .dir-locals.el

```.dir-locals.el
((c++-mode
  . ((eglot-server-programs
      . ((c++-mode
          . (
             "arduino-language-server"
             "-clangd"     "/opt/homebrew/opt/llvm/bin/clangd"
             "-cli"        "/opt/homebrew/bin/arduino-cli"
             "-cli-config" "/home/sweethome/src/arduino/cli-config/attiny.yaml"
             "-fqbn"       "ATTinyCore:avr:attinyx5opti")))))))
```

# おわりに

esp32のノリでnewしたりfloat使ったりオシャレなdebug logを出そうとすると過積載で死にますのでご注意。