-
VScode AVR 개발환경 구축개발환경 구축 2023. 2. 16. 20:22
아두이노는 c++11표준을 따르며, 컴파일러는 avr-gcc 7.3.0로 상당히 옛날 버전을 사용하고 있다. Microchip Stdio 설치하는데도 한참 걸리고, UI도 그렇게 맘에 들지 않는데 이번 기회에 높은 버전의 avr-g++도 사용해볼 겸, VScode AVR 개발환경을 구축해볼 것이다.(gcc 컴파일러 다운로드하면 g++도 포함되어있다.)
아두이노의 컴파일러를 사용할 것이라면 아두이노 IDE 폴더를 열고 hardware/tool/avr/bin에 있는 컴파일러를 사용하면 된다.
설치
AVR-GCC 다운로드
https://github.com/ZakKemble/avr-gcc-build/releases
Releases · ZakKemble/avr-gcc-build
Contribute to ZakKemble/avr-gcc-build development by creating an account on GitHub.
github.com
컴파일러 압축을 푼 뒤, bin폴더를 열어보면 avr-gcc, avr-g++ 컴파일러와 avrdude가 있는 것을 확인할 수 있다.
avrdude는 아두이노에서 보드에 hex파일을 업로드할 때 사용하는 프로그램으로 Arduino ISP를 사용할 수 있어 따로 ISP/ASP 장치를 살 필요가 없다. 여기경로를 환경 변수 Path에 추가하면 된다.
직접 시스템 속성을 찾아가도 되지만 cmd 명령어로 추가하려면
1.추가할 경로 복사
2.win + R 누른 후 cmd 입력
3. Ctrl + Shift+ Enter 누르기(관리자 권한으로 실행, 관리자 권한이 아니면 접근 거부된다.)
4. 아래 명령어 실행
setx Path "%Path%;(추가할 경로)" -m #ex setx Path "%Path%;D:\2.Program\avr-gcc-12.1.0-x64-windows\bin" -m
avrdude의 버전을 확인해보면 7.0인데 글을 쓴 시점기준으로 7.1이 최신이니 업그레이드 하고 싶다면 아래에서 다운로드 후 컴파일러에 포함된 avrdude를 덮어쓰기 하면된다.
https://github.com/avrdudes/avrdude/releases
Releases · avrdudes/avrdude
AVRDUDE is a utility to program AVR microcontrollers - avrdudes/avrdude
github.com
참고로 GUI 버전인 AVRDUDESS도 있다.
https://github.com/ZakKemble/AVRDUDESS
GitHub - ZakKemble/AVRDUDESS: A GUI for AVRDUDE
A GUI for AVRDUDE. Contribute to ZakKemble/AVRDUDESS development by creating an account on GitHub.
github.com
Blink.cpp 업로드
원하는 곳에 blink 폴더를 만들고 VScode로 연다. F1을 누른뒤 edit를 입력한다.
이 명령어를 실행해서 c_cpp_properties.json을 열고 아래 설정을 추가한다.(컴파일러 경로는 본인에게 맞게 수정!)
"compilerPath": "D:/2.Program/avr-gcc-12.1.0-x64-windows/bin/avr-g++.exe",
만약 VScode로 AVR만 코딩한다면, 기본 컴파일러를 avr-gcc or avr-g++로 바꿔서 c_cpp_properties.json 으로 컴파일러를 지역적으로 설정하는 것을 스킵할 수 있다.
Microchip Stdio와 다르게 #define으로 어떤 AVR을 사용할 지 정의해주어야 DDRB같은 레지스터를 제대로 사용할 수 있다. avr/io.h를 F12눌러서 확인해보면 무엇을 정의해야하는 지 알 수 있을 것이다.
avr-g++ blink.cpp -o blink.hex -O2 avrdude -c arduino -P COM3 -b 115200 -p atmega328p -U flash:w:blink.hex
VScode에서 콘솔을 열고 다음 명령어를 자신의 상황에 맞춰서 업로드 하면 제대로 blink가 실행되는 것을 확인할 수 있다.
#참고로 인수와 타겟의 순서는 하고싶은데로 바꿔도 된다. avr-g++ -o blink.hex -O2 blink.cpp
avrdude 설정
https://www.ladyada.net/learn/avr/avrdude.html
AVR Tutorial - AVRDUDE
OK now you have a target board and a programmer next you will use the software you installed in step 2 to talk to the chip. This software is very powerful but its also difficult to use the first time. However, you should persevere and after a few times it
www.ladyada.net
Options: -p <partno> Required. Specify AVR device. -b <baudrate> Override RS-232 baud rate. -B <bitclock> Specify JTAG/STK500v2 bit clock period (us). -C <config-file> Specify location of configuration file. -c <programmer> Specify programmer type. -D Disable auto erase for flash memory -i <delay> ISP Clock Delay [in microseconds] -P <port> Specify connection port. -F Override invalid signature check. -e Perform a chip erase. -O Perform RC oscillator calibration (see AVR053). -U <memtype>:r|w|v:<filename>[:format] Memory operation specification. Multiple -U options are allowed, each request is performed in the order specified. -n Do not write anything to the device. -V Do not verify. -t Enter terminal mode. -E <exitspec>[,<exitspec>] List programmer exit specifications. -x <extended_param> Pass <extended_param> to programmer. -v Verbose output. -v -v for more. -q Quell progress output. -q -q for less. -l logfile Use logfile rather than stderr for diagnostics. -? Display this usage.
제일 중요한 -U 옵션부분에서 mentype은 flash, eeprom, hfuse, lfuse, efuse가 있으며, hex/elf 파일인 경우는 flash를 사용하면 된다.
r/w/v는 Read, Write, Verify에 해당하며, 뒤에 포멧은 선택사항이다.
보통 i(Intel hex file)을 사용한다고 적혀있는데, 해보니 안됬다. auto가 기본값이긴 하지만 :a로 명시해줄 수 도 있다.
-U 옵션은 여러개를 쓸 수 있어, 한번에 eeprom 및 퓨즈비트까지 설정할 수 있다.(퓨즈비트 잘못건드리면 AVR이 실리콘 덩어리로 바뀌니 함부로 건들지 말자)
Makefile
CC=avr-g++ CFLAGS=-std=c++17 -g -Wall -O2 TARGET=blink.hex SRCDIR= src BUILDDIR= build SRC = $(wildcard ./*.cpp) $(wildcard $(SRCDIR)/*.cpp) OBJS = $(subst .cpp,.o,$(SRC)) #avrdude PROGRMER = arduino COMPORT = COM3 BAUTRATE = 115200 AVR = atmega328p DUDEFLAGS = -c $(PROGRMER) -P $(COMPORT) -b $(BAUTRATE) -p $(AVR) all: dir $(BUILDDIR)/$(TARGET) dir: $(BUILDDIR) $(BUILDDIR): mkdir -p $(BUILDDIR) $(BUILDDIR)/$(TARGET): $(BUILDDIR)/$(OBJS) $(CC) $(BUILDDIR)/$(OBJS) -o $@ $(CFLAGS) $(BUILDDIR)/$(OBJS): $(SRC) $(CC) $^ -c -o $@ $(CFLAGS) .PHONY:clean clean: rm -rf build .PHONY:upload upload: avrdude $(DUDEFLAGS) -U flash:w:$(BUILDDIR)/$(TARGET)
업로드할 때 명령어치기가 귀찮으니 Makefile로 자동화 해보았다.
'개발환경 구축' 카테고리의 다른 글
[WSL]윈도우에서 리눅스 시스템 사용하기 (2) 2023.12.22 ESP-IDF VScode Window 개발환경 구축 (0) 2023.06.28 VScode 라즈베리파이 피코 Windows C/C++ 개발환경 구축 (0) 2023.02.20 윈도우 콘솔 시리얼 통신 (0) 2023.02.20 윈도우 C/C++ 개발 환경 구축 (0) 2023.02.14