Skip to content

Commit 53782a2

Browse files
committed
AVR callbacks example added
1 parent ff369d2 commit 53782a2

17 files changed

Lines changed: 719 additions & 10 deletions

File tree

examples/avr/basic_usage/build/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
# Specify project name and default compilers
3-
PROJECTNAME = minbasecli_basicusage
3+
PROJECTNAME = minbasecli_basic_usage
44

55
# CLI library path
66
MINBASECLIPATH = $(WORKSPACE)/../../../src

examples/avr/basic_usage/src/main.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,22 @@
5353
// Address of DDRx is address of PORTx-1
5454
#define DDR(x) (*(&x - 1))
5555

56+
// Current Firmware Application Version
57+
#define FW_APP_VERSION "1.0.0"
58+
5659
/*****************************************************************************/
5760

58-
/* In-Scope Function Prototypes */
61+
/* Global Elements */
5962

60-
static void led_init(void);
61-
static void led_on(void);
62-
static void led_off(void);
63+
AvrUart Serial(UART0, F_CPU);
6364

6465
/*****************************************************************************/
6566

66-
/* Global Elements */
67+
/* Function Prototypes */
6768

68-
AvrUart Serial(UART0, F_CPU);
69+
void led_init(void);
70+
void led_on(void);
71+
void led_off(void);
6972

7073
/*****************************************************************************/
7174

@@ -104,9 +107,9 @@ int main(void)
104107
if(strcmp(cli_read.cmd, "help") == 0)
105108
{
106109
Cli.printf("Available Commands:\n");
107-
Cli.printf(" help - Current info\n");
108-
Cli.printf(" led [on/off] - Turn LED ON or OFF\n");
109-
Cli.printf(" version - Shows current firmware version\n");
110+
Cli.printf(" help - Current info.\n");
111+
Cli.printf(" led [on/off] - Turn LED ON or OFF.\n");
112+
Cli.printf(" version - Shows current firmware version.\n");
110113
}
111114
else if(strcmp(cli_read.cmd, "led") == 0)
112115
{
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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# MinBaseCLI AVR Basic Usage Example
2+
3+
Basic usage example of MinBaseCLI library for AVR devices.
4+
5+
## Requirements
6+
7+
- Get external UART driver GIT submodule:
8+
9+
```bash
10+
git submodule update --init --recursive
11+
```
12+
13+
- Install required toolchain components:
14+
15+
```bash
16+
sudo apt-get update
17+
sudo apt-get -y install binutils
18+
sudo apt-get -y install make
19+
sudo apt-get -y install gcc-avr
20+
sudo apt-get -y install avr-libc
21+
sudo apt-get -y install cflow
22+
```
23+
24+
- Install Flashing Tool:
25+
26+
```bash
27+
sudo apt-get -y install avrdude
28+
```
29+
30+
- Give execute permissions to tools scripts:
31+
32+
```bash
33+
chmod +x basic_usage/tools/*
34+
```
35+
36+
## Usage
37+
38+
Build:
39+
40+
1. Physically connect the hardware programmer to target device and the PC.
41+
42+
2. Using a shell, go to **build/** directory and run **make** to see build instructions.
43+
44+
3. Run **make build DEVICE=X F_CPU=X** to launch the Build.
45+
46+
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: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
2+
# Specify project name and default compilers
3+
PROJECTNAME = minbasecli_basic_usage_callbacks
4+
5+
# CLI library 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+
52+
# Libraries Sources
53+
SRCS += $(shell find $(WORKSPACE)/lib/avr-uart-driver/src -type f -name *.c)
54+
SRCS += $(shell find $(WORKSPACE)/lib/avr-uart-driver/src -type f -name *.cpp)
55+
56+
# MinBaseCLI Sources
57+
SRCS += $(MINBASECLIPATH)/minbasecli.cpp
58+
SRCS += $(MINBASECLIPATH)/hal/avr/minbasecli_avr.cpp
59+
60+
# Specify Headers files (Automatic search in specific directories)
61+
HEADS = $(shell find $(WORKSPACE)/inc -type f -name *.h)
62+
HEADS += $(shell find $(WORKSPACE)/inc -type f -name *.hpp)
63+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.h)
64+
HEADS += $(shell find $(WORKSPACE)/src -type f -name *.hpp)
65+
66+
# Libraries Headers
67+
HEADS += $(shell find $(WORKSPACE)/lib/avr-uart-driver/src -type f -name *.h)
68+
HEADS += $(shell find $(WORKSPACE)/lib/avr-uart-driver/src -type f -name *.hpp)
69+
70+
# MinBaseCLI Headers
71+
HEADS += $(MINBASECLIPATH)/minbasecli.h
72+
HEADS += $(MINBASECLIPATH)/hal/avr/minbasecli_avr.h
73+
74+
# Get Headers directories from headers paths
75+
HEADERSDIR = $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-I{}")
76+
HEADERSDIR += $(shell echo $(HEADS) | xargs -n1 dirname | sort -u | xargs -n1 -i echo "-L{}")
77+
78+
# Get objects files from sources and output object
79+
_OBJS = $(SRCS:.c=.o)
80+
OBJS = $(_OBJS:.cpp=.o)
81+
BUILDOBJS = $(shell echo $(OBJS) | xargs -n1 basename | xargs -n1 -i echo "$(BUILDDIR)/{}")
82+
83+
###############################################################################
84+
85+
# Specify default compilers and tools
86+
CC = avr-gcc
87+
CXX = avr-g++
88+
AVRDUDE = avrdude -c $(PROGRAMMER) -p $(DEVICE)
89+
AVRDUDE_ARDUINO = avrdude -c arduino -b 115200 -P /dev/ttyUSB0 -p $(DEVICE)
90+
OBJCOPY = avr-objcopy
91+
OBJDUMP = avr-objdump
92+
NM = avr-nm --line-numbers --print-size --size-sort --radix=d
93+
SIZE = avr-size --format=avr --mcu=$(DEVICE)
94+
95+
# Setup compilation flags
96+
CFLAGS = -Os -Wall -g -fstack-usage -flto
97+
CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm
98+
CFLAGS += '-mmcu=$(DEVICE)'
99+
CFLAGS += '-DF_CPU=$(F_CPU)'
100+
ifeq ($(TEST), 1)
101+
CFLAGS += -DTEST
102+
endif
103+
CFLAGS += $(HEADERSDIR)
104+
CXXFLAGS = $(CFLAGS)
105+
106+
# Flash Section to place the binary
107+
# Application placed at "Flash_Start" (0x0000)
108+
# Bootloader placed at "Flash_End" - "Boot_Section_Size"
109+
# Boot Section size must be defined with BOOTSZ0 and BOOTSZ1 fuses
110+
# Example:
111+
# Device of 128KB Flash with 4KB Boot Section: (128-4)*1024 = 0x1F000
112+
ifeq ($(BOOTLOADER), 1)
113+
PRJ = 'fw_boot_$(PROJECTNAME)'
114+
FLASHSECTION = --section-start=.text=0x1F000
115+
else
116+
PRJ = 'fw_app_$(PROJECTNAME)'
117+
FLASHSECTION = --section-start=.text=0x00000
118+
BOOTLOADER=0
119+
endif
120+
121+
# Linker flags
122+
LDFLAGS = '-Wl,$(FLASHSECTION)'
123+
124+
# Compile instruction
125+
COMPILE_C = $(CC) $(CFLAGS) $(LDFLAGS)
126+
COMPILE_CXX = $(CXX) $(CXXFLAGS) $(LDFLAGS)
127+
128+
###############################################################################
129+
130+
help:
131+
@cat README.md
132+
133+
# Target: make all (build project generating output directory)
134+
build: $(PRJ).bin
135+
rm -f ${RELEASEDIR}/*
136+
mkdir -p ${RELEASEDIR}
137+
cp -a $(PRJ).elf $(RELEASEDIR)
138+
cp -a $(PRJ).hex $(RELEASEDIR)
139+
cp -a $(PRJ).bin $(RELEASEDIR)
140+
@echo "ADDRESS SIZE FUNCTION FILE:LINE\n" > memory_program.txt
141+
@$(NM) $(PRJ).elf >> memory_program.txt
142+
@chmod +x $(BUILDDIR)/../tools/*
143+
@$(BUILDDIR)/../tools/memusageram $(BUILDDIR)/memory_ram.txt >/dev/null 2>&1
144+
@rm -f $(BUILDDIR)/*.su
145+
@mkdir -p $(BUILDDIR)/obj
146+
@mv $(BUILDDIR)/*.o $(BUILDDIR)/obj/
147+
$(SIZE) $(PRJ).elf
148+
@if [ ${BOOTLOADER} = "1" ]; then\
149+
echo "Bootloader Built";\
150+
else\
151+
echo "Application Built";\
152+
fi
153+
@$(BUILDDIR)/../tools/binary_size $(PRJ).bin
154+
@echo ""
155+
156+
# Target: make clean (remove all previously builds)
157+
clean:
158+
rm -rf $(BUILDDIR)/obj
159+
rm -f $(BUILDDIR)/*.su
160+
rm -f $(BUILDDIR)/$(PRJ).elf
161+
rm -f $(BUILDDIR)/$(PRJ).hex
162+
rm -f $(BUILDDIR)/$(PRJ).bin
163+
rm -f $(BUILDDIR)/dump.bin
164+
rm -f $(BUILDDIR)/dump.hex
165+
rm -f $(BUILDDIR)/memory_program.txt
166+
rm -f $(BUILDDIR)/memory_ram.txt
167+
168+
# Target: make rebuild (clean previously builds and build again)
169+
rebuild: clean build
170+
171+
# Flash program to MCU
172+
flash: build
173+
$(AVRDUDE) -U flash:w:$(PRJ).hex:i
174+
175+
# Flash program to MCU
176+
flash_arduino: build
177+
$(AVRDUDE_ARDUINO) -U flash:w:$(PRJ).hex:i
178+
179+
# Write fuses to MCU
180+
fuse:
181+
$(AVRDUDE) -U lfuse:w:$(LFU):m -U hfuse:w:$(HFU):m -U efuse:w:$(EFU):m
182+
183+
# Read flash memory from MCU
184+
dump:
185+
rm -f $(BUILDDIR)/dump.bin
186+
rm -f $(BUILDDIR)/dump.hex
187+
$(AVRDUDE) -U flash:r:dump.bin:r
188+
$(OBJCOPY) -I binary -O ihex dump.bin dump.hex
189+
190+
# Target: check (custom target to check build variables)
191+
check:
192+
@echo ""
193+
@echo "HEADERS:"
194+
@echo " $(HEADS)"
195+
@echo ""
196+
@echo "SRCS:"
197+
@echo " $(SRCS)"
198+
@echo ""
199+
@echo "BUILD FLAGS:"
200+
@echo " $(CFLAGS)"
201+
@echo ""
202+
@echo "LINK FLAGS:"
203+
@echo " $(LDFLAGS)"
204+
@echo ""
205+
@echo "OBJS:"
206+
@echo " $(BUILDOBJS)"
207+
@echo ""
208+
@echo "BINDIR:"
209+
@echo " $(BINDIR)"
210+
@echo ""
211+
212+
###############################################################################
213+
214+
# Target for generate BIN file from .elf file
215+
$(PRJ).bin: $(PRJ).hex
216+
rm -f $(PRJ).bin
217+
$(OBJCOPY) -j .text -j .data -O binary $(PRJ).elf $(PRJ).bin
218+
219+
# Target for generate HEX file from .elf file
220+
$(PRJ).hex: $(PRJ).elf
221+
rm -f $(PRJ).hex
222+
$(OBJCOPY) -j .text -j .data -O ihex $(PRJ).elf $(PRJ).hex
223+
224+
# Target for generate ELF file linking all .o files
225+
$(PRJ).elf: $(OBJS)
226+
$(COMPILE_C) -o $(PRJ).elf $(BUILDOBJS)
227+
228+
# Target for generate object file of each .c file
229+
%.o: %.c
230+
$(COMPILE_C) -c $<
231+
232+
# Target for generate object file of each .cpp file
233+
%.o: %.cpp
234+
$(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.

0 commit comments

Comments
 (0)