Skip to content

Commit d991ccd

Browse files
Copy tests from simavr
1 parent 79e5b9e commit d991ccd

22 files changed

+1510
-0
lines changed

tests/Makefile

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#
2+
# This makefile takes each "at*" file, extracts it's part name
3+
# And compiles it into an ELF binary.
4+
# It also disassembles it for debugging purposes.
5+
#
6+
# Copyright 2008-2012 Michel Pollet <[email protected]>
7+
#
8+
# This file is part of simavr.
9+
#
10+
# simavr is free software: you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation, either version 3 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# simavr is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with simavr. If not, see <http://www.gnu.org/licenses/>.
22+
23+
sources := $(wildcard at*.cc)
24+
simavr = ..
25+
26+
IPATH += ${simavr}/include
27+
IPATH += ${simavr}/simavr/sim
28+
29+
tests_src := ${wildcard test_*.c}
30+
31+
all: obj axf tests
32+
33+
include ../Makefile.common
34+
35+
tests := ${patsubst %.c, ${OBJ}/%.tst, ${tests_src}}
36+
37+
tests: ${tests}
38+
39+
axf: ${sources:.c=.axf}
40+
41+
42+
${OBJ}/%.tst: tests.c %.c
43+
ifeq ($(V),1)
44+
$(CC) -MMD ${CPPFLAGS} ${CFLAGS} ${LFLAGS} -o $@ ${patsubst %.h,, ${^}} $(LDFLAGS)
45+
else
46+
@echo TST $@
47+
@$(CC) -MMD ${CPPFLAGS} ${CFLAGS} ${LFLAGS} -o $@ ${patsubst %.h,, ${^}} $(LDFLAGS)
48+
endif
49+
50+
run_tests: all
51+
@export LD_LIBRARY_PATH=${simavr}/simavr/${OBJ} ;\
52+
num_failed=0 ;\
53+
num_run=0 ;\
54+
for test in ${OBJ}/test_*.tst; do \
55+
num_run=$$(($$num_run+1)) ;\
56+
if ! $$test; then \
57+
echo "$$test returned with exit value $$?." ;\
58+
num_failed=$$(($$num_failed+1)) ;\
59+
fi ;\
60+
done ;\
61+
echo "Tests run: $$num_run Successes: $$(($$num_run-$$num_failed)) Failures: $$num_failed"
62+
63+
clean: clean-${OBJ}
64+
rm -f *.axf *.vcd

