Skip to content

Commit 0eea2d7

Browse files
committed
Added Example: AVR Basic Usage
1 parent 184f603 commit 0eea2d7

19 files changed

Lines changed: 817 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
build/memory_program.txt
3+
build/memory_ram.txt
4+
5+
**/*.elf
6+
**/*.bin
7+
**/*.hex
8+
**/*.o
9+
**/*.d
10+
**/*.lss
11+
**/*.map
12+
**/*.sym
13+
**/*.su
14+
**/*.tmp
15+
**/*.optimized
16+
**/*.pdf
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{
3+
"configurations": [
4+
{
5+
"name": "ARM-Cortex-M",
6+
"includePath": [
7+
"/lib/avr/include/**",
8+
"/lib/avr/x86_64-linux-gnu/include/**",
9+
"/usr/lib/gcc/avr/5.4.0/include",
10+
"${workspaceFolder}/**"
11+
],
12+
"defines": [
13+
"_AVR_",
14+
"_AVR",
15+
"AVR",
16+
"__AVR_ATmega328P__"
17+
],
18+
"compilerPath": "",
19+
"cStandard": "c99",
20+
"cppStandard": "c++98",
21+
"intelliSenseMode": "gcc-arm",
22+
"compilerArgs": [
23+
"-mmcu=atmega328p"
24+
]
25+
}
26+
],
27+
"version": 4
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Build and Debug",
6+
"type": "cppdbg",
7+
"request": "launch",
8+
"program": "${workspaceFolder}/build/firmware_app.elf",
9+
"stopAtEntry": true,
10+
"cwd": "${workspaceFolder}",
11+
"environment": [],
12+
"externalConsole": false,
13+
"MIMode": "gdb",
14+
"setupCommands": [
15+
{
16+
"description": "Enable print identation in gdb",
17+
"text": "-enable-pretty-printing",
18+
"ignoreFailures": true
19+
}
20+
],
21+
"preLaunchTask": "Build and Debug",
22+
"miDebuggerPath": "/usr/bin/avr-gdb"
23+
}
24+
]
25+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"cmake.configureOnOpen": false
3+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Build all",
6+
"group": "build",
7+
"type": "shell",
8+
"options": {
9+
"cwd": "${workspaceRoot}/build"
10+
},
11+
"command": "make",
12+
"args": []
13+
},
14+
{
15+
"label": "Build clean",
16+
"group": "build",
17+
"type": "shell",
18+
"options": {
19+
"cwd": "${workspaceRoot}"
20+
},
21+
"command": "make",
22+
"args": ["clean"]
23+
},
24+
{
25+
"label": "Flash all",
26+
"group": "build",
27+
"type": "shell",
28+
"options": {
29+
"cwd": "${workspaceRoot}"
30+
},
31+
"command": "make",
32+
"args": ["flash"]
33+
}
34+
]
35+
}

