Move armcm_main() to stm32f0.c, stm32f1.c, and stm32f4.c. This gives
the arch specific code more control on the early boot setup.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The main() function has a special meaning to gcc and using it can
result in different code generation. Use armcm_main() to avoid that.
Also, invoke SystemInit() from the board specific armcm_main() code.
This gives the board code more control over board initialization.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The stm32f0 line uses 16bit packet memory reads/writes (as opposed to
the goofy 32bit accesses required on the stm32f103).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
When enabling "low level options" in Kconfig, selecting a chip that is
20Mhz capable would result in 20Mhz being the default. A 16Mhz speed
is the preferred default, so list that first in the menu.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The FIOSET and FIOCLR registers may not set an initial value if the
pin is not in an output mode. So, use FIOPIN to set the initial
value.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The usbfs receive buffers must also have space for the 2 byte crc at
the end of a transmission. This fixes an error that could lead to
lost messages from mcu to host (resulting in bytes_invalid increasing
in stats) on the stm32f103.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It appears the usbotg controller can get confused if the usb irq
handler runs while processing a usb request. Disable usb irqs during
usb processing to avoid this.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Use the XFRC interrupt instead of TXFE. Don't mask/unmask the tx
interrupts during runtime. This fixes some race conditions where a tx
notification may have previously gotten lost.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The ADC pins (on at least the sam3x8e) need to be configured in input
mode with the internal pullups disabled in order to get accurate ADC
readings.
Reported by @bryanboettcher.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rename the linker script so the build does not have any conflicts with
the armcm_boot.d dependency file.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Replace the custom encoding with a hex encoding. This makes it a
little easier to inspect the CTR conversions.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Store the CTR requests in compile_time_request.txt with the null
characters removed. This makes it slightly easier to inspect the CTR
conversions.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add basic ARM Cortex-M C init code and build linker scripts to
src/generic/ code. This can be used to simplify the various ARM board
code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add a general build rule to precompile *.lds.S files to *.ld files.
Remove the custom rules in src/atsamd/Makefile and src/stm32/Makefile.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
On fast MCUs (like the SAMD51) it is possible for the gpio to toggle
faster than the st7920 can accept. Add a small delay around the clock
rise signal.
Signed-off-by: Lars R. Hansen <popshansen@hotmail.com>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rework the spi_config commands so that bus configuration and shutdown
message configuration is done separately from the main "config_spi"
command. This makes the spi configuration more flexible. It's now
possible to use software spi without a CS pin. It's now possible to
define multiple SPI messages to send on a shutdown event.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Now that the stm32f1 code has been merged into the stm32 code, there
is no longer a need to keep the stm32f1/ directory.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rename the clock.c file to stm32f4.c to make it more clear that the
code is specific to the stm32f4 chips.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Now that the code in stm32f4/ can handle both stm32f1 and stm32f4
chips, rename the directory to just "stm32".
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Update the stm32f1 code to the latest from ST. Merge the
lib/cmsis-stm32f1/ and lib/hal-stm32f1/ into a single lib/stm32f1/
directory. Document all the differences from the pristine upstream
code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Rename the commands.
The main reason for this change is to force a micro-controller code
updated (due to important bugfix in 7fefe52d).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If the move is not added to the move queue, then the SF_LAST_DIR
setting must not be changed. Otherwise, it could result in an
incorrect direction being set on the stepper after a homing operation.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The popular "LPC17xx-DFU-Bootloader" alters the LPC_UART0->FDR
register, so be sure to reset it back to its default when using serial
in Klipper.
Reported by @b-vs.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Commit 1096075d changed CONFIG_CLOCK_FREQ to the actual cpu frequency,
but that commit failed to update adc.c accordingly. That made the adc
run 4 times slower. Make the corresponding change to adc.c.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Improve the precision of soft pwm pulses by allowing the host to
directly specify the clock duration of the pulse. This improvement in
precision may be noticeable when controller servos (and bltouch).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The replicape servo pins (P9_14/P9_16) are muxed to the SOCs hardware
PWM unit driven by a 13MHz GP timer. They have to be driven by the
linux host mcu. This commits adds hardware PWM support using the linux
sysfs user space interface.
Signed-off-by: Janne Grunau <janne-3d@jannau.net>
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The sampling time of the ADC was too slow (239 ADC clock cycles), causing the reading of the next ADC channel to have cross talk as per issue #1261. Sampling time updated to 41 ADC clock cycles.
Signed-off-by: Marco D'Alessio <marco@wrecklab.com>
It appears the PRU timer will not trigger on a match of exactly zero.
If the next timer happened to be set to exactly zero, it could cause
software timers to get stuck and result in "move queue empty" errors.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It appears some host machines may not recognize that the USB device
has disconnected upon start of bootloader mode. Explicitly disable
USB and add a 5ms delay to help signal the host of the upcoming
change.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Commit b9b03dd0 had a typo in the flag updating. The typo could
result in corruption of the stepper state.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Now that enumerations are available, any variable named "pin" can only
be used to describe a gpio pin. Rename the end_stop_state "pin"
parameter to "pin_value". Also, rename "end_stop_query" command to
"end_stop_query_state" to notify users to upgrade both host and mcu
code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Make it clear that the name of the constant being defined is a string.
When the value being defined is also a string, use a new
DECL_CONSTANT_STR() macro.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If the stepper is stopped by the endstop code, then ignore any further
stepper moves until a reset_step_clock command is received.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Default to running dfu-util via sudo as most machines will not have
the user setup with permissions to access the raw usb device.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add support for entering the "stm32duino" bootloader via the arduino
1200 baud USB request. Add supprot for flashing over USB via the
"make flash" command.
Rename the existing "make flash" command to "make serialflash".
Default to using a bootloader in Kconfig.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It's rare to use all of a target's prerequisites in a recipe - replace
most cases with $< (the target's first prerequisite).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Commit e18501d0 broke the build for targets that added objects to
klipper.elf via adding dependencies. Rework the build so that it is
possible to add objects to klipper.elf via a new OBJS_klipper.elf
makefile variable.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Some of the samd21 chips have limited memory - change the code to
reduce the size of global variables.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Use a local linker script instead of the linker script provided by
Atmel. This will allow Klipper to use dynamic memory allocation.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Most samd boards have the crystal and it does improve the clock
accuracy, so default to enabling it.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add support for using the internal clocks (with USB clock recovery
mode if available) on the SAMD21. Don't use the internal clock if the
external crystal is requested (instead use the PLL synced to the
external 32Khz signal).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Add support for USB clock recovery mode if an external 32Khz crystal
is not in use. If using an external crystal, then don't use the
internal 48Mhz DFLL48Mhz clock (just use the PLLs synced to the
external 32Khz signal).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The SAMD51 ADC is only clocked on rising edges (vs both rising and
falling edges on the SAMD21) and it has a greater minimum frequency
than the SAMD21. So, increase the ADC clock.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The cpu is almost always 16Mhz and the serial port is almost always
UART0, so no need to prompt in the normal case.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Now that the internal DWT timer is used for software timers, there is
no need to clock the peripherals at a higher clock speed. Clock all
peripherals at 48Mhz and simplify the clock generation code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Clearing the watchdog while it is in progress can cause bus stalls on
the SAMD21. It appears that clearing the watchdog on the SAMD51 can
cause lockups of the watchdog device.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The sram matrix configuration is, bizarrely, configured with a slow
default. It will typically add an extra wait state to every memory
access. Set the matrix sram controller to improve the performance.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Avoid using the "low-level" library timing utilities. This is in
preparation for using SysTick as part of the timer implementation.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The ARM CortexM3 (and later) chips almost always have a 32bit timer in
the "Debug and Watch Trace" (DWT) unit which resides within the cpu
core itself. This timer is generally faster to access than timers
located on the chip's peripheral bus. When combined with the standard
ARM SysTick timer it provides a (generally) portable mechanism to
implement Klipper's software timer mechanism. It's generally also
faster than using the vendor specific timers.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Move the definition of SERIAL_BAUD to the main src/Kconfig file. Only
show this as an option if the user selects the ability to configure
low-level options.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The lpc176x ADC is extremely noisy. Implement a 5 order median filter
on each ADC read to weed out obviously incorrect readings.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Order the adc config code so that a shutdown from gpio_adc_setup()
doesn't result in analog_in_shutdown() running on a 'struct analog_in'
that hasn't yet been configured.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Make sure to explicitly cast to 32bit integers when doing math on
16bit integers that require the extra precision as the AVR uses a
16bit int.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The sam3 i2c code and the linux code use a 7-bit i2c address, while
the avr, lpc176x, and samd21 i2c code uses an 8-bit address with the
least significant bit always zero. A similar issue occurred in the
host code (sx1509.py and replicape.py use 7-bit addresses while
uc1701.py and mcp4451.py use 8-bit addresses).
Consistently use 7-bit addresses in all the code. This breaks
compatibility between host and mcu software, so make a change to the
config_i2c command to force users to synchronize software updates.
This also breaks common Smoothieboard configs, so update the mcp4451
code to validate the i2c_address.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Most of the peripherals on the sam4e8e are similar to the ones on the
sam3x8e mcu. Merge the code together and use just one code directory.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Remove the 'pin' and 'afec' variables from 'struct gpio_adc'. This is
in preparation to merge the sam3x8e and sam4e8e code.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Use internal.h in a similar way to the internal.h in sam3x8e. Also,
move the twi pin definitions from internal.h to i2c.c (as they are only
used there).
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
These two files from atmel have different licenses than the other
files. Neither files provide any real value, so just remove them to
avoid any confusion.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Pass the gpio id instead of the bank/pin to gpio_peripheral(). This
is in keeping with other ARM ports.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Scale the bus speed within the peripheral instead of on the system bus
as scaling on the system bus slows down general register access to the
peripheral.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Simultaneously define the hardware pin and the timer registers. This
makes the table a little easier to understand.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Increase the amount of time to "busy wait" in the timer dispatch loop
to 2 micro-seconds. This, in practice, causes stepper step and unstep
events to occur within a single hardware interrupt. Doing that helps
stabilize the single active stepper performance benchmarks.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
The atmega328p is basically the same as the atmega328 - add explicit
support for it so that avrdude doesn't complain while flashing.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Sometime bossac reports an error during chip restart. It appears this
error is spurious - just suppress it.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Scaling the timer on the system bus has a notable impact on
performance. So, scale the timer within the timer hardware itself.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
It appears the sam3x8e has a primitive cache prefetch mechanism (it
prefetches 32 bytes at a time aligned to a 16 byte boundary).
Aligning the main loop in timer_dispatch_many() to a 16 byte boundary
significantly improves performance.
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
If sched_try_shutdown() is not inlined into timer_dispatch_many() it
can have a significant impact on benchmarks. (Even though the
function isn't called in practice, the call causes gcc to organize the
function differently.)
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
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>