C-code AVR toolchains for Mac

Try1: osx-cross/homebrew-avr -> FAILED see Try2

Recent avr-gcc version

$ which avr-gcc
/usr/local/CrossPack-AVR/bin/avr-gcc
$ avr-gcc -v
gcc version 4.8.1 (GCC) 

Install homebrew-avr

$ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates

latest version of avr-gcc

$ brew tap osx-cross/avr
$ brew install avr-gcc

version

$ which avr-gcc
/usr/local/bin/avr-gcc
$ avr-gcc -v
gcc version 9.3.0 (Homebrew AVR GCC 9.3.0) 

$PATH

$ printenv PATH
/usr/local/bin:
/usr/local/CrossPack-AVR/bin:

About gcc 9.3.0

Make shellscript

コンパイル

  • 以下のコマンドで,AVRに書き込むための.hexファイルが作成できます.
avr-gcc -mmcu=atmega88 -Wall -O2 -o a.out led_test.c
avr-objcopy -O ihex -R .eeprom a.out led_test.hex

まず,avr-gccでコンパイルして,objcopyで書き込むためのhex形式のファイルに変換しています.

Try to make .o from .c

$ cd Desktop/c
ls
hello.c
$ avr-gcc -mmcu=attiny1614 -Wall -O2 -o a.out hello.c
In file included from hello.c:1:
/usr/local/Cellar/avr-gcc/9.3.0/avr/include/avr/io.h:581:6: warning: #warning "device type not defined" [-Wcpp]
  581 | #    warning "device type not defined"
      |      ^~~~~~~
collect2: error: ld returned 1 exit status

or

$ avr-gcc -mmcu=atmega1614 -Wall -O2 -o a.out hello.c
avr-gcc: fatal error: cannot read spec file 'device-specs/specs-atmega1614': No such file or directory
compilation terminated.
  • Faile

reason: /usr/local/Cellar/avr-gcc/9.3.0/avr/include/avr/io.h dese not have attiny1614 device file

$ cd
$ cd /usr/local/Cellar/avr-gcc/9.3.0/avr/include/avr
$ ls
io.h
$ nano io.h

attiny1614 is not included

in the io.h file, it said…

#elif defined (__AVR_DEV_LIB_NAME__)
#  define __concat__(a,b) a##b
#  define __header1__(a,b) __concat__(a,b)
#  define __AVR_DEVICE_HEADER__ <avr/__header1__(io,__AVR_DEV_LIB_NAME__).h>
#  include __AVR_DEVICE_HEADER__

need to find how to add my_IC.h

Try2: Use Arduino IDE’s avr-gcc

$ cd
$ cd ~/../../Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/
$ ls 
avr-gcc
$ pwd
/Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/
$ cd ~/Desktop/c-code
$ ls
t1614.simpleblink.c
$ pwd
/Users/yuichitamiya/Desktop/c-code
$ ~/../../Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-gcc -mmcu=attiny3216 -Wall -O2 -o a.out t1614.simpleblink.c
$ ls
a.out
t1614.simpleblink.c
$ which avr-objcopy
/usr/local/bin/avr-objcopy
$ avr-objcopy -O ihex -R .eeprom a.out t1614.simpleblink.c.hex
$ ls
a.out
t1614.simpleblink.c
t1614.simpleblink.c.hex

Script example

  • commandline
$ avr-gcc -mmcu=atmega88 -Wall -O2 -o a.out led_test.c
$ avr-objcopy -O ihex -R .eeprom a.out led_test.hex
  • shellscript example
    • gcc-m88.sh for mac
#!/bin/sh
avr-gcc -mmcu=atmega88 -Wall -O2 -o a.out $*
avr-objcopy -O ihex -R .eeprom a.out ${1%.*}.hex
  • ref.gcc manual
  • -mmcu=mcu
    • machine-option
  • Wall
    • Warning all
  • -O2
    • Optimization
    • ‘-O2’ turns on all optimization flags specified by ‘-O’
  • o file
    • Place output in file file.

make own shellscript

  • name: c2hex.sh
#!/bin/sh
~/../../Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-gcc -mmcu=attiny3216 -Wall -O2 -o a.out $*
avr-objcopy -O ihex -R .eeprom a.out ${1%.*}.hex
  • make script
$ pwd
/Users/yuichitamiya/Desktop/c-code
$ touch c2hex.sh
$ nano c2hex.sh

COPY&PASTE
#!/bin/sh
~/../../Applications/Arduino.app/Contents/Java/hardware/tools/avr/bin/avr-gcc -mmcu=attiny3216 -Wall -O2 -o a.out $*
avr-objcopy -O ihex -R .eeprom a.out ${1%.*}.hex

control x save yes name c2hex.sh ok
$ chmod +x c2hex.sh
$ ls
t1614.simpleblink.c
c2hex.sh
$ ./c2hex.sh t1614.simpleblink.c
$ls
a.out
t1614.simpleblink.c
t1614.simpleblink.c.hex
c2hex.sh
  • PATHの通った~/binに移動する
$ ls
c2hex.sh
$ mv c2hex.sh ~/bin
$ cd ~/bin
$ ls
c2hex.sh

Useage

$ cd Desktop/c-code
$ ls 
t1614.simpleblink.c
$ c2hex.sh t1614.simpleblink.c
$ ls
t1614.simpleblink.c
t1614.simpleblink.hex
# ファイルをつくる
$ touch hello.sh
# ファイルの中身を書く
$ echo "echo 'hello world'" > hello.sh
# シェルスクリプトを実行
$ hello.sh
-bash: ./hello.sh: Permission denied
# 実行権限を与える
$ chmod +x hello.sh
$ hello.sh
hello world
# ファイル名を.commandに変更するとダブルクリックで実行できる(しかし入力ファイル.cの指定ができない)
$ mv hello.sh hello.command