Commit Graph

48 Commits

Author SHA1 Message Date
Markus Hitter eb166ce3ef timer.h: remove some obsolete stuff, replace char with uint8_t.
No functional change.
2015-08-03 12:11:19 +02:00
Phil Hord 22b640697b Replace SIMULATOR with __AVR__ in several places.
Previously some features were excluded based on whether SIMULATOR
was defined. But in fact these should have been included when __AVR__
was defined. These used to be the same thing, but now with ARM coming
into the picture, they are not. Fix the situation so AVR includes are
truly only used when __AVR__ is defined.

The _crc16_update function appears to be specific to AVR; I've kept the
alternate implementation limited to AVR in that case in crc.c. I think
this is the right thing to do, but I am not sure. Maybe ARM has some
equivalent function in their libraries.
2015-07-29 21:05:38 +02:00
Phil Hord 503d2c75a1 Fix typos: "whether" and whitespace. 2015-07-17 16:16:42 +02:00
Markus Hitter 9dd7426f9d DDA: on short schedules, repeat step routine immediately ...
... instead of trying to fire an interrupt as quickly as possible.
This affects ACCELERATION_TEMPORAL only. It almost doubles the
achievable step rate. Measured maximum step rate (X axis only,
100 mm moves) is 40'000 steps/s on a 16 MHz electronics, so
approx. 50'000 steps/s on a 20 MHz controller, which is even
a bit faster than the ACCELERATION_RAMPING algorithm.

Tests with temporary test code were run and judging by these
tests, clock interrupts are now very reliable up to the point
where processing speed is simply exhaused.

Performance with ACCELERATION_RAMPING: this costs 10 bytes
binary size and exactly 2 clock cycles per step interrupt or
0.6% performance even. We could avoid this with a lot
of #ifdefs, but considering ACCELERATION_TEMPORAL will one
day be the default acceleration, skip these #ifdefs, also
for better code readability.

$ cd testcases
$ ./run-in-simulavr.sh short-moves.gcode smooth-curves.gcode triangle-odd.gcode
    SIZES             ATmega...  '168    '328(P)    '644(P)    '1280
    FLASH  : 20528 bytes         144%        67%        33%      16%
    RAM    :  2188 bytes         214%       107%        54%      27%
    EEPROM :    32 bytes           4%         2%         2%       1%

short-moves.gcode statistics:
LED on occurences: 838.
LED on time minimum: 304 clock cycles.
LED on time maximum: 715 clock cycles.
LED on time average: 310.717 clock cycles.

smooth-curves.gcode statistics:
LED on occurences: 8585.
LED on time minimum: 309 clock cycles.
LED on time maximum: 712 clock cycles.
LED on time average: 360.051 clock cycles.

triangle-odd.gcode statistics:
LED on occurences: 1636.
LED on time minimum: 304 clock cycles.
LED on time maximum: 710 clock cycles.
LED on time average: 332.32 clock cycles.
2015-04-21 02:51:32 +02:00
Markus Hitter 8b88334b06 Rename setTimer() to timer_set() for more consistency.
Pure cosmetical change.

Performance check:

$ cd testcases
$ ./run-in-simulavr.sh short-moves.gcode smooth-curves.gcode triangle-odd.gcode
[...]
    SIZES             ATmega...  '168    '328(P)    '644(P)    '1280
    FLASH  : 20518 bytes         144%        67%        33%      16%
    RAM    :  2188 bytes         214%       107%        54%      27%
    EEPROM :    32 bytes           4%         2%         2%       1%

short-moves.gcode statistics:
LED on occurences: 838.
LED on time minimum: 302 clock cycles.
LED on time maximum: 713 clock cycles.
LED on time average: 308.72 clock cycles.

smooth-curves.gcode statistics:
LED on occurences: 8585.
LED on time minimum: 307 clock cycles.
LED on time maximum: 710 clock cycles.
LED on time average: 358.051 clock cycles.

triangle-odd.gcode statistics:
LED on occurences: 1636.
LED on time minimum: 302 clock cycles.
LED on time maximum: 708 clock cycles.
LED on time average: 330.322 clock cycles.
2015-04-21 02:51:32 +02:00
Markus Hitter f26623d173 Move clock stuff from timer.c/.h to clock.c/.h.
I guess that's where it belongs.
2014-10-18 20:57:32 +02:00
Phil Hord 21e5343552 Add config.h wrapper to simplify test automation
Test code which wants to customize config.h can do so without
touching config.h itself by wrapping config.h in a macro variable
which is passed in to the compiler.  It defaults to "config.h" if
no override is provided.