examples/avr/basic_usage/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# MinBaseCLI AVR Basic Usage Example
2+
3+
Basic usage example of MinBaseCLI library for AVR devices.
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 basic_usage/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: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
2+
# Specify project name and default compilers
3+
PROJECTNAME = minbasecli_basicusage
4+
5+
# UART Driver path
6+
MINBASECLIPATH = $(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+
54+
# MinBaseCLI Sources
55+
SRCS += $(MINBASECLIPATH)/hal/avr/minbasecli_avr.cpp
56+
SRCS += $(shell find $(MINBASECLIPATH)/hal/avr/avr-uart-driver/src -type f -name *.cpp)
57+
58+
# Specify Headers files (Automatic search in specific directories)
59+
HEADS = $(shell find $(WORKSPACE)/inc -type f -name *.h)
60+
HEADS += $(shell find $(WORKSPACE)/inc -type f -name *.hpp)
61+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.h)
62+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.hpp)
63+
HEADS += $(shell find $(WORKSPACE)/lib -type f -name *.h)
64+
HEADS += $(shell find $(WORKSPACE)/lib -type f -name *.hpp)
65+
66+
# MinBaseCLI Headers
67+
HEADS += $(MINBASECLIPATH)/minbasecli.h
68+
HEADS += $(MINBASECLIPATH)/hal/avr/minbasecli_avr.h
69+
HEADS += $(shell find $(MINBASECLIPATH)/hal/avr/avr-uart-driver/src -type f -name *.h)
70+
71+
# Get Headers directories from headers paths
72+
HEADERSDIR = $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-I{}")
73+
HEADERSDIR += $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-L{}")
74+
75+
# Get objects files from sources and output object
76+
_OBJS = $(SRCS:.c=.o)
77+
OBJS = $(_OBJS:.cpp=.o)
78+
BUILDOBJS = $(shell echo $(OBJS) | xargs -n1 basename | xargs -n1 -i echo "$(BUILDDIR)/{}")
79+
80+
###############################################################################
81+
82+
# Specify default compilers and tools
83+
CC = avr-gcc
84+
CXX = avr-g++
85+
AVRDUDE = avrdude -c $(PROGRAMMER) -p $(DEVICE)
86+
AVRDUDE_ARDUINO = avrdude -c arduino -b 115200 -P /dev/ttyUSB0 -p $(DEVICE)
87+
OBJCOPY = avr-objcopy
88+
OBJDUMP = avr-objdump
89+
NM = avr-nm --line-numbers --print-size --size-sort --radix=d
90+
SIZE = avr-size --format=avr --mcu=$(DEVICE)
91+
92+
# Setup compilation flags
93+
CFLAGS = -Os -Wall -g -fstack-usage -flto
94+
CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm
95+
CFLAGS += '-mmcu=$(DEVICE)'
96+
CFLAGS += '-DF_CPU=$(F_CPU)'
97+
ifeq ($(TEST), 1)
98+
CFLAGS += -DTEST
99+
endif
100+
CFLAGS += $(HEADERSDIR)
101+
CXXFLAGS = $(CFLAGS)
102+
103+
# Flash Section to place the binary
104+
# Application placed at "Flash_Start" (0x0000)
105+
# Bootloader placed at "Flash_End" - "Boot_Section_Size"
106+
# Boot Section size must be defined with BOOTSZ0 and BOOTSZ1 fuses
107+
# Example:
108+
# Device of 128KB Flash with 4KB Boot Section: (128-4)*1024 = 0x1F000
109+
ifeq ($(BOOTLOADER), 1)
110+
PRJ = 'fw_boot_$(PROJECTNAME)'
111+
FLASHSECTION = --section-start=.text=0x1F000
112+
else
113+
PRJ = 'fw_app_$(PROJECTNAME)'
114+
FLASHSECTION = --section-start=.text=0x00000
115+
BOOTLOADER=0
116+
endif
117+
118+
# Linker flags
119+
LDFLAGS = '-Wl,$(FLASHSECTION)'
120+
121+
# Compile instruction
122+
COMPILE_C = $(CC) $(CFLAGS) $(LDFLAGS)
123+
COMPILE_CXX = $(CXX) $(CXXFLAGS) $(LDFLAGS)
124+
125+
###############################################################################
126+
127+
help:
128+
@cat README.md
129+
130+
# Target: make all (build project generating output directory)
131+
build: $(PRJ).bin
132+
rm -f ${RELEASEDIR}/*
133+
mkdir -p ${RELEASEDIR}
134+
cp -a $(PRJ).elf $(RELEASEDIR)
135+
cp -a $(PRJ).hex $(RELEASEDIR)
136+
cp -a $(PRJ).bin $(RELEASEDIR)
137+
@echo "ADDRESS SIZE FUNCTION FILE:LINE\n" > memory_program.txt
138+
@$(NM) $(PRJ).elf >> memory_program.txt
139+
@../tools/memusageram $(BUILDDIR)/memory_ram.txt >/dev/null 2>&1
140+
@rm -f $(BUILDDIR)/*.su
141+
@mkdir -p $(BUILDDIR)/obj
142+
@mv $(BUILDDIR)/*.o $(BUILDDIR)/obj/
143+
$(SIZE) $(PRJ).elf
144+
@if [ ${BOOTLOADER} = "1" ]; then\
145+
echo "Bootloader Built";\
146+
else\
147+
echo "Application Built";\
148+
fi
149+
@../tools/binary_size $(PRJ).bin
150+
@echo ""
151+
152+
# Target: make clean (remove all previously builds)
153+
clean:
154+
rm -rf $(BUILDDIR)/obj
155+
rm -f $(BUILDDIR)/*.su
156+
rm -f $(BUILDDIR)/$(PRJ).elf
157+
rm -f $(BUILDDIR)/$(PRJ).hex
158+
rm -f $(BUILDDIR)/$(PRJ).bin
159+
rm -f $(BUILDDIR)/dump.bin
160+
rm -f $(BUILDDIR)/dump.hex
161+
rm -f $(BUILDDIR)/memory_program.txt
162+
rm -f $(BUILDDIR)/memory_ram.txt
163+
164+
# Target: make rebuild (clean previously builds and build again)
165+
rebuild: clean build
166+
167+
# Flash program to MCU
168+
flash: build
169+
$(AVRDUDE) -U flash:w:$(PRJ).hex:i
170+
171+
# Flash program to MCU
172+
flash_arduino: build
173+
$(AVRDUDE_ARDUINO) -U flash:w:$(PRJ).hex:i
174+
175+
# Write fuses to MCU
176+
fuse:
177+
$(AVRDUDE) -U lfuse:w:$(LFU):m -U hfuse:w:$(HFU):m -U efuse:w:$(EFU):m
178+
179+
# Read flash memory from MCU
180+
dump:
181+
rm -f $(BUILDDIR)/dump.bin
182+
rm -f $(BUILDDIR)/dump.hex
183+
$(AVRDUDE) -U flash:r:dump.bin:r
184+
$(OBJCOPY) -I binary -O ihex dump.bin dump.hex
185+
186+
# Target: check (custom target to check build variables)
187+
check:
188+
@echo ""
189+
@echo "HEADERS:"
190+
@echo " $(HEADS)"
191+
@echo ""
192+
@echo "SRCS:"
193+
@echo " $(SRCS)"
194+
@echo ""
195+
@echo "BUILD FLAGS:"
196+
@echo " $(CFLAGS)"
197+
@echo ""
198+
@echo "LINK FLAGS:"
199+
@echo " $(LDFLAGS)"
200+
@echo ""
201+
@echo "OBJS:"
202+
@echo " $(BUILDOBJS)"
203+
@echo ""
204+
@echo "BINDIR:"
205+
@echo " $(BINDIR)"
206+
@echo ""
207+
208+
###############################################################################
209+
210+
# Target for generate BIN file from .elf file
211+
$(PRJ).bin: $(PRJ).hex
212+
rm -f $(PRJ).bin
213+
$(OBJCOPY) -j .text -j .data -O binary $(PRJ).elf $(PRJ).bin
214+
215+
# Target for generate HEX file from .elf file
216+
$(PRJ).hex: $(PRJ).elf
217+
rm -f $(PRJ).hex
218+
$(OBJCOPY) -j .text -j .data -O ihex $(PRJ).elf $(PRJ).hex
219+
220+
# Target for generate ELF file linking all .o files
221+
$(PRJ).elf: $(OBJS)
222+
$(COMPILE_C) -o $(PRJ).elf $(BUILDOBJS)
223+
224+
# Target for generate object file of each .c file
225+
%.o: %.c
226+
$(COMPILE_C) -c $<
227+
228+
# Target for generate object file of each .cpp file
229+
%.o: %.cpp
230+
$(COMPILE_CXX) -c $<

0 commit comments

Comments
 (0)