Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

The unmodified software from

  • Loading branch information...
commit 40883712781a93f3d193098e029897363fbccaf2 0 parents
@timkrins authored
Showing with 6,462 additions and 0 deletions.
  1. +19 −0 .gitignore
  2. +340 −0 LICENSE
  3. +170 −0 Makefile
  4. +212 −0 README
  5. +103 −0 analog.c
  6. +31 −0 analog.h
  7. +64 −0 arduino.h
  8. +363 −0 arduino_644.h
  9. +86 −0 calc.pl
  10. +45 −0 clock.c
  11. +7 −0 clock.h
  12. +435 −0 config.h.dist
  13. +101 −0 copier.c
  14. +45 −0 copier.h
  15. +591 −0 dda.c
  16. +130 −0 dda.h
  17. +135 −0 dda_queue.c
  18. +42 −0 dda_queue.h
  19. +3 −0  debug.c
  20. +21 −0 debug.h
  21. +50 −0 delay.c
  22. +15 −0 delay.h
  23. +312 −0 func.sh
  24. +29 −0 fuses.h
  25. +381 −0 gcode_parse.c
  26. +83 −0 gcode_parse.h
  27. +432 −0 gcode_process.c
  28. +22 −0 gcode_process.h
  29. +253 −0 heater.c
  30. +20 −0 heater.h
  31. +219 −0 intercom.c
  32. +17 −0 intercom.h
  33. +172 −0 mendel.c
  34. +312 −0 mendel_cmd
  35. +20 −0 pinio.c
  36. +154 −0 pinio.h
  37. +25 −0 sender.sh
  38. +219 −0 serial.c
  39. +30 −0 serial.h
  40. +53 −0 sermsg.c
  41. +21 −0 sermsg.h
  42. +133 −0 sersendf.c
  43. +9 −0 sersendf.h
  44. +290 −0 temp.c
  45. +29 −0 temp.h
  46. +120 −0 timer.c
  47. +30 −0 timer.h
  48. +47 −0 watchdog.c
  49. +22 −0 watchdog.h
