Raspberry Pi: Difference between revisions

From Leo's Notes
This page was last edited on 5 March 2017, at 23:16.
Line 71: Line 71:
* https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/Documentation/gpio/gpio.txt
* https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/Documentation/gpio/gpio.txt
* https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/Documentation/gpio/sysfs.txt
* https://git.kernel.org/cgit/linux/kernel/git/stable/linux-stable.git/tree/Documentation/gpio/sysfs.txt
=== I2C ===
The steps below assume you are on ArchLinux. Substitute pacman with the proper package tool on your system.
==== Setup ====
Install the {{code|i2c-tools}} package.
{{highlight|lang=terminal|code=
# pacman -S i2c-tools
}}
Load the i2c modules on boot by adding the following lines to {{code|/etc/modules-load.d/raspberrypi.conf}}
{{highlight|lang=text|code=
i2c-dev
i2c-bcm2708
}}
Enable the hardware by editing {{code|/boot/config.txt}} and adding:
{{highlight|lang=text|code=
dtparam=i2c_arm=on
}}
==== Usage ====
To detect devices on the i2c bus, use the {{code|i2cdetect}} command.
''Eg.'' To list all devices on the first i2c port, run {{code|i2cdetect -y PORT#}}.
{{highlight|lang=terminal|code=
# i2cdetect -y 0
'    0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
}}
See Also:
* https://wiki.archlinux.org/index.php/Raspberry_Pi#I2C


== Miscellaneous ==
== Miscellaneous ==

Revision as of 23:16, 5 March 2017

Setup

Arch Linux

Download ArchLinux's Raspberry Pi tarball.

Setup the SD card to contain two partitions using fdisk:

  1. 100MB (W95 FAT32 (LBA))
  2. Rest: OS / Data
# fdisk /dev/sdcard
## Set up the SD card partitions
## Commands are: o, n, p, [enter], +100M, t, c, n, p, 2, [enter], [enter], w
# mkfs.vfat /dev/sdcard1
# mkfs.ext4 /dev/sdcard2
# cd /mnt
# mkdir root root/boot
# mount /dev/sdcard2 root
# mount /dev/sdcard1 root/boot
# tar -xpf ArchLinuxARM-rpi-latest.tar.gz -C root
# umount root/boot root

Once the SD card is prepared, insert it to the Pi and power it on. Log in to ArchLinux using the default user alarm using password alarm. The default root password is root.

For more information and to see the latest images see:

Configuration

Wi-Fi

Wi-Fi on the Raspberry pi is basically the same as on a linux desktop. See the main page on Wifi.

GPIO

GPIO (General Purpose Input/Output) pins can be interfaced using the GPIO Sysfs on the Linux kernel.

Without going into details, you can interface with GPIO at /sys/class/gpio with these common commands:

Command Description
cat /sys/class/gpio/gpiochip0/ngpio Returns the number of GPIOs managed by this device.

Eg. RPi 1 has 54 GPIOs on the Broadcom SoC.

More precisely, this device handles GPIOs 0 through 53 (N to N + ngpio - 1).

cat /sys/class/gpio/gpiochip0/base Returns the first GPIO number. (This should be the same as the number after gpiochipN).
echo 11 > /sys/class/gpio/export Exports GPIO 11. Creates a device /sys/class/gpio/gpio11
echo in > /sys/class/gpio/gpio11/direction

echo out > /sys/class/gpio/gpio11/direction

Sets the GPIO to either in or out
echo 0 > /sys/class/gpio/gpio11/value

echo 1 > /sys/class/gpio/gpio11/value

Writes a binary value to the GPIO
echo 11 > /sys/class/gpio/unexport Removes GPIO 11.

If you plan on doing something advance with the GIOs, you probably should use a library instead.

See also:


I2C

The steps below assume you are on ArchLinux. Substitute pacman with the proper package tool on your system.

Setup

Install the i2c-tools package.

# pacman -S i2c-tools

Load the i2c modules on boot by adding the following lines to /etc/modules-load.d/raspberrypi.conf

i2c-dev
i2c-bcm2708

Enable the hardware by editing /boot/config.txt and adding:

dtparam=i2c_arm=on

Usage

To detect devices on the i2c bus, use the i2cdetect command.

Eg. To list all devices on the first i2c port, run i2cdetect -y PORT#.

# i2cdetect -y 0
'     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- 27 -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

See Also:

Miscellaneous

Control Status LEDs

The Raspberry Pi 2 (and higher) LEDs are driven by the kernel's led-GPIO drivers. This allows for the LEDs to be controlled by software rather than hardware as is the case with the older first generation Pi's. The two LEDs on the Raspberry Pi 2 are the green status light (led0) and the red power light (led1).

To see the available triggers that are in use and available for a particular LED:

# cat /sys/class/leds/led1/trigger 
none usb-gadget usb-host [mmc0] timer oneshot heartbeat backlight gpio cpu0 cpu1 cpu2 cpu3 default-on transient flash torch input

The active trigger is in [ ] and can be changed by writing to the file. For example, to turn off the LED entirely:

# echo none > /sys/class/leds/led1/trigger
# cat /sys/class/leds/led1/trigger 
[none] usb-gadget usb-host mmc0 timer oneshot heartbeat backlight gpio cpu0 cpu1 cpu2 cpu3 default-on transient flash torch input

You may also edit the config.txt file to change the behavior on startup.

# Disable the ACT LED.
dtparam=act_led_trigger=none
dtparam=act_led_activelow=off

# Disable the PWR LED.
dtparam=pwr_led_trigger=none
dtparam=pwr_led_activelow=off

See Also: http://www.armadeus.com/wiki/index.php?title=GPIO_LEDS

Mounting /var/log as tmpfs

Append to the /etc/fstab with the following line.

tmpfs           /var/log tmpfs nodev,nosuid,mode=0755,size=50m  0 0

This will mount /var/log as a 50MB ramdisk. If your system is using systemd, this will break systemd-journald. To fix it, you will need to create a startup script after the mount has completed that creates the journald's directory in /var/log/journal/.

Reboot the Pi for the changes to take affect, or run mount -a which will mount the ramdisk to /var/log immediately, though you'll need to restart any services that has handles to any files there.

Why would you want to do this? To limit the amount of writes that is made to the SD card to prevent premature wearing.

UART Serial

By default, ArchLinux on the Raspberry Pi will start a getty on the UART serial port. To make use of the port, you will have to disable the getty first.

Edit /boot/cmdline.txt and remove all instances of ttyAMA0. Mine ended up looking like:

root=/dev/mmcblk0p2 rw rootwait console=tty1 selinux=0 plymouth.enable=0 smsc95xx.turbo_mode=N dwc_otg.lpm_enable=0 elevator=noop

Then, run:

systemctl disable serial-getty@ttyAMA0.service

Reboot.

If everything is working, you should be able to talk with /dev/ttyAMA0.

# stty < /dev/ttyAMA0
speed 9600 baud; line = 0;
kill = ^H; min = 100; time = 2;
-icrnl -imaxbel
-opost -onlcr
-isig -icanon -echo

# cat /dev/ttyAMA0 &
# echo hello > /dev/ttyAMA0

If you need to change the baud rate, run stty -F /dev/ttyAMA0 115200