diff --git a/.gitignore b/.gitignore index 9a596bf..96aec6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ # Build artefacts bin/* +# astyle formatter preserved files +*.orig + # # Templates from https://github.com/github/gitignore # diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5f4fe7a --- /dev/null +++ b/Makefile @@ -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 \ No newline at end of file diff --git a/doc/Arduino-Mega-USB-UART-wiring.markdown b/doc/Arduino-Mega-USB-UART-wiring.markdown new file mode 100644 index 0000000..8bfb0f9 --- /dev/null +++ b/doc/Arduino-Mega-USB-UART-wiring.markdown @@ -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 + +![Arduino Mega USB UART wiring.png](Arduino-Mega-USB-UART-wiring.png) + +## 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 | + diff --git a/doc/Arduino-Mega-USB-UART-wiring.png b/doc/Arduino-Mega-USB-UART-wiring.png new file mode 100644 index 0000000..71eec7f Binary files /dev/null and b/doc/Arduino-Mega-USB-UART-wiring.png differ diff --git a/tooling/format-code.sh b/tooling/format-code.sh new file mode 100755 index 0000000..5a8fdef --- /dev/null +++ b/tooling/format-code.sh @@ -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