This change would break makefile dependency checking since the selection
of a different header file on the command line is not noticed by make
as a build-trigger.  To solve this, we add a layer to the BUILDDIR path
so build products are now specific to the USER_CONFIG choice if it is
not "config.h".
2014-03-04 19:56:23 +01:00
Phil Hord 96495fb9bb simulator: Make time-scale=0 a cmdline option
Teach the simulator to run with no delays when time-scale=0.

Let the user specify the time-scale on the command line.
2013-12-06 19:24:58 +01:00
Markus Hitter c8b0da53c1 timer.c: we never managed to recover from an emergency stop ...
... other than by pressing the reset button. Accordingly, save
these 2 clock cycles at step interrupt time.
2013-12-06 19:24:58 +01:00
Phil Hord 452e2e5cd9 Restore simulation build target.
This code was accidentally removed long ago in a botched merge. This
patch recovers it and makes it build again. I've done minimal testing
and some necessary cleanup. It compiles and runs, but it probably still
has a few dust bunnies here and there.

I added registers and pin definitions to simulator.h and
simulator/simulator.c which I needed to match my Gen7-based config.
Other configs or non-AVR ports will need to define more or different
registers. Some registers are 16-bits, some are 8-bit, and some are just
constant values (enums). A more clever solution would read in the
chip-specific header and produce saner definitions which covered all
GPIOs. But this commit just takes the quick and easy path to support my
own hardware.

Most of this code originated in these commits:

	commit cbf41dd4ad
	Author: Stephan Walter <stephan@walter.name>
	Date:   Mon Oct 18 20:28:08 2010 +0200

	    document simulation

	commit 3028b297f3
	Author: Stephan Walter <stephan@walter.name>
	Date:   Mon Oct 18 20:15:59 2010 +0200

	    Add simulation code: use "make sim"

Additional tweaks:

Revert va_args processing for AVR, but keep 'int' generalization
for simulation. gcc wasn't lying. The sim really aborts without this.

Remove delay(us) from simulator (obsolete).

Improve the README.sim to demonstrate working pronterface connection
to sim. Also fix the build instructions.

Appease all stock configs.

Stub out intercom and shush usb_serial when building simulator.

Pretend to be all chip-types for config appeasement.

Replace sim_timer with AVR-simulator timer:

The original sim_timer and sim_clock provided direct replacements
for timer/clock.c in the main code. But when the main code changed,
simcode did not. The main clock.c was dropped and merged into timer.c.
Also, the timer.c now has movement calculation code in it in some
cases (ACCELERATION_TEMPORAL) and it would be wrong to teach the
simulator to do the same thing. Instead, teach the simulator to
emulate the AVR Timer1 functionality, reacting to values written to
OCR1A and OCR1B timer comparison registers.

Whenever OCR1A/B are changed, the sim_setTimer function needs to be
called. It is called automatically after a timer event, so changes
within the timer ISRs do not need to bother with this.

A C++ class could make this requirement go away by noticing the
assignment. On the other hand, a chip-agnostic timer.c would help
make the main code more portable. The latter cleanup is probably
better for us in the long run.
2013-12-06 19:24:58 +01:00
Markus Hitter ed6a66c25d timer.c: the concept of a "new move" was abandoned long time ago.
This doesn't matter much, as the timer overflows 300 times/second
worst case, so the very first step of a series of moves is
delayed never more than 30 milliseconds. Hardly recognizeable
by a human.

Saves a nice 40 bytes and improves max step rate by several percent.
2013-10-27 20:01:51 +01:00
Markus Hitter 0c100fe5f8 According to avr-libc documentation, ISR() handles SREG its self. 2013-10-27 20:01:51 +01:00
Markus Hitter da5c29a7dd timer.c/.h: make TICK_TIME macros available globally. 2013-10-27 20:01:51 +01:00
Markus Hitter ccec75d9f8 dda.c: allow protected re-entry for time based stepper calculations.
This becomes more important when acceleration is calculated
there, too.
2013-10-27 20:01:51 +01:00
Markus Hitter 13ec2d7521 Move endstop handling into a time-based procedure.
Before, endstops were checked on every step, wasting precious time.
Checking them 500 times a second should be more than sufficient.

