0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2024-10-19

はじめに

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

想定環境

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

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

インストールと初期設定

Arduino-CLIのインストール

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

$ brew install arduino-cli

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

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

$ 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では使えません)

$ 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

ボードのインストール

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

platform.txtの書き換え

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

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

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を出そうとすると過積載で死にますのでご注意。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?