docs: Improvements to RPi_microcontroller.md

Wraps lines to 80 columns.  Only use triple backticks on multi-line
code excerpts.  Add backticks around "sudo raspi-config".  Move
"Enabling SPI" section to be near pwm and gpio configuration.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2022-01-28 18:24:25 -05:00
parent fb6d6d381c
commit 31cefe15cf
1 changed files with 59 additions and 31 deletions

View File

@ -1,23 +1,26 @@
# RPi microcontroller
This document describes the process of running Klipper on a RPi
and use the same RPi as secondary mcu.
This document describes the process of running Klipper on a RPi and
use the same RPi as secondary mcu.
## Why use RPi as a secondary MCU?
Often the MCUs dedicated to controlling 3D printers have a limited and
pre-configured number of exposed pins to manage the main printing
functions (thermal resistors, extruders, steppers ...).
Using the RPi where Klipper is installed as a secondary MCU gives the
possibility to directly use the GPIOs and the buses (i2c, spi) of the RPi
inside klipper without using Octoprint plugins (if used) or external
programs giving the ability to control everything within the print GCODE.
functions (thermal resistors, extruders, steppers ...). Using the RPi
where Klipper is installed as a secondary MCU gives the possibility to
directly use the GPIOs and the buses (i2c, spi) of the RPi inside
klipper without using Octoprint plugins (if used) or external programs
giving the ability to control everything within the print GCODE.
**Warning**: If your platform is a _Beaglebone_ and you have correctly followed the installation steps, the linux mcu is already installed and configured for your system.
**Warning**: If your platform is a _Beaglebone_ and you have correctly
followed the installation steps, the linux mcu is already installed
and configured for your system.
## Install the rc script
If you want to use the host as a secondary MCU the klipper_mcu process must run before the klippy process.
If you want to use the host as a secondary MCU the klipper_mcu process
must run before the klippy process.
After installing Klipper, install the script. run:
```
@ -26,10 +29,6 @@ sudo cp "./scripts/klipper-mcu-start.sh" /etc/init.d/klipper_mcu
sudo update-rc.d klipper_mcu defaults
```
## Enabling SPI
Make sure the Linux SPI driver is enabled by running sudo raspi-config and enabling SPI under the "Interfacing options" menu.
## Building the micro-controller code
To compile the Klipper micro-controller code, start by configuring it
@ -38,7 +37,9 @@ for the "Linux process":
cd ~/klipper/
make menuconfig
```
In the menu, set "Microcontroller Architecture" to "Linux process," then save and exit.
In the menu, set "Microcontroller Architecture" to "Linux process,"
then save and exit.
To build and install the new micro-controller code, run:
```
@ -47,9 +48,10 @@ make flash
sudo service klipper start
```
If klippy.log reports a "Permission denied" error when attempting to connect
to `/tmp/klipper_host_mcu` then you need to add your user to the tty group.
The following command will add the "pi" user to the tty group:
If klippy.log reports a "Permission denied" error when attempting to
connect to `/tmp/klipper_host_mcu` then you need to add your user to
the tty group. The following command will add the "pi" user to the
tty group:
```
sudo usermod -a -G tty pi
```
@ -61,12 +63,24 @@ following the instructions in
[RaspberryPi sample config](../config/sample-raspberry-pi.cfg) and
[Multi MCU sample config](../config/sample-multi-mcu.cfg).
## Optional: Enabling SPI
Make sure the Linux SPI driver is enabled by running
`sudo raspi-config` and enabling SPI under the "Interfacing options"
menu.
## Optional: Identify the correct gpiochip
On Rasperry Pi and on many clones the pins exposed on the GPIO belong to the first gpiochip. They can therefore be used on klipper simply by referring them with the name `gpio0..n`.
However, there are cases in which the exposed pins belong to gpiochips other than the first. For example in the case of some OrangePi models or if a Port Expander is used. In these cases it is useful to use the commands to access the _Linux GPIO character device_ to verify the configuration.
On Rasperry and on many clones the pins exposed on the GPIO belong to
the first gpiochip. They can therefore be used on klipper simply by
referring them with the name `gpio0..n`. However, there are cases in
which the exposed pins belong to gpiochips other than the first. For
example in the case of some OrangePi models or if a Port Expander is
used. In these cases it is useful to use the commands to access the
_Linux GPIO character device_ to verify the configuration.
To install the _Linux GPIO character device - binary_ on a debian based distro like OctoPi run:
To install the _Linux GPIO character device - binary_ on a debian
based distro like octopi run:
```
sudo apt-get install gpiod
```
@ -81,9 +95,13 @@ To check the pin number and the pin availability tun:
gpioinfo
```
The chosen pin can thus be used within the configuration as `gpiochip<n>/gpio<o>` where **n** is the chip number as seen by the `gpiodetect` command and **o** is the line number seen by the` gpioinfo` command.
The chosen pin can thus be used within the configuration as
`gpiochip<n>/gpio<o>` where **n** is the chip number as seen by the
`gpiodetect` command and **o** is the line number seen by the`
gpioinfo` command.
***Warning:*** only gpio marked as `unused` can be used. It is not possible for a _line_ to be used by multiple processes simultaneously.
***Warning:*** only gpio marked as `unused` can be used. It is not
possible for a _line_ to be used by multiple processes simultaneously.
For example on a RPi 3B+ where klipper use the GPIO20 for a switch:
```
@ -160,23 +178,32 @@ gpiochip1 - 8 lines:
## Optional: Hardware PWM
Raspberry Pi's have two PWM channels (PWM0 and PWM1) which are exposed on the header or if not, can be routed to existing gpio pins.
The Linux mcu daemon uses the pwmchip sysfs interface to control hardware pwm devices on Linux hosts.
The pwm sysfs interface is not exposed by default on a Raspberry and can be activated by adding a line to ```/boot/config.txt```:
Raspberry Pi's have two PWM channels (PWM0 and PWM1) which are exposed
on the header or if not, can be routed to existing gpio pins. The
Linux mcu daemon uses the pwmchip sysfs interface to control hardware
pwm devices on Linux hosts. The pwm sysfs interface is not exposed by
default on a Raspberry and can be activated by adding a line to
`/boot/config.txt`:
```
# Enable pwmchip sysfs interface
dtoverlay=pwm,pin=12,func=4
```
This example enables only PWM0 and routes it to gpio12. If both PWM channels need to be enabled you can use ```pwm-2chan```.
This example enables only PWM0 and routes it to gpio12. If both PWM
channels need to be enabled you can use `pwm-2chan`.
The overlay does not expose the pwm line on sysfs on boot and needs to be exported by echo'ing the number of the pwm channel to ```/sys/class/pwm/pwmchip0/export```:
The overlay does not expose the pwm line on sysfs on boot and needs to
be exported by echo'ing the number of the pwm channel to
`/sys/class/pwm/pwmchip0/export`:
```
echo 0 > /sys/class/pwm/pwmchip0/export
```
This will create device ```/sys/class/pwm/pwmchip0/pwm0``` in the filesystem.
The easiest way to do this is by adding this to ```/etc/rc.local``` before the ```exit 0``` line.
With the sysfs in place, you can now use either the pwm channel(s) by adding the following piece of configuration to your ```printer.cfg```:
This will create device `/sys/class/pwm/pwmchip0/pwm0` in the
filesystem. The easiest way to do this is by adding this to
`/etc/rc.local` before the `exit 0` line.
With the sysfs in place, you can now use either the pwm channel(s) by
adding the following piece of configuration to your `printer.cfg`:
```
[output_pin caselight]
pin: host:pwmchip0/pwm0
@ -184,7 +211,8 @@ pwm: True
hardware_pwm: True
cycle_time: 0.000001
```
This will add hardware pwm control to gpio12 on the Pi (because the overlay was configured to route pwm0 to pin=12).
This will add hardware pwm control to gpio12 on the Pi (because the
overlay was configured to route pwm0 to pin=12).
PWM0 can be routed to gpio12 and gpio18, PWM1 can be routed to gpio13
and gpio19: