Skip to content

Commit 2ef2b60

Browse files
committed
fix: Add a docs/examples/ folder to fix #1021 for old releases.
This patch cures issue #1021 (404 for asmdemp.c etc.) for the existing HTML online documentations in docs/ by adding an examples/ folder parallel to the dosc/avr-libc-user-manual-<version>/ folders. The obvious drawback is that all versions of the documentation are using the same examples/ folder, hence there should be a proper fix for future releases.
1 parent 2db75ca commit 2ef2b60

File tree

21 files changed

+3033
-0
lines changed

21 files changed

+3033
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ doc/api/malloc-x1.png
3939
doc/api/malloc-std.png
4040
doc/api/malloc-x2.png
4141

42+
!docs/examples/*/Makefile
43+
4244
__pycache__

docs/examples/asmdemo/Makefile

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
# Hey Emacs, this is a -*- makefile -*-
2+
3+
# AVR-GCC Makefile template, derived from the WinAVR template (which
4+
# is public domain), believed to be neutral to any flavor of "make"
5+
# (GNU make, BSD make, SysV make)
6+
7+
8+
MCU = attiny13
9+
#MCU = attiny45
10+
FORMAT = ihex
11+
TARGET = asmdemo
12+
SRC = $(TARGET).c
13+
ASRC = isrs.S
14+
OPT = s
15+
16+
# Name of this Makefile (used for "make depend").
17+
MAKEFILE = Makefile
18+
19+
# Compiler flag to set the C Standard level.
20+
# c89 - "ANSI" C
21+
# gnu89 - c89 plus GCC extensions
22+
# c99 - ISO C99 standard (not yet fully implemented)
23+
# gnu99 - c99 plus GCC extensions
24+
CSTANDARD = -std=gnu99
25+
26+
# Place -D or -U options here
27+
CDEFS =
28+
29+
# Place -I options here
30+
CINCS =
31+
32+
33+
CDEBUG = -g
34+
CWARN = -Wall -Wstrict-prototypes
35+
CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
36+
#CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
37+
CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA)
38+
39+
40+
#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
41+
42+
43+
#Additional libraries.
44+
45+
# Minimalistic printf version
46+
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
47+
48+
# Floating point printf version (requires MATH_LIB = -lm below)
49+
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
50+
51+
PRINTF_LIB =
52+
53+
# Minimalistic scanf version
54+
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
55+
56+
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
57+
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
58+
59+
SCANF_LIB =
60+
61+
MATH_LIB = -lm
62+
63+
# External memory options
64+
65+
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
66+
# used for variables (.data/.bss) and heap (malloc()).
67+
#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff
68+
69+
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
70+
# only used for heap (malloc()).
71+
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
72+
73+
EXTMEMOPTS =
74+
75+
#LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref
76+
LDFLAGS = $(EXTMEMOPTS) $(LDMAP) $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
77+
78+
79+
# Programming support using avrdude. Settings and variables.
80+
81+
AVRDUDE_PROGRAMMER = stk500v2
82+
AVRDUDE_PORT = /dev/cuaa1
83+
84+
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
85+
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
86+
87+
88+
# Uncomment the following if you want avrdude's erase cycle counter.
89+
# Note that this counter needs to be initialized first using -Yn,
90+
# see avrdude manual.
91+
#AVRDUDE_ERASE_COUNTER = -y
92+
93+
# Uncomment the following if you do /not/ wish a verification to be
94+
# performed after programming the device.
95+
#AVRDUDE_NO_VERIFY = -V
96+
97+
# Increase verbosity level. Please use this when submitting bug
98+
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
99+
# to submit bug reports.
100+
#AVRDUDE_VERBOSE = -v -v
101+
102+
AVRDUDE_BASIC = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
103+
AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER)
104+
105+
106+
CC = avr-gcc
107+
OBJCOPY = avr-objcopy
108+
OBJDUMP = avr-objdump
109+
SIZE = avr-size
110+
NM = avr-nm
111+
AVRDUDE = avrdude
112+
REMOVE = rm -f
113+
MV = mv -f
114+
115+
# Define all object files.
116+
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
117+
118+
# Define all listing files.
119+
LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
120+
121+
# Combine all necessary flags and optional flags.
122+
# Add target processor to flags.
123+
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
124+
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
125+
126+
127+
# Default target.
128+
all: build
129+
130+
build: elf hex eep
131+
132+
elf: $(TARGET).elf
133+
hex: $(TARGET).hex
134+
eep: $(TARGET).eep
135+
lss: $(TARGET).lss
136+
sym: $(TARGET).sym
137+
138+
139+
# Program the device.
140+
program: $(TARGET).hex $(TARGET).eep
141+
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
142+
143+
144+
145+
146+
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
147+
COFFCONVERT=$(OBJCOPY) --debugging \
148+
--change-section-address .data-0x800000 \
149+
--change-section-address .bss-0x800000 \
150+
--change-section-address .noinit-0x800000 \
151+
--change-section-address .eeprom-0x810000
152+
153+
154+
coff: $(TARGET).elf
155+
$(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof
156+
157+
158+
extcoff: $(TARGET).elf
159+
$(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof
160+
161+
162+
.SUFFIXES: .elf .hex .eep .lss .sym
163+
164+
.elf.hex:
165+
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
166+
167+
.elf.eep:
168+
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
169+
--change-section-lma .eeprom=0 -O $(FORMAT) $< $@
170+
171+
# Create extended listing file from ELF output file.
172+
.elf.lss:
173+
$(OBJDUMP) -h -S $< > $@
174+
175+
# Create a symbol table from ELF output file.
176+
.elf.sym:
177+
$(NM) -n $< > $@
178+
179+
180+
181+
# Link: create ELF output file from object files.
182+
$(TARGET).elf: $(OBJ)
183+
$(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
184+
185+
186+
# Compile: create object files from C source files.
187+
.c.o:
188+
$(CC) -c $(ALL_CFLAGS) $< -o $@
189+
190+
191+
# Compile: create assembler files from C source files.
192+
.c.s:
193+
$(CC) -S $(ALL_CFLAGS) $< -o $@
194+
195+
196+
# Assemble: create object files from assembler source files.
197+
.S.o:
198+
$(CC) -c $(ALL_ASFLAGS) $< -o $@
199+
200+
201+
202+
# Target: clean project.
203+
clean:
204+
$(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf \
205+
$(TARGET).map $(TARGET).sym $(TARGET).lss \
206+
$(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d)
207+
208+
depend:
209+
if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
210+
then \
211+
sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \
212+
$(MAKEFILE).$$$$ && \
213+
$(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \
214+
fi
215+
echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
216+
>> $(MAKEFILE); \
217+
$(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)
218+
219+
.PHONY: all build elf hex eep lss sym program coff extcoff clean depend
220+
221+

docs/examples/asmdemo/asmdemo.c

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* ----------------------------------------------------------------------------
3+
* "THE BEER-WARE LICENSE" (Revision 42):
4+
* Joerg Wunsch wrote this file. As long as you retain this notice you
5+
* can do whatever you want with this stuff. If we meet some day, and you think
6+
* this stuff is worth it, you can buy me a beer in return. Joerg Wunsch
7+
* ----------------------------------------------------------------------------
8+
*
9+
* Demo combining C and assembly source files.
10+
*
11+
* This demo implements an RC model type PWM decoder. The incoming
12+
* PWM signal consists of a pulse sequence with a pulse width of 920
13+
* microseconds up to 2120 microseconds (1520 microseconds being the
14+
* neutral point). Depending on the value of the decoded incoming
15+
* PWM, an outgoing PWM is controlled between 0 and 100 %.
16+
*
17+
* The project is intended to be run on an ATtiny13 that has only one
18+
* timer channel (timer 0), so both the incoming signal discrimination
19+
* as well as the outgoing PWM need to run on the same timer.
20+
*
21+
* For verification purposes, the same project can also be run on an
22+
* ATtiny25/45/85, where timer 1 can be used to evaluate the incoming
23+
* PWM signal, and timer 0 to generate the outgoing PWM. In that
24+
* case, no additional assembly code is needed.
25+
*/
26+
27+
/*
28+
* This is the main C source file for the demo.
29+
*/
30+
#include <avr/interrupt.h>
31+
#include <avr/io.h>
32+
#include <avr/sleep.h>
33+
34+
#include "project.h"
35+
36+
volatile uint16_t pwm_incoming;
37+
volatile struct
38+
{
39+
uint8_t pwm_received: 1;
40+
}
41+
intbits;
42+
43+
void
44+
ioinit(void)
45+
{
46+
counter_hi = 0;
47+
flags = 0;
48+
49+
/*
50+
* Timer 0 runs as phase-correct PWM at full clock, OC0B connects to
51+
* the PWM engine.
52+
*/
53+
TCCR0A = (1 << COM0B1) | (1 << WGM00);
54+
TCCR0B = (1 << CS00);
55+
OCR0A = 255;
56+
57+
#if defined(__AVR_ATtiny13__)
58+
TIMSK0 = (1 << TOIE0) | (1 << OCIE0A);
59+
60+
# define F_CPU 1200000ul
61+
/* Minimal PWM pulse width is 920 us. */
62+
# define MIN_PWM_VAL ((920ul * F_CPU) / 1000000ul)
63+
/* Maximal PWM pulse width is 2120 us */
64+
# define MAX_PWM_VAL ((2120ul * F_CPU) / 1000000ul)
65+
66+
#elif defined(__AVR_ATtiny25__) ||\
67+
defined(__AVR_ATtiny45__) ||\
68+
defined(__AVR_ATtiny85__)
69+
70+
# define F_CPU 1000000ul
71+
/*
72+
* We use a prescaler of 16 here to avoid the 32-bit calculations
73+
* below.
74+
*/
75+
/* Minimal PWM pulse width is 920 us. */
76+
# define MIN_PWM_VAL ((920ul * F_CPU) / 16 / 1000000ul)
77+
/* Maximal PWM pulse width is 2120 us */
78+
# define MAX_PWM_VAL ((2120ul * F_CPU) / 16 / 1000000ul)
79+
80+
#else
81+
# error "Don't know how to run on your MCU_TYPE."
82+
#endif
83+
84+
PCMSK = (1 << 4);
85+
GIFR = (1 << PCIF);
86+
GIMSK = (1 << PCIE);
87+
88+
DDRB = (1 << PB1);
89+
PORTB = 0;
90+
91+
sei();
92+
}
93+
94+
#if defined(__AVR_ATtiny25__) ||\
95+
defined(__AVR_ATtiny45__) ||\
96+
defined(__AVR_ATtiny85__)
97+
ISR(PCINT0_vect)
98+
{
99+
uint8_t tcnt1;
100+
101+
if (PINB & (1 << 4))
102+
{
103+
/* Start timer 1 with a prescaler of 16. */
104+
TCNT1 = 0;
105+
TCCR1 = (1 << CS12) | (1 << CS10);
106+
return;
107+
}
108+
109+
/* Stop timer 1, current value is pulse width. */
110+
tcnt1 = TCNT1;
111+
TCCR1 = 0;
112+
GIMSK &= ~(1 << PCIE);
113+
114+
pwm_incoming = tcnt1;
115+
intbits.pwm_received = 1;
116+
}
117+
#endif /* ATtinyX5 */
118+
119+
int
120+
main(void)
121+
{
122+
123+
ioinit();
124+
125+
for (;;)
126+
{
127+
if (intbits.pwm_received)
128+
{
129+
intbits.pwm_received = 0;
130+
#if defined(__AVR_ATtiny13__)
131+
if (pwm_incoming < MIN_PWM_VAL)
132+
pwm_incoming = MIN_PWM_VAL;
133+
else if (pwm_incoming > MAX_PWM_VAL)
134+
pwm_incoming = MAX_PWM_VAL;
135+
OCR0B = (pwm_incoming - MIN_PWM_VAL) * 255ul / (MAX_PWM_VAL - MIN_PWM_VAL);
136+
#else
137+
OCR0B = (pwm_incoming - MIN_PWM_VAL) * 255u / (MAX_PWM_VAL - MIN_PWM_VAL);
138+
#endif
139+
GIFR = (1 << PCIF);
140+
GIMSK |= (1 << PCIE);
141+
}
142+
sleep_mode();
143+
}
144+
}

0 commit comments

Comments
 (0)