Skip to content

Commit d8f4aee

Browse files
committed
AVR examples added avr-uart-driver library
1 parent 673fa70 commit d8f4aee

44 files changed

Lines changed: 4408 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
**/*.elf
3+
**/*.bin
4+
**/*.hex
5+
**/*.o
6+
**/*.d
7+
**/*.lss
8+
**/*.map
9+
**/*.sym
10+
**/*.su
11+
**/*.tmp
12+
**/*.optimized
13+
**/*.pdf

examples/avr/basic_usage/lib/avr-uart-driver/LICENSE

Lines changed: 504 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# avr-uart-driver
2+
3+
UART low level driver for AVR devices.
4+
5+
## Supported Devices
6+
7+
The driver has been developed to keep a flexible and simple way to add support for new devices by writting specific Hardware Abstraction Layer C++ class for each microcontroller.
8+
9+
List of current AVR devices that are supported:
10+
11+
- ATmega48A/48PA/88A/88PA/168A/168PA/328/328P.
12+
- ATmega640/1280/1281/2560/2561.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# avr-uart-driver echo example
2+
3+
UART low level driver for AVR devices "echo" example that reads all bytes received from an UART and write it back to it.
4+
5+
## Requirements
6+
7+
- Install required toolchain components:
8+
9+
```bash
10+
sudo apt-get update
11+
sudo apt-get -y install binutils
12+
sudo apt-get -y install make
13+
sudo apt-get -y install gcc-avr
14+
sudo apt-get -y install avr-libc
15+
sudo apt-get -y install cflow
16+
```
17+
18+
- Install Flashing Tool:
19+
20+
```bash
21+
sudo apt-get -y install avrdude
22+
```
23+
24+
- Give execute permissions to tools scripts:
25+
26+
```bash
27+
chmod +x vscode-skelly-avr/tools/*
28+
```
29+
30+
## Usage
31+
32+
Build:
33+
34+
1. Physically connect the hardware programmer to target device and the PC.
35+
36+
2. Using a shell, go to **build/** directory and run **make** to see build instructions.
37+
38+
3. Run **make build DEVICE=X F_CPU=X** to launch the Build.
39+
40+
4. Run **make flash PROGRAMMER=X DEVICE=X F_CPU=X** to build and flash the firmware into target device.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Ignore everything in this directory
2+
*
3+
4+
# Except this file
5+
!.gitignore
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
2+
# Specify project name and default compilers
3+
PROJECTNAME = uartecho
4+
5+
# UART Driver path
6+
UARTDRIVERPATH = $(WORKSPACE)/../../src
7+
8+
# Project root WorkSpace
9+
WORKSPACE = ..
10+
11+
# Build and Binary Release directories
12+
BUILDDIR = $(WORKSPACE)/build
13+
BINDIR = $(WORKSPACE)/bin
14+
15+
# Specify target Microcontroller
16+
ifndef $(DEVICE)
17+
DEVICE = none
18+
endif
19+
20+
# Specify MCU CPU Frequency
21+
ifndef $(F_CPU)
22+
F_CPU = 16000000UL
23+
endif
24+
25+
# Low, High and Extended Fuses Values
26+
ifndef $(LFU)
27+
LFU = "none"
28+
endif
29+
ifndef $(LFU)
30+
HFU = "none"
31+
endif
32+
ifndef $(LFU)
33+
EFU = "none"
34+
endif
35+
36+
# Avrdude Programmer
37+
ifndef $(PROGRAMMER)
38+
PROGRAMMER = usbasp
39+
endif
40+
41+
###############################################################################
42+
43+
# Get actual date and setup output binary directory name
44+
#DATE = $(shell date '+%Y_%m_%d_%H_%M_%S')
45+
DATE = $(shell date '+%Y_%m_%d')
46+
RELEASEDIR = $(WORKSPACE)/bin/$(DATE)
47+
48+
# Specify Sources files (Automatic search in specific directories)
49+
SRCS = $(shell find $(WORKSPACE)/src -type f -name *.c)
50+
SRCS += $(shell find $(WORKSPACE)/src -type f -name *.cpp)
51+
SRCS += $(shell find $(WORKSPACE)/lib -type f -name *.c)
52+
SRCS += $(shell find $(WORKSPACE)/lib -type f -name *.cpp)
53+
SRCS += $(shell find $(UARTDRIVERPATH) -type f -name *.c)
54+
SRCS += $(shell find $(UARTDRIVERPATH) -type f -name *.cpp)
55+
56+
# Specify Headers files (Automatic search in specific directories)
57+
HEADS = $(shell find $(WORKSPACE)/inc -type f -name *.h)
58+
HEADS += $(shell find $(WORKSPACE)/inc -type f -name *.hpp)
59+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.h)
60+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.hpp)
61+
HEADS += $(shell find $(WORKSPACE)/lib -type f -name *.h)
62+
HEADS += $(shell find $(WORKSPACE)/lib -type f -name *.hpp)
63+
HEADS += $(shell find $(UARTDRIVERPATH) -type f -name *.h)
64+
HEADS += $(shell find $(UARTDRIVERPATH) -type f -name *.hpp)
65+
66+
# Get Headers directories from headers paths
67+
HEADERSDIR = $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-I{}")
68+
HEADERSDIR += $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-L{}")
69+
70+
# Get objects files from sources and output object
71+
_OBJS = $(SRCS:.c=.o)
72+
OBJS = $(_OBJS:.cpp=.o)
73+
BUILDOBJS = $(shell echo $(OBJS) | xargs -n1 basename | xargs -n1 -i echo "$(BUILDDIR)/{}")
74+
75+
###############################################################################
76+
77+
# Specify default compilers and tools
78+
CC = avr-gcc
79+
CXX = avr-g++
80+
AVRDUDE = avrdude -c $(PROGRAMMER) -p $(DEVICE)
81+
AVRDUDE_ARDUINO = avrdude -c arduino -b 115200 -P /dev/ttyUSB0 -p $(DEVICE)
82+
OBJCOPY = avr-objcopy
83+
OBJDUMP = avr-objdump
84+
SIZE = avr-size --format=avr --mcu=$(DEVICE)
85+
86+
# Setup compilation flags
87+
CFLAGS = -Os -Wall -g -fstack-usage -flto
88+
CFLAGS += '-mmcu=$(DEVICE)'
89+
CFLAGS += '-DF_CPU=$(F_CPU)'
90+
ifeq ($(TEST), 1)
91+
CFLAGS += -DTEST
92+
endif
93+
CFLAGS += $(HEADERSDIR)
94+
CXXFLAGS = $(CFLAGS)
95+
96+
# Flash Section to place the binary
97+
# Application placed at "Flash_Start" (0x0000)
98+
# Bootloader placed at "Flash_End" - "Boot_Section_Size"
99+
# Boot Section size must be defined with BOOTSZ0 and BOOTSZ1 fuses
100+
# Example:
101+
# Device of 128KB Flash with 4KB Boot Section: (128-4)*1024 = 0x1F000
102+
ifeq ($(BOOTLOADER), 1)
103+
PRJ = 'fw-boot-$(PROJECTNAME)'
104+
FLASHSECTION = --section-start=.text=0x1F000
105+
else
106+
PRJ = 'fw-app-$(PROJECTNAME)'
107+
FLASHSECTION = --section-start=.text=0x00000
108+
BOOTLOADER=0
109+
endif
110+
111+
# Linker flags
112+
LDFLAGS = '-Wl,$(FLASHSECTION)'
113+
114+
# Compile instruction
115+
COMPILE_C = $(CC) $(CFLAGS) $(LDFLAGS)
116+
COMPILE_CXX = $(CXX) $(CXXFLAGS) $(LDFLAGS)
117+
118+
###############################################################################
119+
120+
help:
121+
@cat README.md
122+
123+
# Target: make all (build project generating output directory)
124+
build: $(PRJ).bin
125+
rm -f ${RELEASEDIR}/*
126+
mkdir -p ${RELEASEDIR}
127+
cp -a $(PRJ).elf $(RELEASEDIR)
128+
cp -a $(PRJ).hex $(RELEASEDIR)
129+
cp -a $(PRJ).bin $(RELEASEDIR)
130+
@echo "ADDRESS SIZE FUNCTION FILE:LINE\n" > memory_program.txt
131+
@$(NM) $(PRJ).elf >> memory_program.txt
132+
@chmod +x $(BUILDDIR)/../tools/*
133+
@$(BUILDDIR)/../tools/memusageram $(BUILDDIR)/memory_ram.txt >/dev/null 2>&1
134+
@rm -f $(BUILDDIR)/*.su
135+
@mkdir -p $(BUILDDIR)/obj
136+
@mv $(BUILDDIR)/*.o $(BUILDDIR)/obj/
137+
$(SIZE) $(PRJ).elf
138+
@if [ ${BOOTLOADER} = "1" ]; then\
139+
echo "Bootloader Built";\
140+
else\
141+
echo "Application Built";\
142+
fi
143+
@$(BUILDDIR)/../tools/binary_size $(PRJ).bin
144+
@echo ""
145+
146+
# Target: make clean (remove all previously builds)
147+
clean:
148+
rm -rf $(BUILDDIR)/obj
149+
rm -f $(BUILDDIR)/*.su
150+
rm -f $(BUILDDIR)/$(PRJ).elf
151+
rm -f $(BUILDDIR)/$(PRJ).hex
152+
rm -f $(BUILDDIR)/$(PRJ).bin
153+
rm -f $(BUILDDIR)/dump.bin
154+
rm -f $(BUILDDIR)/dump.hex
155+
rm -f $(BUILDDIR)/memory_program.txt
156+
rm -f $(BUILDDIR)/memory_ram.txt
157+
158+
# Target: make rebuild (clean previously builds and build again)
159+
rebuild: clean build
160+
161+
# Flash program to MCU
162+
flash: build
163+
$(AVRDUDE) -U flash:w:$(PRJ).hex:i
164+
165+
# Flash program to MCU
166+
flash_arduino: build
167+
$(AVRDUDE_ARDUINO) -U flash:w:$(PRJ).hex:i
168+
169+
# Write fuses to MCU
170+
fuse:
171+
$(AVRDUDE) -U lfuse:w:$(LFU):m -U hfuse:w:$(HFU):m -U efuse:w:$(EFU):m
172+
173+
# Read flash memory from MCU
174+
dump:
175+
rm -f $(BUILDDIR)/dump.bin
176+
rm -f $(BUILDDIR)/dump.hex
177+
$(AVRDUDE) -U flash:r:dump.bin:r
178+
$(OBJCOPY) -I binary -O ihex dump.bin dump.hex
179+
180+
# Target: check (custom target to check build variables)
181+
check:
182+
@echo "HEADERS:"
183+
@echo " $(HEADS)"
184+
@echo "SRCS:"
185+
@echo " $(SRCS)"
186+
@echo "BUILD FLAGS:"
187+
@echo " $(CFLAGS)"
188+
@echo "LINK FLAGS:"
189+
@echo " $(LDFLAGS)"
190+
@echo "OBJS:"
191+
@echo " $(BUILDOBJS)"
192+
@echo "BINDIR:"
193+
@echo " $(BINDIR)"
194+
195+
###############################################################################
196+
197+
# Target for generate BIN file from .elf file
198+
$(PRJ).bin: $(PRJ).hex
199+
rm -f $(PRJ).bin
200+
$(OBJCOPY) -j .text -j .data -O binary $(PRJ).elf $(PRJ).bin
201+
202+
# Target for generate HEX file from .elf file
203+
$(PRJ).hex: $(PRJ).elf
204+
rm -f $(PRJ).hex
205+
$(OBJCOPY) -j .text -j .data -O ihex $(PRJ).elf $(PRJ).hex
206+
207+
# Target for generate ELF file linking all .o files
208+
$(PRJ).elf: $(OBJS)
209+
$(COMPILE_C) -o $(PRJ).elf $(BUILDOBJS)
210+
211+
# Target for generate object file of each .c file
212+
%.o: %.c
213+
$(COMPILE_C) -c $<
214+
215+
# Target for generate object file of each .cpp file
216+
%.o: %.cpp
217+
$(COMPILE_CXX) -c $<
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Build Help
2+
3+
```bash
4+
Usage: make <TARGET> [OPTION=]...
5+
Targets:
6+
help: Show build usage information (current file).
7+
check: Show build parameters.
8+
clean: Clean last built files.
9+
build: Build the project.
10+
rebuild: Force clean last built files and build again.
11+
flash: Build and write firmware to target device.
12+
fuses: Write fuses to target device.
13+
dump: Read target device flash memory into a dump.hex file.
14+
Options:
15+
DEVICE: Specify MCU device to build/flash (check list of availables with 'avrdude -c usbasp -p none').
16+
F_CPU: Specify MCU CPU Frequency to use (16MHz if not provided).
17+
PROGRAMMER: Specify avrdude programmer to use (check the list using 'avrdude -c none').
18+
LFU/HFU/EFU: Specify High/Low/Extended Fuses values to program.
19+
TEST: Build a Test Firmware (add TEST global Define to code).
20+
BOOTLOADER: Build as Bootloader.
21+
```
22+
23+
# Build Examples
24+
25+
```bash
26+
make help
27+
make check
28+
make clean
29+
make build DEVICE=atmega328p F_CPU=16000000L
30+
make build DEVICE=atmega328p F_CPU=16000000L BOOTLOADER=1
31+
make build DEVICE=t85 F_CPU=1000000L TEST=1
32+
make rebuild DEVICE=atmega328p
33+
make flash PROGRAMMER=usbasp # arduino, atmelice, jtagmkII, usbtiny, stk500...
34+
make fuses LFU=0xFF HFU=0xDE EFU=0xFD
35+
make dump
36+
```
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory project documentation files.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# About
2+
3+
Place in this directory project headers files.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
/**
3+
* @file constants.h
4+
* @author Jose Miguel Rios Rubio <jrios.github@gmail.com>
5+
* @date 26-01-2022
6+
* @version 1.0.0
7+
*
8+
* @section DESCRIPTION
9+
*
10+
* General Project constants.
11+
*
12+
* @section LICENSE
13+
*
14+
* Copyright (c) 2021 Jose Miguel Rios Rubio. All right reserved.
15+
*
16+
* This library is free software; you can redistribute it and/or
17+
* modify it under the terms of the GNU Lesser General Public
18+
* License as published by the Free Software Foundation; either
19+
* version 2.1 of the License, or (at your option) any later version.
20+
*
21+
* This library is distributed in the hope that it will be useful,
22+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
23+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24+
* Lesser General Public License for more details.
25+
*
26+
* You should have received a copy of the GNU Lesser General Public
27+
* License along with this library; if not, write to the Free Software
28+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29+
*/
30+
31+
/*****************************************************************************/
32+
33+
/* Include Guard */
34+
35+
#ifndef CONSTANTS_H_
36+
#define CONSTANTS_H_
37+
38+
/*****************************************************************************/
39+
40+
/* Constants & Defines */
41+
42+
// Default CPU Frequency to use if has not been provided
43+
#if !defined(F_CPU)
44+
#define F_CPU 16000000UL
45+
#endif
46+
47+
// Firmware Application Version
48+
#define FW_APP_VERSION "1.0.0"
49+
50+
// USART UART number to use (i.e. UART0)
51+
#define UART_NUM UART0
52+
53+
// UART Serial Speed Baud Rate
54+
#define UART_BAUD_RATE 9600
55+
56+
/*****************************************************************************/
57+
58+
#endif /* CONSTANTS_H_ */

0 commit comments

Comments
 (0)