202 lines
5.8 KiB
Plaintext
202 lines
5.8 KiB
Plaintext
|
AVR: Alf and Regards's RISC Processor
|
||
|
|
||
|
Installing AVR toolchain:
|
||
|
|
||
|
Basic requirements:
|
||
|
|
||
|
gcc-avr : a GNU C cross-compiler for specifically for AVR
|
||
|
|
||
|
avr-libc: it is a package for AVR C library
|
||
|
more info: https://www.nongnu.org/avr-libc/user-manual/index.html
|
||
|
https://exploreembedded.com/wiki/AVR_C_Library
|
||
|
|
||
|
|
||
|
avrdude: it is a utility that transfers codes from the UBUNTU to the microcontroller. AVR downloader uploader: software(utility program) for downloading and uploading on-chip memories of Microchip's AVR microcontrollers
|
||
|
|
||
|
arduino CLI: Arduino Command LIne Interface- Arduino CLI is a command line tool that contains all you need to easily build applications around the Arduino ecosystem.
|
||
|
|
||
|
|
||
|
Basic installation commands:
|
||
|
sudo apt-get install gcc-avr binutils-avr avr-libc
|
||
|
sudo apt-get install avrdude
|
||
|
sudo apt-get install arduino-cli or snap install arduino-cli
|
||
|
|
||
|
More info about installation: https://www.nongnu.org/avr-libc/user-manual/install_tools.html
|
||
|
|
||
|
|
||
|
|
||
|
Basic processes involved:
|
||
|
1)Compilation which creates object files
|
||
|
2)Linking the object files to create ELF files(Executable and Linkable Format)
|
||
|
3)Object-copy(assembly) to create hex files
|
||
|
4)Flashing(dumping) the hex file on the board using avrdude
|
||
|
|
||
|
|
||
|
more info: https://www.avrfreaks.net/forum/avr-gcc-compiling-linking-and-assembly --(pic in this website)--
|
||
|
|
||
|
|
||
|
Instructions to compile the code:
|
||
|
Compile and linking Instruction(generating object files and linking them):
|
||
|
avr-gcc -Os -mmcu=atmega2560 -c -o <destination_file-blinking_led> <source.c> //avoid using '-c' to avoid improper linking
|
||
|
|
||
|
Object Copy Instruction:
|
||
|
avr-objcopy -O ihex -R .eeprom <destination_file-blinking_led> <file_name.hex>
|
||
|
|
||
|
Programming(flashing the hex file on board):
|
||
|
avrdude -C /usr/share/arduino/hardware/tools/avrdude.conf -patmega2560 -cwiring -P /dev/ttyACM0 -b 115200 -D -U flash:w:<file_name.hex>:i
|
||
|
|
||
|
|
||
|
|
||
|
Execution using make:
|
||
|
|
||
|
GNU make:
|
||
|
The "make" utility automates the mundane aspects of building executable from source code. "make" uses a so-called makefile, which contains rules on how to build the executables. Make is different from a script as a script shows no intelligence. All instructions run blindly, without dpending on the consequences of the previous instructions.
|
||
|
|
||
|
more info: https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html#zz-1.6
|
||
|
makefile youtube tutorial: https://www.youtube.com/playlist?list=PLNmACol6lYY7Dzvg7jKgvMdDaDEDFnNqD
|
||
|
|
||
|
based on the makefile created..
|
||
|
//change parameters accordingly in makefile to make sure the correct files get executed
|
||
|
Instructions:
|
||
|
make all
|
||
|
make flash
|
||
|
|
||
|
Makefile:
|
||
|
# simple AVR Makefile
|
||
|
#
|
||
|
# written by michael cousins (http://github.com/mcous)
|
||
|
# released to the public domain
|
||
|
|
||
|
# Makefile
|
||
|
#
|
||
|
# targets:
|
||
|
# all: compiles the source code
|
||
|
# test: tests the isp connection to the mcu
|
||
|
# flash: writes compiled hex file to the mcu's flash memory
|
||
|
# fuse: writes the fuse bytes to the MCU
|
||
|
# disasm: disassembles the code for debugging
|
||
|
# clean: removes all .hex, .elf, and .o files in the source code and library directories
|
||
|
|
||
|
# parameters (change this stuff accordingly)
|
||
|
# project name
|
||
|
PRJ = main
|
||
|
# avr mcu
|
||
|
MCU = atmega2560
|
||
|
# mcu clock frequency
|
||
|
CLK = 16000000
|
||
|
# avr programmer (and port if necessary)
|
||
|
# e.g. PRG = usbtiny -or- PRG = arduino -P /dev/tty.usbmodem411
|
||
|
PRG = wiring -P /dev/ttyACM0
|
||
|
# fuse values for avr: low, high, and extended
|
||
|
# these values are from an Arduino Uno (ATMega328P)
|
||
|
# see http://www.engbedded.com/fusecalc/ for other MCUs and options
|
||
|
LFU = 0xFF
|
||
|
HFU = 0xD0
|
||
|
EFU = 0xFD
|
||
|
# program source files (not including external libraries)
|
||
|
SRC = $(PRJ).c
|
||
|
# where to look for external libraries (consisting of .c/.cpp files and .h files)
|
||
|
# e.g. EXT = ../../EyeToSee ../../YouSART
|
||
|
EXT =
|
||
|
|
||
|
|
||
|
#################################################################################################
|
||
|
# \/ stuff nobody needs to worry about until such time that worrying about it is appropriate \/ #
|
||
|
#################################################################################################
|
||
|
|
||
|
# include path
|
||
|
INCLUDE := $(foreach dir, $(EXT), -I$(dir))
|
||
|
# c flags
|
||
|
CFLAGS = -Wall -Os -DF_CPU=$(CLK) -mmcu=$(MCU) $(INCLUDE)
|
||
|
# any aditional flags for c++
|
||
|
CPPFLAGS =
|
||
|
|
||
|
# executables
|
||
|
AVRDUDE = avrdude -C /usr/share/arduino/hardware/tools/avrdude.conf -c $(PRG) -p $(MCU) -b 115200 -D
|
||
|
OBJCOPY = avr-objcopy
|
||
|
OBJDUMP = avr-objdump
|
||
|
SIZE = avr-size --format=avr --mcu=$(MCU)
|
||
|
CC = avr-gcc
|
||
|
|
||
|
# generate list of objects
|
||
|
CFILES = $(filter %.c, $(SRC))
|
||
|
EXTC := $(foreach dir, $(EXT), $(wildcard $(dir)/*.c))
|
||
|
CPPFILES = $(filter %.cpp, $(SRC))
|
||
|
EXTCPP := $(foreach dir, $(EXT), $(wildcard $(dir)/*.cpp))
|
||
|
OBJ = $(CFILES:.c=.o) $(EXTC:.c=.o) $(CPPFILES:.cpp=.o) $(EXTCPP:.cpp=.o)
|
||
|
|
||
|
# user targets
|
||
|
# compile all files
|
||
|
all: $(PRJ).hex
|
||
|
|
||
|
# test programmer connectivity
|
||
|
test:
|
||
|
$(AVRDUDE) -v
|
||
|
|
||
|
# flash program to mcu
|
||
|
flash: all
|
||
|
$(AVRDUDE) -U flash:w:$(PRJ).hex:i
|
||
|
|
||
|
# write fuses to mcu
|
||
|
fuse:
|
||
|
$(AVRDUDE) -U lfuse:w:$(LFU):m -U hfuse:w:$(HFU):m -U efuse:w:$(EFU):m
|
||
|
|
||
|
# generate disassembly files for debugging
|
||
|
disasm: $(PRJ).elf
|
||
|
$(OBJDUMP) -d $(PRJ).elf
|
||
|
|
||
|
# remove compiled files
|
||
|
clean:
|
||
|
rm -f *.hex *.elf *.o
|
||
|
$(foreach dir, $(EXT), rm -f $(dir)/*.o;)
|
||
|
|
||
|
# other targets
|
||
|
# objects from c files
|
||
|
.c.o:
|
||
|
$(CC) $(CFLAGS) -c $< -o $@
|
||
|
|
||
|
# objects from c++ files
|
||
|
.cpp.o:
|
||
|
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
|
||
|
|
||
|
# elf file
|
||
|
$(PRJ).elf: $(OBJ)
|
||
|
$(CC) $(CFLAGS) -o $(PRJ).elf $(OBJ)
|
||
|
|
||
|
# hex file
|
||
|
$(PRJ).hex: $(PRJ).elf
|
||
|
rm -f $(PRJ).hex
|
||
|
$(OBJCOPY) -j .text -j .data -O ihex $(PRJ).elf $(PRJ).hex
|
||
|
$(SIZE) $(PRJ).elf
|
||
|
|
||
|
|
||
|
main.c file for blinking of led code:
|
||
|
#include <avr/io.h>
|
||
|
#define F_CPU 16000000//16MHz
|
||
|
#define MS_DELAY 1000//1 second=1000 ms
|
||
|
#include <util/delay.h>
|
||
|
|
||
|
int main(void)
|
||
|
{
|
||
|
//pin 37-port C0
|
||
|
DDRC = 0xff;
|
||
|
PORTC = 0xff;
|
||
|
|
||
|
while(1)
|
||
|
{
|
||
|
PORTC = 0x01;
|
||
|
_delay_ms(1000);//1 second delay
|
||
|
PORTC = 0x00;
|
||
|
_delay_ms(1000);//1 second delay
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|