Skip to content

Commit

Permalink
added 'hello world' to the self-test
Browse files Browse the repository at this point in the history
  • Loading branch information
squell committed Jan 22, 2016
1 parent 675a6f1 commit 506b592
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ ASFLAGS = --32
tester: ihexread.o avr_core_x86.o tester.o

clean:
rm *.o tester
rm -f *.o tester

# the tinyTwofish known-answer-test sends the mcu in an infinite loop
# if an error is encountered; or it will stop with a sleep instruction
# (reporting last opcode 9588) -- we check this
selftest: tester tinyTwofish/2fish_avr.s
make -B -C example | grep 'Hello, world!'
make -B -C tinyTwofish ckat CHIP=atmega2560
avr-objcopy -O ihex tinyTwofish/ckat twofish.hex
./tester twofish.hex 2>&1 | grep '\[9588\]$$'
Expand Down
13 changes: 13 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.SUFFIXES: .hex .elf

test: hello.hex
../tester hello.hex 2> /dev/null

clean:
rm -f hello.hex hello.o hello.elf

.elf.hex:
avr-objcopy -j .text -j .data -O ihex $< $@

.c.elf:
avr-gcc -mmcu=atmega2560 $< -o$@
84 changes: 84 additions & 0 deletions example/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* Copyright (c) 2002, 2005, 2007 Joerg Wunsch
All rights reserved.
Portions of documentation Copyright (c) 1990, 1991, 1993
The Regents of the University of California.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of the copyright holders nor the names of
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

/* Demo "Hello world"; taken from the stdiodemo in avr-libc with slight modifications
* -- Marc Schoolderman */

#include <avr/io.h>
#include <avr/sleep.h>
#define F_CPU 16000000
#define UART_BAUD 38400

void
uart_init(void)
{
#if F_CPU < 2000000UL && defined(U2X)
UCSR0A = _BV(U2X); /* improve baud rate error by using 2x clk */
UBRR0L = (F_CPU / (8UL * UART_BAUD)) - 1;
#else
UBRR0L = (F_CPU / (16UL * UART_BAUD)) - 1;
#endif
UCSR0B = _BV(TXEN0) | _BV(RXEN0); /* tx/rx enable */
}

#include <stdio.h>

static int uart_putchar(char c, FILE *stream);

static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
_FDEV_SETUP_WRITE);

static int
uart_putchar(char c, FILE *stream)
{

if (c == '\n')
uart_putchar('\r', stream);
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}

int
main(void)
{
uart_init();
stdout = &mystdout;
printf("Hello, world!\n");

sleep_cpu();
return 0;
}

0 comments on commit 506b592

Please sign in to comment.