23 Commits

Author SHA1 Message Date
bdd27a6bea Add 0 to 127 ascii print out and month lookup 2016-09-26 19:57:14 +03:00
a882534979 Fix bug in print_for_human 2016-09-26 19:56:46 +03:00
a7a915faf4 Remove assert code and add ascii table print 2016-09-26 17:33:47 +03:00
f1f06f056a Add humaninterface messages header 2016-09-26 17:33:21 +03:00
5b78b7da02 Add print helper functions 2016-09-26 17:33:02 +03:00
713cc80417 Add header guard to uart.h 2016-09-26 16:39:07 +03:00
7d422c5494 Merge gitlab.com:artizirk/skeleton into HEAD 2016-09-26 16:31:33 +03:00
8436ba76b0 Remove unnecessary build.sh script 2016-09-19 18:01:25 +03:00
9cd6f6cf82 Add package-and-deliver.sh script 2016-09-18 23:55:33 +03:00
d827bbe427 Fix uart3_init comment 2016-09-10 01:09:54 +03:00
85dca87ff3 Added assert test to main.c with stderr debug console output 2016-09-10 00:57:01 +03:00
ae55fbac04 Renamed uart functions and added support for stderr via uart3 2016-09-10 00:56:05 +03:00
8c5f723619 Add uart functions from https://github.com/tuupola/avr_demo 2016-09-10 00:23:43 +03:00
90a1b412df Formated the code with astyle 2016-09-10 00:23:14 +03:00
dca26ac547 Merge branch 'master' of https://gitlab.com/I237-2016/skeleton 2016-09-09 23:54:45 +03:00
8c8f203465 Add Arduino Mega UART wiring schema 2016-09-08 16:14:02 +03:00
aeb9930651 Fix doc file names 2016-09-08 15:52:29 +03:00
771f8d7380 Add Arduino Mega UART wiring schema 2016-09-08 14:41:27 +03:00
80127401b5 Amend git describe to always show short SHA and fix macro name 2016-09-08 13:12:20 +03:00
667a72739c Add avrdude -D argument. Some ardus do not write new user code without it 2016-09-08 12:40:53 +03:00
1a7a0a4630 Do not remove placeholder in make clean target 2016-09-08 09:36:00 +03:00
5c5ed7a0b5 Add Makefile 2016-09-06 22:34:10 +03:00
5abb360450 Add astyle code formatter usage script and change .gitignore to exclude astyle preserved files 2016-09-06 22:15:30 +03:00
13 changed files with 380 additions and 10 deletions

3
.gitignore vendored
View File

@ -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
View 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

View File

@ -1,4 +0,0 @@
avr-gcc -Os -DF_CPU=16000000UL -Wall -Wextra -Wpedantic -Wformat -pedantic-errors -Werror -Wfatal-errors -mmcu=atmega2560 -c -o src/main.o src/main.c
avr-gcc -mmcu=atmega2560 src/main.o -o bin/atmega2560-user-code.elf
avr-objcopy -O ihex -R .eeprom bin/atmega2560-user-code.elf bin/atmega2560-user-code.ihx
avrdude -v -F -V -c stk500v2 -p m2560 -P /dev/ttyACM0 -b 115200 -U flash:w:bin/atmega2560-user-code.ihx

View 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
![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 |

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB

12
src/hmi_msg.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef _HMI_MSG_H_
#define _HMI_MSG_H_
#define STUD_NAME "Arti Zirk"
const char *ENG_MONTH[6] = {"January",
"February",
"March",
"April",
"May",
"June",};
#endif /* _HMI_MSG_H_ */

View File

@ -1,17 +1,51 @@
#include <stdio.h>
#include <string.h>
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#include "hmi_msg.h"
#include "uart.h"
#include "print_helper.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);
/* Init stdio on UART0 and UART3 and print user code info */
uart0_init();
uart3_init();
stdout = stdin = &uart0_io;
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 stdio init and info print */
fprintf(stdout, STUD_NAME "\n");
/* ASCII table print */
print_ascii_tbl(stdout);
unsigned char ascii[128] = {0};
for (unsigned char i = 0; i < sizeof(ascii); i++) {
ascii[i] = i;
}
print_for_human(stdout, ascii, sizeof(ascii));
while (1) { while (1) {
/* set pin 25 high to turn led on */ char month_first_leter;
fprintf(stdout, "Enter Month name first letter >");
fscanf(stdin, "%c", &month_first_leter);
fprintf(stdout, "%c\n", month_first_leter);
for (int i = 0; i < 6; i++) {
if (!strncmp(strupr(&month_first_leter), ENG_MONTH[i], 1)) {
fprintf(stdout, "%s\n", ENG_MONTH[i]);
}
}
/* 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 */ /* set pin 3 low to turn led off */
PORTA &= ~_BV(PORTA3); PORTA &= ~_BV(PORTA3);
_delay_ms(BLINK_DELAY_MS); _delay_ms(BLINK_DELAY_MS);
} }

27
src/print_helper.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include "print_helper.h"
int print_ascii_tbl (FILE *stream) {
for (char c = ' '; c <= '~'; c++) {
if(!fprintf(stream, "%c ", c)) {
return 0;
}
}
return fprintf(stream, "\n");
}
int print_for_human (FILE *stream, const unsigned char *array, const int len) {
for (int i = 0; i < len; i++) {
if (array[i] >= ' ' && array[i] <= '~') {
if(!fprintf(stream, "%c", array[i])) {
return 0;
}
} else {
if(!fprintf(stream, "\"0x%02x\"", array[i])) {
return 0;
}
}
}
return fprintf(stream, "\n");;
}

7
src/print_helper.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef _PRINT_HELPER_H_
#define _PRINT_HELPER_H_
int print_ascii_tbl (FILE *stream);
int print_for_human (FILE *stream, const unsigned char *array, const int len);
#endif /* _PRINT_HELPER_H_ */

72
src/uart.c Normal file
View 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;
}

17
src/uart.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef _UART_H_
#define _UART_H_
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);
#endif /* _UART_H_ */

48
tooling/format-code.sh Executable file
View 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

51
tooling/package-and-deliver.sh Executable file
View File

@ -0,0 +1,51 @@
#!/bin/bash
echo "Test project build"
make clean && make
if [ $? -ne 0 ] ; then
echo "Build failed!"
exit 1
else
echo "Build OK!"
fi
echo "Format code"
make format
# Test if there are changed files that are not commited
if [ -n "$(git status --porcelain)" ] ; then
echo "Uncommited files detected"
git status
exit 1
else
echo "OK"
fi
echo "Currently set tags on this project"
git tag
echo -n "Are the required tags added? (Y/n)"
read ANSWER
if [ "$ANSWER" == "n" ]; then
echo "Please add required tags"
exit 1
fi
echo "Packaging the project"
make clean && make
TEMP_DIR=$(mktemp -d)
cp bin/atmega2560-user-code.ihx $TEMP_DIR
make clean
git archive --format=tar.gz -o $TEMP_DIR/$(git describe --abbrev=6 --dirty --always --tags --long).tar.gz HEAD
mv $TEMP_DIR/* bin/
rm -rf $TEMP_DIR
echo "Project packaging succeeded"
exit 0