Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
bdd27a6bea | |||
a882534979 | |||
a7a915faf4 | |||
f1f06f056a | |||
5b78b7da02 | |||
713cc80417 | |||
7d422c5494 | |||
8436ba76b0 | |||
9cd6f6cf82 | |||
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
|
||||
bin/*
|
||||
|
||||
# astyle formatter preserved files
|
||||
*.orig
|
||||
|
||||
#
|
||||
# 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
|
4
build.sh
4
build.sh
@ -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
|
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 |
12
src/hmi_msg.h
Normal file
12
src/hmi_msg.h
Normal 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_ */
|
44
src/main.c
44
src/main.c
@ -1,17 +1,51 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <avr/io.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)
|
||||
{
|
||||
/* set pin 25 of PORTB for output*/
|
||||
/* set pin 3 of PORTA for output*/
|
||||
DDRA |= _BV(DDA3);
|
||||
while(1) {
|
||||
/* set pin 25 high to turn led on */
|
||||
/* 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) {
|
||||
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);
|
||||
_delay_ms(BLINK_DELAY_MS);
|
||||
/* set pin 25 low to turn led off */
|
||||
/* set pin 3 low to turn led off */
|
||||
PORTA &= ~_BV(PORTA3);
|
||||
_delay_ms(BLINK_DELAY_MS);
|
||||
}
|
||||
|
27
src/print_helper.c
Normal file
27
src/print_helper.c
Normal 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
7
src/print_helper.h
Normal 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
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;
|
||||
}
|
17
src/uart.h
Normal file
17
src/uart.h
Normal 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
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
|
51
tooling/package-and-deliver.sh
Executable file
51
tooling/package-and-deliver.sh
Executable 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
|
Reference in New Issue
Block a user