From e8e5650006961593559b57176c4d2916b5c32d4e Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Tue, 26 Feb 2013 16:44:22 +0100 Subject: restructure c implementation directory --- arduino/ace/.gitignore | 7 - arduino/ace/Makefile | 7 - arduino/ace/ace.c | 116 --------------- arduino/ace/ace.h | 27 ---- arduino/ace/arduino.mk | 379 ------------------------------------------------- arduino/ace/arq.c | 122 ---------------- arduino/ace/arq.h | 52 ------- arduino/ace/framing.c | 88 ------------ arduino/ace/framing.h | 47 ------ arduino/ace/test.ino | 44 ------ c/arduino/.gitignore | 7 + c/arduino/Makefile | 7 + c/arduino/ace.c | 116 +++++++++++++++ c/arduino/ace.h | 27 ++++ c/arduino/arduino.mk | 379 +++++++++++++++++++++++++++++++++++++++++++++++++ c/arduino/arq.c | 122 ++++++++++++++++ c/arduino/arq.h | 52 +++++++ c/arduino/framing.c | 88 ++++++++++++ c/arduino/framing.h | 47 ++++++ c/arduino/test.ino | 44 ++++++ 20 files changed, 889 insertions(+), 889 deletions(-) delete mode 100644 arduino/ace/.gitignore delete mode 100644 arduino/ace/Makefile delete mode 100644 arduino/ace/ace.c delete mode 100644 arduino/ace/ace.h delete mode 100644 arduino/ace/arduino.mk delete mode 100644 arduino/ace/arq.c delete mode 100644 arduino/ace/arq.h delete mode 100644 arduino/ace/framing.c delete mode 100644 arduino/ace/framing.h delete mode 100644 arduino/ace/test.ino create mode 100644 c/arduino/.gitignore create mode 100644 c/arduino/Makefile create mode 100644 c/arduino/ace.c create mode 100644 c/arduino/ace.h create mode 100644 c/arduino/arduino.mk create mode 100644 c/arduino/arq.c create mode 100644 c/arduino/arq.h create mode 100644 c/arduino/framing.c create mode 100644 c/arduino/framing.h create mode 100644 c/arduino/test.ino diff --git a/arduino/ace/.gitignore b/arduino/ace/.gitignore deleted file mode 100644 index a9dd55f..0000000 --- a/arduino/ace/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -.lib/ -.dep/ -*.o -*.hex -*.elf -*~ - diff --git a/arduino/ace/Makefile b/arduino/ace/Makefile deleted file mode 100644 index 3e0f36d..0000000 --- a/arduino/ace/Makefile +++ /dev/null @@ -1,7 +0,0 @@ -#SOURCES := main.cc other.cc -#LIBRARIES := EEPROM - -ARDUINODIR=$(ARDUINO_HOME) -BOARD=mega2560 -#export LIBRARIES=LiquidCrystal -include arduino.mk diff --git a/arduino/ace/ace.c b/arduino/ace/ace.c deleted file mode 100644 index ea05e1c..0000000 --- a/arduino/ace/ace.c +++ /dev/null @@ -1,116 +0,0 @@ -#include "ace.h" - -#include - - -typedef struct { - uint8_t buffer[SERIAL_BUFFER_SIZE]; - volatile uint16_t head; - volatile uint16_t tail; -} ring_buffer; - -static ring_buffer tx_buffer0 = {{0}, 0, 0}; -static framer framer0; -static arq arq0; -static uint32_t max_ticks0; -static uint32_t ticks0; - -//initialize UART -static void init_s0(uint32_t baud) { - UCSR0A |= (1 << U2X0); //enable double speed transmission - uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2; - UBRR0H = baud_setting >> 8; - UBRR0L = baud_setting; - UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); // defaults to 8-bit, no parity, 1 stop bit - UCSR0B &= ~(1 << UDRIE0) | (1 << TXEN0) | (1 << RXCIE0); -} - -static void send_to_s0(uint8_t byte) { - uint16_t i = (tx_buffer0.head + 1) % SERIAL_BUFFER_SIZE; - // If the output buffer is full, there's nothing for it other than to - // wait for the interrupt handler to empty it a bit - if (i == tx_buffer0.tail) return; - //while (i == tx_buffer0.tail) {}; - tx_buffer0.buffer[tx_buffer0.head] = byte; - tx_buffer0.head = i; - //enable data register empty interrupt - UCSR0B |= (1 << UDRIE0); -} - -static void send_to_framer0(int16_t size, const uint8_t* const data) { - send_frame(&framer0, size, data); -} - -static void receive_from_framer0(int16_t size, uint8_t* data) { - receive_frame(&arq0, size, data); -} - -//called when byte is received -ISR(USART0_RX_vect) { - uint8_t c = UDR0; - receive_byte(&framer0, c); -} - -//called when data register is empty -ISR(USART0_UDRE_vect) { - if (tx_buffer0.head == tx_buffer0.tail) { - UCSR0B &= ~(1 << UDRIE0); //buffer empty, disable interruot - } else { - uint8_t c = tx_buffer0.buffer[tx_buffer0.tail]; - tx_buffer0.tail = (tx_buffer0.tail + 1) % SERIAL_BUFFER_SIZE; - UDR0 = c; //write next byte - } -} - -//initialize timer -static void init_t0(uint32_t ticks) { - cli(); - TCCR1A = 0; - TCCR1B = 0; - TCCR1B |= (1 << WGM12); // turn on CTC mode: - TCCR1B |= (1 << CS11); // set CS31 for 64 prescaler - // set compare match register to desired timer count: - OCR1A = F_CPU / 1000 / 64; // should be 250 for F_CPU=16Mhz and f = 1000Hz - sei(); - //the timer should now interrupt every ms - max_ticks0 = ticks; - ticks0 = 0; -} - -inline static void start_t0() { - TIMSK1 |= (1 << OCIE1A); -} - -inline static void stop_t0() { - TIMSK1 &= ~(1 << OCIE1A); -} - -ISR(TIMER1_COMPA_vect) { - ticks0 += 1; - if (ticks0 > max_ticks0) { - timeout(&arq0); - ticks0 = 0; - } -} - -void init_ace0(uint32_t baud, uint32_t extra_time) { - init_s0(baud); - init_framer(&framer0); - init_arq(&arq0); - - framer0.sender = send_to_s0; - arq0.sender = send_to_framer0; - - framer0.receiver = receive_from_framer0; - arq0.event_handler = ace_event0; - - uint32_t bits = (MAX_FRAME_SIZE + 3) * 8 * 2; - uint32_t tx_time = bits * 1000 / baud; // transmission time in milliseconds - init_t0(tx_time + extra_time); - arq0.start_timer = start_t0; - arq0.stop_timer = stop_t0; -} - -void ace_send0(uint8_t size, const uint8_t* const message) { - send_message(&arq0, size, message); -} diff --git a/arduino/ace/ace.h b/arduino/ace/ace.h deleted file mode 100644 index 75b178c..0000000 --- a/arduino/ace/ace.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef ACE_H -#define ACE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include "framing.h" -#include "arq.h" - -#define SERIAL_BUFFER_SIZE 64 - -void init_ace0(uint32_t baud, uint32_t extra_time); -void ace_send0(uint8_t size, const uint8_t* const message); -void ace_event0(message_event event, int16_t size, const uint8_t* const message); - -#define init_ace init_ace0 -#define ace_send ace_send0 -#define ace_event ace_event0 - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif /* ACE_H */ diff --git a/arduino/ace/arduino.mk b/arduino/ace/arduino.mk deleted file mode 100644 index 9fce9c7..0000000 --- a/arduino/ace/arduino.mk +++ /dev/null @@ -1,379 +0,0 @@ -#_______________________________________________________________________________ -# -# edam's Arduino makefile -#_______________________________________________________________________________ -# version 0.4 -# -# Copyright (C) 2011, 2012 Tim Marston . -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included in -# all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. -# -#_______________________________________________________________________________ -# -# -# This is a general purpose makefile for use with Arduino hardware and -# software. It works with the arduino-1.0 software release. To download the -# latest version of this makefile, visit the following website, where you can -# also find more information and documentation on it's use. The following text -# can only really be considered a reference to it's use. -# -# http://ed.am/dev/make/arduino-mk -# -# This makefile can be used as a drop-in replacement for the Arduino IDE's -# build system. To use it, save arduino.mk somewhere (I keep mine at -# ~/src/arduino.mk) and create a symlink to it in your project directory named -# "Makefile". For example: -# -# $ ln -s ~/src/arduino.mk Makefile -# -# The Arduino software (version 1.0 or later) is required. If you are using -# Debian (or a derivative), type `apt-get install arduino`. Otherwise, you -# will have to download the Arduino software manually from http://arduino.cc/. -# It is suggested that you install it at ~/opt/arduino if you are unsure. -# -# If you downloaded the Arduino software manually and unpacked it somewhere -# other than ~/opt/arduino, you will need to set up ARDUINODIR to be the path -# where you unpacked it. (If unset, ARDUINODIR defaults to ~/opt/arduino and -# then /usr/share/arduino, in that order.) You might be best to set this in -# your ~/.profile by adding something like this: -# -# export ARDUINODIR=~/somewhere/arduino-1.0 -# -# You will also need to set BOARD to the type of Arduino you're building for. -# Type `make boards` for a list of acceptable values. You could set a default -# in your ~/.profile if you want, but it is suggested that you specify this at -# build time, especially if you work with different types of Arduino. For -# example: -# -# $ export BOARD=uno -# $ make -# -# You may also need to set SERIALDEV if it is not detected correctly. -# -# The presence of a .ino (or .pde) file causes the arduino.mk to automatically -# determine values for SOURCES, TARGET and LIBRARIES. Any .c, .cc and .cpp -# files in the project directory (or any "util" or "utility" subdirectories) -# are automatically included in the build and are scanned for Arduino libraries -# that have been #included. Note, there can only be one .ino (or .pde) file. -# -# Alternatively, if you want to manually specify build variables, create a -# Makefile that defines SOURCES and LIBRARIES and then includes arduino.mk. -# (There is no need to define TARGET). Here is an example Makefile: -# -# SOURCES := main.cc other.cc -# LIBRARIES := EEPROM -# include ~/src/arduino.mk -# -# Here is a complete list of configuration parameters: -# -# ARDUINODIR The path where the Arduino software is installed on your system. -# -# ARDUINOCONST The Arduino software version, as an integer, used to define the -# ARDUINO version constant. This defaults to 100 if undefined. -# -# AVRDUDECONF The avrdude.conf to use. If undefined, this defaults to a guess -# based on where the avrdude in use is. If empty, no avrdude.conf -# is passed to avrdude (to the system default is used). -# -# AVRTOOLSPATH A space-separated list of directories to search in order when -# looking for the avr build tools. This defaults to the system PATH -# followed by subdirectories in ARDUINODIR if undefined. -# -# BOARD Specify a target board type. Run `make boards` to see available -# board types. -# -# LIBRARIES A list of Arduino libraries to build and include. This is set -# automatically if a .ino (or .pde) is found. -# -# SERIALDEV The unix device name of the serial device that is the Arduino. -# If unspecified, an attempt is made to determine the name of a -# connected Arduino's serial device. -# -# SOURCES A list of all source files of whatever language. The language -# type is determined by the file extension. This is set -# automatically if a .ino (or .pde) is found. -# -# TARGET The name of the target file. This is set automatically if a -# .ino (or .pde) is found, but it is not necessary to set it -# otherwise. -# -# This makefile also defines the following goals for use on the command line -# when you run make: -# -# all This is the default if no goal is specified. It builds the -# target. -# -# target Builds the target. -# -# upload Uploads the last built target to an attached Arduino. -# -# clean Deletes files created during the build. -# -# boards Display a list of available board names, so that you can set the -# BOARD environment variable appropriately. -# -# monitor Start `screen` on the serial device. This is meant to be an -# equivalent to the Arduino serial monitor. -# -# size Displays size information about the bulit target. -# -# Builds the specified file, either an object file or the target, -# from those that that would be built for the project. -#_______________________________________________________________________________ -# - -# default arduino software directory, check software exists -ifndef ARDUINODIR -ARDUINODIR := $(firstword $(wildcard ~/opt/arduino /usr/share/arduino)) -endif -ifeq "$(wildcard $(ARDUINODIR)/hardware/arduino/boards.txt)" "" -$(error ARDUINODIR is not set correctly; arduino software not found) -endif - -# default arduino version -ARDUINOCONST ?= 100 - -# default path for avr tools -ifndef AVRTOOLSPATH -AVRTOOLSPATH := $(subst :, , $(PATH)) -AVRTOOLSPATH += $(ARDUINODIR)/hardware/tools -AVRTOOLSPATH += $(ARDUINODIR)/hardware/tools/avr/bin -endif - -# auto mode? -INOFILE := $(wildcard *.ino *.pde) -ifdef INOFILE -ifneq "$(words $(INOFILE))" "1" -$(error There is more than one .pde or .ino file in this directory!) -endif - -# automatically determine sources and targeet -TARGET := $(basename $(INOFILE)) -SOURCES := $(INOFILE) \ - $(wildcard *.c *.cc *.cpp) \ - $(wildcard $(addprefix util/, *.c *.cc *.cpp)) \ - $(wildcard $(addprefix utility/, *.c *.cc *.cpp)) - -# automatically determine included libraries -LIBRARIES := $(filter $(notdir $(wildcard $(ARDUINODIR)/libraries/*)), \ - $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES))) - -endif - -# no serial device? make a poor attempt to detect an arduino -SERIALDEVGUESS := 0 -ifeq "$(SERIALDEV)" "" -SERIALDEV := $(firstword $(wildcard \ - /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbserial* /dev/tty.usbmodem*)) -SERIALDEVGUESS := 1 -endif - -# software -findsoftware = $(firstword $(wildcard $(addsuffix /$(1), $(AVRTOOLSPATH)))) -CC := $(call findsoftware,avr-gcc) -CXX := $(call findsoftware,avr-g++) -LD := $(call findsoftware,avr-ld) -AR := $(call findsoftware,avr-ar) -OBJCOPY := $(call findsoftware,avr-objcopy) -AVRDUDE := $(call findsoftware,avrdude) -AVRSIZE := $(call findsoftware,avr-size) - -# files -TARGET := $(if $(TARGET),$(TARGET),a.out) -OBJECTS := $(addsuffix .o, $(basename $(SOURCES))) -DEPFILES := $(patsubst %, .dep/%.dep, $(SOURCES)) -ARDUINOCOREDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino -ARDUINOLIB := .lib/arduino.a -ARDUINOLIBLIBSDIR := $(ARDUINODIR)/libraries -ARDUINOLIBLIBSPATH := $(foreach lib, $(LIBRARIES), \ - $(ARDUINODIR)/libraries/$(lib)/ $(ARDUINODIR)/libraries/$(lib)/utility/ ) -ARDUINOLIBOBJS := $(foreach dir, $(ARDUINOCOREDIR) $(ARDUINOLIBLIBSPATH), \ - $(patsubst %, .lib/%.o, $(wildcard $(addprefix $(dir)/, *.c *.cpp)))) -ifeq "$(AVRDUDECONF)" "" -ifeq "$(AVRDUDE)" "$(ARDUINODIR)/hardware/tools/avr/bin/avrdude" -AVRDUDECONF := $(ARDUINODIR)/hardware/tools/avr/etc/avrdude.conf -else -AVRDUDECONF := $(wildcard $(AVRDUDE).conf) -endif -endif - -# no board? -ifndef BOARD -ifneq "$(MAKECMDGOALS)" "boards" -ifneq "$(MAKECMDGOALS)" "clean" -$(error BOARD is unset. Type 'make boards' to see possible values) -endif -endif -endif - -# obtain board parameters from the arduino boards.txt file -BOARDS_FILE := $(ARDUINODIR)/hardware/arduino/boards.txt -BOARD_BUILD_MCU := \ - $(shell sed -ne "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_FILE)) -BOARD_BUILD_FCPU := \ - $(shell sed -ne "s/$(BOARD).build.f_cpu=\(.*\)/\1/p" $(BOARDS_FILE)) -BOARD_BUILD_VARIANT := \ - $(shell sed -ne "s/$(BOARD).build.variant=\(.*\)/\1/p" $(BOARDS_FILE)) -BOARD_UPLOAD_SPEED := \ - $(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE)) -BOARD_UPLOAD_PROTOCOL := \ - $(shell sed -ne "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_FILE)) -BOARD_USB_VID := \ - $(shell sed -ne "s/$(BOARD).build.vid=\(.*\)/\1/p" $(BOARDS_FILE)) -BOARD_USB_PID := \ - $(shell sed -ne "s/$(BOARD).build.pid=\(.*\)/\1/p" $(BOARDS_FILE)) - -# invalid board? -ifeq "$(BOARD_BUILD_MCU)" "" -ifneq "$(MAKECMDGOALS)" "boards" -ifneq "$(MAKECMDGOALS)" "clean" -$(error BOARD is invalid. Type 'make boards' to see possible values) -endif -endif -endif - -# flags -CPPFLAGS := -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU) -CPPFLAGS += -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=$(ARDUINOCONST) -CPPFLAGS += -DUSB_VID=$(BOARD_USB_VID) -DUSB_PID=$(BOARD_USB_PID) -CPPFLAGS += -I. -Iutil -Iutility -I $(ARDUINOCOREDIR) -CPPFLAGS += -I $(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/ -CPPFLAGS += $(addprefix -I $(ARDUINODIR)/libraries/, $(LIBRARIES)) -CPPFLAGS += $(patsubst %, -I $(ARDUINODIR)/libraries/%/utility, $(LIBRARIES)) -CPPDEPFLAGS = -MMD -MP -MF .dep/$<.dep -CPPINOFLAGS := -x c++ -include $(ARDUINOCOREDIR)/Arduino.h -AVRDUDEFLAGS := $(addprefix -C , $(AVRDUDECONF)) -DV -AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV) -AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED) -LINKFLAGS := -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU) - -# figure out which arg to use with stty (for OS X, GNU and busybox stty) -STTYFARG := $(shell stty --help 2>&1 | \ - grep -q 'illegal option' && echo -f || echo -F) - -# include dependencies -ifneq "$(MAKECMDGOALS)" "clean" --include $(DEPFILES) -endif - -# default rule -.DEFAULT_GOAL := all - -#_______________________________________________________________________________ -# RULES - -.PHONY: all target upload clean boards monitor size - -all: target - -target: $(TARGET).hex - -upload: target - @echo "\nUploading to board..." - @test -n "$(SERIALDEV)" || { \ - echo "error: SERIALDEV could not be determined automatically." >&2; \ - exit 1; } - @test 0 -eq $(SERIALDEVGUESS) || { \ - echo "*GUESSING* at serial device:" $(SERIALDEV); \ - echo; } - stty $(STTYFARG) $(SERIALDEV) hupcl - $(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i - -clean: - rm -f $(OBJECTS) - rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~ - rm -rf .lib .dep - -boards: - @echo Available values for BOARD: - @sed -nEe '/^#/d; /^[^.]+\.name=/p' $(BOARDS_FILE) | \ - sed -Ee 's/([^.]+)\.name=(.*)/\1 \2/' \ - -e 's/(.{12}) *(.*)/\1 \2/' - -monitor: - @test -n "$(SERIALDEV)" || { \ - echo "error: SERIALDEV could not be determined automatically." >&2; \ - exit 1; } - @test -n `which screen` || { \ - echo "error: can't find GNU screen, you might need to install it." >&2 \ - ecit 1; } - @test 0 -eq $(SERIALDEVGUESS) || { \ - echo "*GUESSING* at serial device:" $(SERIALDEV); \ - echo; } - screen $(SERIALDEV) - -size: $(TARGET).elf - echo && $(AVRSIZE) --format=avr --mcu=$(BOARD_BUILD_MCU) $(TARGET).elf - -# building the target - -$(TARGET).hex: $(TARGET).elf - $(OBJCOPY) -O ihex -R .eeprom $< $@ - -.INTERMEDIATE: $(TARGET).elf - -$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS) - $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -lm -o $@ - -%.o: %.c - mkdir -p .dep/$(dir $<) - $(COMPILE.c) $(CPPDEPFLAGS) -o $@ $< - -%.o: %.cpp - mkdir -p .dep/$(dir $<) - $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $< - -%.o: %.cc - mkdir -p .dep/$(dir $<) - $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $< - -%.o: %.C - mkdir -p .dep/$(dir $<) - $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $< - -%.o: %.ino - mkdir -p .dep/$(dir $<) - $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $< - -%.o: %.pde - mkdir -p .dep/$(dir $<) - $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ -x c++ -include $(ARDUINOCOREDIR)/Arduino.h $< - -# building the arduino library - -$(ARDUINOLIB): $(ARDUINOLIBOBJS) - $(AR) rcs $@ $? - -.lib/%.c.o: %.c - mkdir -p $(dir $@) - $(COMPILE.c) -o $@ $< - -.lib/%.cpp.o: %.cpp - mkdir -p $(dir $@) - $(COMPILE.cpp) -o $@ $< - -.lib/%.cc.o: %.cc - mkdir -p $(dir $@) - $(COMPILE.cpp) -o $@ $< - -.lib/%.C.o: %.C - mkdir -p $(dir $@) - $(COMPILE.cpp) -o $@ $< diff --git a/arduino/ace/arq.c b/arduino/ace/arq.c deleted file mode 100644 index 9d4a08c..0000000 --- a/arduino/ace/arq.c +++ /dev/null @@ -1,122 +0,0 @@ -#include "arq.h" - -#include -/* Expected frame data for ARQ - * - * +-----+-----+------+ - * | seq | cmd | data | - * +-----+-----+------+ - * - * seq: sequence number of frame (1 byte) - * cmd: command id of frame (1 byte), currently either ACK or DATA - * data: actual data of frame (arbitraty length, respecting frame limitations) - * - * Note: frame checking, headers and footers are not handled by arq, see 'framing' instead - * -*/ - -#define MAX_RESENDS 5 //number of resends before a timeout is generated -#define MAX_SEQ 255 //maximum sequence number of frames -#define DATA 0x05 -#define ACK 0x06 - -#define SEQ_INDEX 0 -#define CMD_INDEX 1 -#define MESSAGE_OFFSET 2 -#define MAX_MESSAGE_SIZE MAX_FRAME_SIZE - MESSAGE_OFFSET - - -typedef enum { - RECEIVED_FRAME, - TIMEOUT -} arq_event; - -static void send_ack(arq* a, uint8_t seq) { - uint8_t data[] = {seq, ACK}; - a->sender(2, data); -} - -static void process_event(arq* a, arq_event event, int16_t data_size, uint8_t* data) { - if (event == RECEIVED_FRAME) { - uint8_t seq = data[SEQ_INDEX]; - uint8_t cmd = data[CMD_INDEX]; - uint8_t* message = &(data[MESSAGE_OFFSET]); - int16_t message_size = data_size - MESSAGE_OFFSET; - - if (!a->awaiting_ack) { //ready to receive - if (cmd == DATA) { - if (a->last_received_seq != seq) { - a->last_received_seq = seq; - a->event_handler(RECEIVED, message_size, message); - } - send_ack(a, seq); - } - //ignore case in which an ack is received even though none is awaited - } else { //awaiting ack - a->awaiting_ack = false; - a->stop_timer(); - - if (cmd == ACK && seq == a->last_sent_buffer[SEQ_INDEX]) { - a->event_handler(SEND_SUCCESS, a->last_sent_size - MESSAGE_OFFSET, &(a->last_sent_buffer[MESSAGE_OFFSET])); - } else { - a->event_handler(BAD_ACK, a->last_sent_size - MESSAGE_OFFSET, &(a->last_sent_buffer[MESSAGE_OFFSET])); - } - } - - } else if (event == TIMEOUT) { - if (a->resends > MAX_RESENDS) { - a->awaiting_ack = false; - a->stop_timer(); - a->event_handler(NO_ACK, a->last_sent_size - MESSAGE_OFFSET, &(a->last_sent_buffer[MESSAGE_OFFSET])); - } else { - a->resends += 1; - a->sender(a->last_sent_size, a->last_sent_buffer); - a->awaiting_ack = true; - a->start_timer(); - } - } -} - -void receive_frame(arq* a, int16_t size, uint8_t* data) { - process_event(a, RECEIVED_FRAME, size, data); -} - -void timeout(arq* a) { - process_event(a, TIMEOUT, 0, NULL); -} - -void send_message(arq* a, int16_t size, const uint8_t * const message) { - if (a->awaiting_ack) { - a->event_handler(BUSY, size, message); - return; - } - - if (size > MAX_MESSAGE_SIZE) { - a->event_handler(SIZE_ERROR, size, message); - return; - } - - - a->last_sent_buffer[SEQ_INDEX] += 1; //increment seq - a->last_sent_buffer[CMD_INDEX] = DATA; - int i; - for (i = 0; i < size; ++i) { - a->last_sent_buffer[i + MESSAGE_OFFSET] = message[i]; - } - - a->last_sent_size = MESSAGE_OFFSET + size; - - a->sender(a->last_sent_size, a->last_sent_buffer); - a->awaiting_ack = true; - a->resends = 0; - a->start_timer(); -} - -void init_arq(arq* a) { - a->last_sent_size = 0; - a->last_received_seq = 0; - a->resends = 0; - a->awaiting_ack = false; -} - - diff --git a/arduino/ace/arq.h b/arduino/ace/arq.h deleted file mode 100644 index 592b0d4..0000000 --- a/arduino/ace/arq.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef ARQ_H -#define ARQ_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -#include "framing.h" - - -typedef enum { - RECEIVED = 0, - SEND_SUCCESS = 1, - NO_ACK = 3, - BAD_ACK = 4, - SIZE_ERROR = 5, - BUSY = 6 -} message_event; - -typedef void (*send_frame_function)(int16_t size, uint8_t* data); -typedef void (*message_event_function)(message_event e, int16_t size, const uint8_t* const message); -typedef void (*void_function)(); - -typedef struct { - uint8_t last_sent_buffer[MAX_FRAME_SIZE]; - int16_t last_sent_size; - uint8_t last_received_seq; - uint8_t resends; - bool awaiting_ack; - - send_frame_function sender; - message_event_function event_handler; - void_function start_timer; - void_function stop_timer; - -} arq; - -void init_arq(arq* a); -void receive_frame(arq* a, int16_t size, uint8_t* data); -void timeout(arq* a); -void send_message(arq* a, int16_t size, const uint8_t * const message); - - - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif /* ARQ_H */ diff --git a/arduino/ace/framing.c b/arduino/ace/framing.c deleted file mode 100644 index 4d9c5de..0000000 --- a/arduino/ace/framing.c +++ /dev/null @@ -1,88 +0,0 @@ -#include "framing.h" - -static inline bool is_cmd(uint8_t byte) { - return (byte == FRAME_START || byte == FRAME_STOP || byte == FRAME_ESCAPE); -} - -static inline void reset(framer* f) { - f->position = -1; - f->checksum = 0x00; -} - -static inline void accept(framer* f, uint8_t byte) { - - //if a new byte would cause an overflow, restart frame - //note that this should not happen if both communicating parties have defined the same maximum frame length - if (f->position >= MAX_FRAME_SIZE) { - reset(f); - return; - } - - if (f->position != -1) { // i.e. a previous byte exists - f->buffer[f->position] = f->staged; - f->checksum = f->checksum ^ f->staged; - } - - f->position += 1; - f->staged = byte; -} - -void receive_byte(framer* f, uint8_t byte) { - - switch(f->state) { - case ESCAPING: - accept(f, byte); - f->state = RECEIVING; - break; - - case WAITING: - if (byte == FRAME_START) { - reset(f); - f->state = RECEIVING; - } - break; - - case RECEIVING: - switch(byte) { - case FRAME_ESCAPE: - f->state = ESCAPING; - break; - case FRAME_START: - reset(f); - break; - case FRAME_STOP: - f->state = WAITING; - if (f->staged == f->checksum) { //at this point, staged byte is the checksum sent in the frame (last byte of frame) - f->receiver(f->position, f->buffer); - } - break; - - default: - accept(f, byte); - break; - } - } -} - -void send_frame(framer* f, int16_t size, const uint8_t * const data_out) { - uint8_t checksum = 0x00; - uint8_t byte; - send_byte_function sender = f->sender; - - sender(FRAME_START); - int i; - for (i = 0; i < size; ++i) { - byte = data_out[i]; - if (is_cmd(byte)) sender(FRAME_ESCAPE); - sender(byte); - checksum = checksum ^ byte; - } - if (is_cmd(checksum)) sender(FRAME_ESCAPE); - sender(checksum); - sender(FRAME_STOP); -} - -void init_framer(framer* f) { - f->state = WAITING; - reset(f); -} diff --git a/arduino/ace/framing.h b/arduino/ace/framing.h deleted file mode 100644 index daacba7..0000000 --- a/arduino/ace/framing.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef FRAMING_H -#define FRAMING_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -#define MAX_FRAME_SIZE 128 -#define FRAME_ESCAPE 0x02 -#define FRAME_START 0x03 -#define FRAME_STOP 0x10 - -typedef enum { - WAITING, - RECEIVING, - ESCAPING -} link_state; - -typedef void (*receive_frame_function)(int16_t size, uint8_t* data); -typedef void (*send_byte_function)(uint8_t byte); - - -typedef struct { - link_state state; // current state - uint8_t buffer[MAX_FRAME_SIZE]; // in data buffer - uint8_t staged; // previously read byte, will be put in buffer after next read - int16_t position; //current position of next byte to be read in buffer - uint8_t checksum; //current value of calculated checksum - - receive_frame_function receiver; - send_byte_function sender; - -} framer; - -void init_framer(framer* f); -void receive_byte(framer* f, uint8_t byte); -void send_frame(framer* f, int16_t size, const uint8_t * const data_out); - - -#ifdef __cplusplus -} // extern "C" -#endif - -#endif /* FRAMING_H */ diff --git a/arduino/ace/test.ino b/arduino/ace/test.ino deleted file mode 100644 index e9df65f..0000000 --- a/arduino/ace/test.ino +++ /dev/null @@ -1,44 +0,0 @@ -#include "Arduino.h" - -#include "LiquidCrystal.h" -#include "ace.h" - -LiquidCrystal lcd(8, 9, 4, 5, 6, 11); - -#define ERR_PIN 3 - -void ace_event(message_event e, int16_t size, const uint8_t* const message) { - - if (e == 0 || e == 1) { - digitalWrite(ERR_PIN, LOW); - lcd.clear(); - for(int i = 0; i < size; ++i) { - lcd.write(message[i]); - } - } - else { - digitalWrite(ERR_PIN, HIGH); - lcd.clear(); - lcd.print(e); - } -} - -void setup() { - init_ace(115200, 20); - lcd.begin(16,2); - lcd.clear(); - lcd.print("ready"); - pinMode(ERR_PIN, OUTPUT); -} - -uint8_t i = 0; -void loop() { - - lcd.clear(); - lcd.print("|"); - - - delay(1000); - ace_send0(1, &i); - delay(1000); -} diff --git a/c/arduino/.gitignore b/c/arduino/.gitignore new file mode 100644 index 0000000..a9dd55f --- /dev/null +++ b/c/arduino/.gitignore @@ -0,0 +1,7 @@ +.lib/ +.dep/ +*.o +*.hex +*.elf +*~ + diff --git a/c/arduino/Makefile b/c/arduino/Makefile new file mode 100644 index 0000000..3e0f36d --- /dev/null +++ b/c/arduino/Makefile @@ -0,0 +1,7 @@ +#SOURCES := main.cc other.cc +#LIBRARIES := EEPROM + +ARDUINODIR=$(ARDUINO_HOME) +BOARD=mega2560 +#export LIBRARIES=LiquidCrystal +include arduino.mk diff --git a/c/arduino/ace.c b/c/arduino/ace.c new file mode 100644 index 0000000..ea05e1c --- /dev/null +++ b/c/arduino/ace.c @@ -0,0 +1,116 @@ +#include "ace.h" + +#include + + +typedef struct { + uint8_t buffer[SERIAL_BUFFER_SIZE]; + volatile uint16_t head; + volatile uint16_t tail; +} ring_buffer; + +static ring_buffer tx_buffer0 = {{0}, 0, 0}; +static framer framer0; +static arq arq0; +static uint32_t max_ticks0; +static uint32_t ticks0; + +//initialize UART +static void init_s0(uint32_t baud) { + UCSR0A |= (1 << U2X0); //enable double speed transmission + uint16_t baud_setting = (F_CPU / 4 / baud - 1) / 2; + UBRR0H = baud_setting >> 8; + UBRR0L = baud_setting; + UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0); // defaults to 8-bit, no parity, 1 stop bit + UCSR0B &= ~(1 << UDRIE0) | (1 << TXEN0) | (1 << RXCIE0); +} + +static void send_to_s0(uint8_t byte) { + uint16_t i = (tx_buffer0.head + 1) % SERIAL_BUFFER_SIZE; + // If the output buffer is full, there's nothing for it other than to + // wait for the interrupt handler to empty it a bit + if (i == tx_buffer0.tail) return; + //while (i == tx_buffer0.tail) {}; + tx_buffer0.buffer[tx_buffer0.head] = byte; + tx_buffer0.head = i; + //enable data register empty interrupt + UCSR0B |= (1 << UDRIE0); +} + +static void send_to_framer0(int16_t size, const uint8_t* const data) { + send_frame(&framer0, size, data); +} + +static void receive_from_framer0(int16_t size, uint8_t* data) { + receive_frame(&arq0, size, data); +} + +//called when byte is received +ISR(USART0_RX_vect) { + uint8_t c = UDR0; + receive_byte(&framer0, c); +} + +//called when data register is empty +ISR(USART0_UDRE_vect) { + if (tx_buffer0.head == tx_buffer0.tail) { + UCSR0B &= ~(1 << UDRIE0); //buffer empty, disable interruot + } else { + uint8_t c = tx_buffer0.buffer[tx_buffer0.tail]; + tx_buffer0.tail = (tx_buffer0.tail + 1) % SERIAL_BUFFER_SIZE; + UDR0 = c; //write next byte + } +} + +//initialize timer +static void init_t0(uint32_t ticks) { + cli(); + TCCR1A = 0; + TCCR1B = 0; + TCCR1B |= (1 << WGM12); // turn on CTC mode: + TCCR1B |= (1 << CS11); // set CS31 for 64 prescaler + // set compare match register to desired timer count: + OCR1A = F_CPU / 1000 / 64; // should be 250 for F_CPU=16Mhz and f = 1000Hz + sei(); + //the timer should now interrupt every ms + max_ticks0 = ticks; + ticks0 = 0; +} + +inline static void start_t0() { + TIMSK1 |= (1 << OCIE1A); +} + +inline static void stop_t0() { + TIMSK1 &= ~(1 << OCIE1A); +} + +ISR(TIMER1_COMPA_vect) { + ticks0 += 1; + if (ticks0 > max_ticks0) { + timeout(&arq0); + ticks0 = 0; + } +} + +void init_ace0(uint32_t baud, uint32_t extra_time) { + init_s0(baud); + init_framer(&framer0); + init_arq(&arq0); + + framer0.sender = send_to_s0; + arq0.sender = send_to_framer0; + + framer0.receiver = receive_from_framer0; + arq0.event_handler = ace_event0; + + uint32_t bits = (MAX_FRAME_SIZE + 3) * 8 * 2; + uint32_t tx_time = bits * 1000 / baud; // transmission time in milliseconds + init_t0(tx_time + extra_time); + arq0.start_timer = start_t0; + arq0.stop_timer = stop_t0; +} + +void ace_send0(uint8_t size, const uint8_t* const message) { + send_message(&arq0, size, message); +} diff --git a/c/arduino/ace.h b/c/arduino/ace.h new file mode 100644 index 0000000..75b178c --- /dev/null +++ b/c/arduino/ace.h @@ -0,0 +1,27 @@ +#ifndef ACE_H +#define ACE_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include "framing.h" +#include "arq.h" + +#define SERIAL_BUFFER_SIZE 64 + +void init_ace0(uint32_t baud, uint32_t extra_time); +void ace_send0(uint8_t size, const uint8_t* const message); +void ace_event0(message_event event, int16_t size, const uint8_t* const message); + +#define init_ace init_ace0 +#define ace_send ace_send0 +#define ace_event ace_event0 + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif /* ACE_H */ diff --git a/c/arduino/arduino.mk b/c/arduino/arduino.mk new file mode 100644 index 0000000..9fce9c7 --- /dev/null +++ b/c/arduino/arduino.mk @@ -0,0 +1,379 @@ +#_______________________________________________________________________________ +# +# edam's Arduino makefile +#_______________________________________________________________________________ +# version 0.4 +# +# Copyright (C) 2011, 2012 Tim Marston . +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +#_______________________________________________________________________________ +# +# +# This is a general purpose makefile for use with Arduino hardware and +# software. It works with the arduino-1.0 software release. To download the +# latest version of this makefile, visit the following website, where you can +# also find more information and documentation on it's use. The following text +# can only really be considered a reference to it's use. +# +# http://ed.am/dev/make/arduino-mk +# +# This makefile can be used as a drop-in replacement for the Arduino IDE's +# build system. To use it, save arduino.mk somewhere (I keep mine at +# ~/src/arduino.mk) and create a symlink to it in your project directory named +# "Makefile". For example: +# +# $ ln -s ~/src/arduino.mk Makefile +# +# The Arduino software (version 1.0 or later) is required. If you are using +# Debian (or a derivative), type `apt-get install arduino`. Otherwise, you +# will have to download the Arduino software manually from http://arduino.cc/. +# It is suggested that you install it at ~/opt/arduino if you are unsure. +# +# If you downloaded the Arduino software manually and unpacked it somewhere +# other than ~/opt/arduino, you will need to set up ARDUINODIR to be the path +# where you unpacked it. (If unset, ARDUINODIR defaults to ~/opt/arduino and +# then /usr/share/arduino, in that order.) You might be best to set this in +# your ~/.profile by adding something like this: +# +# export ARDUINODIR=~/somewhere/arduino-1.0 +# +# You will also need to set BOARD to the type of Arduino you're building for. +# Type `make boards` for a list of acceptable values. You could set a default +# in your ~/.profile if you want, but it is suggested that you specify this at +# build time, especially if you work with different types of Arduino. For +# example: +# +# $ export BOARD=uno +# $ make +# +# You may also need to set SERIALDEV if it is not detected correctly. +# +# The presence of a .ino (or .pde) file causes the arduino.mk to automatically +# determine values for SOURCES, TARGET and LIBRARIES. Any .c, .cc and .cpp +# files in the project directory (or any "util" or "utility" subdirectories) +# are automatically included in the build and are scanned for Arduino libraries +# that have been #included. Note, there can only be one .ino (or .pde) file. +# +# Alternatively, if you want to manually specify build variables, create a +# Makefile that defines SOURCES and LIBRARIES and then includes arduino.mk. +# (There is no need to define TARGET). Here is an example Makefile: +# +# SOURCES := main.cc other.cc +# LIBRARIES := EEPROM +# include ~/src/arduino.mk +# +# Here is a complete list of configuration parameters: +# +# ARDUINODIR The path where the Arduino software is installed on your system. +# +# ARDUINOCONST The Arduino software version, as an integer, used to define the +# ARDUINO version constant. This defaults to 100 if undefined. +# +# AVRDUDECONF The avrdude.conf to use. If undefined, this defaults to a guess +# based on where the avrdude in use is. If empty, no avrdude.conf +# is passed to avrdude (to the system default is used). +# +# AVRTOOLSPATH A space-separated list of directories to search in order when +# looking for the avr build tools. This defaults to the system PATH +# followed by subdirectories in ARDUINODIR if undefined. +# +# BOARD Specify a target board type. Run `make boards` to see available +# board types. +# +# LIBRARIES A list of Arduino libraries to build and include. This is set +# automatically if a .ino (or .pde) is found. +# +# SERIALDEV The unix device name of the serial device that is the Arduino. +# If unspecified, an attempt is made to determine the name of a +# connected Arduino's serial device. +# +# SOURCES A list of all source files of whatever language. The language +# type is determined by the file extension. This is set +# automatically if a .ino (or .pde) is found. +# +# TARGET The name of the target file. This is set automatically if a +# .ino (or .pde) is found, but it is not necessary to set it +# otherwise. +# +# This makefile also defines the following goals for use on the command line +# when you run make: +# +# all This is the default if no goal is specified. It builds the +# target. +# +# target Builds the target. +# +# upload Uploads the last built target to an attached Arduino. +# +# clean Deletes files created during the build. +# +# boards Display a list of available board names, so that you can set the +# BOARD environment variable appropriately. +# +# monitor Start `screen` on the serial device. This is meant to be an +# equivalent to the Arduino serial monitor. +# +# size Displays size information about the bulit target. +# +# Builds the specified file, either an object file or the target, +# from those that that would be built for the project. +#_______________________________________________________________________________ +# + +# default arduino software directory, check software exists +ifndef ARDUINODIR +ARDUINODIR := $(firstword $(wildcard ~/opt/arduino /usr/share/arduino)) +endif +ifeq "$(wildcard $(ARDUINODIR)/hardware/arduino/boards.txt)" "" +$(error ARDUINODIR is not set correctly; arduino software not found) +endif + +# default arduino version +ARDUINOCONST ?= 100 + +# default path for avr tools +ifndef AVRTOOLSPATH +AVRTOOLSPATH := $(subst :, , $(PATH)) +AVRTOOLSPATH += $(ARDUINODIR)/hardware/tools +AVRTOOLSPATH += $(ARDUINODIR)/hardware/tools/avr/bin +endif + +# auto mode? +INOFILE := $(wildcard *.ino *.pde) +ifdef INOFILE +ifneq "$(words $(INOFILE))" "1" +$(error There is more than one .pde or .ino file in this directory!) +endif + +# automatically determine sources and targeet +TARGET := $(basename $(INOFILE)) +SOURCES := $(INOFILE) \ + $(wildcard *.c *.cc *.cpp) \ + $(wildcard $(addprefix util/, *.c *.cc *.cpp)) \ + $(wildcard $(addprefix utility/, *.c *.cc *.cpp)) + +# automatically determine included libraries +LIBRARIES := $(filter $(notdir $(wildcard $(ARDUINODIR)/libraries/*)), \ + $(shell sed -ne "s/^ *\# *include *[<\"]\(.*\)\.h[>\"]/\1/p" $(SOURCES))) + +endif + +# no serial device? make a poor attempt to detect an arduino +SERIALDEVGUESS := 0 +ifeq "$(SERIALDEV)" "" +SERIALDEV := $(firstword $(wildcard \ + /dev/ttyACM? /dev/ttyUSB? /dev/tty.usbserial* /dev/tty.usbmodem*)) +SERIALDEVGUESS := 1 +endif + +# software +findsoftware = $(firstword $(wildcard $(addsuffix /$(1), $(AVRTOOLSPATH)))) +CC := $(call findsoftware,avr-gcc) +CXX := $(call findsoftware,avr-g++) +LD := $(call findsoftware,avr-ld) +AR := $(call findsoftware,avr-ar) +OBJCOPY := $(call findsoftware,avr-objcopy) +AVRDUDE := $(call findsoftware,avrdude) +AVRSIZE := $(call findsoftware,avr-size) + +# files +TARGET := $(if $(TARGET),$(TARGET),a.out) +OBJECTS := $(addsuffix .o, $(basename $(SOURCES))) +DEPFILES := $(patsubst %, .dep/%.dep, $(SOURCES)) +ARDUINOCOREDIR := $(ARDUINODIR)/hardware/arduino/cores/arduino +ARDUINOLIB := .lib/arduino.a +ARDUINOLIBLIBSDIR := $(ARDUINODIR)/libraries +ARDUINOLIBLIBSPATH := $(foreach lib, $(LIBRARIES), \ + $(ARDUINODIR)/libraries/$(lib)/ $(ARDUINODIR)/libraries/$(lib)/utility/ ) +ARDUINOLIBOBJS := $(foreach dir, $(ARDUINOCOREDIR) $(ARDUINOLIBLIBSPATH), \ + $(patsubst %, .lib/%.o, $(wildcard $(addprefix $(dir)/, *.c *.cpp)))) +ifeq "$(AVRDUDECONF)" "" +ifeq "$(AVRDUDE)" "$(ARDUINODIR)/hardware/tools/avr/bin/avrdude" +AVRDUDECONF := $(ARDUINODIR)/hardware/tools/avr/etc/avrdude.conf +else +AVRDUDECONF := $(wildcard $(AVRDUDE).conf) +endif +endif + +# no board? +ifndef BOARD +ifneq "$(MAKECMDGOALS)" "boards" +ifneq "$(MAKECMDGOALS)" "clean" +$(error BOARD is unset. Type 'make boards' to see possible values) +endif +endif +endif + +# obtain board parameters from the arduino boards.txt file +BOARDS_FILE := $(ARDUINODIR)/hardware/arduino/boards.txt +BOARD_BUILD_MCU := \ + $(shell sed -ne "s/$(BOARD).build.mcu=\(.*\)/\1/p" $(BOARDS_FILE)) +BOARD_BUILD_FCPU := \ + $(shell sed -ne "s/$(BOARD).build.f_cpu=\(.*\)/\1/p" $(BOARDS_FILE)) +BOARD_BUILD_VARIANT := \ + $(shell sed -ne "s/$(BOARD).build.variant=\(.*\)/\1/p" $(BOARDS_FILE)) +BOARD_UPLOAD_SPEED := \ + $(shell sed -ne "s/$(BOARD).upload.speed=\(.*\)/\1/p" $(BOARDS_FILE)) +BOARD_UPLOAD_PROTOCOL := \ + $(shell sed -ne "s/$(BOARD).upload.protocol=\(.*\)/\1/p" $(BOARDS_FILE)) +BOARD_USB_VID := \ + $(shell sed -ne "s/$(BOARD).build.vid=\(.*\)/\1/p" $(BOARDS_FILE)) +BOARD_USB_PID := \ + $(shell sed -ne "s/$(BOARD).build.pid=\(.*\)/\1/p" $(BOARDS_FILE)) + +# invalid board? +ifeq "$(BOARD_BUILD_MCU)" "" +ifneq "$(MAKECMDGOALS)" "boards" +ifneq "$(MAKECMDGOALS)" "clean" +$(error BOARD is invalid. Type 'make boards' to see possible values) +endif +endif +endif + +# flags +CPPFLAGS := -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections +CPPFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +CPPFLAGS += -mmcu=$(BOARD_BUILD_MCU) +CPPFLAGS += -DF_CPU=$(BOARD_BUILD_FCPU) -DARDUINO=$(ARDUINOCONST) +CPPFLAGS += -DUSB_VID=$(BOARD_USB_VID) -DUSB_PID=$(BOARD_USB_PID) +CPPFLAGS += -I. -Iutil -Iutility -I $(ARDUINOCOREDIR) +CPPFLAGS += -I $(ARDUINODIR)/hardware/arduino/variants/$(BOARD_BUILD_VARIANT)/ +CPPFLAGS += $(addprefix -I $(ARDUINODIR)/libraries/, $(LIBRARIES)) +CPPFLAGS += $(patsubst %, -I $(ARDUINODIR)/libraries/%/utility, $(LIBRARIES)) +CPPDEPFLAGS = -MMD -MP -MF .dep/$<.dep +CPPINOFLAGS := -x c++ -include $(ARDUINOCOREDIR)/Arduino.h +AVRDUDEFLAGS := $(addprefix -C , $(AVRDUDECONF)) -DV +AVRDUDEFLAGS += -p $(BOARD_BUILD_MCU) -P $(SERIALDEV) +AVRDUDEFLAGS += -c $(BOARD_UPLOAD_PROTOCOL) -b $(BOARD_UPLOAD_SPEED) +LINKFLAGS := -Os -Wl,--gc-sections -mmcu=$(BOARD_BUILD_MCU) + +# figure out which arg to use with stty (for OS X, GNU and busybox stty) +STTYFARG := $(shell stty --help 2>&1 | \ + grep -q 'illegal option' && echo -f || echo -F) + +# include dependencies +ifneq "$(MAKECMDGOALS)" "clean" +-include $(DEPFILES) +endif + +# default rule +.DEFAULT_GOAL := all + +#_______________________________________________________________________________ +# RULES + +.PHONY: all target upload clean boards monitor size + +all: target + +target: $(TARGET).hex + +upload: target + @echo "\nUploading to board..." + @test -n "$(SERIALDEV)" || { \ + echo "error: SERIALDEV could not be determined automatically." >&2; \ + exit 1; } + @test 0 -eq $(SERIALDEVGUESS) || { \ + echo "*GUESSING* at serial device:" $(SERIALDEV); \ + echo; } + stty $(STTYFARG) $(SERIALDEV) hupcl + $(AVRDUDE) $(AVRDUDEFLAGS) -U flash:w:$(TARGET).hex:i + +clean: + rm -f $(OBJECTS) + rm -f $(TARGET).elf $(TARGET).hex $(ARDUINOLIB) *~ + rm -rf .lib .dep + +boards: + @echo Available values for BOARD: + @sed -nEe '/^#/d; /^[^.]+\.name=/p' $(BOARDS_FILE) | \ + sed -Ee 's/([^.]+)\.name=(.*)/\1 \2/' \ + -e 's/(.{12}) *(.*)/\1 \2/' + +monitor: + @test -n "$(SERIALDEV)" || { \ + echo "error: SERIALDEV could not be determined automatically." >&2; \ + exit 1; } + @test -n `which screen` || { \ + echo "error: can't find GNU screen, you might need to install it." >&2 \ + ecit 1; } + @test 0 -eq $(SERIALDEVGUESS) || { \ + echo "*GUESSING* at serial device:" $(SERIALDEV); \ + echo; } + screen $(SERIALDEV) + +size: $(TARGET).elf + echo && $(AVRSIZE) --format=avr --mcu=$(BOARD_BUILD_MCU) $(TARGET).elf + +# building the target + +$(TARGET).hex: $(TARGET).elf + $(OBJCOPY) -O ihex -R .eeprom $< $@ + +.INTERMEDIATE: $(TARGET).elf + +$(TARGET).elf: $(ARDUINOLIB) $(OBJECTS) + $(CC) $(LINKFLAGS) $(OBJECTS) $(ARDUINOLIB) -lm -o $@ + +%.o: %.c + mkdir -p .dep/$(dir $<) + $(COMPILE.c) $(CPPDEPFLAGS) -o $@ $< + +%.o: %.cpp + mkdir -p .dep/$(dir $<) + $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $< + +%.o: %.cc + mkdir -p .dep/$(dir $<) + $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $< + +%.o: %.C + mkdir -p .dep/$(dir $<) + $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $< + +%.o: %.ino + mkdir -p .dep/$(dir $<) + $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ $(CPPINOFLAGS) $< + +%.o: %.pde + mkdir -p .dep/$(dir $<) + $(COMPILE.cpp) $(CPPDEPFLAGS) -o $@ -x c++ -include $(ARDUINOCOREDIR)/Arduino.h $< + +# building the arduino library + +$(ARDUINOLIB): $(ARDUINOLIBOBJS) + $(AR) rcs $@ $? + +.lib/%.c.o: %.c + mkdir -p $(dir $@) + $(COMPILE.c) -o $@ $< + +.lib/%.cpp.o: %.cpp + mkdir -p $(dir $@) + $(COMPILE.cpp) -o $@ $< + +.lib/%.cc.o: %.cc + mkdir -p $(dir $@) + $(COMPILE.cpp) -o $@ $< + +.lib/%.C.o: %.C + mkdir -p $(dir $@) + $(COMPILE.cpp) -o $@ $< diff --git a/c/arduino/arq.c b/c/arduino/arq.c new file mode 100644 index 0000000..9d4a08c --- /dev/null +++ b/c/arduino/arq.c @@ -0,0 +1,122 @@ +#include "arq.h" + +#include +/* Expected frame data for ARQ + * + * +-----+-----+------+ + * | seq | cmd | data | + * +-----+-----+------+ + * + * seq: sequence number of frame (1 byte) + * cmd: command id of frame (1 byte), currently either ACK or DATA + * data: actual data of frame (arbitraty length, respecting frame limitations) + * + * Note: frame checking, headers and footers are not handled by arq, see 'framing' instead + * +*/ + +#define MAX_RESENDS 5 //number of resends before a timeout is generated +#define MAX_SEQ 255 //maximum sequence number of frames +#define DATA 0x05 +#define ACK 0x06 + +#define SEQ_INDEX 0 +#define CMD_INDEX 1 +#define MESSAGE_OFFSET 2 +#define MAX_MESSAGE_SIZE MAX_FRAME_SIZE - MESSAGE_OFFSET + + +typedef enum { + RECEIVED_FRAME, + TIMEOUT +} arq_event; + +static void send_ack(arq* a, uint8_t seq) { + uint8_t data[] = {seq, ACK}; + a->sender(2, data); +} + +static void process_event(arq* a, arq_event event, int16_t data_size, uint8_t* data) { + if (event == RECEIVED_FRAME) { + uint8_t seq = data[SEQ_INDEX]; + uint8_t cmd = data[CMD_INDEX]; + uint8_t* message = &(data[MESSAGE_OFFSET]); + int16_t message_size = data_size - MESSAGE_OFFSET; + + if (!a->awaiting_ack) { //ready to receive + if (cmd == DATA) { + if (a->last_received_seq != seq) { + a->last_received_seq = seq; + a->event_handler(RECEIVED, message_size, message); + } + send_ack(a, seq); + } + //ignore case in which an ack is received even though none is awaited + } else { //awaiting ack + a->awaiting_ack = false; + a->stop_timer(); + + if (cmd == ACK && seq == a->last_sent_buffer[SEQ_INDEX]) { + a->event_handler(SEND_SUCCESS, a->last_sent_size - MESSAGE_OFFSET, &(a->last_sent_buffer[MESSAGE_OFFSET])); + } else { + a->event_handler(BAD_ACK, a->last_sent_size - MESSAGE_OFFSET, &(a->last_sent_buffer[MESSAGE_OFFSET])); + } + } + + } else if (event == TIMEOUT) { + if (a->resends > MAX_RESENDS) { + a->awaiting_ack = false; + a->stop_timer(); + a->event_handler(NO_ACK, a->last_sent_size - MESSAGE_OFFSET, &(a->last_sent_buffer[MESSAGE_OFFSET])); + } else { + a->resends += 1; + a->sender(a->last_sent_size, a->last_sent_buffer); + a->awaiting_ack = true; + a->start_timer(); + } + } +} + +void receive_frame(arq* a, int16_t size, uint8_t* data) { + process_event(a, RECEIVED_FRAME, size, data); +} + +void timeout(arq* a) { + process_event(a, TIMEOUT, 0, NULL); +} + +void send_message(arq* a, int16_t size, const uint8_t * const message) { + if (a->awaiting_ack) { + a->event_handler(BUSY, size, message); + return; + } + + if (size > MAX_MESSAGE_SIZE) { + a->event_handler(SIZE_ERROR, size, message); + return; + } + + + a->last_sent_buffer[SEQ_INDEX] += 1; //increment seq + a->last_sent_buffer[CMD_INDEX] = DATA; + int i; + for (i = 0; i < size; ++i) { + a->last_sent_buffer[i + MESSAGE_OFFSET] = message[i]; + } + + a->last_sent_size = MESSAGE_OFFSET + size; + + a->sender(a->last_sent_size, a->last_sent_buffer); + a->awaiting_ack = true; + a->resends = 0; + a->start_timer(); +} + +void init_arq(arq* a) { + a->last_sent_size = 0; + a->last_received_seq = 0; + a->resends = 0; + a->awaiting_ack = false; +} + + diff --git a/c/arduino/arq.h b/c/arduino/arq.h new file mode 100644 index 0000000..592b0d4 --- /dev/null +++ b/c/arduino/arq.h @@ -0,0 +1,52 @@ +#ifndef ARQ_H +#define ARQ_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#include "framing.h" + + +typedef enum { + RECEIVED = 0, + SEND_SUCCESS = 1, + NO_ACK = 3, + BAD_ACK = 4, + SIZE_ERROR = 5, + BUSY = 6 +} message_event; + +typedef void (*send_frame_function)(int16_t size, uint8_t* data); +typedef void (*message_event_function)(message_event e, int16_t size, const uint8_t* const message); +typedef void (*void_function)(); + +typedef struct { + uint8_t last_sent_buffer[MAX_FRAME_SIZE]; + int16_t last_sent_size; + uint8_t last_received_seq; + uint8_t resends; + bool awaiting_ack; + + send_frame_function sender; + message_event_function event_handler; + void_function start_timer; + void_function stop_timer; + +} arq; + +void init_arq(arq* a); +void receive_frame(arq* a, int16_t size, uint8_t* data); +void timeout(arq* a); +void send_message(arq* a, int16_t size, const uint8_t * const message); + + + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif /* ARQ_H */ diff --git a/c/arduino/framing.c b/c/arduino/framing.c new file mode 100644 index 0000000..4d9c5de --- /dev/null +++ b/c/arduino/framing.c @@ -0,0 +1,88 @@ +#include "framing.h" + +static inline bool is_cmd(uint8_t byte) { + return (byte == FRAME_START || byte == FRAME_STOP || byte == FRAME_ESCAPE); +} + +static inline void reset(framer* f) { + f->position = -1; + f->checksum = 0x00; +} + +static inline void accept(framer* f, uint8_t byte) { + + //if a new byte would cause an overflow, restart frame + //note that this should not happen if both communicating parties have defined the same maximum frame length + if (f->position >= MAX_FRAME_SIZE) { + reset(f); + return; + } + + if (f->position != -1) { // i.e. a previous byte exists + f->buffer[f->position] = f->staged; + f->checksum = f->checksum ^ f->staged; + } + + f->position += 1; + f->staged = byte; +} + +void receive_byte(framer* f, uint8_t byte) { + + switch(f->state) { + case ESCAPING: + accept(f, byte); + f->state = RECEIVING; + break; + + case WAITING: + if (byte == FRAME_START) { + reset(f); + f->state = RECEIVING; + } + break; + + case RECEIVING: + switch(byte) { + case FRAME_ESCAPE: + f->state = ESCAPING; + break; + case FRAME_START: + reset(f); + break; + case FRAME_STOP: + f->state = WAITING; + if (f->staged == f->checksum) { //at this point, staged byte is the checksum sent in the frame (last byte of frame) + f->receiver(f->position, f->buffer); + } + break; + + default: + accept(f, byte); + break; + } + } +} + +void send_frame(framer* f, int16_t size, const uint8_t * const data_out) { + uint8_t checksum = 0x00; + uint8_t byte; + send_byte_function sender = f->sender; + + sender(FRAME_START); + int i; + for (i = 0; i < size; ++i) { + byte = data_out[i]; + if (is_cmd(byte)) sender(FRAME_ESCAPE); + sender(byte); + checksum = checksum ^ byte; + } + if (is_cmd(checksum)) sender(FRAME_ESCAPE); + sender(checksum); + sender(FRAME_STOP); +} + +void init_framer(framer* f) { + f->state = WAITING; + reset(f); +} diff --git a/c/arduino/framing.h b/c/arduino/framing.h new file mode 100644 index 0000000..daacba7 --- /dev/null +++ b/c/arduino/framing.h @@ -0,0 +1,47 @@ +#ifndef FRAMING_H +#define FRAMING_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#define MAX_FRAME_SIZE 128 +#define FRAME_ESCAPE 0x02 +#define FRAME_START 0x03 +#define FRAME_STOP 0x10 + +typedef enum { + WAITING, + RECEIVING, + ESCAPING +} link_state; + +typedef void (*receive_frame_function)(int16_t size, uint8_t* data); +typedef void (*send_byte_function)(uint8_t byte); + + +typedef struct { + link_state state; // current state + uint8_t buffer[MAX_FRAME_SIZE]; // in data buffer + uint8_t staged; // previously read byte, will be put in buffer after next read + int16_t position; //current position of next byte to be read in buffer + uint8_t checksum; //current value of calculated checksum + + receive_frame_function receiver; + send_byte_function sender; + +} framer; + +void init_framer(framer* f); +void receive_byte(framer* f, uint8_t byte); +void send_frame(framer* f, int16_t size, const uint8_t * const data_out); + + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif /* FRAMING_H */ diff --git a/c/arduino/test.ino b/c/arduino/test.ino new file mode 100644 index 0000000..e9df65f --- /dev/null +++ b/c/arduino/test.ino @@ -0,0 +1,44 @@ +#include "Arduino.h" + +#include "LiquidCrystal.h" +#include "ace.h" + +LiquidCrystal lcd(8, 9, 4, 5, 6, 11); + +#define ERR_PIN 3 + +void ace_event(message_event e, int16_t size, const uint8_t* const message) { + + if (e == 0 || e == 1) { + digitalWrite(ERR_PIN, LOW); + lcd.clear(); + for(int i = 0; i < size; ++i) { + lcd.write(message[i]); + } + } + else { + digitalWrite(ERR_PIN, HIGH); + lcd.clear(); + lcd.print(e); + } +} + +void setup() { + init_ace(115200, 20); + lcd.begin(16,2); + lcd.clear(); + lcd.print("ready"); + pinMode(ERR_PIN, OUTPUT); +} + +uint8_t i = 0; +void loop() { + + lcd.clear(); + lcd.print("|"); + + + delay(1000); + ace_send0(1, &i); + delay(1000); +} -- cgit v1.2.3