On some architectures, gcc will allocate a register for inline
assembler with an "m" constraint. Use "i" to avoid that.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Change the range of values used for software PWM to avoid doing an
integer division in the main code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Instead of assuming the maximum PWM value is 255, export a constant
from the firmware to the host with the maximum value.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
On regular 32bit machines there is no need to implement explicit
signed conversion on 16bit integers.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Introduce a new board function alloc_chunk() to allocate dynamic
memory. This allows the board code to implement memory allocations
without using the standard malloc() interface.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Detect avr gcc v4.8.1 and then disable warnings during the klipper.o
linking to suppress bogus "misspelled signal handler" warnings.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Support a "make flash FLASH_DEVICE=/dev/ttyACM0" rule with the default
commands for flashing a device.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Move the PWM, ADC, and SPI pin tables closer to their corresponding
code. This is code movement only - no code changes.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Extend the endstop code so that more than one stepper can be halted
during endstop homing. Some kinematic setups (eg, corexy) require an
endstop to support this.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The relax option corrupts the compilation on at least some versions of
gcc/binutils (eg, on fedora's avr-gcc 6.2.0 / binutils 2.27) due to
corruption of switch tables that use jump offsets. This issue is also
the root cause that resulted in commit d67f962a. Since the --relax
option provides minimal size / performance improvements it can simply
be dropped.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Commmit a1c61563 renamed timer_shutdown() to timer_reset() but
neglected to delete timer_shutdown().
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Commit 16e3dbb1 changed the avr implementation of timer_read_time().
Unfortunately, it raised the possibility for timer_read_time() to be
out of sync with the scheduler's understanding of the current time.
In particular, it was common for the timer irq to overflow the 16bit
hardware counter once at startup, and this would lead to
timer_read_time() always returning a time ~4ms ahead of the scheduler
on 16Mhz chips. This resulted in "Move queue empty" errors.
To resolve this issue, only increment timer_high from timer_periodic()
and make sure the timer irqs start immediately after timer_init().
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The code alignment of the TC0_Handler function seems to noticeably
impact performance benchmarks. Set the function alignment to 16 bytes
to improve testing consistency. An alignment of 16 doesn't
necessarily improve performance, but it seems to improve testing
consistency on code changes unrelated to timer dispatch.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rename the file to make it clear that the code is helper functions for
boards with irq based timers.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rename sched_timer_kick() to sched_timer_dispatch() and move its loop
into its callers in the board code. This eliminates the need to
export timer_try_set_next() from the board code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Reset the timer repeat checks on shutdown, otherwise it is possible to
get into an infinite shutdown loop.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
On faster MCUs where a delay is needed between step and unstep use a
"busy loop" in the scheduler instead of trying to schedule to the
unstep time. This reduces the chance of jitter in the scheduler
accumulating.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Directly use the Kconfig defined CONFIG_CLOCK_FREQ in the code and
avoid defining F_CPU. Also, remove the unnecessary O2 option - that
is already the default from the main makefile.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Explicilty load the timer waketime variable into local variables in
sched_timer_kick(). Change the optimization level from Os to O2.
This helps gcc to avoid unnecessary reloads from memory in the common
stepper_event() case.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Provide hand-coded assembler for timer_is_before() on AVR as that code
is used frequently in the time-critical timer dispatch loop and gcc
doesn't do a good job at compiling that comparison code. Remove the
no longer needed waketime+1 hack from reschedule_timer().
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Move sched_is_before() from sched.c to timer_is_before() in the board
specific timer code. This allows the board code to provide its own
definition.
Also, remove the sched_from_us() and sched_read_time() wrapper
functions and change the callers to directly invoke timer_from_us() /
timer_read_time().
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Commit f28eb902 reworked the switch to fix int16 encoding. However,
at least one version of avr gcc doesn't like that switch layout (it
uses a jump table). Reorg the switch to avoid that issue.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The code wasn't properly sign-extending 16bit integers which caused
int16_t reports in output() to appear as uint16_t.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Most of sam3x8e/timer.c is going to be platform agnostic for any board
with standard irq handling. Move the generic code into a new file
generic/timer.c.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If a shutdown occurs while the machine is already shutdown, then keep
the original shutdown reason code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It's tricky to reschedule the timer irq correctly (due to race
conditions with the irq) and in practice it's very rarely needed.
Handle the special cases in the generic sched.c code so that the board
code doesn't have to handle it.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Introduce a dummy sentinel timer object that is always the last item
on timer_list. This optimizes the timer_list walking code as it no
longer needs to check for NULL when traversing the list.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The hardware timer overflow bit can be used to optimize the conversion
from 16bit timers to 32bit timers.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Move the logic to calculate and report the stepper's current position
from endstop.c to stepper.c. This localizes the stepper code into
stepper.c.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It's better to not have the unimplemented command defined so that the
host can detect when it is actually implemented.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Consistently use an "oid_" prefix on the oid functions - this makes
them similar to other functions with a common prefix.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Detect the maximum size of each "move queue" item during the
configuration phase instead of using the stepper move struct. This
allows the stepper code to be contained entirely in stepper.c and it
allows for future run time allocations from other types of objects.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Store a full 64bit uptime in the mcu and query it at the start of each
connection. This ensures the host's 64bit clock is always in synch
with the mcu's clock.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The irq handling in sam3x8e isn't specific to the sam3x8e proccessor -
it's generic for all armcm type machines. So, move the definitions
into a new file generic/armcm-irq.c
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It's possible for sched_del_timer() to be called on a timer that fires
just after sched_del_timer disables irqs but before the next timer is
scheduled. In this case be sure to clear the irq pending status flag
after scheduling the next timer so that a delayed irq doesn't cause
the wrong timer to be run. For the same reason, make sure to check
the irq pending status flag at the start of the timer irq.
Also, as a safety check, make sure timer_set_next() isn't called from
within the timer irq dispatch loop.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
There is a small possibility that a shutdown could occur between
clearing transmit_max and clearing transmit_pos - so make sure to
handle the case where transmit_pos > transmit_max.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
As long as the code is careful when writing the in_sendf variable it
should be safe to update it without having to disable irqs.
Also, make sure in_sendf is cleared on shutdown.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Allow sendf() to be called from irq and timer context - check for the
case where sendf() is called while already in sendf() and simply
discard those messages. This makes it safe to use output() debugging
calls even in irq and timer context.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Use a base of 256 instead of 65536 when calculating the sum of the
square of the clock differences in the stats. This makes the
calculation more accurate. Export the new base via DECL_CONSTANT for
the host to access. Use DIV_ROUND_UP() when adjusting for the base to
ensure no lost ticks. Do the division after multiplication in the
common case where the time between stats_task() invocations is less
than 64K ticks.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Don't assume the hardware ADC has 10bit resultion - instead have the
firmware define a constant and read that constant in the host.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add a DECL_CONSTANT macro to allow the firmware to define constants
that are to be exported to the host during the "identify" phase. This
replaces the existing hardcoded mechanism of scanning the Kconfig
header file for certain constants.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Automatically reset the next step time to zero on a stepper_stop()
call. This makes the host code simpler as it no longer needs to
schedule an explicit reset_step_clock command on the step after a
homing operation.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Invert the default meaning of the stepper direction pin. Instead of
treating a low value as position motion, treat a high value as
positive motion. This matches what other firmwares do, and it matches
what common stepper motor drivers document.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Return the number of clock ticks to wait directly from
gpio_adc_sample(). This simplifies the ADC interface.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rework the timer prioritization code so that is compares against the
current time instead of the number of repeat timers in a given
interrupt. This makes the code slightly faster and it should provide
better protection against task starvation.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Tweak the AVR register pressure optimization in reschedule_timer() to
optimize it further. This improves the performance of AVR timers when
there are several pending timers.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rework the timer prioritization code so that is compares against the
current time instead of the number of repeat timers in a given
interrupt. This makes the code slightly faster and it should provide
better protection against task starvation.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Increase the size of the serial receive buffer. With transmit rates
of 250000 baud, it only takes a little over a millisecond to overflow
the existing buffer.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It's possible for a "count=1" stepper move immediately after a
set_next_step_dir or reset_step_clock command to be valid and have an
interval less than min_stop_interval. Make sure this case does not
result in a shutdown.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Commit da305e6b changed the objcopy command to use the --dump-section
flag. However, the 2.24 version of avr-objcopy (which is common on
raspbian installs) does not support this flag. Avoid using
--dump-section and continue to use the -j option instead.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This adds basic support for running on the Atmel SAM3x8e
micro-controllers that are found in the Arudino Due boards.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
On faster MCUs the step and unstep events may be too close for the
stepper motor driver. Add a CONFIG_NO_UNSTEP_DELAY build option and
support the case where it is not set. This allows faster MCUs to
schedule two events for each step (one for the step and one for the
unstep).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Introduce enable_tx_irq() for manipulating the AVR hardware. This
keeps the low-level hardware code together.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Default to O2 and set Os in the AVR makefile. Platforms besides AVR
are likely to produce better code with O2 so make that the default.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Some architectures are faster passing regular integers than 8bit
integers. Use uint_fast8_t so that the architecture chooses the
appropriate type.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The AVR wants a uint8_t return type for irq_save(), but other
architectures will generally prefer int. Allow the board to configure
the size of the flag by introducing an irqstatus_t typedef.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Task and command handlers always run with irqs enabled, so it is not
necessary to save/restore the irq state when disabling irqs in these
handlers.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Some boards may not support hardware based PWM. Update the build so
that those commands do not need to be compiled if they are not
available.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Some boards may not support SPI transfers. Update the build so that
those commands do not need to be compiled if they are not available.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Some boards may not support the ADC hardware. Update the build so
that those commands do not need to be compiled if they are not
available.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Move the definitions of the readb() style functions to a new header
generic/io.h. This eliminates the dependency of stdint.h on
compiler.h.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Instead of creating a misc.h file in each board directory, create a
generic board directory and declare misc.h there.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Some AVR chips ship with a 1/8th clock divisor set. Add a compile
time option to manually clear this field at startup.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The gpio_adc_info only contains a single uint8_t field - it's simpler
to use an array of uint8_t instead.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>