Additionally, an endstop stop now properly decelerates the movement.
This is one important step towards handling accidental endstop hits
gracefully, as it avoids step losses in such situations.
2013-10-27 20:01:10 +01:00
Markus Hitter 3c5d9d8950 Replace #define HOST by #define MOTHERBOARD.
Apparently, HOST conflicts with some AVR headers for the
USB-equipped ATmegas.

Patch by DaveX, thank you Dave. See also:
http://forums.reprap.org/read.php?147,33082,182021#msg-182021
2013-03-24 16:17:14 +01:00
Markus Hitter 7528037d4e Save status register on each interrupt.
Costs 28 bytes binary size and two CPU cycles per interrupt,
at the advantage of getting rid of possible well hidden bugs.
2012-03-04 19:02:38 +01:00
Markus Hitter c126629ec8 Make the step timer fit for very short delays.
This isn't the magic patch which suddenly allows us to run at
arbitrary high step rates, but can deal with short bursts of
very short delays only. Typical for a 45 deg move when using
ACCELERATION_TEMPORAL.

The implementation simply extends the impossible small delay.
To keep overall timing promises regardless, the extra is stored
and compensated for when a longer delay request comes in.

Wrapped all this in #ifdef ACCELERATION_TEMPORAL, as this adds
about 300 bytes of binary size, so it likely slows down the
setTimer() code a bit and non-temporal algorithms are expected to
never request an unreasonable short delay.
2012-03-04 19:02:28 +01:00
Markus Hitter fd91ee7e8b dda.c: re-introduce ACCELERATION_TEMPORAL.
The implementation is slightly different this time, as it's not
using these famous bresenham algorithms. The intention is to
allow axis-independent movements, as it's required for
EMC-quality look-ahead.
2012-03-04 19:02:17 +01:00
Markus Hitter 69610a6dae timer.c: simplify clock counting a bit.
Nothing fancy, but it saves 8 bytes, so likely 8 cycles.
2011-11-21 09:56:12 +01:00
Markus Hitter 9c489911b6 timer.c: get rid of explicitely disabling the step timer.
This is no longer neccessary, as the step timer is not designed to
fire always exactly once.

Saves 54 bytes binary size.
2011-11-21 09:54:41 +01:00
Markus Hitter 53490bb318 Get rid of defered enabling of the step interrupt again.
This makes the code cleaner and the reduction of code
probably easily compensates for keeping global interrupts
enabled for a bit longer. Talked to macscifi about this.

Saves about 300 bytes of binary size.
2011-11-21 09:54:38 +01:00
Jens Ch. Restemeier cea2a656a5 Fix assumption that debug led is on SCK pin
Signed-off-by: Michael Moon <triffid.hunter@gmail.com>
2011-06-10 22:58:25 +10:00
Jim McGee f555887eb5 Testing and setting the delay to a minimum value needs to occur only
if delay is not zero, otherwise the timer is not turned off when it
should be. Move the test and setting back inside the block that is only
executed if delay > 0.
2011-05-15 16:48:25 +10:00
Jim McGee 5dc0c80f0b Defer enabling of timer1_compa interrupt the end of the interrupt handler.
Specifically, disable interrupts just before returning and then enable
the timer interrupt if appropriate. This means that the timer interrupt
cannot actually fire until after the RETI, so the function cannot be
entered recursively.
2011-05-15 09:56:33 +10:00
Jim McGee 57b30b0ff1 Set a minimum delay instead of attempting to call the timer interrupt
service routine in the case that the requested timer interval is too
small.

Calling the interrupt service routine at this point is likely to
recursively clobber the stack.

Setting a lower bounds on the interrupt delay will limit the upper
speed of pulse generation, but it should not change the relative
pulse rates, and will not recurvisely clobber the stack.

Note that the lower limit of 17 has not been researched, it is
simply the value below which the old code attempted to call
the interrupt service routine directly.
2011-05-15 09:56:33 +10:00
Jim McGee 8241549276 Remove volatile from next_step_time as it can be easily managed using
memory barriers since it is only touched inside a single interrupt
handler or while interrupts are disabled in setTimer().

