Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
d827bbe427 | |||
85dca87ff3 | |||
ae55fbac04 | |||
8c5f723619 | |||
90a1b412df | |||
dca26ac547 | |||
8c8f203465 | |||
aeb9930651 | |||
771f8d7380 | |||
80127401b5 | |||
667a72739c | |||
1a7a0a4630 | |||
5c5ed7a0b5 | |||
5abb360450 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,6 +1,9 @@
|
|||||||
# Build artefacts
|
# Build artefacts
|
||||||
bin/*
|
bin/*
|
||||||
|
|
||||||
|
# astyle formatter preserved files
|
||||||
|
*.orig
|
||||||
|
|
||||||
#
|
#
|
||||||
# Templates from https://github.com/github/gitignore
|
# Templates from https://github.com/github/gitignore
|
||||||
#
|
#
|
||||||
|
86
Makefile
Normal file
86
Makefile
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
###############################################################################
|
||||||
|
#
|
||||||
|
# Simple Makefile for Arduino MEGA 2560 C projects
|
||||||
|
#
|
||||||
|
###############################################################################
|
||||||
|
|
||||||
|
CC = avr-gcc
|
||||||
|
OBJCOPY = avr-objcopy
|
||||||
|
AVRDUDE = avrdude
|
||||||
|
CODE_FORMATTER = tooling/format-code.sh
|
||||||
|
|
||||||
|
BOARD = atmega2560
|
||||||
|
|
||||||
|
# Use shell command export to define it
|
||||||
|
# Example: export ARDUINO=/dev/ttyACM0
|
||||||
|
DEVICE = $(ARDUINO)
|
||||||
|
|
||||||
|
# Build artifacts
|
||||||
|
BINDIR = bin
|
||||||
|
TARGET = $(BINDIR)/$(BOARD)-user-code.ihx
|
||||||
|
ELF = $(BINDIR)/$(BOARD)-user-code.elf
|
||||||
|
|
||||||
|
# Source files. wildard "uses" all .c files in src directory
|
||||||
|
SRCDIR = src
|
||||||
|
SRC = $(wildcard $(SRCDIR)/*.c)
|
||||||
|
|
||||||
|
# Define object files from .c files defined above
|
||||||
|
OBJ=$(SRC:.c=.o)
|
||||||
|
|
||||||
|
# Compiler flags
|
||||||
|
# Note that those beginning with -D are acctually pre-processor macros
|
||||||
|
# -Wall ... -Wfatal-errors All possible warning options
|
||||||
|
# -Os Optimize code. The special option -Os is meant to turn on all -O2
|
||||||
|
# optimizations that are not expected to increase code size.
|
||||||
|
# -std=c11 use C11 standard
|
||||||
|
CFLAGS = -Wall \
|
||||||
|
-Wextra \
|
||||||
|
-Wpedantic \
|
||||||
|
-Wformat \
|
||||||
|
-pedantic-errors \
|
||||||
|
-Werror \
|
||||||
|
-Wfatal-errors \
|
||||||
|
-Os \
|
||||||
|
-mmcu=$(BOARD) \
|
||||||
|
-DF_CPU=16000000UL \
|
||||||
|
-DGIT_DESCR=\"$(shell git describe --abbrev=6 --dirty --always --tags --long)\" \
|
||||||
|
-std=c11
|
||||||
|
|
||||||
|
# Linker flags
|
||||||
|
LDFLAGS = -mmcu=$(BOARD)
|
||||||
|
|
||||||
|
OBJCOPYARGS = -O ihex \
|
||||||
|
-R .eeprom
|
||||||
|
|
||||||
|
# FIXME Find out why some Arduinos require -D to write code
|
||||||
|
AVRDUDEARGS = -p $(BOARD) \
|
||||||
|
-c wiring \
|
||||||
|
-F \
|
||||||
|
-P $(DEVICE) \
|
||||||
|
-b 115200 \
|
||||||
|
-V \
|
||||||
|
-D
|
||||||
|
|
||||||
|
all: $(ELF) $(TARGET)
|
||||||
|
|
||||||
|
%.o : %.c
|
||||||
|
$(CC) -c $(CFLAGS) -o $*.o $<
|
||||||
|
|
||||||
|
$(ELF): $(OBJ)
|
||||||
|
$(CC) $(LDFLAGS) $^ -o $@
|
||||||
|
|
||||||
|
$(TARGET):
|
||||||
|
$(OBJCOPY) $(OBJCOPYARGS) $(ELF) $(TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
#Do not remove .placeholder in BINDIR
|
||||||
|
find $(BINDIR) -type f -not -name '.placeholder' -print0 | xargs -0 rm -f --
|
||||||
|
rm -f $(SRCDIR)/*.o
|
||||||
|
|
||||||
|
install:
|
||||||
|
$(AVRDUDE) $(AVRDUDEARGS) -U flash:w:$(TARGET)
|
||||||
|
|
||||||
|
format:
|
||||||
|
$(CODE_FORMATTER) $(SRC)
|
||||||
|
|
||||||
|
.PHONY: clean install format
|
17
doc/Arduino-Mega-USB-UART-wiring.markdown
Normal file
17
doc/Arduino-Mega-USB-UART-wiring.markdown
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# Arduino Mega USB UART converter wiring
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
This wiring schema uses only Tx from Arduino and is suitable to be used as standard error console.
|
||||||
|
|
||||||
|
## Wiring illustration
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Wiring table
|
||||||
|
|
||||||
|
| Signal | ATMega2560 port and pin | Arduino Mega 2560 pin | USB UART converter pin |
|
||||||
|
| --- | --- | --- | --- |
|
||||||
|
| Ground (GND) | - | GND | GND |
|
||||||
|
| Transmit data from Arduino (TxD) | PORTJ 1 (TXD3) | 14 (TX3) | TxD |
|
||||||
|
|
BIN
doc/Arduino-Mega-USB-UART-wiring.png
Normal file
BIN
doc/Arduino-Mega-USB-UART-wiring.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 133 KiB |
41
src/main.c
41
src/main.c
@ -1,18 +1,47 @@
|
|||||||
|
#include <stdio.h>
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
|
#define __ASSERT_USE_STDERR
|
||||||
|
#include <assert.h>
|
||||||
|
#include "uart.h"
|
||||||
|
|
||||||
#define BLINK_DELAY_MS 1000
|
#define BLINK_DELAY_MS 100
|
||||||
|
|
||||||
int main (void)
|
int main (void)
|
||||||
{
|
{
|
||||||
/* set pin 25 of PORTB for output*/
|
/* set pin 3 of PORTA for output*/
|
||||||
DDRA |= _BV(DDA3);
|
DDRA |= _BV(DDA3);
|
||||||
while(1) {
|
/* Init error console as stderr in UART3 and print user code info */
|
||||||
/* set pin 25 high to turn led on */
|
uart3_init();
|
||||||
|
stderr = &uart3_out;
|
||||||
|
fprintf(stderr, "Version: %s built on: %s %s\n",
|
||||||
|
GIT_DESCR, __DATE__, __TIME__);
|
||||||
|
fprintf(stderr, "avr-libc version: %s\n", __AVR_LIBC_VERSION_STRING__);
|
||||||
|
/*End UART3 init and info print */
|
||||||
|
/* Test assert - REMOVE IN FUTURE LABS */
|
||||||
|
char *array;
|
||||||
|
uint32_t i = 1;
|
||||||
|
extern int __heap_start, *__brkval;
|
||||||
|
int v;
|
||||||
|
array = malloc( i * sizeof(char));
|
||||||
|
assert(array);
|
||||||
|
/* End test assert */
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
/* set pin 3 high to turn led on */
|
||||||
PORTA |= _BV(PORTA3);
|
PORTA |= _BV(PORTA3);
|
||||||
_delay_ms(BLINK_DELAY_MS);
|
_delay_ms(BLINK_DELAY_MS);
|
||||||
/* set pin 25 low to turn led off */
|
/* Test assert - REMOVE IN FUTURE LABS */
|
||||||
PORTA &= ~_BV(PORTA3);
|
/* Increase memory allocated for array by 100 chars
|
||||||
|
* until we have eaten it all and print space between Stack and Heap.
|
||||||
|
* Thats how assert works in run-time */
|
||||||
|
array = realloc( array, (i++ * 100) * sizeof(char));
|
||||||
|
fprintf(stderr, "%d ",
|
||||||
|
(int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval));
|
||||||
|
assert(array);
|
||||||
|
/* End test assert */
|
||||||
|
/* set pin 3 low to turn led off */
|
||||||
|
PORTA &= ~_BV(PORTA3);
|
||||||
_delay_ms(BLINK_DELAY_MS);
|
_delay_ms(BLINK_DELAY_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
72
src/uart.c
Normal file
72
src/uart.c
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
#include <avr/io.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#ifndef F_CPU
|
||||||
|
#define F_CPU 16000000UL
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef BAUD
|
||||||
|
#define BAUD 9600
|
||||||
|
#endif
|
||||||
|
#include <util/setbaud.h>
|
||||||
|
|
||||||
|
/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */
|
||||||
|
|
||||||
|
void uart0_init(void)
|
||||||
|
{
|
||||||
|
UBRR0H = UBRRH_VALUE;
|
||||||
|
UBRR0L = UBRRL_VALUE;
|
||||||
|
#if USE_2X
|
||||||
|
UCSR0A |= _BV(U2X0);
|
||||||
|
#else
|
||||||
|
UCSR0A &= ~(_BV(U2X0));
|
||||||
|
#endif
|
||||||
|
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
|
||||||
|
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
|
||||||
|
}
|
||||||
|
|
||||||
|
void uart3_init(void)
|
||||||
|
{
|
||||||
|
UBRR3H = UBRRH_VALUE;
|
||||||
|
UBRR3L = UBRRL_VALUE;
|
||||||
|
#if USE_2X
|
||||||
|
UCSR3A |= _BV(U2X3);
|
||||||
|
#else
|
||||||
|
UCSR3A &= ~(_BV(U2X3));
|
||||||
|
#endif
|
||||||
|
UCSR3C = _BV(UCSZ31) | _BV(UCSZ30); /* 8-bit data */
|
||||||
|
UCSR3B = _BV(TXEN3); /* Enable TX */
|
||||||
|
}
|
||||||
|
|
||||||
|
int uart0_putchar(char c, FILE *stream)
|
||||||
|
{
|
||||||
|
(void) stream;
|
||||||
|
|
||||||
|
if (c == '\n') {
|
||||||
|
uart0_putchar('\r', stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||||
|
UDR0 = c;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int uart3_putchar(char c, FILE *stream)
|
||||||
|
{
|
||||||
|
(void) stream;
|
||||||
|
|
||||||
|
if (c == '\n') {
|
||||||
|
uart3_putchar('\r', stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
loop_until_bit_is_set(UCSR3A, UDRE3);
|
||||||
|
UDR3 = c;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int uart0_getchar(FILE *stream)
|
||||||
|
{
|
||||||
|
(void) stream;
|
||||||
|
loop_until_bit_is_set(UCSR0A, RXC0);
|
||||||
|
return UDR0;
|
||||||
|
}
|
12
src/uart.h
Normal file
12
src/uart.h
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
int uart0_putchar(char c, FILE *stream);
|
||||||
|
int uart0_getchar(FILE *stream);
|
||||||
|
|
||||||
|
int uart3_putchar(char c, FILE *stream);
|
||||||
|
|
||||||
|
void uart0_init(void);
|
||||||
|
void uart3_init(void);
|
||||||
|
|
||||||
|
/* http://www.ermicro.com/blog/?p=325 */
|
||||||
|
|
||||||
|
FILE uart0_io = FDEV_SETUP_STREAM(uart0_putchar, uart0_getchar, _FDEV_SETUP_RW);
|
||||||
|
FILE uart3_out = FDEV_SETUP_STREAM(uart3_putchar, NULL, _FDEV_SETUP_WRITE);
|
48
tooling/format-code.sh
Executable file
48
tooling/format-code.sh
Executable file
@ -0,0 +1,48 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Simple script for code formatting in 1tbs
|
||||||
|
# See http://astyle.sourceforge.net/astyle.html for syntax and defaults
|
||||||
|
|
||||||
|
MINPARAMS=1
|
||||||
|
ORIG_SUFFIX=orig
|
||||||
|
|
||||||
|
if [ $# -lt "$MINPARAMS" ]
|
||||||
|
then
|
||||||
|
echo "This script needs C source files passed as arguments"
|
||||||
|
echo "USAGE: format-code.sh src/main.c src/somecode.c ..."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
#
|
||||||
|
for FILE in "$@"
|
||||||
|
do
|
||||||
|
RESULT="$(astyle --style=1tbs \
|
||||||
|
--indent-col1-comments \
|
||||||
|
--break-blocks \
|
||||||
|
--pad-oper \
|
||||||
|
--pad-header \
|
||||||
|
--delete-empty-lines \
|
||||||
|
--add-brackets \
|
||||||
|
--convert-tabs \
|
||||||
|
--max-code-length=80 \
|
||||||
|
--break-after-logical \
|
||||||
|
--mode=c \
|
||||||
|
--suffix=.$ORIG_SUFFIX \
|
||||||
|
--lineend=linux \
|
||||||
|
$FILE)"
|
||||||
|
|
||||||
|
# if file unchanged print unchanged result message
|
||||||
|
if [[ "$RESULT" = Unchanged* ]]
|
||||||
|
then
|
||||||
|
echo "$RESULT"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# if file formatted print result and renamed file name
|
||||||
|
if [[ "$RESULT" = Formatted* ]]
|
||||||
|
then
|
||||||
|
echo "$RESULT"
|
||||||
|
echo "Original code was preserved in file $FILE.$ORIG_SUFFIX"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
exit 0
|
Reference in New Issue
Block a user