Some older versions of gcc need the __always_inline directive in order
to inline timer_read_time. Inlining that function is important for
performance on the stm32f1.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
As long as timer_read_time() is inlined, I get better performance with
gcc -O2 optimization. The binary is also dramatically smaller and O2
better matches the other platforms.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It's possible for the code to stay in the timer irq for up to 100ms,
so serial irqs should have a higher irq priority to prevent them from
being starved. (The timer code disables irqs during event dispatch,
so serial irqs would only be permitted between events anyway.)
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The usb_cdc code will arrange to call usb_read_ep0_setup() after a
shutdown, so no need to have an explicit shutdown handler.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Call usb_read_bulk_out() before calling command_find_and_dispatch() as
this optimizes the common case where each usb packet contains a single
new message block.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Recent code additions have increased the stack usage and it is no
longer clear that everything will fit in 128 bytes. Increase all
targets to 256 bytes (the atmega2560 was already at 256 bytes).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Fix travis-ci build failure by forcing the data field of the struct
usb_string_descriptor to be the same type as a 16bit unicode string.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Use new usb_read_ep0_setup() function when reading a setup packet -
this allows the low-level usb hardware code to better handle errors.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
A zero length packet should only be sent at the end of a transmission
if the host is expecting more data.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Maintain the state tracking code entirely within a usb_do_xfer()
method. This simplifies the callers.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Use "ep0" when referring to endpoint0 - don't use "setup" as that can
be confused with the low-level usb setup token.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The atmega168 doesn't have enough flash space to fit all features -
disable the "bit banging" interfaces to make room.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The bank_id was calculated erroneously. Also unify gpio_in_setup and gpio_out_setup a bit. Also disable make flash for the sam4e8e port as it currently references a version of bossac that is not present on all systems.
Signed-off-by: Florian Heilmann <Florian.Heilmann@gmx.net>
This can be flashed to e.g. the duet wifi using bossac. It requires a
later version as is currently included in the klipper repo (1.8
vs. 1.2). Comms are currently via UART0 only, USB serial is still TBD
Signed-off-by: Florian Heilmann <Florian.Heilmann@gmx.net>
It's common for boards to have restrictions on the type of each
endpoint. So, make it possible for the board to select the endpoint
ids for each endpoint.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
No need to include separate cmsis files from the mbed project - the
standard CMSIS_5 files work fine.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Update the sam3x8e port use the most recent SAM3X CMSIS files from
Atmel. The Atmel files appear to have a more consistent license and
it is unclear if there were Arduino specific changes in the CMSIS
files obtained from the Arduino repo.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Send the temperature before checking for an out of range error (that
makes it easier to debug).
The query_thermocouple clock parameter is unpredictable and a value of
zero can't be used to disable the query - use rest_ticks instead.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The MAX6675 chip has a different read sequence than the MAX31855 chip.
Signed-off-by: Ricardo Amézquita <ramezquitao@cihologramas.com>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Change the simulator to use the generic timer_irq.c and serial_irq.c
code for (dummy) timer and io handling. This is just to make the code
a better example for other developers (most micro-controllers will use
the timer_irq.c and serial_irq.c code).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The SPI interface needs to be enabled and configured to the correct
settings of a given oid before CS is asserted. The new function
spi_prepare() allows ports to do that.
This port only introduces the new function in all ports with no
implementation and adds the call to the Klipper generic firmware code.
That means everything still works as before. Ports need to be changed
to fix the underlying issue.
Discussion about the motivation here:
https://github.com/KevinOConnor/klipper/pull/453#issuecomment-403131149
Signed-off-by: Grigori Goronzy <greg@chown.ath.cx>
Add basic SPI support and associated documentation.
v2: remove baud rate check, fix baud rate calculations
v3: finish transaction with BSY check, disable SPI when not in use
Signed-off-by: Grigori Goronzy <greg@chown.ath.cx>
Extend the ADC out of range check so that it is possible to sample
multiple times before going into a shutdown state. This reduces the
chance that measurement noise will cause an error. In an actual over
temperature (or under temperature event) it is expected that the
sensor will consistently report the problem, so extra checks for an
additional second or two should not substantially increase risk.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add mcu support for periodically polling for a button press. Add host
code support for registering buttons and invoking callbacks for them.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
For some reason, the upper two pin definitions were missing. Found while
writing the port documentation.
Signed-off-by: Grigori Goronzy <greg@chown.ath.cx>
Add a command that can immediately set the value of a digital output
pin created with config_digital_out.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The AVR requires that the SS pin be an output pin for correct SPI
operation. Some boards use the SS pin to control devices separate
from SPI, however. Don't change the output level if the pin is
already an output, and prefer setting it low if it is not already an
output pin.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The PRU code size has grown too close to the maximum - remove the ADC
code to reduce its overall size. (On the beaglebone, the Linux
process can, and typically does, do the ADC reading.)
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It appears the st7920 requires a longer delay when switching from
command to data mode (and vice-versa). Slower MCUs don't show a
problem because the klipper command processing time results in a
sufficient delay. However, some of the faster MCUs can process
klipper commands fast enough that the next st7920 transfer is sent too
fast. Add an additional delay to account for this.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Change the get_status command to get_clock. Don't report the shutdown
status in the new get_clock command.
The primary purpose of this change is to force the host code to report
a firmware version mismatch with older firmwares as recent changes
(namely the ordering of message block acks) have subtle
incompatibilities if different host/mcu code is used.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Normally, the MCU is restarted on a config change. However, that
should not be done automatically if the MCU is in a shutdown state.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If the mcu is using a traditional serial port, then only send a new
message block if there is space available in the mcu receive buffer.
This should make it significantly less likely that high load on the
mcu will result in retransmits.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Send the ack after processing commands - this gives the host code more
information on serial buffer utilization.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Prefer using 'uint8_t' buffers as it is too easy to run into C sign
extension problems with 'char' buffers. Prefer using 'uint_fast8_t'
for buffer lengths as gcc does a better job compiling them on 32bit
mcus.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The gpio_out_toggle() function in the sam3x8e and stm32f1 code was
only valid if it was called with irqs disabled.
Commits 018c5daa and 9c52ad43 enabled the lcd code which called
gpio_out_toggle() with irqs enabled. This could cause corruption of
the gpio state.
Introduce a gpio_out_toggle_noirq() function that will only be invoked
with irqs disabled, and fix gpio_out_toggle() on sam3x8e and stm32f1
so that it safe to call even if irqs are enabled.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Use the generic spi send/receive code on Linux. Update the replicape
code to use the updated command format.
Also, update the replicape code to turn off the stepper motors on a
shutdown event.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Improve the SPI message transmit system. Add support for bus speed
and bus mode. Add support for sending SPI messages on shutdown.
Signed-off-by: Petri Honkala <cruwaller@gmail.com>
Signed-off-by: Douglas Hammond <wizhippo@gmail.com>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The AVR chips (bizarrely) start an ADC conversion when entering Idle
mode. This behavior can cause the ADC to be busy when a sample is
required. Worse, if a series of events cause the cpu to enter and
leave Idle mode with a timing similar to the ADC checking rate then it
can cause the ADC to show as busy for extended periods. This could
cause high MCU load and possibly lead to a "Rescheduled timer in the
past" shutdown.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Don't logically or the ADSC bit on the start of a conversion -
explicitly set the full contents of the register. Also, clear the
ADIF flag on each write.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Define unique register aliases for all of the hardware serial port
definitions. This makes it easier to deal with the AVR chips that use
different register names.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Extract out common code from avr/serial.c, sam3x8e/serial.c, and
stm32f1/serial.c into a new generic/serial_irq.c file.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
With the optimized timer_read_time() it is no longer necessary to
implement custom timer dispatch code - use the generic mechanism in
timer_irq.c. This simplifies the code and provides a small
performance increase.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Implement 32bit timer conversion without disabling interrupts. This
uses the 16th bit of timer_high as a rollover detection flag.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add a fully functional STM32F1 port, currently mostly targeting STM32F103
microcontrollers. This requires an 8 MHz XTAL. The maximum possible step rate
is around 282K steps per second.
This uses stm32flash to burn the firmware. The bootloader needs to be started
by setting BOOT0 to 1 and resetting the MCU. There is no automatic bootloader,
unlike on Arduino.
Signed-off-by: Grigori Goronzy <greg@kinoho.net>
When initializing the ADC, explicitly clear the registers (instead of
logically or'ing them with their previous values).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It appears the most common type of avrdude protocol for devices other
than the atmega2560 is the "arduino" protocol. Update the build to
select a different protocol based on the avr processor type.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
On the AVR platform (and possibly others) the longjmp() call will
restore the interrupt state saved during the setjmp() call. So, the
setjmp() call must be invoked with interrupts disabled to ensure that
shutdown handling is run with interrupts disabled. This fixes
potential corruption of the shutdown processing on AVR.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Properly handle the (unlikely) case that a schedule_soft_pwm_out
command is received before a previous command is fully processed,
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Only open the watchdog device after the console has been opened. The
machine should not reboot in the unlikely event the console can't be
opened.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Allow the start value to be different from the default/shutdown value
for the pin. This will be useful for "heater fans" that should
startup in the off state, and transition to full on in a shutdown
state.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Continue to sample the ADC input pins even if the MCU goes into a
shutdown state. This enables the printer to continue reporting
temperatures even on an mcu error.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Pass the sample_ticks and sample_count parameters directly in the
end_stop_home command instead. This simplifies the code.
Also, simplify calculation of next wakeup time in
end_stop_oversample_event().
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Some printers can show occasional noise on the endstop pin. Support
sampling the endstop pin multiple times to attempt to filter out this
noise.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Update the scripts used to install and "flash" the pru
micro-controller code. Also, add a "flash" script for the linux
micro-controller code. This makes it easier to install Klipper on a
Beaglebone board that uses a Replicape.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add support for reading analog values via the standard Linux IIO
interface. This can be used on Replicape boards to sample analog
input pins.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add support for sending SPI messages to devices via the standard Linux
SPI interface. This can be used to configure the shift registers on
Replicape boards.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add support for controlling pca9685 PWM drivers using the standard
Linux I2C interface. The pca9685 device is found on Replicape boards.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add support for compiling the Klipper micro-controller code as a
real-time process capable of running on standard Linux systems.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If pru0 waits in sleep mode then a full beaglebone reboot is needed to
reprogram the pru0 firmware. For now, avoid using the "slp"
instruction as a workaround.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Allow pru0 to gather multiple outgoing message blocks into a single
rpmsg. This can reduce communication overhead.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Each irq flag must only be cleared after it has been serviced. This
fixes a race condition that could cause incoming commands to be
delayed for extended period.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It's possible for multiple blocks to be pending on the incoming
"rpmsg" stream. Don't sleep unless the input is confirmed to be
empty.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Compile the PRU binary with -Os optimization, but request that the
timer dispatch code be compiled with -O2 optimization. This improves
the performance of timers slightly.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The gcc -Os option significantly reduces the size of the PRU binary
and it has little impact on performance.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The command_encodef() can read the max_size parameter directly from
the 'struct command_encoder' passed into it. Also, there is no need
to check that a message will fit in a buffer if the buffer is declared
to be MESSAGE_MAX in size.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Go back to scheduling the unstep time instead of busy waiting in the
timer dispatch. With the unstep time increased to 2us, it no longer
makes sense to spin while waiting for the unstep.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Allow timer_dispatch_many() to run for extended periods if there are
no tasks pending. This reduces the amount of lost cpu time spent
entering and exiting the irq handler.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Send a "startup" message after completing the mcu init functions.
This may help detect mcu reboots during debugging.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Allow the timer dispatch irq handler to run for extended periods if
there are no tasks pending. This reduces the amount of lost cpu time
spent entering and exiting the irq handler.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Track when tasks are pending and spin in irq_wait() when no tasks are
pending. This improves the mechanism for sleeping the processor -
it's simpler for the board specific code and it reduces the
possibility of the processor sleeping when tasks are busy.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add function to indicate when tasks need to be run. This will allow
the scheduler code to know if there are any tasks that need to be
processed.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Don't rely on the generic scheduler code to always have a timer no
more than 1ms in the future. Instead, create an avr specific timer
that will be called every 0x8000 ticks. This simplifies the generic
code and it reduces the amount of code that needs to be run every
millisecond.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If sched_add_timer() is called on a timer that would make it the new
head of the list, then add it and signal the board code that the timer
should be rescheduled.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It's not necessary to keep a millisecond counter. Replace the two
users of sched_check_periodic() with explicit task wakeup flags.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The simulavr simulator will warn when writing to the watchdog
registers, but running code closer to what real hardware runs is worth
a few extra warnings.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Allow the micro-controller code to be built without support for
regular gpio pins. In this case, the code for endstops, steppers, and
gpiocmds will be disabled.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Implement new dynmem_start() and dynmem_end() functions instead of
alloc_chunk() and alloc_chunks() in the board code. This simplifies
the board code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Change the command dispatch and response generation so that most of
the work is done on pru0 instead of pru1. This allows more code to
fit into the limited space on pru1.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Don't use the sendf() macro for ack and nak messages - directly call
the command_sendf() code instead.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Now that console_get_input(), console_pop_input(),
console_get_output() and console_push_output() are local functions,
integrate them into their callers. This simplifies the code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Now that console_get_input(), console_pop_input(),
console_get_output() and console_push_output() are local functions,
integrate them into their callers. This simplifies the code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Now that console_get_input(), console_pop_input(),
console_get_output() and console_push_output() are local functions,
integrate them into their callers. This simplifies the code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Export a new console_sendf() function from the board code instead of
console_get_output() and console_push_output(). This enables more
flexibility in how the board specific code produces output.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Move the command_task() code from the generic code to the board
specific code. This enables more flexibility in how the board
specific code processes input.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Refactor the code so that message block framing, command parsing, and
command dispatch are distinct.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It appears this option is commonly needed on the at90usb1286 avr chip
(the printrboard requires it), so default it on.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add a CONFIG_SIMULAVR option to the Kbuild menus and don't directly
prompt users for CONFIG_WATCHDOG or CONFIG_SERIAL_BAUD_U2X. The only
reason to disable these options would be if one were running on
simulavr. This simplifies the user visible menu options.
Also, only show CONFIG_CLEAR_PRESCALER for at90usb1286 chips.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The atmega328 is basically the same as the atmega168 - it just adds
some additional memory. Allow the chip to be selected during the
build.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Reorder the MCU list so that newer chips are at the top of the list.
This causes the very popular atmega2560 to be the default chip
selected.
Reorder the frequency list so that higher frequencies are at the top
of the list. Restrict the 20Mhz frequency to only chips that support
that speed. This causes the popular 16Mhz frequency to be the default
speed on AVR.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Avoid using linker magic to define the init, task, and shutdown
functions. Instead, use the compile_time_request system. This
simplifies the build and produces more efficient code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Avoid generating unique ids via memory locations and linker scripts.
Instead, generate them using code produced by buildcommands.py.
Utilize gcc's ability to perform static string comparisons at compile
time to produce a unique id for each unique string.
This fixes a build failure on ARM introduced in 142b92b8. It also
reduces the complexity of the build.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The gpio_out_write() and gpio_out_setup() calls will check for zero
and non-zero, so it is not necessary to explicitly convert to 0/1 in
the stepper.c code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
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>