tests/atmega2560_uart_echo.cc

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
atmega88_uart_echo.c
3+
4+
This test case enables uart RX interupts, does a "printf" and then receive characters
5+
via the interupt handler until it reaches a \r.
6+
7+
This tests the uart reception fifo system. It relies on the uart "irq" input and output
8+
to be wired together (see simavr.c)
9+
*/
10+
11+
#include <avr/io.h>
12+
#include <stdio.h>
13+
#include <avr/interrupt.h>
14+
#include <avr/eeprom.h>
15+
#include <avr/sleep.h>
16+
#include <util/delay.h>
17+
18+
/*
19+
* This demonstrate how to use the avr_mcu_section.h file
20+
* The macro adds a section to the ELF file with useful
21+
* information for the simulator
22+
*/
23+
#include "avr_mcu_section.h"
24+
AVR_MCU(F_CPU, "atmega2560");
25+
// tell simavr to listen to commands written in this (unused) register
26+
AVR_MCU_SIMAVR_COMMAND(&GPIOR0);
27+
AVR_MCU_SIMAVR_CONSOLE(&GPIOR1);
28+
29+
/*
30+
* This small section tells simavr to generate a VCD trace dump with changes to these
31+
* registers.
32+
* Opening it with gtkwave will show you the data being pumped out into the data register
33+
* UDR0, and the UDRE0 bit being set, then cleared
34+
*/
35+
const struct avr_mmcu_vcd_trace_t _mytrace[] _MMCU_ = {
36+
{ AVR_MCU_VCD_SYMBOL("UDR3"), .what = (void*)&UDR3, },
37+
{ AVR_MCU_VCD_SYMBOL("UDRE3"), .mask = (1 << UDRE3), .what = (void*)&UCSR3A, },
38+
{ AVR_MCU_VCD_SYMBOL("GPIOR1"), .what = (void*)&GPIOR1, },
39+
};
40+
#ifdef USART3_RX_vect_num // stupid ubuntu has antique avr-libc
41+
AVR_MCU_VCD_IRQ(USART3_RX); // single bit trace
42+
#endif
43+
AVR_MCU_VCD_ALL_IRQ(); // also show ALL irqs running
44+
45+
volatile uint8_t cnt = 0;
46+
volatile uint8_t done = 0;
47+
48+
static int uart_putchar(char c, FILE *stream)
49+
{
50+
uint8_t startcnt;
51+
if (c == '\n')
52+
uart_putchar('\r', stream);
53+
loop_until_bit_is_set(UCSR3A, UDRE3);
54+
55+
startcnt = cnt;
56+
UDR3 = c;
57+
// _delay_us(100);
58+
59+
// Wait until we have received the character back
60+
while(!done && cnt == startcnt)
61+
{
62+
UDR1 = 'a';
63+
UDR1 = '\n';
64+
sleep_cpu();
65+
}
66+
67+
UDR1 = 'b';
68+
UDR1 = '\n';
69+
70+
_delay_us(1000);
71+
72+
return 0;
73+
}
74+
75+
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
76+
_FDEV_SETUP_WRITE);
77+
78+
volatile uint8_t bindex = 0;
79+
uint8_t buffer[80];
80+
81+
ISR(USART3_RX_vect)
82+
{
83+
UDR1 = 'c';
84+
UDR1 = '\n';
85+
86+
uint8_t b = UDR3;
87+
GPIOR1 = b; // for the trace file
88+
buffer[bindex++] = b;
89+
buffer[bindex] = 0;
90+
cnt++;
91+
if (b == '\n')
92+
done++;
93+
// sleep_cpu();
94+
}
95+
96+
int main()
97+
{
98+
stdout = &mystdout;
99+
100+
UCSR3C = (3 << UCSZ30); // 8 bits
101+
// see http://www.nongnu.org/avr-libc/user-manual/group__util__setbaud.html
102+
#define BAUD 38400
103+
#include <util/setbaud.h>
104+
UBRR3H = UBRRH_VALUE;
105+
UBRR3L = UBRRL_VALUE;
106+
#if USE_2X
107+
UCSR3A |= (1 << U2X3);
108+
#else
109+
UCSR3A &= ~(1 << U2X3);
110+
#endif
111+
112+
// enable receiver & transmitter
113+
UCSR3B |= (1 << RXCIE3) | (1 << RXEN3) | (1 << TXEN3);
114+
115+
// this tells simavr to start the trace
116+
GPIOR0 = SIMAVR_CMD_VCD_START_TRACE;
117+
sei();
118+
printf("Hey there, this should be received back\n");
119+
loop_until_bit_is_set(UCSR3A, UDRE3);
120+
121+
while (!done)
122+
sleep_cpu();
123+
124+
cli();
125+
126+
printf("Received: %s", buffer);
127+
128+
// this quits the simulator, since interupts are off
129+
// this is a "feature" that allows running tests cases and exit
130+
sleep_cpu();
131+
}

tests/atmega48_disabled_timer.cc

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* avrtest.c
3+
*
4+
* Created on: 1 Dec 2009
5+
* Author: jone
6+
*/
7+
8+
#include "arduino--.h"
9+
10+
#include "avr_mcu_section.h"
11+
AVR_MCU(F_CPU, "atmega48");
12+
13+
ISR(TIMER0_COMPA_vect)
14+
{
15+
}
16+
17+
int main(void)
18+
{
19+
// Set up timer0 - do not start yet
20+
TCCR0A |= (1 << WGM01); // Configure timer 0 for CTC mode
21+
TIMSK0 |= (1 << OCIE0A); // Enable CTC interrupt
22+
OCR0A = 0xAA; // CTC compare value
23+
24+
//TCCR0B |= (1 << CS00) | (1 << CS01); // Start timer: clk/64
25+
26+
sei(); // Enable global interrupts
27+
28+
// here the interupts are enabled, but the interupt
29+
// vector should not be called
30+
sleep_mode();
31+
32+
// this should not be reached
33+
cli();
34+
sleep_mode();
35+
}