This saves about 90 bytes since it no longer needs to be reloaded multiple
times.
2011-05-15 09:56:33 +10:00
Jim McGee f22e691fee Convert the clock_flag variable into 3 separate varables.
This costs 2 bytes of ram, but saves 60 bytes of flash. Doing so
also eliminates the need to disable interrupts while clearing flags
in the ifclock macro.

Conflicts:

	clock.c
	timer.c
	timer.h
2011-05-15 09:56:32 +10:00
Michael Moon be81a13584 make sure timer is initialised properly
Reported-by: jv4779 <jv4779@gmail.com>
2011-04-30 11:00:11 +10:00
Michael Moon 0dc7d77885 Massive Doxygen documentation addition
'make doc' then point your browser at doc/html/

Needs plenty of cleanup and polishing, but the main bulk is here

even documents your configuration! ;)
2011-03-22 01:34:36 +11:00
Markus Hitter 851a91306a timer.c: don't reset next_step_time when doing a step.
Doing so adds most of the interrupt execution time to the
step time, see line 126 of the same file:

    if (next_step_time == 0) {
      // new move, take current time as start value
      step_start = TCNT1;
    }
2011-03-14 17:34:26 +01:00
Michael Moon d0601716e8 reorganise intercom to send packets from extruder main loop, also generalise protocol as discussed in forum 2011-03-05 13:38:12 +11:00
Markus Hitter 4ca9e470a5 Move timer macros from config.h.dist and derivates to timer.c.
This sould confuse users less and neither value is subject to change
per configuration. Reviewed also by Markus Amsler.
2011-02-23 12:37:15 +01:00
Michael Moon 3c5acd7ca7 disable interrupt if setTimer(0) is called 2011-02-11 11:53:27 +11:00
Michael Moon d91f30eda1 ensure step timer doesn't fire by itself after a period of inactivity 2011-02-10 19:57:16 +11:00
Markus Amsler 69f30f0691 timer: Use 2 comparator interrupts, one for the clock, the other for stepping.
The old implementation with an overflow interrupt for the clock and a comparator interrupt for stepping, had an unsolvable bug: If the comparator interrupt should happen very shortly after the overflow interrupt the comparator interrupt would miss. And with 2 comparators the implementation is more straightforward.
2011-02-10 19:53:58 +11:00
Markus Amsler d5033895b2 Fix timer for high speeds. 2011-02-08 21:57:58 +11:00
Michael Moon 00fc05a49b fix timer bug- if delay is smaller than TICK_TIME but counter has passed delay when timer is set, it will wait a whole TICK_TIME before firing instead of setting comparator to fire at correct time
check next_step_time instead of delay in comparison

Reported-by: Markus Amsler <markus.amsler@oribi.org>
2011-02-08 09:43:50 +11:00
Michael Moon 096d7dfdf3 Merge release-candidate-triffid branch 2011-01-07 23:09:13 +11:00
Michael Moon e3d0aa7b62 split delay functions into separate files 2010-10-11 10:12:35 +11:00
Markus Hitter 95939ecc22 Don't disable the stepper timer interrupt while stepping.
After lots of try and error the conclusion was, disabling this
interrupt makes the timer vulnerable to be messed up by
characters incoming over the serial line. So, now the
interrupt is enabled as a move starts and not disabled before
the move, and all subsequent moves are done.
2010-10-04 16:05:01 +02:00
Markus Hitter f98772ff25 timer.c: remove a few obsolete #includes. 2010-10-04 16:04:12 +02:00
Michael Moon e78381c56d Move configuration to config.h.dist 2010-09-27 09:20:07 +10:00
Markus Hitter 64683e6b6a Remove timer reset, as this doesn't play well. See
http://github.com/triffid/FiveD_on_Arduino/issues#issue/2/comment/428863 .
2010-09-25 12:08:23 +02:00
Michael Moon c1dbd869f5 minor fixes: comments, indenting, etc 2010-09-18 08:08:31 +10:00
Michael Moon 92eb4c97ca reset timer properly for more accurate timing 2010-09-16 21:42:24 +10:00
Michael Moon 48cf0e05d7 some code cleanup, added M114, wrapped M25[0..5] in ifdef DEBUG wrapper 2010-09-12 12:54:58 +10:00
Michael Moon 595b66a341 setting up new branch 2 2010-08-10 14:26:24 +10:00