SPEd 2013 Workshop
Embedded Linux Class by Mark A. Yoder
Here are the labs for the afternoon Linux part of the SPEd2013 Workshop
Contents
Wiring Up
First wire up an LED, switch and a potentiometer. The table below shows what pins are available. The LED and switch can go on any GPIO (general purpose IO) pin. The pot goes on one of the AIN (analog in) pins.
P9 P8
Wiring an LED
Take an LED and attach the long lead (anode) into pin P9_11. That is, pin 11 on the P9 (left) header. Put the short lead on a column on the breadboard. Put one end of the 270Ω resistor in the same column as the LED and the other end in pin P9_1 (GND).
Wiring a switch
Attach one end of the switch on P9_2 (GND) and the other on P9_13. If you don't have a switch, put one end of a wire in P9_2 and touch the other end to P9_13 when you want to turn it on.
Wiring a potentiometer
Attach the middle pin of the pot to P9_36 (AIN5). Place one of the outer pins to P9_32 (Vdd_ADC) which is a 3.3V analog reference. Place the other on pin P9_34 (GNDA_ADC) the analog ground.
IO via BoneScript
Writing a pin
- Click on the digitalWrite() link on the left column under BoneScript Functions.
- Click run and verify that the USR0 LED is on.
- Edit the code and replace 'USR0' with 'P9_11' (the pin you wired the LED to).
- Click run
- Play around
Reading a pin
- Click on the digitalRead() link on the left column under BoneScript Functions.
- Edit the code and replace 'P8_19' with 'P9_13' (the pin you wired the switch to).
- Click run. Push the switch and click run
- Play around
Can you modify the code to read the switch and turn on the LED in response?
Reading analog
- Click on the analogRead() link on the left column under BoneScript Functions.
- Click run (you wired the pot to P9_36). Change the pot and click run again.
- Play around
Cloud 9
- Open Cloud 9 via http://192.168.7.2:3000
- Open analog2.js
- Change the IO pins to match what you've wired
- Save the file (ctrl-s) and click run
- Play around
- Open blinkled.js and repeat the steps above.
Linux Warm Up
Before we can interact with LEDs and switches we need to learn some simple Linux commands.
- On your host computer, running Windows, start up puTTY.
- If you get a Security Warning, click Run.
- Enter 192.168.7.2 in the Host Name field and click Open
- Login as root with no password.
- Enter ls to list what files you have. You shouldn't see much.
At this point you need to learn a few simple Linux commands for creating and displaying files. Once you know these commands it's easy to turn an LED on and off.
First, let's edit a file using the nano editor. Nano is a simple editor that easy to learn. This will edit (and create) the file play.txt.
bone$ nano play.txt
Add a couple of lines of text to the file, it doesn't really matter what and then Exit. You can list the files in the current directory with ls and show the contents of a file with cat.
bone$ ls Desktop play.txt bone$ cat play.txt A couple of lines of text.
Use echo to print a line of text.
bone$ echo This is a line of text This is a line of text
Here's a powerful operator. You can take the output of any command and redirect it to a file with >.
bone$ echo This is a line of text > here.txt bone$ cat here.txt This is a line of text
We are almost there. Use cd to change directories. / is the top level directory.
bone$ cd / bone$ ls bin dev home lost+found mnt run sys usr boot etc lib media proc sbin tmp var
If you ever get lost, cd alone takes you home.
bone$ cd gone$ ls Desktop here.txt play.txt
Now you are ready to flash an LED.
Blinking an LED
gpio via the Shell Command Line and sysfs
Another easy way to do general purpose I/O (gpio) on the Beagle is through a terminal window and a shell prompt. In Linux, almost everything is treated as a file, even things that aren't files. Here we'll use a virtual file system called sysfs. sysfs exposes the drivers for the hardware so you can easily use them.
Try this:
bone$ cd /sys bone$ ls block bus class dev devices firmware fs kernel module power
The "/" after the name means it's a directory. Here we see several directories that represent hardware we can control. Explore a bit and see what you find.
Now try:
beagle$ cd /sys/class beagle$ ls backlight firmware leds power_supply scsi_host uio bdi gpio mbox pwm sound usbmon block graphics mdio_bus rc spi_master vc bluetooth hwmon mem regulator spidev video4linux bsg i2c-adapter misc rfkill thermal vtconsole dma i2c-dev mmc_host rtc tty watchdog drm input mtd scsi_device ubi dvb lcd net scsi_disk udc
Explore some.
Blinking a USR LED
The Beagle Black has four user LEDS, user0 - user3, that you can control. Try this:
bone$ cd /sys/class/leds bone$ ls beaglebone:green:usr0 beaglebone:green:usr2 beaglebone:green:usr1 beaglebone:green:usr3
Here you see the directories for controlling each of the usr LEDs. By default, usr0 flashes a heartbeat pattern and usr1 flashes when the micro SD card is accessed. Let's control usr0.
bone$ cd beagleboard\:\:usr0 bone$ ls brightness device max_brightness power subsystem trigger uevent
See what's in brightness, max_brightness and trigger by using the cat
command. For example:
bone$ cat trigger none nand-disk mmc0 timer oneshot [heartbeat] backlight gpio cpu0 default-on transient
This shows trigger can have many values. The present value is heartbeat. Check the LED, is it beating? You can stop the heartbeat via:
bone$ echo none > trigger bone$ cat trigger [none] nand-disk mmc0 timer oneshot heartbeat backlight gpio cpu0 default-on transient
Did it stop beating? You can now turn it on and off with:
bone$ echo 1 > brightness bone$ echo 0 > brightness
Is it responding correctly?
The Bone has more trigger options. Try:
bone$ cat trigger [none] mmc0 timer heartbeat backlight gpio default-on bone$ echo timer > trigger bone$ ls brightness delay_on max_brightness subsystem uevent delay_off device power trigger bone$ echo 100 > delay_on bone$ echo 900 > delay_off
What does this do?
Blinking an External LED via gpio
Earlier we wired an LED to the P9_12 General Purpose IO (gpio) port and controlled it via BoneScript. Here we'll control it via a shell command. First we need to figure out which gpio pin P9_12 is attached to. The following figure shows it attached to gpio_60.
Here's how you turn it on
bone$ cd /sys/class/gpio bone$ ls export gpio22 gpio31 gpio7 gpiochip32 gpiochip96 gpio20 gpio23 gpio50 gpiochip0 gpiochip64 unexport
(Your ls may look different depending on what you have used with BoneScript.) Presently no gpio pins are visible. You need to tell it which pin to export
bone$ echo 30 > export bone$ ls export gpio22 gpio30 gpio50 gpiochip0 gpiochip64 unexport gpio20 gpio23 gpio31 gpio7 gpiochip32 gpiochip96
Notice gpio30 has appeared. All we need to do is tell it which direction and then turn it on.
bone$ cd gpio30 bone$ ls active_low direction edge power subsystem uevent value bone$ echo out > direction bone$ echo 1 > value
Your LED should be on!
bone$ echo 0 > value
Now it's off.
Reading a switch
Now that you have an LED working, wiring in a switch is easy. Earlier you wired a switch to P9_42, which from the table above is gpio_7.
Based on what you saw above.
bone$ cd /sys/class/gpio bone$ echo 7 > export bone$ cd gpio7 bone$ ls bone$ echo in > direction bone$ cat value 0
Now hold the button down and try again.
bone$ cat value 1
Once you have the switch and LED working use nano and put the following in a file.
bone$ cd (Go back home) bone$ nano button.sh #!/bin/bash cd /sys/class/gpio while [ 1 ] do cat gpio7/value sleep 0.25 done
Quit nano and run
bone$ chmod +x button.sh (This makes button.sh executable) bone$ ./button.sh
What happens when you push the button? Hit Ctrl-C to quit button.sh.
Now experiment around. Can you flash the LED? How fast? Make the LED read the switch.
Other Languages
Out-of-the-box the bone can run
- C
- C++
- bash
- perl
- python
- Javascript
Here's a simple C example
bone$ cd bone$ nano hello.c #include <stdio.h> main () { printf("hello, world\n"); }
bone$ cc hello.c bone$ ./a.out hello, world
Try your favorite language.
Embedded Linux Class by Mark A. Yoder