tests/atmega48_enabled_timer.cc

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* avrtest.c
3+
*
4+
* Created on: 4 Feb 2011
5+
* Author: sliedes
6+
* This is a very slightly modified version of atmega48_disabled_timer.c
7+
* by jone.
8+
*/
9+
10+
#include <avr/io.h>
11+
#include <avr/interrupt.h>
12+
#include <avr/sleep.h>
13+
14+
#include "avr_mcu_section.h"
15+
AVR_MCU(F_CPU, "atmega48");
16+
17+
ISR(TIMER0_COMPA_vect)
18+
{
19+
}
20+
21+
int main(void)
22+
{
23+
// Set up timer0 - do not start yet
24+
TCCR0A |= (1 << WGM01); // Configure timer 0 for CTC mode
25+
TIMSK0 |= (1 << OCIE0A); // Enable CTC interrupt
26+
OCR0A = 0xAA; // CTC compare value
27+
28+
TCCR0B |= (1 << CS00) | (1 << CS01); // Start timer: clk/64
29+
30+
sei(); // Enable global interrupts
31+
32+
// here the interupts are enabled, but the interupt
33+
// vector should not be called
34+
sleep_mode();
35+
36+
// this should not be reached
37+
cli();
38+
sleep_mode();
39+
}

tests/atmega48_watchdog_test.cc

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
attiny13_watchdog_test.c
3+
4+
Copyright 2008, 2009 Michel Pollet <[email protected]>
5+
6+
This file is part of simavr.
7+
8+
simavr is free software: you can redistribute it and/or modify
9+
it under the terms of the GNU General Public License as published by
10+
the Free Software Foundation, either version 3 of the License, or
11+
(at your option) any later version.
12+
13+
simavr is distributed in the hope that it will be useful,
14+
but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
GNU General Public License for more details.
17+
18+
You should have received a copy of the GNU General Public License
19+
along with simavr. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
23+
#include <avr/io.h>
24+
#include <stdio.h>
25+
#include <avr/interrupt.h>
26+
#include <util/delay.h>
27+
#include <avr/sleep.h>
28+
#include <avr/wdt.h>
29+
30+
/*
31+
* This demonstrate how to use the avr_mcu_section.h file
32+
* The macro adds a section to the ELF file with useful
33+
* information for the simulator
34+
*/
35+
#include "avr_mcu_section.h"
36+
AVR_MCU(F_CPU, "atmega48");
37+
38+
static int uart_putchar(char c, FILE *stream) {
39+
if (c == '\n')
40+
uart_putchar('\r', stream);
41+
loop_until_bit_is_set(UCSR0A, UDRE0);
42+
UDR0 = c;
43+
return 0;
44+
}
45+
46+
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
47+
_FDEV_SETUP_WRITE);
48+
49+
50+
ISR(WDT_vect)
51+
{
52+
// nothing to do here, we're just here to wake the CPU
53+
}
54+
55+
int main()
56+
{
57+
stdout = &mystdout;
58+
DDRD = (1<<PD1); // configure TxD as output
59+
60+
wdt_enable(WDTO_120MS);
61+
62+
// enable watchdog interupt
63+
// NOTE: since the Change Enable bit is no longer on, it should not
64+
// change the timer value that is already set!
65+
WDTCSR = (1 << WDIE);
66+
67+
sei();
68+
69+
printf("Watchdog is active\n");
70+
uint8_t count = 20;
71+
while (count--) {
72+
_delay_ms(10);
73+
wdt_reset();
74+
}
75+
printf("Waiting for Watchdog to kick\n");
76+
// now , stop calling the watchdog reset, and just sleep until it fires
77+
sleep_cpu();
78+
printf("Watchdog kicked us!\n");
79+
80+
// when arriving here, the watchdog timer interupt was called and woke up
81+
// the core from sleep, so we can just quit properly
82+
cli();
83+
sleep_cpu();
84+
}

0 commit comments

Comments
 (0)