19 .gitignore
@@ -0,0 +1,19 @@
+*.o
+*.elf
+*.lst
+*.map
+*.sym
+*.lss
+*.eep
+*.srec
+*.bin
+*.hex
+*.al
+*.i
+*.s
+*~
+
+temporal.png
+temporal_data
+config.h
+sim
340 LICENSE
@@ -0,0 +1,340 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+ 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+ <one line to give the program's name and a brief idea of what it does.>
+ Copyright (C) <year> <name of author>
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+ Gnomovision version 69, Copyright (C) year name of author
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+ This is free software, and you are welcome to redistribute it
+ under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License. Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+ <signature of Ty Coon>, 1 April 1989
+ Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library. If this is what you want to do, use the GNU Library General
+Public License instead of this License.
170 Makefile
@@ -0,0 +1,170 @@
+##############################################################################
+# #
+# FiveD on Arduino - alternative firmware for repraps #
+# #
+# by Triffid Hunter, Traumflug, jakepoz #
+# #
+# #
+# This firmware is Copyright (C) 2009-2010 Michael Moon aka Triffid_Hunter #
+# #
+# This program is free software; you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation; either version 2 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program; if not, write to the Free Software #
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #
+# #
+##############################################################################
+
+##############################################################################
+# #
+# Change these to suit your hardware #
+# #
+##############################################################################
+
+# MCU_TARGET = atmega168
+# MCU_TARGET = atmega328p
+MCU_TARGET = atmega644p
+# MCU_TARGET = atmega1280
+
+# F_CPU = 16000000L
+
+##############################################################################
+# #
+# Available Defines: #
+# #
+# DEBUG #
+# enables tons of debugging output. may cause host-side talkers to choke #
+# XONXOFF #
+# enables XON/XOFF flow control for stupid or crude talkers #
+# ACCELERATION_REPRAP #
+# enables reprap-style acceleration #
+# ACCELERATION_RAMPING #
+# enables start/stop ramping #
+# ACCELERATION_TEMPORAL #
+# enables experimental temporal step algorithm - not technically a type of #
+# acceleration, but since it controls step timing it seems appropriate #
+# GEN3 #
+# build for standard reprap electronics instead of your custom rig #
+# HOST #
+# this is the motherboard for GEN3- don't touch! Extruder has its own #
+# Makefile. #
+# #
+##############################################################################
+
+# DEFS = -DF_CPU=$(F_CPU) -DHOST -DGEN3
+# DEFS += "-DDEBUG=1"
+
+##############################################################################
+# #
+# Programmer settings for "make program" #
+# #
+##############################################################################
+
+AVRDUDE = avrdude
+AVRDUDECONF = /etc/avrdude.conf
+
+##############################################################################
+# #
+# udev rule for /dev/arduino (insert into /etc/udev/rules.d/99-local.rules) #
+# SUBSYSTEMS=="usb", ATTRS{idProduct}=="6001", ATTRS{idVendor}=="0403", #
+# NAME="%k", SYMLINK+="arduino", SYMLINK+="arduino_$attr{serial}", #
+# MODE="0660" #
+# #
+##############################################################################
+
+PROGPORT = /dev/arduino
+
+# atmega168
+#PROGBAUD = 19200
+# atmega328p, 644p, 1280
+PROGBAUD = 57600
+
+
+##############################################################################
+# #
+# These defaults should be ok, change if you need to #
+# #
+##############################################################################
+
+PROGRAM = mendel
+
+SOURCES = $(PROGRAM).c serial.c dda.c gcode_parse.c gcode_process.c timer.c temp.c sermsg.c dda_queue.c watchdog.c debug.c sersendf.c heater.c analog.c delay.c intercom.c pinio.c clock.c
+
+ARCH = avr-
+CC = $(ARCH)gcc
+OBJDUMP = $(ARCH)objdump
+OBJCOPY = $(ARCH)objcopy
+
+OPTIMIZE = -Os -ffunction-sections -finline-functions-called-once -mcall-prologues
+# OPTIMIZE = -O0
+CFLAGS = -g -Wall -Wstrict-prototypes $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) -std=gnu99 -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -save-temps
+LDFLAGS = -Wl,--as-needed -Wl,--gc-sections
+
+OBJ = $(patsubst %.c,%.o,${SOURCES})
+
+.PHONY: all program clean size
+.PRECIOUS: %.o %.elf
+
+all: config.h $(PROGRAM).hex $(PROGRAM).lst $(PROGRAM).sym size
+
+program: $(PROGRAM).hex config.h
+ stty $(PROGBAUD) raw ignbrk hup < $(PROGPORT)
+ @sleep 0.1
+ @stty $(PROGBAUD) raw ignbrk hup < $(PROGPORT)
+ $(AVRDUDE) -cstk500v1 -b$(PROGBAUD) -p$(MCU_TARGET) -P$(PROGPORT) -C$(AVRDUDECONF) -U flash:w:$^
+ stty 115200 raw ignbrk -hup -echo ixoff < $(PROGPORT)
+
+program-fuses:
+ avr-objdump -s -j .fuse mendel.o | perl -ne '/\s0000\s([0-9a-f]{2})/ && print "$$1\n"' > lfuse
+ avr-objdump -s -j .fuse mendel.o | perl -ne '/\s0000\s..([0-9a-f]{2})/ && print "$$1\n"' > hfuse
+ avr-objdump -s -j .fuse mendel.o | perl -ne '/\s0000\s....([0-9a-f]{2})/ && print "$$1\n"' > efuse
+ $(AVRDUDE) -cstk500v1 -b$(PROGBAUD) -p$(MCU_TARGET) -P$(PROGPORT) -C$(AVRDUDECONF) -U lfuse:w:lfuse
+ $(AVRDUDE) -cstk500v1 -b$(PROGBAUD) -p$(MCU_TARGET) -P$(PROGPORT) -C$(AVRDUDECONF) -U hfuse:w:hfuse
+ $(AVRDUDE) -cstk500v1 -b$(PROGBAUD) -p$(MCU_TARGET) -P$(PROGPORT) -C$(AVRDUDECONF) -U efuse:w:efuse
+
+clean:
+ rm -rf *.o *.elf *.lst *.map *.sym *.lss *.eep *.srec *.bin *.hex *.al *.i *.s *~ *fuse
+
+size: $(PROGRAM).elf
+ @echo " SIZE Atmega168 Atmega328p Atmega644"
+ @$(OBJDUMP) -h $^ | perl -MPOSIX -ne '/.(text)\s+([0-9a-f]+)/ && do { $$a += eval "0x$$2" }; END { printf " FLASH : %5d bytes (%2d%% of %2dkb) (%2d%% of %2dkb) (%2d%% of %2dkb)\n", $$a, ceil($$a * 100 / (15 * 1024)), 15, ceil($$a * 100 / (31 * 1024)), 31, ceil($$a * 100 / (63 * 1024)), 63 }'
+ @$(OBJDUMP) -h $^ | perl -MPOSIX -ne '/.(data|bss)\s+([0-9a-f]+)/ && do { $$a += eval "0x$$2" }; END { printf " RAM : %5d bytes (%2d%% of %2dkb) (%2d%% of %2dkb) (%2d%% of %2dkb)\n", $$a, ceil($$a * 100 / (1 * 1024)), 1, ceil($$a * 100 / (2 * 1024)), 2, ceil($$a * 100 / (4 * 1024)), 4 }'
+ @$(OBJDUMP) -h $^ | perl -MPOSIX -ne '/.(eeprom)\s+([0-9a-f]+)/ && do { $$a += eval "0x$$2" }; END { printf " EEPROM: %5d bytes (%2d%% of %2dkb) (%2d%% of %2dkb) (%2d%% of %2dkb)\n", $$a, ceil($$a * 100 / (1 * 1024)), 1, ceil($$a * 100 / (2 * 1024)), 2, ceil($$a * 100 / (2 * 1024)), 2 }'
+
+config.h: config.h.dist
+ @echo "Please review config.h, as config.h.dist is more recent."
+ @echo
+ @diff -bBEuF '^. [[:digit:]]. [[:upper:]]' config.h config.h.dist
+ @false
+
+%.o: %.c config.h Makefile
+ @echo " CC $@"
+ @$(CC) -c $(CFLAGS) -Wa,-adhlns=$(<:.c=.al) -o $@ $(subst .o,.c,$@)
+
+%.elf: $(OBJ)
+ @echo " LINK $@"
+ @$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
+
+%.lst: %.elf
+ @echo " OBJDUMP $@"
+ @$(OBJDUMP) -h -S $< > $@
+
+%.hex: %.elf
+ @echo " OBJCOPY $@"
+ @$(OBJCOPY) -j .text -j .data -O ihex $< $@
+
+%.bin: %.elf
+ @echo " OBJCOPY $@"
+ @$(OBJCOPY) -j .text -j .data -O binary $< $@
+
+%.sym: %.elf
+ @echo " SYM $@"
+ @$(OBJDUMP) -t $< | perl -ne 'BEGIN { printf " ADDR NAME SIZE\n"; } /([0-9a-f]+)\s+(\w+)\s+O\s+\.(bss|data)\s+([0-9a-f]+)\s+(\w+)/ && printf "0x%04x %-20s +%d\n", eval("0x$$1") & 0xFFFF, $$5, eval("0x$$4")' | sort -k1 > $@
212 README
@@ -0,0 +1,212 @@
+Rewrite of Reprap Mendel firmware:
+
+* 100% integer computations
+* serial transmit buffer
+* can fit onto atmega168 depending on selected options
+* works on atmega328p
+* works on atmega644p
+* porting to atmega1280 in progress
+* will work on larger atmegas with minor porting
+
+##############################################################################
+# #
+# How to use #
+# #
+##############################################################################
+
+1) COPY config.h.dist to config.h and edit to suit your electronics
+2) check programming settings in Makefile
+3) make
+4) make program
+4a) if programming blank chip, make program-fuses
+5) ./sender.sh
+6) have a play, go to 1) if not right
+7) try printing something!
+
+##############################################################################
+# #
+# Requirements #
+# #
+##############################################################################
+
+Compile:
+ gnu make
+ binutils, gcc, etc built for avr target (avr-gcc, avr-as, etc)
+ avr-libc
+Program:
+ avrdude
+ something that avrdude supports: bootloader, separate programmer, whatever
+
+##############################################################################
+# #
+# License #
+# #
+##############################################################################
+
+This firmware is Copyright (C) 2009-2010 Michael Moon aka Triffid_Hunter
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+##############################################################################
+# #
+# Rationale and History #
+# #
+##############################################################################
+
+I started building my electronics with only a regular arduino to test with.
+This was perfectly sufficient for playing with the pololu stepper controllers and the max6675 I bought after reading about all the issues with thermistors that people were having. After a while I decided to check out the official firmware but it required an atmega644. I wondered why.
+So, I decided to skim through the code to see what took up so much space. From what I could see, it was written by someone who was familiar with programming desktop systems and larger embedded devices, but didn't have much experience with small devices such as the atmega168 and atmega644.
+This showed in the use of C++ which served only to make the code harder to read, and the prolific use of floating-point math, with some appearing even in interrupt context!
+I came to the conclusion that there was no reason that the main body of code couldn't fit onto an atmega168 except for the burdensome and unnecessary overheads from object-oriented code and floating point math. A quick count assured me that the atmega168 had enough pins, but only barely, and I started reading the official firmware properly, with an eye to rewriting as much as possible in a fashion suitable for small microcontrollers.
+
+Starting with an arduino skeleton library I had assembled over time, some of my test code and the official firmware, I hacked up a passable integer-only, straight C implementation of the dda, and wrote my own gcode parser from scratch which processed each character as it arrived (with some buffering of course) instead of waiting for a whole line and then trying to process it all at once.
+
+As soon as my new firmware was able to run a few consecutive moves, I released it for peer review.
+
+The forum thread http://forums.reprap.org/read.php?147,33082 has much of the history from this point on.
+
+Traumflug was the first to send patches, and has done a significant amount of work on a number of different parts of this firmware.
+jakepoz ported it to official reprap electronics (gen3 branch)
+Cefiar posted me some thermistors to sponsor addition of thermistor-reading code
+
+Many others have given encouragement and suggestions without which this firmware may never be what it is today.
+
+
+##############################################################################
+# #
+# Architectural Overview #
+# #
+##############################################################################
+
+FiveD on Arduino is quite similar to the official firmware in some ways, and markedly different in others. FiveD on Arduino has as much modularity as I could get away with without sacrificing efficiency.
+
+// FIXME: make next paragraph easier to read
+At startup, the code in mendel.c is run first. This initialises all the modules that need it, then starts polling the clock flags and feeding incoming serial characters to the gcode parser. The gcode parser processes each character individually, keeping track via internal state rather than buffering a line and skipping back and forth. The gcode parser converts floating values to integer or fixed-point representations as soon as it encounters a non-numeric character. It calls many module functions directly, but the most interesting part is move creation, where it passes a target position and speed to enqueue()[dda_queue.c] which adds it to the queue, and fires up dda_start()[dda.c] if the queue was empty. dda_start initialises the dda, figures out the stepper directions and first step timeout and a few other bits of housekeeping, then sets the timer for the appropriate timeout. When the timer fires, it calls dda_step()[dda.c] which sends all the step signals then figures out the next step timeout based on acceleration and speed settings. When the last step has been made, the dda "dies" (sets 'live' property to 0) after which queue_step[dda_queue.c] advances the queue read pointer and starts the next dda.
+
+It is necessary to keep interrupts very short on small microcontrollers, and I have endeavoured to keep them all as short as possible. Unfortunately, dda_step[dda.c] is fairly large. I simply hope that it doesn't take so much time that it interferes with the other interrupts too much.
+
+
+##############################################################################
+# #
+# Interesting code sections #
+# #
+##############################################################################
+
+The serial ringbuffers are critical for good communication, but for some reason the official arduino libraries don't implement a tx queue, all but preventing sending stuff from interrupt context. As long as the queues have a length of 2^n, we can use bitwise operations rather than numerical comparison to trim the read and write pointers. The serial send function (serial_writechar[serial.c]) is necessarily careful about checking if it's in an interrupt and only waiting for space in the queue if it's not.
+The dda queue is also a ringbuffer, although its implementation is harder to see as it's embedded in lots of other stuff.
+
+The gcode parser shows how to parse each character as it comes in, so 99% of a command can be processed before the EOL is even received. It started off as a simple state machine, which then grew and shrank and morphed until it was both smaller and more functional.
+
+The fixed-point stuff is fun, although we have to manually ensure that the decimal point stays in the right spot. decfloat_to_int[gcode.h] is used to convert incoming floats to integer implementations by starting off with a (very!) crude floating point implementation, then choosing appropriate scaling factors within the gcode parser itself. This allows us to do a little stuff that looks like floating-point math without the burdensome overhead of a full fp implementation.
+
+The PID code in heater.c is probably quite generalisable, and seems to work well when tuned. Google knows of plenty of PID tuning guides.
+
+##############################################################################
+# #
+# Resources #
+# #
+##############################################################################
+
+Forum thread: http://forums.reprap.org/read.php?147,33082
+Source Repository: http://github.com/triffid/FiveD_on_Arduino
+Wiki Page: http://objects.reprap.org/wiki/FiveD_on_Arduino
+
+##############################################################################
+# #
+# File descriptions #
+# #
+##############################################################################
+
+*** analog.[ch]
+This is the analog subsystem. Only used if you have a thermistor or ad595
+
+*** arduino.h, arduino_[chip].h
+Pin mappings and helper functions for various atmegas
+
+*** clock.[ch]
+Regular functions that run in main loop rather than an interrupt
+
+*** config.h.dist, config.h
+Configuration for your electronics and hardware. Copy config.h.dist to config.h, edit config.h to suit
+
+*** copier.[ch]
+A totally untested and currently unused chunk of code for copying firmware to another identical chip
+
+*** dda.[ch]
+A rather complex block of math that figures out when to step each axis according to speed and acceleration profiles and received moves
+
+*** dda_queue.[ch]
+The queue of moves received from the host.
+
+*** debug.[ch]
+Debugging aids
+
+*** delay.[ch]
+Delay functions
+
+*** FiveD_on_Arduino.pde
+Allows firmware to be built in arduino ide
+
+*** func.sh
+Lots of host-side shell scripts for talking to firmware
+
+*** gcode_parse.[ch]
+Gcode parser. Scaling of factors to internally used integer or fixed point happens here too.
+
+*** gcode_process.[ch]
+Gcodes actually get executed here after being parsed.
+
+*** heater.[ch]
+Heater management, including PID and PWM algorithms, and some configuration parameters
+
+*** intercom.[ch]
+Gen3 serial link control and communication
+
+*** LICENSE
+Gnu GPL2 license
+
+*** Makefile
+instructions for make on how to build firmware. has a list of modules to build which may need to be updated every so often
+
+*** mendel.c
+Firmware startup and main loop code
+
+*** pinio.h
+A few I/O primitives
+
+*** README
+this file
+
+*** sender.sh
+A simple talker
+
+*** serial.[ch]
+Serial management and buffers
+
+*** sermsg.[ch]
+Functions for sending messages and values to host
+
+*** sersendf.[ch]
+A small, crude printf implementation
+
+*** temp.[ch]
+Temperature sensor management, includes some configuration parameters
+
+*** timer.[ch]
+Timer management, used primarily by dda.c for timing steps
+
+*** watchdog.[ch]
+Watchdog management. resets chip if firmware locks up or does something strange
+
103 analog.c
@@ -0,0 +1,103 @@
+#include "analog.h"
+
+#include <avr/interrupt.h>
+
+#ifndef ANALOG_MASK
+ #warning ANALOG_MASK not defined - analog subsystem disabled
+ #define ANALOG_MASK 0
+#endif
+
+uint8_t adc_running_mask, adc_counter;
+
+#if ANALOG_MASK & 2
+ #define ANALOG_START 1
+ #define ANALOG_START_MASK 2
+#elif ANALOG_MASK & 4
+ #define ANALOG_START 2
+ #define ANALOG_START_MASK 4
+#elif ANALOG_MASK & 8
+ #define ANALOG_START 3
+ #define ANALOG_START_MASK 8
+#elif ANALOG_MASK & 16
+ #define ANALOG_START 4
+ #define ANALOG_START_MASK 16
+#elif ANALOG_MASK & 32
+ #define ANALOG_START 5
+ #define ANALOG_START_MASK 32
+#elif ANALOG_MASK & 64
+ #define ANALOG_START 6
+ #define ANALOG_START_MASK 64
+#elif ANALOG_MASK & 128
+ #define ANALOG_START 7
+ #define ANALOG_START_MASK 128
+#else
+ // ANALOG_MASK == 1 or 0, either way defines are the same except they're not used if ANALOG_MASK == 0
+ #define ANALOG_START 0
+ #define ANALOG_START_MASK 1
+#endif
+
+volatile uint16_t adc_result[8] __attribute__ ((__section__ (".bss")));
+
+void analog_init() {
+ #if ANALOG_MASK > 0
+ #ifdef PRR
+ PRR &= ~MASK(PRADC);
+ #elif defined PRR0
+ PRR0 &= ~MASK(PRADC);
+ #endif
+
+ ADMUX = REFERENCE;
+
+ // ADC frequency must be less than 200khz or we lose precision. At 16MHz system clock, we must use the full prescale value of 128 to get an ADC clock of 125khz.
+ ADCSRA = MASK(ADEN) | MASK(ADPS2) | MASK(ADPS1) | MASK(ADPS0);
+
+ adc_counter = 0;
+ adc_running_mask = 1;
+
+ AIO0_DDR &= ANALOG_MASK;
+ DIDR0 = ANALOG_MASK & 0x3F;
+
+ // now we start the first conversion and leave the rest to the interrupt
+ ADCSRA |= MASK(ADIE) | MASK(ADSC);
+ #endif /* ANALOG_MASK > 0 */
+}
+
+ISR(ADC_vect, ISR_NOBLOCK) {
+ // emulate free-running mode but be more deterministic about exactly which result we have, since this project has long-running interrupts
+ adc_result[ADMUX & 0x0F] = ADC;
+ // find next channel
+ do {
+ adc_counter++;
+ adc_running_mask <<= 1;
+ if (adc_counter == 8) {
+ adc_counter = ANALOG_START;
+ adc_running_mask = ANALOG_START_MASK;
+ }
+ } while ((adc_running_mask & ANALOG_MASK) == 0);
+
+ // start next conversion
+ ADMUX = (adc_counter) | REFERENCE;
+ ADCSRA |= MASK(ADSC);
+}
+
+uint16_t analog_read(uint8_t channel) {
+ #if ANALOG_MASK > 0
+ uint16_t r;
+
+ uint8_t sreg;
+ // save interrupt flag
+ sreg = SREG;
+ // disable interrupts
+ cli();
+
+ // atomic 16-bit copy
+ r = adc_result[channel];
+
+ // restore interrupt flag
+ SREG = sreg;
+
+ return r;
+ #else
+ return 0;
+ #endif
+}
31 analog.h
@@ -0,0 +1,31 @@
+#ifndef _ANALOG_H
+#define _ANALOG_H
+
+#include <stdint.h>
+
+#define REFERENCE_AREF 0
+#define REFERENCE_AVCC 64
+#if defined (__AVR_ATmega168__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
+ #define REFERENCE_1V1 192
+#elif defined (__AVR_ATmega_644__) || defined (__AVR_ATmega644p__)
+ #define REFERENCE_1V1 128
+ #define REFERENCE_2V56 192
+#endif
+
+#include "config.h"
+
+#ifndef REFERENCE
+#warning define REFERENCE as one of
+#if defined (__AVR_ATmega168__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
+ #warning REFERENCE_AREF, REFERENCE_AVCC or REFERENCE_1V1
+#elif defined (__AVR_ATmega_644__) || defined (__AVR_ATmega644p__)
+ #warning REFERENCE_AREF, REFERENCE_AVCC, REFERENCE_1V1 or REFERENCE_2V56
+#endif
+#warning in your config.h
+#error REFERENCE undefined
+#endif
+
+void analog_init(void);
+uint16_t analog_read(uint8_t channel);
+
+#endif /* _ANALOG_H */
64 arduino.h
@@ -0,0 +1,64 @@
+#ifndef _ARDUINO_H
+#define _ARDUINO_H
+
+#include <avr/io.h>
+
+/*
+ utility functions
+*/
+
+#ifndef MASK
+#define MASK(PIN) (1 << PIN)
+#endif
+
+/*
+ magic I/O routines
+
+ now you can simply SET_OUTPUT(STEP); WRITE(STEP, 1); WRITE(STEP, 0);
+*/
+
+#define _READ(IO) (IO ## _RPORT & MASK(IO ## _PIN))
+#define _WRITE(IO, v) do { if (v) { IO ## _WPORT |= MASK(IO ## _PIN); } else { IO ## _WPORT &= ~MASK(IO ## _PIN); }; } while (0)
+#define _TOGGLE(IO) do { IO ## _RPORT = MASK(IO ## _PIN); } while (0)
+
+#define _SET_INPUT(IO) do { IO ## _DDR &= ~MASK(IO ## _PIN); } while (0)
+#define _SET_OUTPUT(IO) do { IO ## _DDR |= MASK(IO ## _PIN); } while (0)
+
+#define _GET_INPUT(IO) ((IO ## _DDR & MASK(IO ## _PIN)) == 0)
+#define _GET_OUTPUT(IO) ((IO ## _DDR & MASK(IO ## _PIN)) != 0)
+
+// why double up on these macros? see http://gcc.gnu.org/onlinedocs/cpp/Stringification.html
+
+#define READ(IO) _READ(IO)
+#define WRITE(IO, v) _WRITE(IO, v)
+#define TOGGLE(IO) _TOGGLE(IO)
+
+#define SET_INPUT(IO) _SET_INPUT(IO)
+#define SET_OUTPUT(IO) _SET_OUTPUT(IO)
+
+#define GET_INPUT(IO) _GET_INPUT(IO)
+#define GET_OUTPUT(IO) _GET_OUTPUT(IO)
+
+/*
+ ports and functions
+
+ added as necessary or if I feel like it- not a comprehensive list!
+*/
+
+#if defined (__AVR_ATmega168__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
+ #include "arduino_168_328p.h"
+#endif /* _AVR_ATmega{168,328,328P}__ */
+
+#if defined (__AVR_ATmega644__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__)
+ #include "arduino_644.h"
+#endif /* _AVR_ATmega{644,644P,644PA}__ */
+
+#if defined (__AVR_ATmega1280__)
+ #include "arduino_1280.h"
+#endif /* __AVR_ATmega1280__ */
+
+#ifndef DIO0_PIN
+#error pins for this chip not defined in arduino.h! If you write an appropriate pin definition and have this firmware work on your chip, please tell us via the forum thread
+#endif
+
+#endif /* _ARDUINO_H */
363 arduino_644.h
@@ -0,0 +1,363 @@
+// UART
+#define RXD DIO8
+#define TXD DIO9
+#define RXD0 DIO8
+#define TXD0 DIO9
+
+#define RXD1 DIO10
+#define TXD1 DIO11
+
+// SPI
+#define SCK DIO7
+#define MISO DIO6
+#define MOSI DIO5
+#define SS DIO4
+
+// TWI (I2C)
+#define SCL DIO16
+#define SDA DIO17
+
+// timers and PWM
+#define OC0A DIO3
+#define OC0B DIO4
+#define OC1A DIO13
+#define OC1B DIO12
+#define OC2A DIO15
+#define OC2B DIO14
+
+#define DEBUG_LED DIO0
+/*
+pins
+*/
+
+#define DIO0_PIN PINB0
+#define DIO0_RPORT PINB
+#define DIO0_WPORT PORTB
+#define DIO0_DDR DDRB
+
+#define DIO1_PIN PINB1
+#define DIO1_RPORT PINB
+#define DIO1_WPORT PORTB
+#define DIO1_DDR DDRB
+
+#define DIO2_PIN PINB2
+#define DIO2_RPORT PINB
+#define DIO2_WPORT PORTB
+#define DIO2_DDR DDRB
+
+#define DIO3_PIN PINB3
+#define DIO3_RPORT PINB
+#define DIO3_WPORT PORTB
+#define DIO3_DDR DDRB
+
+#define DIO4_PIN PINB4
+#define DIO4_RPORT PINB
+#define DIO4_WPORT PORTB
+#define DIO4_DDR DDRB
+
+#define DIO5_PIN PINB5
+#define DIO5_RPORT PINB
+#define DIO5_WPORT PORTB
+#define DIO5_DDR DDRB
+
+#define DIO6_PIN PINB6
+#define DIO6_RPORT PINB
+#define DIO6_WPORT PORTB
+#define DIO6_DDR DDRB
+
+#define DIO7_PIN PINB7
+#define DIO7_RPORT PINB
+#define DIO7_WPORT PORTB
+#define DIO7_DDR DDRB
+
+#define DIO8_PIN PIND0
+#define DIO8_RPORT PIND
+#define DIO8_WPORT PORTD
+#define DIO8_DDR DDRD
+
+#define DIO9_PIN PIND1
+#define DIO9_RPORT PIND
+#define DIO9_WPORT PORTD
+#define DIO9_DDR DDRD
+
+#define DIO10_PIN PIND2
+#define DIO10_RPORT PIND
+#define DIO10_WPORT PORTD
+#define DIO10_DDR DDRD
+
+#define DIO11_PIN PIND3
+#define DIO11_RPORT PIND
+#define DIO11_WPORT PORTD
+#define DIO11_DDR DDRD
+
+#define DIO12_PIN PIND4
+#define DIO12_RPORT PIND
+#define DIO12_WPORT PORTD
+#define DIO12_DDR DDRD
+
+#define DIO13_PIN PIND5
+#define DIO13_RPORT PIND
+#define DIO13_WPORT PORTD
+#define DIO13_DDR DDRD
+
+#define DIO14_PIN PIND6
+#define DIO14_RPORT PIND
+#define DIO14_WPORT PORTD
+#define DIO14_DDR DDRD
+
+#define DIO15_PIN PIND7
+#define DIO15_RPORT PIND
+#define DIO15_WPORT PORTD
+#define DIO15_DDR DDRD
+
+#define DIO16_PIN PINC0
+#define DIO16_RPORT PINC
+#define DIO16_WPORT PORTC
+#define DIO16_DDR DDRC
+
+#define DIO17_PIN PINC1
+#define DIO17_RPORT PINC
+#define DIO17_WPORT PORTC
+#define DIO17_DDR DDRC
+
+#define DIO18_PIN PINC2
+#define DIO18_RPORT PINC
+#define DIO18_WPORT PORTC
+#define DIO18_DDR DDRC
+
+#define DIO19_PIN PINC3
+#define DIO19_RPORT PINC
+#define DIO19_WPORT PORTC
+#define DIO19_DDR DDRC
+
+#define DIO20_PIN PINC4
+#define DIO20_RPORT PINC
+#define DIO20_WPORT PORTC
+#define DIO20_DDR DDRC
+
+#define DIO21_PIN PINC5
+#define DIO21_RPORT PINC
+#define DIO21_WPORT PORTC
+#define DIO21_DDR DDRC
+
+#define DIO22_PIN PINC6
+#define DIO22_RPORT PINC
+#define DIO22_WPORT PORTC
+#define DIO22_DDR DDRC
+
+#define DIO23_PIN PINC7
+#define DIO23_RPORT PINC
+#define DIO23_WPORT PORTC
+#define DIO23_DDR DDRC
+
+#define DIO24_PIN PINA7
+#define DIO24_RPORT PINA
+#define DIO24_WPORT PORTA
+#define DIO24_DDR DDRA
+
+#define DIO25_PIN PINA6
+#define DIO25_RPORT PINA
+#define DIO25_WPORT PORTA
+#define DIO25_DDR DDRA
+
+#define DIO26_PIN PINA5
+#define DIO26_RPORT PINA
+#define DIO26_WPORT PORTA
+#define DIO26_DDR DDRA
+
+#define DIO27_PIN PINA4
+#define DIO27_RPORT PINA
+#define DIO27_WPORT PORTA
+#define DIO27_DDR DDRA
+
+#define DIO28_PIN PINA3
+#define DIO28_RPORT PINA
+#define DIO28_WPORT PORTA
+#define DIO28_DDR DDRA
+
+#define DIO29_PIN PINA2
+#define DIO29_RPORT PINA
+#define DIO29_WPORT PORTA
+#define DIO29_DDR DDRA
+
+#define DIO30_PIN PINA1
+#define DIO30_RPORT PINA
+#define DIO30_WPORT PORTA
+#define DIO30_DDR DDRA
+
+#define DIO31_PIN PINA0
+#define DIO31_RPORT PINA
+#define DIO31_WPORT PORTA
+#define DIO31_DDR DDRA
+
+#define AIO0_PIN PINA0
+#define AIO0_RPORT PINA
+#define AIO0_WPORT PORTA
+#define AIO0_DDR DDRA
+
+#define AIO1_PIN PINA1
+#define AIO1_RPORT PINA
+#define AIO1_WPORT PORTA
+#define AIO1_DDR DDRA
+
+#define AIO2_PIN PINA2
+#define AIO2_RPORT PINA
+#define AIO2_WPORT PORTA
+#define AIO2_DDR DDRA
+
+#define AIO3_PIN PINA3
+#define AIO3_RPORT PINA
+#define AIO3_WPORT PORTA
+#define AIO3_DDR DDRA
+
+#define AIO4_PIN PINA4
+#define AIO4_RPORT PINA
+#define AIO4_WPORT PORTA
+#define AIO4_DDR DDRA
+
+#define AIO5_PIN PINA5
+#define AIO5_RPORT PINA
+#define AIO5_WPORT PORTA
+#define AIO5_DDR DDRA
+
+#define AIO6_PIN PINA6
+#define AIO6_RPORT PINA
+#define AIO6_WPORT PORTA
+#define AIO6_DDR DDRA
+
+#define AIO7_PIN PINA7
+#define AIO7_RPORT PINA
+#define AIO7_WPORT PORTA
+#define AIO7_DDR DDRA
+
+#define PA0_PIN PINA0
+#define PA0_RPORT PINA
+#define PA0_WPORT PORTA
+#define PA0_DDR DDRA
+#define PA1_PIN PINA1
+#define PA1_RPORT PINA
+#define PA1_WPORT PORTA
+#define PA1_DDR DDRA
+#define PA2_PIN PINA2
+#define PA2_RPORT PINA
+#define PA2_WPORT PORTA
+#define PA2_DDR DDRA
+#define PA3_PIN PINA3
+#define PA3_RPORT PINA
+#define PA3_WPORT PORTA
+#define PA3_DDR DDRA
+#define PA4_PIN PINA4
+#define PA4_RPORT PINA
+#define PA4_WPORT PORTA
+#define PA4_DDR DDRA
+#define PA5_PIN PINA5
+#define PA5_RPORT PINA
+#define PA5_WPORT PORTA
+#define PA5_DDR DDRA
+#define PA6_PIN PINA6
+#define PA6_RPORT PINA
+#define PA6_WPORT PORTA
+#define PA6_DDR DDRA
+#define PA7_PIN PINA7
+#define PA7_RPORT PINA
+#define PA7_WPORT PORTA
+#define PA7_DDR DDRA
+
+#define PB0_PIN PINB0
+#define PB0_RPORT PINB
+#define PB0_WPORT PORTB
+#define PB0_DDR DDRB
+#define PB1_PIN PINB1
+#define PB1_RPORT PINB
+#define PB1_WPORT PORTB
+#define PB1_DDR DDRB
+#define PB2_PIN PINB2
+#define PB2_RPORT PINB
+#define PB2_WPORT PORTB
+#define PB2_DDR DDRB
+#define PB3_PIN PINB3
+#define PB3_RPORT PINB
+#define PB3_WPORT PORTB
+#define PB3_DDR DDRB
+#define PB4_PIN PINB4
+#define PB4_RPORT PINB
+#define PB4_WPORT PORTB
+#define PB4_DDR DDRB
+#define PB5_PIN PINB5
+#define PB5_RPORT PINB
+#define PB5_WPORT PORTB
+#define PB5_DDR DDRB
+#define PB6_PIN PINB6
+#define PB6_RPORT PINB
+#define PB6_WPORT PORTB
+#define PB6_DDR DDRB
+#define PB7_PIN PINB7
+#define PB7_RPORT PINB
+#define PB7_WPORT PORTB
+#define PB7_DDR DDRB
+
+#define PC0_PIN PINC0
+#define PC0_RPORT PINC
+#define PC0_WPORT PORTC
+#define PC0_DDR DDRC
+#define PC1_PIN PINC1
+#define PC1_RPORT PINC
+#define PC1_WPORT PORTC
+#define PC1_DDR DDRC
+#define PC2_PIN PINC2
+#define PC2_RPORT PINC
+#define PC2_WPORT PORTC
+#define PC2_DDR DDRC
+#define PC3_PIN PINC3
+#define PC3_RPORT PINC
+#define PC3_WPORT PORTC
+#define PC3_DDR DDRC
+#define PC4_PIN PINC4
+#define PC4_RPORT PINC
+#define PC4_WPORT PORTC
+#define PC4_DDR DDRC
+#define PC5_PIN PINC5
+#define PC5_RPORT PINC
+#define PC5_WPORT PORTC
+#define PC5_DDR DDRC
+#define PC6_PIN PINC6
+#define PC6_RPORT PINC
+#define PC6_WPORT PORTC
+#define PC6_DDR DDRC
+#define PC7_PIN PINC7
+#define PC7_RPORT PINC
+#define PC7_WPORT PORTC
+#define PC7_DDR DDRC
+
+#define PD0_PIN PIND0
+#define PD0_RPORT PIND
+#define PD0_WPORT PORTD
+#define PD0_DDR DDRD
+#define PD1_PIN PIND1
+#define PD1_RPORT PIND
+#define PD1_WPORT PORTD
+#define PD1_DDR DDRD
+#define PD2_PIN PIND2
+#define PD2_RPORT PIND
+#define PD2_WPORT PORTD
+#define PD2_DDR DDRD
+#define PD3_PIN PIND3
+#define PD3_RPORT PIND
+#define PD3_WPORT PORTD
+#define PD3_DDR DDRD
+#define PD4_PIN PIND4
+#define PD4_RPORT PIND
+#define PD4_WPORT PORTD
+#define PD4_DDR DDRD
+#define PD5_PIN PIND5
+#define PD5_RPORT PIND
+#define PD5_WPORT PORTD
+#define PD5_DDR DDRD
+#define PD6_PIN PIND6
+#define PD6_RPORT PIND
+#define PD6_WPORT PORTD
+#define PD6_DDR DDRD
+#define PD7_PIN PIND7
+#define PD7_RPORT PIND
+#define PD7_WPORT PORTD
+#define PD7_DDR DDRD
86 calc.pl
@@ -0,0 +1,86 @@
+#!/usr/bin/perl
+
+my ($ss, $es, $f, $dn, $dt, $a, $n0, $nn, $np, $c0, $cn, $cp, $n, $c, $t, $tp, $v, $vp);
+my ($c0_exact, $cn_exact, $v_exact, $t_exact, $vp_exact, $tp_exact);
+my ($n_pre);
+
+$ss = 300;
+$es = 400;
+$f = 16000000;
+$dn = 50000;
+
+$ssq = $ss * $ss;
+$esq = $es * $es;
+$dsq = ($esq - $ssq);
+
+# $a = $dsq / ($dn << 1);
+
+#$n0 = int(($ss * $ss) / (2 * $a));
+#$nn = int(($es * $es) / (2 * $a));
+# $n0 = int($ssq * $dn / $dsq);
+# $nn = int($esq * $dn / $dsq);
+
+$c0 = int($f / $ss);
+# $c0_exact = $f * sqrt(2 / abs($a));
+
+
+# $dt = ($es - $ss) / $a;
+
+# printf "A:\t%d-%d/%g: %d\n", $es, $ss, $dt, $a;
+# printf "N:\t%d-%d %d:%d\n", $n0, $nn, $nn - $n0, $dn, $a;
+# printf "C:\t%d\t%g\n", $c0, $c0_exact * (sqrt(abs($n0) + 1) - sqrt(abs($n0)));
+# $n = $np = $n0;
+$c = $cp = int($f / $ss);
+$end_c = int($f / $es);
+$t = $tp = $t_exact = 0;
+# $v = $vp = $ss;
+
+$n_pre = int(4 * $ssq * $dn / $dsq) | 1;
+
+# $cn_exact = $c0_exact * (sqrt(abs($n0) + 1) - sqrt(abs($n0)));
+# $v_exact = $vp_exact = $f / $cn_exact;
+
+printf "\tt:i\t\t\tdt\tn\tV\t\ta\n";
+for (0..$dn) {
+ # approximation
+ # $c = int($c * 1000) / 1000;
+ printf "Approx:\t%8.6f:%i\t%10d\t%d\t%12.3f\t%12.3f\n", $t, $_, $c, ($n_pre / 4) - 1, $f / $c, ($t > 0)?($v - $ss) / ($t):0;
+
+# $tp = $t;
+# $cp = $c;
+# $np = $n;
+# $vp = $v;
+
+ $t += $c / $f;
+ if (
+ (($n_pre > 0) && ($c > $end_c)) ||
+ (($n_pre < 0) && ($c < $end_c))
+ ) {
+ $c = int($c - ((2 * $c) / $n_pre));
+ $n_pre += 4;
+ }
+# $v = $f / $c;
+
+ # exact
+# printf "Exact:\t%8.6f:%i\t%10.3f\t%i\t%12.3f\t%12.3f\n\n", $t_exact, $_, $cn_exact, $n, $v_exact, ($t_exact > 0)?($v_exact - $ss) / ($t_exact):0
+# if ($_ % 10 == 0);
+#
+# $vp_exact = $v_exact;
+# $tp_exact = $t_exact;
+#
+# $t_exact += $cn_exact / $f;
+# $cn_exact = $c0_exact * (sqrt(abs($n) + 1) - sqrt(abs($n)));
+# $v_exact = $f / $cn_exact;
+
+ # loop increment
+# if ($nn > $n0) {
+# $n++;
+ $n_pre += 4;
+# }
+# else {
+# $n--;
+# $n_pre -= 4;
+# }
+}
+
+printf "dt:%8.3f\tv:%8.3f\n", int(($f / $es) + 0.5), $f / int(($f / $es) + 0.5);
45 clock.c
@@ -0,0 +1,45 @@
+#include "clock.h"
+
+#include "pinio.h"
+#include "sersendf.h"
+#include "dda_queue.h"
+#include "watchdog.h"
+#include "temp.h"
+#include "timer.h"
+#include "debug.h"
+
+void clock_250ms() {
+ if (steptimeout > (30 * 4)) {
+ power_off();
+ }
+ else
+ steptimeout++;
+
+ ifclock(CLOCK_FLAG_1S) {
+ if (debug_flags & DEBUG_POSITION) {
+ // current position
+ sersendf_P(PSTR("Pos: %ld,%ld,%ld,%ld,%lu\n"), current_position.X, current_position.Y, current_position.Z, current_position.E, current_position.F);
+
+ // target position
+ sersendf_P(PSTR("Dst: %ld,%ld,%ld,%ld,%lu\n"), movebuffer[mb_tail].endpoint.X, movebuffer[mb_tail].endpoint.Y, movebuffer[mb_tail].endpoint.Z, movebuffer[mb_tail].endpoint.E, movebuffer[mb_tail].endpoint.F);
+
+ // Queue
+ print_queue();
+ }
+ // temperature
+ /* if (temp_get_target())
+ temp_print();*/
+ }
+}
+
+void clock_10ms() {
+ // reset watchdog
+ wd_reset();
+
+ temp_tick();
+
+ ifclock(CLOCK_FLAG_250MS) {
+ clock_250ms();
+ }
+}
+
7 clock.h
@@ -0,0 +1,7 @@
+#ifndef _CLOCK_H
+#define _CLOCK_H
+
+void clock_250ms(void);
+void clock_10ms(void);
+
+#endif /* _CLOCK_H */
435 config.h.dist
@@ -0,0 +1,435 @@
+#ifndef _CONFIG_H
+#define _CONFIG_H
+
+/*
+ CONTENTS
+
+ 1. Mechanical/Hardware
+ 2. Acceleration settings
+ 3. Pinouts
+ 4. Temperature sensors
+ 5. Heaters
+ 6. Communication options
+ 7. Miscellaneous
+ 8. Appendix A - PWMable pins and mappings
+*/
+
+/***************************************************************************\
+* *
+* 1. MECHANICAL/HARDWARE *
+* *
+\***************************************************************************/
+
+/*
+ Set your microcontroller type in Makefile! atmega168/atmega328p/atmega644p/atmega1280
+
+ If you want to port this to a new chip, start off with arduino.h and see how you go.
+*/
+
+/*
+ CPU clock rate
+*/
+#ifndef F_CPU
+ #define F_CPU 16000000L
+#endif
+
+/*
+ Are you using the official GEN3 motherboard with separate extruder controller?
+*/
+#define GEN3
+
+/*
+ This is the motherboard, as opposed to the extruder. See extruder/ directory for GEN3 extruder firmware
+*/
+#define HOST
+
+/*
+ Values reflecting the gearing of your machine.
+ All numbers are fixed point integers, so no more than 3 digits to the right of the decimal point, please :-)
+*/
+
+// calculate these values appropriate for your machine
+// for threaded rods, this is (steps motor per turn) / (pitch of the thread)
+// for belts, this is (steps per motor turn) / (number of gear teeth) / (belt module)
+// half-stepping doubles the number, quarter stepping requires * 4, etc.
+#define STEPS_PER_MM_X 320.000
+#define STEPS_PER_MM_Y 320.000
+#define STEPS_PER_MM_Z 320.000
+
+// http://blog.arcol.hu/?p=157 may help with this next one
+#define STEPS_PER_MM_E 320.000
+
+
+/*
+ Values depending on the capabilities of your stepper motors and other mechanics.
+ All numbers are integers, no decimals allowed.
+
+ Units are mm/min
+*/
+
+// used for G0 rapid moves and as a cap for all other feedrates
+#define MAXIMUM_FEEDRATE_X 200
+#define MAXIMUM_FEEDRATE_Y 200
+#define MAXIMUM_FEEDRATE_Z 100
+#define MAXIMUM_FEEDRATE_E 200
+
+// used when searching endstops and as default feedrate
+#define SEARCH_FEEDRATE_X 50
+#define SEARCH_FEEDRATE_Y 50
+#define SEARCH_FEEDRATE_Z 50
+#define SEARCH_FEEDRATE_E 50
+
+// this is how many steps to suck back the filament by when we stop. set to zero to disable
+#define E_STARTSTOP_STEPS 20
+
+
+
+/***************************************************************************\
+* *
+* 2. ACCELERATION *
+* *
+* IMPORTANT: choose only one! These algorithms choose when to step, trying *
+* to use more than one will have undefined and probably *
+* disastrous results! *
+* *
+\***************************************************************************/
+
+
+/*
+ acceleration, reprap style.
+ Each movement starts at the speed of the previous command and accelerates or decelerates linearly to reach target speed at the end of the movement.
+*/
+// #define ACCELERATION_REPRAP
+
+
+/*
+ acceleration and deceleration ramping.
+ Each movement starts at (almost) no speed, linearly accelerates to target speed and decelerates just in time to smoothly stop at the target. alternative to ACCELERATION_REPRAP
+*/
+// #define ACCELERATION_RAMPING
+
+// how fast to accelerate when using ACCELERATION_RAMPING
+// smaller values give quicker acceleration
+// valid range = 1 to 8,000,000; 500,000 is a good starting point
+#define ACCELERATION_STEEPNESS 500000
+
+
+/*
+ temporal step algorithm
+ This algorithm causes the timer to fire when any axis needs to step, instead of synchronising to the axis with the most steps ala bresenham.
+
+ This algorithm is not a type of acceleration, and I haven't worked out how to integrate acceleration with it.
+ However it does control step timing, so acceleration algorithms seemed appropriate
+
+ The Bresenham algorithm is great for drawing lines, but not so good for steppers - In the case where X steps 3 times to Y's two, Y experiences massive jitter as it steps in sync with X every 2 out of 3 X steps. This is a worst-case, but the problem exists for most non-45/90 degree moves. At higher speeds, the jitter /will/ cause position loss and unnecessary vibration.
+ This algorithm instead calculates when a step occurs on any axis, and sets the timer to that value.
+
+ // TODO: figure out how to add acceleration to this algorithm
+*/
+// #define ACCELERATION_TEMPORAL
+
+
+
+/***************************************************************************\
+* *
+* 3. PINOUTS *
+* *
+\***************************************************************************/
+
+/*
+ Machine Pin Definitions
+ - make sure to avoid duplicate usage of a pin
+ - comment out pins not in use, as this drops the corresponding code and makes operations faster
+*/
+
+#include "arduino.h"
+
+#ifndef GEN3
+ /*
+ user defined pins
+ adjust to suit your electronics,
+ or adjust your electronics to suit this
+ */
+
+ #define X_STEP_PIN AIO0
+ #define X_DIR_PIN AIO1
+ #define X_MIN_PIN AIO2
+
+ #define Y_STEP_PIN AIO3
+ #define Y_DIR_PIN AIO4
+ #define Y_MIN_PIN AIO5
+
+ #define Z_STEP_PIN DIO2
+ #define Z_DIR_PIN DIO3
+ #define Z_MIN_PIN DIO4
+
+ #define E_STEP_PIN DIO7
+ #define E_DIR_PIN DIO8
+
+ #define PS_ON_PIN DIO9
+#else
+ /*
+ this is the official gen3 reprap motherboard pinout
+ */
+ #define TX_ENABLE_PIN DIO12
+ #define RX_ENABLE_PIN DIO13
+
+ #define X_STEP_PIN DIO15
+ #define X_DIR_PIN DIO18
+ #define X_MIN_PIN DIO20
+ #define X_MAX_PIN DIO21
+ #define X_ENABLE_PIN DIO19
+
+ #define Y_STEP_PIN DIO23
+ #define Y_DIR_PIN DIO22
+ #define Y_MIN_PIN AIO6
+ #define Y_MAX_PIN AIO5
+ #define Y_ENABLE_PIN DIO7
+
+ #define Z_STEP_PIN AIO4
+ #define Z_DIR_PIN AIO3
+ #define Z_MIN_PIN AIO1
+ #define Z_MAX_PIN AIO0
+ #define Z_ENABLE_PIN AIO2
+
+ #define E_STEP_PIN DIO16
+ #define E_DIR_PIN DIO17
+
+ #define SD_CARD_DETECT DIO2
+ #define SD_WRITE_PROTECT DIO3
+#endif
+
+
+
+/***************************************************************************\
+* *
+* 4. TEMPERATURE SENSORS *
+* *
+\***************************************************************************/
+
+/*
+ TEMP_HYSTERESIS: actual temperature must be target +/- hysteresis before target temperature can be achieved.
+ NOTE: format is 30.2 fixed point, so value of 20 actually means +/- 5 degrees
+
+ TEMP_RESIDENCY_TIME: actual temperature must be close to target for this long before target is achieved
+
+ temperature is "achieved" for purposes of M109 and friends when actual temperature is within [hysteresis] of target for [residency] seconds
+*/
+#define TEMP_HYSTERESIS 20
+#define TEMP_RESIDENCY_TIME 60
+
+// which temperature sensors are you using? (intercom is the gen3-style separate extruder board)
+// #define TEMP_MAX6675
+// #define TEMP_THERMISTOR
+// #define TEMP_AD595
+// #define TEMP_PT100
+#define TEMP_INTERCOM
+
+// if you selected thermistor or AD595, what pin is it on? (this value only used to fill ANALOG_MASK for you)
+#define TEMP_PIN_CHANNEL AIO0_PIN
+
+// ANALOG_MASK is a bitmask of all analog channels used- if you use more than one analog input (more than one temp sensor?), bitwise-or them all together
+#define ANALOG_MASK MASK(TEMP_PIN_CHANNEL)
+
+// how many temperature sensors do you have?
+#define NUM_TEMP_SENSORS 1
+
+/***************************************************************************\
+* *
+* Fill in the following struct according to your hardware *
+* *
+* If your temperature sensor has no associated heater, enter '255' as the *
+* heater index. Unassociated temperature sensors are still read, but they *
+* do not affect firmware operation *
+* *
+* for GEN3 set temp_type to TT_INTERCOM, temp_pin to 0 and heater index to *
+* 255 - the extruder manages the heater for us *
+* *
+* Types are same as TEMP_ list above- TT_MAX6675, TT_THERMISTOR, TT_AD595, *
+* TT_PT100, TT_INTERCOM. See list in temp.c. *
+* *
+\***************************************************************************/
+
+#ifdef TEMP_C
+struct {
+ uint8_t temp_type;
+ uint8_t temp_pin;
+
+ uint8_t heater_index;
+} temp_sensors[NUM_TEMP_SENSORS] =
+{
+ // type pin heater
+ { TT_INTERCOM, 0, 255 }
+};
+#endif
+
+
+
+/***************************************************************************\
+* *
+* 5. HEATERS *
+* *
+\***************************************************************************/
+
+// number of heaters- for GEN3, set to zero as extruder manages the heater by itself
+#define NUM_HEATERS 0
+
+// check if heater responds to changes in target temperature, disable and spit errors if not
+// #define HEATER_SANITY_CHECK
+
+/***************************************************************************\
+* *
+* Fill in the following struct according to your hardware *
+* *
+* If your heater isn't on a PWM-able pin, set heater_pwm to zero and we'll *
+* use bang-bang output. Note that PID will still be used *
+* *
+* If a heater isn't attached to a temperature sensor above, it can still be *
+* controlled by host but otherwise is ignored by firmware *
+* *
+\***************************************************************************/
+
+#ifdef HEATER_C
+struct {
+ volatile uint8_t *heater_port;
+ uint8_t heater_pin;
+ volatile uint8_t *heater_pwm;
+} heaters[NUM_HEATERS]/* =
+{
+ // port pin pwm
+ { &PORTD, PIND6, &OCR0A },
+ { &PORTD, PIND5, &OCR0B }
+}*/;
+#endif
+
+
+
+/***************************************************************************\
+* *
+* 6. COMMUNICATION OPTIONS *
+* *
+\***************************************************************************/
+
+/*
+ RepRap Host changes it's communications protocol from time to time and intentionally avoids backwards compatibility. Set this to the date the source code of your Host was fetched from RepRap's repository, which is likely also the build date.
+ See the discussion on the reprap-dev mailing list from 11 Oct. 2010.
+
+ Undefine it for best human readability, set it to an old date for compatibility with hosts before August 2010
+*/
+// #define REPRAP_HOST_COMPATIBILITY 19750101
+#define REPRAP_HOST_COMPATIBILITY 20100806
+// #define REPRAP_HOST_COMPATIBILITY <date of next RepRap Host compatibility break>
+
+/*
+ Xon/Xoff flow control.
+ Redundant when using RepRap Host for sending GCode, but mandatory when sending GCode files with a plain terminal emulator, like GtkTerm (Linux), CoolTerm (Mac) or HyperTerminal (Windows).
+ Can also be set in Makefile
+*/
+// #define XONXOFF
+
+
+
+/***************************************************************************\
+* *
+* 7. MISCELLANEOUS OPTIONS *
+* *
+\***************************************************************************/
+
+/*
+ DEBUG
+ enables /heaps/ of extra output, and some extra M-codes.
+ WARNING: this WILL break most host-side talkers that expect particular responses from firmware such as reprap host and replicatorG
+ use with serial terminal or other suitable talker only.
+*/
+// #define DEBUG
+
+/*
+ move buffer size, in number of moves
+ note that each move takes a fair chunk of ram (69 bytes as of this writing) so don't make the buffer too big - a bigger serial readbuffer may help more than increasing this unless your gcodes are more than 70 characters long on average.
+ however, a larger movebuffer will probably help with lots of short consecutive moves, as each move takes a bunch of math (hence time) to set up so a longer buffer allows more of the math to be done during preceding longer moves
+*/
+#define MOVEBUFFER_SIZE 8
+
+/*
+ DC extruder
+ If you have a DC motor extruder, configure it as a "heater" above and define this value as the index.
+*/
+// #define DC_EXTRUDER 1
+// #define DC_EXTRUDER_PWM 180
+
+/*
+ FiveD on Arduino implements a watchdog, which has to be reset every 250ms or it will reboot the controller. As rebooting (and letting the GCode sending application trying to continue the build with a then different Home point) is probably even worse than just hanging, and there is no better restore code in place, this is disabled for now.
+*/
+// #define USE_WATCHDOG
+
+/*
+ analog subsystem stuff
+ REFERENCE - which analog reference to use. see analog.h for choices
+*/
+#define REFERENCE REFERENCE_AVCC
+
+/*
+ this option makes the step interrupt interruptible (nested).
+ this should help immensely with dropped serial characters, but may also make debugging infuriating due to the complexities arising from nested interrupts
+*/
+#define STEP_INTERRUPT_INTERRUPTIBLE 1
+
+/*
+ how often we overflow and update our clock; with F_CPU=16MHz, max is < 4.096ms (TICK_TIME = 65535)
+*/
+#define TICK_TIME 2 MS
+#define TICK_TIME_MS (TICK_TIME / (F_CPU / 1000))
+
+/*
+ temperature history count. This is how many temperature readings to keep in order to calculate derivative in PID loop
+ higher values make PID derivative term more stable at the expense of reaction time
+*/
+#define TH_COUNT 8
+
+// this is the scaling of internally stored PID values. 1024L is a good value
+#define PID_SCALE 1024L
+
+
+
+/***************************************************************************\
+* *
+* 8. APPENDIX A - PWMABLE PINS AND MAPPINGS *
+* *
+* *
+* list of PWM-able pins and corresponding timers *
+* timer1 is used for step timing so don't use OC1A/OC1B *
+* they are omitted from this listing for that reason *
+* *
+* For the atmega168/328, timer/pin mappings are as follows *
+* *
+* OCR0A - PD6 *
+* OCR0B - PD5 *
+* OCR2A - PB3 *
+* OCR2B - PD3 *
+* *
+* For the atmega644, timer/pin mappings are as follows *
+* *
+* OCR0A - PB3 *
+* OCR0B - PB4 *
+* OCR2A - PD7 *
+* OCR2B - PD6 *
+* *
+* For the atmega1280, timer/pin mappings are as follows *
+* *
+* OC0A - PB7 *
+* OC0B - PG5 *
+* OC2A - PB4 *
+* OC2B - PH6 *
+* OC3A - PE3 *
+* OC3B - PE4 *
+* OC3C - PE5 *
+* OC4A - PH3 *
+* OC4B - PH4 *
+* OC4C - PH5 *
+* OC5A - PL3 *
+* OC5B - PL4 *
+* OC5C - PL5 *
+* *
+\***************************************************************************/
+
+#endif /* _CONFIG_H */
101 copier.c
@@ -0,0 +1,101 @@
+#ifdef COPIER
+
+#include "copier.h"
+
+#include <avr/pgmspace.h>
+#include <avr/boot.h>
+
+#include "arduino.h"
+#include "delay.h"
+
+uint32_t copier_xchange(uint32_t cmd) {
+ uint32_t r = 0, c = cmd;
+ uint8_t i = 0;
+
+ for (i = 0; i < 32; i++) {
+ WRITE(COPIER_MOSI, c & 0x80000000); delay_us(5);
+ c <<= 1;
+ WRITE(COPIER_SCK, 1); delay_us(5);
+ r = (r << 1) | (READ(COPIER_MISO)?1:0);
+ WRITE(COPIER_SCK, 0);
+ }
+
+ delay_us(5);
+
+ return r;
+}
+
+void init_chip(void);
+void init_chip() {
+ do {
+ WRITE(COPIER_SCK, 0);
+ // power up
+ WRITE(COPIER_RESET, 1);
+ delay_ms(10);
+ WRITE(COPIER_RESET, 0);
+ delay_ms(10);
+ // hopefully enter programming mode
+ } while ((copier_xchange(CMD_PROGRAMMING_ENABLE) & (CMD_PROGRAMMING_ENABLE >> 8)) != (CMD_PROGRAMMING_ENABLE >> 8));
+}
+
+void copy() {
+ // initialise
+ WRITE(COPIER_RESET, 0); SET_OUTPUT(COPIER_RESET);
+ WRITE(COPIER_SCK, 0); SET_OUTPUT(COPIER_SCK);
+ WRITE(COPIER_MOSI, 0); SET_OUTPUT(COPIER_MOSI);
+ SET_INPUT(COPIER_MISO); WRITE(COPIER_MISO, 1);
+
+ delay_ms(50);
+
+ init_chip();
+
+ // verify device signature- should be same as current chip since we haven't the space for the functionality necessary to program anything else
+ if ((copier_xchange(CMD_READ_SIGNATURE | 0x00) & 0xFF) != SIGNATURE_0)
+ return;
+ if ((copier_xchange(CMD_READ_SIGNATURE | 0x01) & 0xFF) != SIGNATURE_1)
+ return;
+ if ((copier_xchange(CMD_READ_SIGNATURE | 0x02) & 0xFF) != SIGNATURE_2)
+ return;
+
+ // erase chip
+ copier_xchange(CMD_CHIP_ERASE);
+ delay_ms(15); //minimum is 9.0ms
+
+ // re-initialise
+ init_chip();
+
+ uint8_t f;
+ // copy fuses
+ // first low byte
+ f = boot_lock_fuse_bits_get(GET_LOW_FUSE_BITS);
+ copier_xchange(CMD_WRITE_FUSE_BITS | f);
+
+ // high byte
+ f = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
+ copier_xchange(CMD_WRITE_FUSE_HIGH_BITS | f);
+
+ // extended byte
+ f = boot_lock_fuse_bits_get(GET_EXTENDED_FUSE_BITS);
+ copier_xchange(CMD_WRITE_FUSE_EXTENDED_BITS | f);
+
+ // start copying flash
+ uint16_t i;
+ for (i = 0; i < (FLASHEND / 2); i += (SPM_PAGESIZE / 2)) {
+ uint8_t j;
+ for (j = 0; j < (SPM_PAGESIZE / 2); j++) {
+ uint16_t w = pgm_read_word_near((i | j) << 1);
+ copier_xchange(CMD_LOAD_PROGMEM_LOW_BYTE | (j << 8) | (w & 0xFF));
+ copier_xchange(CMD_LOAD_PROGMEM_HIGH_BYTE | (j << 8) | (w >> 8));
+ }
+ copier_xchange(CMD_WRITE_PROGMEM_PAGE | ((i / (SPM_PAGESIZE / 2)) << 8));
+ delay_ms(10); //minimum is 4.5ms
+ }
+
+ // reset
+ delay_ms(10);
+ SET_INPUT(MOSI);
+ SET_INPUT(SCK);
+ SET_INPUT(COPIER_RESET);
+}
+
+#endif /* COPIER */
45 copier.h
@@ -0,0 +1,45 @@
+#ifndef _COPIER_H
+#define _COPIER_H
+
+// operation instructions
+#define CMD_PROGRAMMING_ENABLE 0xAC530000
+#define CMD_CHIP_ERASE 0xAC800000
+#define CMD_POLL 0xF0000000
+
+// load instructions
+#define CMD_LOAD_EXTENDED_ADDRESS 0x4D000000
+#define CMD_LOAD_PROGMEM_HIGH_BYTE 0x48000000
+#define CMD_LOAD_PROGMEM_LOW_BYTE 0x40000000
+#define CMD_LOAD_EEPROM_PAGE 0xC1000000
+
+// read instructions
+#define CMD_READ_PROGMEM_HIGH_BYTE 0x28000000
+#define CMD_READ_PROGMEM_LOW_BYTE 0x20000000
+#define CMD_READ_EEPROM 0xA0000000
+#define CMD_READ_LOCK_BITS 0x58000000
+#define CMD_READ_SIGNATURE 0x30000000
+#define CMD_READ_FUSE_BITS 0x50000000
+#define CMD_READ_FUSE_HIGH_BITS 0x58080000
+#define CMD_READ_FUSE_EXTENDED_BITS 0x50080000
+#define CMD_READ_CALIBRATION_BYTE 0x38000000
+
+// write instructions
+#define CMD_WRITE_PROGMEM_PAGE 0x4C000000
+#define CMD_WRITE_EEPROM 0xC0000000
+#define CMD_WRITE_EEPROM_PAGE 0xC2000000
+#define CMD_WRITE_LOCK_BITS 0xACE00000
+#define CMD_WRITE_FUSE_BITS 0xACA00000
+#define CMD_WRITE_FUSE_HIGH_BITS 0xACA80000
+#define CMD_WRITE_FUSE_EXTENDED_BITS 0xACA40000
+
+//pinout
+#define COPIER_RESET AIO1
+#define COPIER_SCK AIO2
+#define COPIER_MOSI AIO3
+#define COPIER_MISO AIO4
+
+//functions
+
+void copy(void);
+
+#endif /* _COPIER_H */
591 dda.c
@@ -0,0 +1,591 @@
+#include "dda.h"
+
+#include <string.h>
+#include <stdlib.h>
+#include <avr/interrupt.h>
+
+#include "timer.h"
+#include "serial.h"
+#include "sermsg.h"
+#include "dda_queue.h"
+#include "debug.h"
+#include "sersendf.h"
+#include "pinio.h"
+#include "config.h"
+
+#ifdef DC_EXTRUDER
+ #include "heater.h"
+#endif
+
+/*
+ Used in distance calculation during DDA setup
+*/
+#define UM_PER_STEP_X 1000L / ((uint32_t) STEPS_PER_MM_X)
+#define UM_PER_STEP_Y 1000L / ((uint32_t) STEPS_PER_MM_Y)
+#define UM_PER_STEP_Z 1000L / ((uint32_t) STEPS_PER_MM_Z)
+#define UM_PER_STEP_E 1000L / ((uint32_t) STEPS_PER_MM_E)
+
+/*
+ step timeout
+*/
+
+uint8_t steptimeout = 0;
+
+/*
+ position tracking
+*/
+
+TARGET startpoint __attribute__ ((__section__ (".bss")));
+TARGET current_position __attribute__ ((__section__ (".bss")));
+
+/*
+ utility functions
+*/
+
+// courtesy of http://www.flipcode.com/archives/Fast_Approximate_Distance_Functions.shtml
+uint32_t approx_distance( uint32_t dx, uint32_t dy )
+{
+ uint32_t min, max, approx;
+
+ if ( dx < dy )
+ {
+ min = dx;
+ max = dy;
+ } else {
+ min = dy;
+ max = dx;
+ }
+
+ approx = ( max * 1007 ) + ( min * 441 );
+ if ( max < ( min << 4 ))
+ approx -= ( max * 40 );
+
+ // add 512 for proper rounding
+ return (( approx + 512 ) >> 10 );
+}
+
+// courtesy of http://www.oroboro.com/rafael/docserv.php/index/programming/article/distance
+uint32_t approx_distance_3( uint32_t dx, uint32_t dy, uint32_t dz )
+{
+ uint32_t min, med, max, approx;
+
+ if ( dx < dy )
+ {
+ min = dy;
+ med = dx;
+ } else {
+ min = dx;
+ med = dy;
+ }
+
+ if ( dz < min )
+ {
+ max = med;
+ med = min;
+ min = dz;
+ } else if ( dz < med ) {
+ max = med;
+ med = dz;
+ } else {
+ max = dz;
+ }
+
+ approx = ( max * 860 ) + ( med * 851 ) + ( min * 520 );
+ if ( max < ( med << 1 )) approx -= ( max * 294 );
+ if ( max < ( min << 2 )) approx -= ( max * 113 );
+ if ( med < ( min << 2 )) approx -= ( med * 40 );
+
+ // add 512 for proper rounding
+ return (( approx + 512 ) >> 10 );
+}
+
+// this is an ultra-crude pseudo-logarithm routine, such that:
+// 2 ^ msbloc(v) >= v
+const uint8_t msbloc (uint32_t v) {
+ uint8_t i;
+ uint32_t c;
+ for (i = 31, c = 0x80000000; i; i--) {
+ if (v & c)
+ return i;
+ c >>= 1;
+ }
+ return 0;
+}
+
+/*
+ CREATE a dda given current_position and a target, save to passed location so we can write directly into the queue
+*/
+
+void dda_create(DDA *dda, TARGET *target) {
+ uint32_t distance, c_limit, c_limit_calc;
+
+ // initialise DDA to a known state
+ dda->allflags = 0;
+
+ if (debug_flags & DEBUG_DDA)
+ serial_writestr_P(PSTR("\n{DDA_CREATE: ["));
+
+ // we end at the passed target
+ memcpy(&(dda->endpoint), target, sizeof(TARGET));
+
+ dda->x_delta = labs(target->X - startpoint.X);
+ dda->y_delta = labs(target->Y - startpoint.Y);
+ dda->z_delta = labs(target->Z - startpoint.Z);
+ dda->e_delta = labs(target->E - startpoint.E);
+
+ dda->x_direction = (target->X >= startpoint.X)?1:0;
+ dda->y_direction = (target->Y >= startpoint.Y)?1:0;
+ dda->z_direction = (target->Z >= startpoint.Z)?1:0;
+ dda->e_direction = (target->E >= startpoint.E)?1:0;
+
+ if (debug_flags & DEBUG_DDA)
+ sersendf_P(PSTR("%ld,%ld,%ld,%ld] ["), target->X - startpoint.X, target->Y - startpoint.Y, target->Z - startpoint.Z, target->E - startpoint.E);
+
+ dda->total_steps = dda->x_delta;
+ if (dda->y_delta > dda->total_steps)
+ dda->total_steps = dda->y_delta;
+ if (dda->z_delta > dda->total_steps)
+ dda->total_steps = dda->z_delta;
+ if (dda->e_delta > dda->total_steps)
+ dda->total_steps = dda->e_delta;
+
+ if (debug_flags & DEBUG_DDA)
+ sersendf_P(PSTR("ts:%lu"), dda->total_steps);
+
+ if (dda->total_steps == 0) {
+ dda->nullmove = 1;
+ }
+ else {
+ // get steppers ready to go
+ steptimeout = 0;
+ power_on();
+ x_enable();
+ y_enable();
+ if (dda->z_delta)
+ z_enable();
+
+ // since it's unusual to combine X, Y and Z changes in a single move on reprap, check if we can use simpler approximations before trying the full 3d approximation.
+ if (dda->z_delta == 0)
+ distance = approx_distance(dda->x_delta * UM_PER_STEP_X, dda->y_delta * UM_PER_STEP_Y);
+ else if (dda->x_delta == 0 && dda->y_delta == 0)
+ distance = dda->z_delta * UM_PER_STEP_Z;
+ else
+ distance = approx_distance_3(dda->x_delta * UM_PER_STEP_X, dda->y_delta * UM_PER_STEP_Y, dda->z_delta * UM_PER_STEP_Z);
+
+ if (distance < 2)
+ distance = dda->e_delta * UM_PER_STEP_E;
+
+ if (debug_flags & DEBUG_DDA)
+ sersendf_P(PSTR(",ds:%lu"), distance);
+
+ #ifdef ACCELERATION_TEMPORAL
+ // bracket part of this equation in an attempt to avoid overflow: 60 * 16MHz * 5mm is >32 bits
+ uint32_t move_duration = distance * (60 * F_CPU / startpoint.F);
+ #else
+ dda->x_counter = dda->y_counter = dda->z_counter = dda->e_counter =
+ -(dda->total_steps >> 1);
+
+ // pre-calculate move speed in millimeter microseconds per step minute for less math in interrupt context
+ // mm (distance) * 60000000 us/min / step (total_steps) = mm.us per step.min
+ // note: um (distance) * 60000 == mm * 60000000
+ // so in the interrupt we must simply calculate
+ // mm.us per step.min / mm per min (F) = us per step
+
+ // break this calculation up a bit and lose some precision because 300,000um * 60000 is too big for a uint32
+ // calculate this with a uint64 if you need the precision, but it'll take longer so routines with lots of short moves may suffer
+ // 2^32/6000 is about 715mm which should be plenty
+
+ // changed * 10 to * (F_CPU / 100000) so we can work in cpu_ticks rather than microseconds.
+ // timer.c setTimer() routine altered for same reason
+
+ // changed distance * 6000 .. * F_CPU / 100000 to
+ // distance * 2400 .. * F_CPU / 40000 so we can move a distance of up to 1800mm without overflowing
+ uint32_t move_duration = ((distance * 2400) / dda->total_steps) * (F_CPU / 40000);
+ #endif
+
+ // similarly, find out how fast we can run our axes.
+ // do this for each axis individually, as the combined speed of two or more axes can be higher than the capabilities of a single one.
+ c_limit = 0;
+ // check X axis
+ c_limit_calc = ( (dda->x_delta * (UM_PER_STEP_X * 2400L)) / dda->total_steps * (F_CPU / 40000) / MAXIMUM_FEEDRATE_X) << 8;
+ if (c_limit_calc > c_limit)
+ c_limit = c_limit_calc;
+ // check Y axis
+ c_limit_calc = ( (dda->y_delta * (UM_PER_STEP_Y * 2400L)) / dda->total_steps * (F_CPU / 40000) / MAXIMUM_FEEDRATE_Y) << 8;
+ if (c_limit_calc > c_limit)
+ c_limit = c_limit_calc;
+ // check Z axis
+ c_limit_calc = ( (dda->z_delta * (UM_PER_STEP_Z * 2400L)) / dda->total_steps * (F_CPU / 40000) / MAXIMUM_FEEDRATE_Z) << 8;
+ if (c_limit_calc > c_limit)
+ c_limit = c_limit_calc;
+ // check E axis
+ c_limit_calc = ( (dda->e_delta * (UM_PER_STEP_E * 2400L)) / dda->total_steps * (F_CPU / 40000) / MAXIMUM_FEEDRATE_E) << 8;
+ if (c_limit_calc > c_limit)
+ c_limit = c_limit_calc;
+
+ #ifdef ACCELERATION_REPRAP
+ // c is initial step time in IOclk ticks
+ dda->c = (move_duration / startpoint.F) << 8;
+ if (dda->c < c_limit)
+ dda->c = c_limit;
+ dda->end_c = (move_duration / target->F) << 8;
+ if (dda->end_c < c_limit)
+ dda->end_c = c_limit;
+
+ if (debug_flags & DEBUG_DDA)
+ sersendf_P(PSTR(",md:%lu,c:%lu"), move_duration, dda->c >> 8);
+
+ if (dda->c != dda->end_c) {
+ uint32_t stF = startpoint.F / 4;
+ uint32_t enF = target->F / 4;
+ // now some constant acceleration stuff, courtesy of http://www.embedded.com/columns/technicalinsights/56800129?printable=true
+ uint32_t ssq = (stF * stF);
+ uint32_t esq = (enF * enF);
+ int32_t dsq = (int32_t) (esq - ssq) / 4;
+
+ uint8_t msb_ssq = msbloc(ssq);
+ uint8_t msb_tot = msbloc(dda->total_steps);
+
+ // the raw equation WILL overflow at high step rates, but 64 bit math routines take waay too much space
+ // at 65536 mm/min (1092mm/s), ssq/esq overflows, and dsq is also close to overflowing if esq/ssq is small
+ // but if ssq-esq is small, ssq/dsq is only a few bits
+ // we'll have to do it a few different ways depending on the msb locations of each
+ if ((msb_tot + msb_ssq) <= 30) {
+ // we have room to do all the multiplies first
+ if (debug_flags & DEBUG_DDA)
+ serial_writechar('A');
+ dda->n = ((int32_t) (dda->total_steps * ssq) / dsq) + 1;
+ }
+ else if (msb_tot >= msb_ssq) {
+ // total steps has more precision
+ if (debug_flags & DEBUG_DDA)
+ serial_writechar('B');
+ dda->n = (((int32_t) dda->total_steps / dsq) * (int32_t) ssq) + 1;
+ }
+ else {
+ // otherwise
+ if (debug_flags & DEBUG_DDA)
+ serial_writechar('C');
+ dda->n = (((int32_t) ssq / dsq) * (int32_t) dda->total_steps) + 1;
+ }
+
+ if (debug_flags & DEBUG_DDA)
+ sersendf_P(PSTR("\n{DDA:CA end_c:%lu, n:%ld, md:%lu, ssq:%lu, esq:%lu, dsq:%lu, msbssq:%u, msbtot:%u}\n"), dda->end_c >> 8, dda->n, move_duration, ssq, esq, dsq, msb_ssq, msb_tot);
+
+ dda->accel = 1;
+ }
+ else
+ dda->accel = 0;
+ #elif defined ACCELERATION_RAMPING
+ // add the last bit of dda->total_steps to always round up
+ dda->ramp_steps = dda->total_steps / 2 + (dda->total_steps & 1);
+ dda->step_no = 0;
+ // c is initial step time in IOclk ticks
+ dda->c = ACCELERATION_STEEPNESS << 8;
+ dda->c_min = (move_duration / target->F) << 8;
+ if (dda->c_min < c_limit)
+ dda->c_min = c_limit;
+ dda->n = 1;
+ dda->ramp_state = RAMP_UP;
+ #elif defined ACCELERATION_TEMPORAL
+ dda->x_counter = dda->x_step_interval = move_duration / dda->x_delta;
+ dda->y_counter = dda->y_step_interval = move_duration / dda->y_delta;
+ dda->z_counter = dda->z_step_interval = move_duration / dda->z_delta;
+ dda->e_counter = dda->e_step_interval = move_duration / dda->e_delta;
+
+ dda->c = dda->x_step_interval;
+ if (dda->y_step_interval < dda->c)
+ dda->c = dda->y_step_interval;
+ if (dda->z_step_interval < dda->c)
+ dda->c = dda->z_step_interval;
+ if (dda->e_step_interval < dda->c)
+ dda->c = dda->e_step_interval;
+
+ dda->c <<= 8;
+ #else
+ dda->c = (move_duration / target->F) << 8;
+ if (dda->c < c_limit)
+ dda->c = c_limit;
+ #endif
+ }
+
+ if (debug_flags & DEBUG_DDA)
+ serial_writestr_P(PSTR("] }\n"));
+
+ // next dda starts where we finish
+ memcpy(&startpoint, target, sizeof(TARGET));
+ // E is always relative, reset it here
+ startpoint.E = 0;
+}
+
+/*
+ Start a prepared DDA
+*/
+
+void dda_start(DDA *dda) {
+ // called from interrupt context: keep it simple!
+ if (dda->nullmove) {
+ // just change speed?
+ current_position.F = dda->endpoint.F;
+ // keep dda->live = 0
+ }
+ else {
+/* if (dda->waitfor_temp) {
+ #ifndef REPRAP_HOST_COMPATIBILITY
+ serial_writestr_P(PSTR("Waiting for target temp\n"));
+ #endif
+ }
+ else {*/
+ // ensure steppers are ready to go
+ steptimeout = 0;
+ power_on();
+ x_enable();
+ y_enable();
+ if (dda->z_delta)
+ z_enable();
+
+ // set direction outputs
+ x_direction(dda->x_direction);
+ y_direction(dda->y_direction);
+ z_direction(dda->z_direction);
+ e_direction(dda->e_direction);
+
+ #ifdef DC_EXTRUDER
+ if (dda->e_delta)
+ heater_set(DC_EXTRUDER, DC_EXTRUDER_PWM);
+ #endif
+
+// }
+
+ // ensure this dda starts
+ dda->live = 1;
+
+ // set timeout for first step
+ setTimer(dda->c >> 8);
+ }
+}
+
+/*
+ STEP
+*/
+
+void dda_step(DDA *dda) {
+ // called from interrupt context! keep it as simple as possible
+ uint8_t did_step = 0;
+
+ #ifdef ACCELERATION_TEMPORAL
+ if (dda->x_counter <= 0) {
+ if ((current_position.X != dda->endpoint.X) /* &&
+ (x_max() != dda->x_direction) && (x_min() == dda->x_direction) */) {
+ x_step();
+ if (dda->x_direction)
+ current_position.X++;
+ else
+ current_position.X--;
+ }
+ dda->x_counter += dda->x_step_interval;
+ dda->x_delta--;
+ }
+ if (dda->y_counter <= 0) {
+ if ((current_position.Y != dda->endpoint.Y) /* &&
+ (y_max() != dda->y_direction) && (y_min() == dda->y_direction) */) {
+ y_step();
+ if (dda->y_direction)
+ current_position.Y++;
+ else
+ current_position.Y--;
+ }
+ dda->y_counter += dda->y_step_interval;
+ dda->y_delta--;
+ }
+ if (dda->z_counter <= 0) {
+ if ((current_position.Z != dda->endpoint.Z) /* &&
+ (z_max() != dda->z_direction) && (z_min() == dda->z_direction) */) {
+ z_step();
+ if (dda->z_direction)
+ current_position.Z++;
+ else
+ current_position.Z--;
+ }
+ dda->z_counter += dda->z_step_interval;
+ dda->z_delta--;
+ }
+ if (dda->e_counter <= 0) {
+ if ((current_position.E != dda->endpoint.E) /* &&
+ (e_max() != dda->e_direction) && (e_min() == dda->e_direction) */) {
+ e_step();
+ if (dda->e_direction)
+ current_position.E++;
+ else
+ current_position.E--;
+ }
+ dda->e_counter += dda->e_step_interval;
+ dda->e_delta--;
+ }
+ #else
+ if ((current_position.X != dda->endpoint.X) /* &&
+ (x_max() != dda->x_direction) && (x_min() == dda->x_direction) */) {
+ dda->x_counter -= dda->x_delta;
+ if (dda->x_counter < 0) {
+ x_step();
+ did_step = 1;
+ if (dda->x_direction)
+ current_position.X++;
+ else
+ current_position.X--;
+
+ dda->x_counter += dda->total_steps;
+ }
+ }
+
+ if ((current_position.Y != dda->endpoint.Y) /* &&
+ (y_max() != dda->y_direction) && (y_min() == dda->y_direction) */) {
+ dda->y_counter -= dda->y_delta;
+ if (dda->y_counter < 0) {
+ y_step();
+ did_step = 1;
+ if (dda->y_direction)
+ current_position.Y++;
+ else
+ current_position.Y--;
+
+ dda->y_counter += dda->total_steps;
+ }
+ }
+
+