commit ed91cc5216e96504282ddc2e159ab932a777c2d4 Author: dave Date: Sat Oct 8 21:39:50 2016 -0700 Initial commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..12dce82 --- /dev/null +++ b/Makefile @@ -0,0 +1,619 @@ +# Hey Emacs, this is a -*- makefile -*- +#---------------------------------------------------------------------------- +# WinAVR Makefile Template written by Eric B. Weddington, Jörg Wunsch, et al. +# +# Released to the Public Domain +# +# Additional material for this makefile was written by: +# Peter Fleury +# Tim Henigan +# Colin O'Flynn +# Reiner Patommel +# Markus Pfaff +# Sander Pool +# Frederik Rouleau +# Carlos Lamas +# +#---------------------------------------------------------------------------- +# On command line: +# +# make all = Make software. +# +# make clean = Clean out built project files. +# +# make coff = Convert ELF to AVR COFF. +# +# make extcoff = Convert ELF to AVR Extended COFF. +# +# make program = Download the hex file to the device, using avrdude. +# Please customize the avrdude settings below first! +# +# make debug = Start either simulavr or avarice as specified for debugging, +# with avr-gdb or avr-insight as the front end for debugging. +# +# make filename.s = Just compile filename.c into the assembler code only. +# +# make filename.i = Create a preprocessed source file for use in submitting +# bug reports to the GCC project. +# +# To rebuild project do "make clean" then "make all". +#---------------------------------------------------------------------------- + + +# MCU name +MCU = attiny84 + + +# Processor frequency. +# This will define a symbol, F_CPU, in all source code files equal to the +# processor frequency. You can then use this symbol in your source code to +# calculate timings. Do NOT tack on a 'UL' at the end, this will be done +# automatically to create a 32-bit value in your source code. +# Typical values are: +# F_CPU = 1000000 +# F_CPU = 1843200 +# F_CPU = 2000000 +# F_CPU = 3686400 +# F_CPU = 4000000 +# F_CPU = 7372800 +# F_CPU = 8000000 +# F_CPU = 11059200 +# F_CPU = 14745600 +# F_CPU = 16000000 +# F_CPU = 18432000 +# F_CPU = 20000000 +F_CPU = 8000000 + + +# Output format. (can be srec, ihex, binary) +FORMAT = ihex + + +# Target file name (without extension). +TARGET = main + + +# Object files directory +# To put object files in current directory, use a dot (.), do NOT make +# this an empty or blank macro! +OBJDIR = . + + +# List C source files here. (C dependencies are automatically generated.) +SRC = $(TARGET).c libs/serialdebug.c + + +# List C++ source files here. (C dependencies are automatically generated.) +CPPSRC = + + +# List Assembler source files here. +# Make them always end in a capital .S. Files ending in a lowercase .s +# will not be considered source files but generated files (assembler +# output from the compiler), and will be deleted upon "make clean"! +# Even though the DOS/Win* filesystem matches both .s and .S the same, +# it will preserve the spelling of the filenames, and gcc itself does +# care about how the name is spelled on its command-line. +ASRC = + + +# Optimization level, can be [0, 1, 2, 3, s]. +# 0 = turn off optimization. s = optimize for size. +# (Note: 3 is not always the best optimization level. See avr-libc FAQ.) +OPT = s + + +# Debugging format. +# Native formats for AVR-GCC's -g are dwarf-2 [default] or stabs. +# AVR Studio 4.10 requires dwarf-2. +# AVR [Extended] COFF format requires stabs, plus an avr-objcopy run. +DEBUG = dwarf-2 + + +# List any extra directories to look for include files here. +# Each directory must be seperated by a space. +# Use forward slashes for directory separators. +# For a directory that has spaces, enclose it in quotes. +EXTRAINCDIRS = libs + + +# Compiler flag to set the C Standard level. +# c89 = "ANSI" C +# gnu89 = c89 plus GCC extensions +# c99 = ISO C99 standard (not yet fully implemented) +# gnu99 = c99 plus GCC extensions +CSTANDARD = -std=gnu99 + + +# Place -D or -U options here for C sources +CDEFS = -DF_CPU=$(F_CPU)UL + + +# Place -D or -U options here for ASM sources +ADEFS = -DF_CPU=$(F_CPU) + + +# Place -D or -U options here for C++ sources +CPPDEFS = -DF_CPU=$(F_CPU)UL +#CPPDEFS += -D__STDC_LIMIT_MACROS +#CPPDEFS += -D__STDC_CONSTANT_MACROS + + + +#---------------- Compiler Options C ---------------- +# -g*: generate debugging information +# -O*: optimization level +# -f...: tuning, see GCC manual and avr-libc documentation +# -Wall...: warning level +# -Wa,...: tell GCC to pass this to the assembler. +# -adhlns...: create assembler listing +CFLAGS = -g$(DEBUG) +CFLAGS += $(CDEFS) +CFLAGS += -O$(OPT) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -Wall +CFLAGS += -Wstrict-prototypes +#CFLAGS += -mshort-calls +#CFLAGS += -fno-unit-at-a-time +#CFLAGS += -Wundef +#CFLAGS += -Wunreachable-code +#CFLAGS += -Wsign-compare +CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst) +CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) +CFLAGS += $(CSTANDARD) + + +#---------------- Compiler Options C++ ---------------- +# -g*: generate debugging information +# -O*: optimization level +# -f...: tuning, see GCC manual and avr-libc documentation +# -Wall...: warning level +# -Wa,...: tell GCC to pass this to the assembler. +# -adhlns...: create assembler listing +CPPFLAGS = -g$(DEBUG) +CPPFLAGS += $(CPPDEFS) +CPPFLAGS += -O$(OPT) +CPPFLAGS += -funsigned-char +CPPFLAGS += -funsigned-bitfields +CPPFLAGS += -fpack-struct +CPPFLAGS += -fshort-enums +CPPFLAGS += -fno-exceptions +CPPFLAGS += -Wall +CPPFLAGS += -Wundef +#CPPFLAGS += -mshort-calls +#CPPFLAGS += -fno-unit-at-a-time +#CPPFLAGS += -Wstrict-prototypes +#CPPFLAGS += -Wunreachable-code +#CPPFLAGS += -Wsign-compare +CPPFLAGS += -Wa,-adhlns=$(<:%.cpp=$(OBJDIR)/%.lst) +CPPFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) +#CPPFLAGS += $(CSTANDARD) + + +#---------------- Assembler Options ---------------- +# -Wa,...: tell GCC to pass this to the assembler. +# -adhlns: create listing +# -gstabs: have the assembler create line number information; note that +# for use in COFF files, additional information about filenames +# and function names needs to be present in the assembler source +# files -- see avr-libc docs [FIXME: not yet described there] +# -listing-cont-lines: Sets the maximum number of continuation lines of hex +# dump that will be displayed for a given single line of source input. +ASFLAGS = $(ADEFS) -Wa,-adhlns=$(<:%.S=$(OBJDIR)/%.lst),-gstabs,--listing-cont-lines=100 + + +#---------------- Library Options ---------------- +# Minimalistic printf version +PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min + +# Floating point printf version (requires MATH_LIB = -lm below) +PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt + +# If this is left blank, then it will use the Standard printf version. +PRINTF_LIB = +#PRINTF_LIB = $(PRINTF_LIB_MIN) +#PRINTF_LIB = $(PRINTF_LIB_FLOAT) + + +# Minimalistic scanf version +SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min + +# Floating point + %[ scanf version (requires MATH_LIB = -lm below) +SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt + +# If this is left blank, then it will use the Standard scanf version. +SCANF_LIB = +#SCANF_LIB = $(SCANF_LIB_MIN) +#SCANF_LIB = $(SCANF_LIB_FLOAT) + + +MATH_LIB = -lm + + +# List any extra directories to look for libraries here. +# Each directory must be seperated by a space. +# Use forward slashes for directory separators. +# For a directory that has spaces, enclose it in quotes. +EXTRALIBDIRS = + + + +#---------------- External Memory Options ---------------- + +# 64 KB of external RAM, starting after internal RAM (ATmega128!), +# used for variables (.data/.bss) and heap (malloc()). +#EXTMEMOPTS = -Wl,-Tdata=0x801100,--defsym=__heap_end=0x80ffff + +# 64 KB of external RAM, starting after internal RAM (ATmega128!), +# only used for heap (malloc()). +#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff + +EXTMEMOPTS = + + + +#---------------- Linker Options ---------------- +# -Wl,...: tell GCC to pass this to linker. +# -Map: create map file +# --cref: add cross reference to map file +LDFLAGS = -Wl,-Map=$(TARGET).map,--cref +LDFLAGS += $(EXTMEMOPTS) +LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS)) +LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB) +#LDFLAGS += -T linker_script.x + + + +#---------------- Programming Options (avrdude) ---------------- + +# Programming hardware +# Type: avrdude -c ? +# to get a full listing. +# +AVRDUDE_PROGRAMMER = usbasp + +# com1 = serial port. Use lpt1 to connect to parallel port. +AVRDUDE_PORT = usb + +AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex +#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep + + +# Uncomment the following if you want avrdude's erase cycle counter. +# Note that this counter needs to be initialized first using -Yn, +# see avrdude manual. +#AVRDUDE_ERASE_COUNTER = -y + +# Uncomment the following if you do /not/ wish a verification to be +# performed after programming the device. +#AVRDUDE_NO_VERIFY = -V + +# Increase verbosity level. Please use this when submitting bug +# reports about avrdude. See +# to submit bug reports. +#AVRDUDE_VERBOSE = -v -v + +AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) +AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY) +AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE) +AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER) + + + +#---------------- Debugging Options ---------------- + +# For simulavr only - target MCU frequency. +DEBUG_MFREQ = $(F_CPU) + +# Set the DEBUG_UI to either gdb or insight. +# DEBUG_UI = gdb +DEBUG_UI = insight + +# Set the debugging back-end to either avarice, simulavr. +DEBUG_BACKEND = avarice +#DEBUG_BACKEND = simulavr + +# GDB Init Filename. +GDBINIT_FILE = __avr_gdbinit + +# When using avarice settings for the JTAG +JTAG_DEV = /dev/com1 + +# Debugging port used to communicate between GDB / avarice / simulavr. +DEBUG_PORT = 4242 + +# Debugging host used to communicate between GDB / avarice / simulavr, normally +# just set to localhost unless doing some sort of crazy debugging when +# avarice is running on a different computer. +DEBUG_HOST = localhost + + + +#============================================================================ + + +# Define programs and commands. +SHELL = sh +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size +AR = avr-ar rcs +NM = avr-nm +AVRDUDE = avrdude +REMOVE = rm -f +REMOVEDIR = rm -rf +COPY = cp +WINSHELL = cmd + + +# Define Messages +# English +MSG_ERRORS_NONE = Errors: none +MSG_BEGIN = -------- begin -------- +MSG_END = -------- end -------- +MSG_SIZE_BEFORE = Size before: +MSG_SIZE_AFTER = Size after: +MSG_COFF = Converting to AVR COFF: +MSG_EXTENDED_COFF = Converting to AVR Extended COFF: +MSG_FLASH = Creating load file for Flash: +MSG_EEPROM = Creating load file for EEPROM: +MSG_EXTENDED_LISTING = Creating Extended Listing: +MSG_SYMBOL_TABLE = Creating Symbol Table: +MSG_LINKING = Linking: +MSG_COMPILING = Compiling C: +MSG_COMPILING_CPP = Compiling C++: +MSG_ASSEMBLING = Assembling: +MSG_CLEANING = Cleaning project: +MSG_CREATING_LIBRARY = Creating library: + + + + +# Define all object files. +OBJ = $(SRC:%.c=$(OBJDIR)/%.o) $(CPPSRC:%.cpp=$(OBJDIR)/%.o) $(ASRC:%.S=$(OBJDIR)/%.o) + +# Define all listing files. +LST = $(SRC:%.c=$(OBJDIR)/%.lst) $(CPPSRC:%.cpp=$(OBJDIR)/%.lst) $(ASRC:%.S=$(OBJDIR)/%.lst) + + +# Compiler flags to generate dependency files. +GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d + + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS) +ALL_CPPFLAGS = -mmcu=$(MCU) -I. -x c++ $(CPPFLAGS) $(GENDEPFLAGS) +ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS) + + + + + +# Default target. +all: begin gccversion sizebefore build sizeafter end + +# Change the build target to build a HEX file or a library. +build: elf hex eep lss sym +#build: lib + + +elf: $(TARGET).elf +hex: $(TARGET).hex +eep: $(TARGET).eep +lss: $(TARGET).lss +sym: $(TARGET).sym +LIBNAME=lib$(TARGET).a +lib: $(LIBNAME) + + + +# Eye candy. +# AVR Studio 3.x does not check make's exit code but relies on +# the following magic strings to be generated by the compile job. +begin: + @echo + @echo $(MSG_BEGIN) + +end: + @echo $(MSG_END) + @echo + + +# Display size of file. +HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex +ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf + +sizebefore: + @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \ + 2>/dev/null; echo; fi + +sizeafter: + @if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \ + 2>/dev/null; echo; fi + + + +# Display compiler version information. +gccversion : + @$(CC) --version + + + +# Program the device. +program: $(TARGET).hex $(TARGET).eep + $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) + + +# Generate avr-gdb config/init file which does the following: +# define the reset signal, load the target file, connect to target, and set +# a breakpoint at main(). +gdb-config: + @$(REMOVE) $(GDBINIT_FILE) + @echo define reset >> $(GDBINIT_FILE) + @echo SIGNAL SIGHUP >> $(GDBINIT_FILE) + @echo end >> $(GDBINIT_FILE) + @echo file $(TARGET).elf >> $(GDBINIT_FILE) + @echo target remote $(DEBUG_HOST):$(DEBUG_PORT) >> $(GDBINIT_FILE) +ifeq ($(DEBUG_BACKEND),simulavr) + @echo load >> $(GDBINIT_FILE) +endif + @echo break main >> $(GDBINIT_FILE) + +debug: gdb-config $(TARGET).elf +ifeq ($(DEBUG_BACKEND), avarice) + @echo Starting AVaRICE - Press enter when "waiting to connect" message displays. + @$(WINSHELL) /c start avarice --jtag $(JTAG_DEV) --erase --program --file \ + $(TARGET).elf $(DEBUG_HOST):$(DEBUG_PORT) + @$(WINSHELL) /c pause + +else + @$(WINSHELL) /c start simulavr --gdbserver --device $(MCU) --clock-freq \ + $(DEBUG_MFREQ) --port $(DEBUG_PORT) +endif + @$(WINSHELL) /c start avr-$(DEBUG_UI) --command=$(GDBINIT_FILE) + + + + +# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB. +COFFCONVERT = $(OBJCOPY) --debugging +COFFCONVERT += --change-section-address .data-0x800000 +COFFCONVERT += --change-section-address .bss-0x800000 +COFFCONVERT += --change-section-address .noinit-0x800000 +COFFCONVERT += --change-section-address .eeprom-0x810000 + + + +coff: $(TARGET).elf + @echo + @echo $(MSG_COFF) $(TARGET).cof + $(COFFCONVERT) -O coff-avr $< $(TARGET).cof + + +extcoff: $(TARGET).elf + @echo + @echo $(MSG_EXTENDED_COFF) $(TARGET).cof + $(COFFCONVERT) -O coff-ext-avr $< $(TARGET).cof + + + +# Create final output files (.hex, .eep) from ELF output file. +%.hex: %.elf + @echo + @echo $(MSG_FLASH) $@ + $(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock $< $@ + +%.eep: %.elf + @echo + @echo $(MSG_EEPROM) $@ + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0 + +# Create extended listing file from ELF output file. +%.lss: %.elf + @echo + @echo $(MSG_EXTENDED_LISTING) $@ + $(OBJDUMP) -h -S -z $< > $@ + +# Create a symbol table from ELF output file. +%.sym: %.elf + @echo + @echo $(MSG_SYMBOL_TABLE) $@ + $(NM) -n $< > $@ + + + +# Create library from object files. +.SECONDARY : $(TARGET).a +.PRECIOUS : $(OBJ) +%.a: $(OBJ) + @echo + @echo $(MSG_CREATING_LIBRARY) $@ + $(AR) $@ $(OBJ) + + +# Link: create ELF output file from object files. +.SECONDARY : $(TARGET).elf +.PRECIOUS : $(OBJ) +%.elf: $(OBJ) + @echo + @echo $(MSG_LINKING) $@ + $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS) + + +# Compile: create object files from C source files. +$(OBJDIR)/%.o : %.c + @echo + @echo $(MSG_COMPILING) $< + $(CC) -c $(ALL_CFLAGS) $< -o $@ + + +# Compile: create object files from C++ source files. +$(OBJDIR)/%.o : %.cpp + @echo + @echo $(MSG_COMPILING_CPP) $< + $(CC) -c $(ALL_CPPFLAGS) $< -o $@ + + +# Compile: create assembler files from C source files. +%.s : %.c + $(CC) -S $(ALL_CFLAGS) $< -o $@ + + +# Compile: create assembler files from C++ source files. +%.s : %.cpp + $(CC) -S $(ALL_CPPFLAGS) $< -o $@ + + +# Assemble: create object files from assembler source files. +$(OBJDIR)/%.o : %.S + @echo + @echo $(MSG_ASSEMBLING) $< + $(CC) -c $(ALL_ASFLAGS) $< -o $@ + + +# Create preprocessed source for use in sending a bug report. +%.i : %.c + $(CC) -E -mmcu=$(MCU) -I. $(CFLAGS) $< -o $@ + + +# Target: clean project. +clean: begin clean_list end + +clean_list : + @echo + @echo $(MSG_CLEANING) + $(REMOVE) $(TARGET).hex + $(REMOVE) $(TARGET).eep + $(REMOVE) $(TARGET).cof + $(REMOVE) $(TARGET).elf + $(REMOVE) $(TARGET).map + $(REMOVE) $(TARGET).sym + $(REMOVE) $(TARGET).lss + $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o) + $(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst) + $(REMOVE) $(SRC:.c=.s) + $(REMOVE) $(SRC:.c=.d) + $(REMOVE) $(SRC:.c=.i) + $(REMOVEDIR) .dep + + +# Create object files directory +$(shell mkdir $(OBJDIR) 2>/dev/null) + + +# Include the dependency files. +-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) + + +# Listing of phony targets. +.PHONY : all begin finish end sizebefore sizeafter gccversion \ +build elf hex eep lss sym coff extcoff \ +clean clean_list program debug gdb-config + + diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..cd79ef1 --- /dev/null +++ b/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +avrdude -c usbasp -p t84 -U lfuse:w:0xe2:m -U hfuse:w:0xdf:m -U efuse:w:0xff:m + +make clean && make && make program diff --git a/libs/macros.h b/libs/macros.h new file mode 100644 index 0000000..9709cb3 --- /dev/null +++ b/libs/macros.h @@ -0,0 +1,5 @@ +#pragma once + +#define SET_BIT(byte, bit) ((byte) |= _BV(bit)) +#define CLEAR_BIT(byte,bit) ((byte) &= ~_BV(bit)) +#define IS_SET(byte,bit) (((byte) & (1UL << (bit))) >> (bit)) diff --git a/libs/serialdebug.c b/libs/serialdebug.c new file mode 100644 index 0000000..493d5ae --- /dev/null +++ b/libs/serialdebug.c @@ -0,0 +1,68 @@ +#include + +#if SERIALDEBUG_USE_INTS +#include +#endif + +void serial_configure(void) { + // Configure serial output + SERIALDEBUG_SOFT_TX_PORT |= (1< +#include +#include +#include +#include + +FILE static serialdebug_usartout = {0}; + +void serial_configure(void); +int usart_putchar (char c, FILE *stream); +int usart_putchar_nl (char c, FILE *stream); diff --git a/libs/serialdebug.lst b/libs/serialdebug.lst new file mode 100644 index 0000000..2fcf550 --- /dev/null +++ b/libs/serialdebug.lst @@ -0,0 +1,595 @@ + 1 .file "serialdebug.c" + 2 __SP_H__ = 0x3e + 3 __SP_L__ = 0x3d + 4 __SREG__ = 0x3f + 5 __tmp_reg__ = 0 + 6 __zero_reg__ = 1 + 7 .text + 8 .Ltext0: + 9 .cfi_sections .debug_frame + 10 .global usart_putchar + 12 usart_putchar: + 13 .LFB8: + 14 .file 1 "libs/serialdebug.c" + 1:libs/serialdebug.c **** #include + 2:libs/serialdebug.c **** + 3:libs/serialdebug.c **** #if SERIALDEBUG_USE_INTS + 4:libs/serialdebug.c **** #include + 5:libs/serialdebug.c **** #endif + 6:libs/serialdebug.c **** + 7:libs/serialdebug.c **** void serial_configure(void) { + 8:libs/serialdebug.c **** // Configure serial output + 9:libs/serialdebug.c **** SERIALDEBUG_SOFT_TX_PORT |= (1< + 43:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #include + 44:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #include + 45:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 46:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** /** \file */ + 47:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** /** \defgroup util_delay : Convenience functions for busy-wait delay loops + 48:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** \code + 49:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #define F_CPU 1000000UL // 1 MHz + 50:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** //#define F_CPU 14.7456E6 + 51:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #include + 52:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** \endcode + 53:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 54:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** \note As an alternative method, it is possible to pass the + 55:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** F_CPU macro down to the compiler from the Makefile. + 56:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Obviously, in that case, no \c \#define statement should be + 57:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** used. + 58:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 59:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** The functions in this header file are wrappers around the basic + 60:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** busy-wait functions from . They are meant as + 61:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** convenience functions where actual time values can be specified + 62:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** rather than a number of cycles to wait for. The idea behind is + 63:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** that compile-time constant expressions will be eliminated by + 64:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** compiler optimization so floating-point expressions can be used + 65:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** to calculate the number of delay cycles needed based on the CPU + 66:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** frequency passed by the macro F_CPU. + 67:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 68:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** \note In order for these functions to work as intended, compiler + 69:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** optimizations must be enabled, and the delay time + 70:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** must be an expression that is a known constant at + 71:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** compile-time. If these requirements are not met, the resulting + 72:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** delay will be much longer (and basically unpredictable), and + 73:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** applications that otherwise do not use floating-point calculations + 74:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** will experience severe code bloat by the floating-point library + 75:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** routines linked into the application. + 76:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 77:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** The functions available allow the specification of microsecond, and + 78:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** millisecond delays directly, using the application-supplied macro + 79:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** F_CPU as the CPU clock frequency (in Hertz). + 80:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 81:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** */ + 82:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 83:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #if !defined(__DOXYGEN__) + 84:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** static inline void _delay_us(double __us) __attribute__((always_inline)); + 85:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** static inline void _delay_ms(double __ms) __attribute__((always_inline)); + 86:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #endif + 87:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 88:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #ifndef F_CPU + 89:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** /* prevent compiler error by supplying a default */ + 90:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** # warning "F_CPU not defined for " + 91:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** # define F_CPU 1000000UL + 92:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #endif + 93:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 94:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #ifndef __OPTIMIZE__ + 95:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** # warning "Compiler optimizations disabled; functions from won't work as designed" + 96:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #endif + 97:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 98:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #if __HAS_DELAY_CYCLES && defined(__OPTIMIZE__) && \ + 99:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** !defined(__DELAY_BACKWARD_COMPATIBLE__) && \ + 100:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __STDC_HOSTED__ + 101:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** # include + 102:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #endif + 103:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 104:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** /** + 105:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** \ingroup util_delay + 106:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 107:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Perform a delay of \c __ms milliseconds, using _delay_loop_2(). + 108:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 109:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** The macro F_CPU is supposed to be defined to a + 110:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** constant defining the CPU clock frequency (in Hertz). + 111:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 112:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** The maximal possible delay is 262.14 ms / F_CPU in MHz. + 113:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 114:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** When the user request delay which exceed the maximum possible one, + 115:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** _delay_ms() provides a decreased resolution functionality. In this + 116:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** mode _delay_ms() will work with a resolution of 1/10 ms, providing + 117:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** delays up to 6.5535 seconds (independent from CPU frequency). The + 118:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** user will not be informed about decreased resolution. + 119:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 120:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** If the avr-gcc toolchain has __builtin_avr_delay_cycles(unsigned long) + 121:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** support, maximal possible delay is 4294967.295 ms/ F_CPU in MHz. For + 122:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** values greater than the maximal possible delay, overflows results in + 123:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** no delay i.e., 0ms. + 124:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 125:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Conversion of __us into clock cycles may not always result in integer. + 126:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** By default, the clock cycles rounded up to next integer. This ensures that + 127:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** the user gets atleast __us microseconds of delay. + 128:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 129:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Alternatively, user can define __DELAY_ROUND_DOWN__ and __DELAY_ROUND_CLOSEST__ + 130:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** to round down and round to closest integer. + 131:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 132:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Note: The new implementation of _delay_ms(double __ms) with + 133:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __builtin_avr_delay_cycles(unsigned long) support is not backward compatible. + 134:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** User can define __DELAY_BACKWARD_COMPATIBLE__ to get a backward compatible delay. + 135:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Also, the backward compatible + 136:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** algorithm will be chosen if the code is compiled in a freestanding + 137:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** environment (GCC option \c -ffreestanding), as the math functions + 138:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** required for rounding are not available to the compiler then. + 139:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 140:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** */ + 141:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** void + 142:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** _delay_ms(double __ms) + 143:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** { + 144:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** double __tmp ; + 145:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #if __HAS_DELAY_CYCLES && defined(__OPTIMIZE__) && \ + 146:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** !defined(__DELAY_BACKWARD_COMPATIBLE__) && \ + 147:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __STDC_HOSTED__ + 148:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** uint32_t __ticks_dc; + 149:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** extern void __builtin_avr_delay_cycles(unsigned long); + 150:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __tmp = ((F_CPU) / 1e3) * __ms; + 151:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 152:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #if defined(__DELAY_ROUND_DOWN__) + 153:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks_dc = (uint32_t)fabs(__tmp); + 154:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 155:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #elif defined(__DELAY_ROUND_CLOSEST__) + 156:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks_dc = (uint32_t)(fabs(__tmp)+0.5); + 157:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 158:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #else + 159:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** //round up by default + 160:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks_dc = (uint32_t)(ceil(fabs(__tmp))); + 161:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #endif + 162:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 163:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __builtin_avr_delay_cycles(__ticks_dc); + 164:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 165:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #else + 166:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** uint16_t __ticks; + 167:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __tmp = ((F_CPU) / 4e3) * __ms; + 168:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** if (__tmp < 1.0) + 169:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks = 1; + 170:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** else if (__tmp > 65535) + 171:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** { + 172:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** // __ticks = requested delay in 1/10 ms + 173:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks = (uint16_t) (__ms * 10.0); + 174:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** while(__ticks) + 175:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** { + 176:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** // wait 1/10 ms + 177:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** _delay_loop_2(((F_CPU) / 4e3) / 10); + 178:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks --; + 179:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** } + 180:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** return; + 181:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** } + 182:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** else + 183:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks = (uint16_t)__tmp; + 184:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** _delay_loop_2(__ticks); + 185:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #endif + 186:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** } + 187:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 188:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** /** + 189:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** \ingroup util_delay + 190:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 191:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Perform a delay of \c __us microseconds, using _delay_loop_1(). + 192:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 193:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** The macro F_CPU is supposed to be defined to a + 194:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** constant defining the CPU clock frequency (in Hertz). + 195:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 196:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** The maximal possible delay is 768 us / F_CPU in MHz. + 197:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 198:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** If the user requests a delay greater than the maximal possible one, + 199:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** _delay_us() will automatically call _delay_ms() instead. The user + 200:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** will not be informed about this case. + 201:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 202:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** If the avr-gcc toolchain has __builtin_avr_delay_cycles(unsigned long) + 203:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** support, maximal possible delay is 4294967.295 us/ F_CPU in MHz. For + 204:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** values greater than the maximal possible delay, overflow results in + 205:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** no delay i.e., 0us. + 206:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 207:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Conversion of __us into clock cycles may not always result in integer. + 208:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** By default, the clock cycles rounded up to next integer. This ensures that + 209:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** the user gets atleast __us microseconds of delay. + 210:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 211:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Alternatively, user can define __DELAY_ROUND_DOWN__ and __DELAY_ROUND_CLOSEST__ + 212:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** to round down and round to closest integer. + 213:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 214:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Note: The new implementation of _delay_us(double __us) with + 215:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __builtin_avr_delay_cycles(unsigned long) support is not backward compatible. + 216:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** User can define __DELAY_BACKWARD_COMPATIBLE__ to get a backward compatible delay. + 217:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** Also, the backward compatible + 218:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** algorithm will be chosen if the code is compiled in a freestanding + 219:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** environment (GCC option \c -ffreestanding), as the math functions + 220:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** required for rounding are not available to the compiler then. + 221:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 222:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** */ + 223:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** void + 224:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** _delay_us(double __us) + 225:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** { + 226:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** double __tmp ; + 227:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #if __HAS_DELAY_CYCLES && defined(__OPTIMIZE__) && \ + 228:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** !defined(__DELAY_BACKWARD_COMPATIBLE__) && \ + 229:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __STDC_HOSTED__ + 230:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** uint32_t __ticks_dc; + 231:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** extern void __builtin_avr_delay_cycles(unsigned long); + 232:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __tmp = ((F_CPU) / 1e6) * __us; + 233:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 234:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #if defined(__DELAY_ROUND_DOWN__) + 235:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks_dc = (uint32_t)fabs(__tmp); + 236:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 237:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #elif defined(__DELAY_ROUND_CLOSEST__) + 238:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks_dc = (uint32_t)(fabs(__tmp)+0.5); + 239:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 240:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #else + 241:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** //round up by default + 242:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __ticks_dc = (uint32_t)(ceil(fabs(__tmp))); + 243:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** #endif + 244:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** + 245:/usr/local/CrossPack-AVR-20131216/avr/include/util/delay.h **** __builtin_avr_delay_cycles(__ticks_dc); + 28 .loc 2 245 0 + 29 0002 EFEC ldi r30,lo8(207) + 30 0004 F0E0 ldi r31,hi8(207) + 31 0006 3197 1: sbiw r30,1 + 32 0008 01F4 brne 1b + 33 000a 00C0 rjmp . + 34 000c 0000 nop + 35 .LVL2: + 36 000e 28E0 ldi r18,lo8(8) + 37 0010 30E0 ldi r19,0 + 38 .LBE9: + 39 .LBE8: + 24:libs/serialdebug.c **** _delay_us(SERIALDEBUG_MICROSECONDS_PER_BIT); + 25:libs/serialdebug.c **** + 26:libs/serialdebug.c **** // data bits + 27:libs/serialdebug.c **** for (bit_mask=0x01; bit_mask; bit_mask<<=1) { + 40 .loc 1 27 0 + 41 0012 91E0 ldi r25,lo8(1) + 42 .LVL3: + 43 .L5: + 28:libs/serialdebug.c **** if (c & bit_mask) { + 44 .loc 1 28 0 + 45 0014 492F mov r20,r25 + 46 0016 4823 and r20,r24 + 47 0018 01F0 breq .L2 + 29:libs/serialdebug.c **** SERIALDEBUG_SOFT_TX_PORT |= (1< +#include +#include +#include +#include +#include +#include + +// User settings +#define HOUR 3600 +// I wanted each block of time to be 45 mins, but my chip's clock is slow +// 40 should roughly scale to 45 +#define BLOCK_MINS 40 +// How long to pour for +#define DISPENSE_MS 12000 + +/* + + + vcc [1 14] gnd + [2 13] input speed_0 + [3 12] input speed_1 + rst [4 11] input speed_2 +INT/input feed button [5 10] input speed_3 + motor [6 9] + debug serial [7 8] +*/ + +// Ports + +#define FEED_DDR DDRA +#define FEED_PORT PORTA +#define FEED_BIT PA7 + +#define PWRON_DDR DDRA +#define PWRON_PORT PORTA +#define PWRON_BIT PA5 + +#define FORCE_DDR DDRB +#define FORCE_PORT PINB +#define FORCE_BIT PB2 + +#define NIBBLE0_DDR DDRA +#define NIBBLE0_PORT PINA +#define NIBBLE0_BIT PA0 + +#define NIBBLE1_DDR DDRA +#define NIBBLE1_PORT PINA +#define NIBBLE1_BIT PA1 + +#define NIBBLE2_DDR DDRA +#define NIBBLE2_PORT PINA +#define NIBBLE2_BIT PA2 + +#define NIBBLE3_DDR DDRA +#define NIBBLE3_PORT PINA +#define NIBBLE3_BIT PA3 + + +void motor_on(void) { + // Start turning the motor + SET_BIT(FEED_PORT, FEED_BIT); +} + +void motor_off(void) { + // Stop turning the motor + CLEAR_BIT(FEED_PORT, FEED_BIT); +} + +void dispense(int ms) { + // Activate motor for X ms + motor_on(); + while(ms > 0) { + _delay_ms(15); + ms -= 15; + } + motor_off(); +} + +void pwr_on(void) { + // Enable power to the motor control and input dial + SET_BIT(PWRON_PORT, PWRON_BIT); +} + +void pwr_off(void) { + // Disable power to the motor control and input dial + CLEAR_BIT(PWRON_PORT, PWRON_BIT); +} + +#define WAKEUP_PULSE 100 +#define WAKEUP_BETWEEN 75 + +void bootpulse(void) { + // Pulse the motor quickly + motor_on(); + _delay_ms(WAKEUP_PULSE); + motor_off(); + _delay_ms(WAKEUP_BETWEEN); +} + +void wakeup(void) { + // Perform + motor_on(); + for(int i=0;i<6;i++) bootpulse(); + motor_off(); +} + +void delay_s(int seconds) { + while(seconds-- > 0) { + for (int i = 0; i < 10; i++) + _delay_ms(100); + #ifdef __DEBUG__ + dbg_putstring("Wait...\n"); + #endif + } +} + +uint8_t get_speed() { + // Get speed setting, 0-15 from 4 pins + uint8_t speed = 0; + speed |= (IS_SET(NIBBLE3_PORT,NIBBLE3_BIT)); + speed |= (IS_SET(NIBBLE2_PORT,NIBBLE2_BIT) << 1); + speed |= (IS_SET(NIBBLE1_PORT,NIBBLE1_BIT) << 2); + speed |= (IS_SET(NIBBLE0_PORT,NIBBLE0_BIT) << 3); + return speed; +} + +void setup() { + // PART 1: setup io pins + // Set motor pin as output + SET_BIT(FEED_DDR, FEED_BIT); + // Set button pin as input + CLEAR_BIT(FORCE_DDR, FORCE_BIT); + // Set speed control pins as input + CLEAR_BIT(NIBBLE0_DDR, NIBBLE0_BIT); + CLEAR_BIT(NIBBLE1_DDR, NIBBLE1_BIT); + CLEAR_BIT(NIBBLE2_DDR, NIBBLE2_BIT); + CLEAR_BIT(NIBBLE3_DDR, NIBBLE3_BIT); + + // PART 2: Set up interupts + cli(); //disable global interrupts + + // Set ISC01= 1 and ISC00 = 0 to generate an external interrupt request + // on any change of INT0 + SET_BIT(MCUCR,ISC00); + SET_BIT(MCUCR,ISC01); + //enable external interrupt request 0 + SET_BIT(GIMSK,INT0); + + // disable ADC (saves power) + ADCSRA = 0; + + // configure sleep mode + WDTCSR |= (_BV(WDCE) | _BV(WDE)); // Enable the WD Change Bit + WDTCSR = _BV(WDIE) | // Enable WDT Interrupt + _BV(WDP1) | _BV(WDP2); // Set Timeout to 1 seconds + + //enable global interrupts + sei(); +} + +void gotosleep(void) { + // Put the MCU to sleep + sleep_bod_disable(); + set_sleep_mode(SLEEP_MODE_PWR_DOWN); + sleep_enable(); + sleep_cpu(); +} + +// Use to track timing +long seconds_since_last_spin = 0; + +// watchdog interrupt +ISR (WDT_vect) { + // break out of sleep mode + sleep_disable(); + // Increment time counter + seconds_since_last_spin += 1; +} + +//external interrupt ISR (for INT0 pin) +ISR(INT0_vect) { + #ifdef __DEBUG__ + dbg_putstring("AYE MATEY\n"); + #endif + pwr_on(); + while(IS_SET(FORCE_PORT, FORCE_BIT)) { + motor_on(); + } + motor_off(); + seconds_since_last_spin = 0; +} + +int main(void) { + // Reset all pin configs + DDRA = 0b00000000; + DDRB = 0b00000000; + + #ifdef __DEBUG__ + serial_configure(); + dbg_putstring("\n\n"); + #endif + + // Configure the mcu + setup(); + + // Wiggle so the user knows we're alive + pwr_on(); + wakeup(); + pwr_off(); + + delay_s(1); + + while (1) { + if (seconds_since_last_spin % 10 == 0) { + pwr_on(); + _delay_ms(1); // Let input pins warm up + + // speed can be 0-15 + // delay = 2hrs + speed * .75h + // range of 1hr to 12hrs25m + uint8_t speed = get_speed(); + long sleep_max = HOUR + (BLOCK_MINS * 60 * speed); // 45 mins per unit of speed + + long to_go = sleep_max - seconds_since_last_spin; + + #ifdef __DEBUG__ + dbg_putstring("Mins to sleep: "); + dbg_putint((unsigned int)sleep_max/60); + dbg_putstring("\n"); + dbg_putstring("Mins to go: "); + dbg_putint((unsigned int)to_go/60); + dbg_putstring("\n\n"); + #endif + + // If it's been long enough, do the thing + if(to_go <= 0) { + dispense(DISPENSE_MS); + seconds_since_last_spin = 0; + } + } + + // Sleep and do it all again tomorrow + pwr_off(); + gotosleep(); + } + + return 0; + +}