Difference between revisions of "ASEE 2013 Workshop"

From eLinux.org
Jump to: navigation, search
m (Blinking an External LED via gpio)
m (Fixed an edit)
 
(12 intermediate revisions by 2 users not shown)
Line 8: Line 8:
 
# On your host computer, running Windows, start up '''puTTY'''.   
 
# On your host computer, running Windows, start up '''puTTY'''.   
 
# If you get a ''Security Warning'', click '''Run'''.
 
# If you get a ''Security Warning'', click '''Run'''.
# Enter '''192.167.7.2''' in the '''Host Name''' field and click '''Open'''
+
# Enter '''192.168.7.2''' in the '''Host Name''' field and click '''Open'''
 
# Login as '''root''' with no password.
 
# Login as '''root''' with no password.
 
# Enter '''ls'''  to list what files you have. You shouldn't see much.
 
# Enter '''ls'''  to list what files you have. You shouldn't see much.
Line 147: Line 147:
 
Your LED should be on!
 
Your LED should be on!
 
  bone$ '''echo 0 > value'''
 
  bone$ '''echo 0 > value'''
Now it's off. When you are done you can unexport the pin and it will disappear.
+
Now it's off.
bone$ '''cd ..'''
 
bone$ '''ls'''
 
export  gpio60  gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport
 
bone$ '''echo 60 > unexport'''
 
bone$ '''ls'''
 
export  gpiochip0  gpiochip32  gpiochip64  gpiochip96  unexport
 
  
 
== Reading a switch ==
 
== Reading a switch ==
Line 171: Line 165:
 
  1
 
  1
 
Once you have the switch and LED working use '''nano''' and put the following in a file.
 
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'''
 
  bone$ '''nano button.sh'''
 
  #!/bin/bash
 
  #!/bin/bash
Line 182: Line 177:
 
  bone$ '''chmod +x button.sh'''  (This makes button.sh executable)
 
  bone$ '''chmod +x button.sh'''  (This makes button.sh executable)
 
  bone$ '''./button.sh'''
 
  bone$ '''./button.sh'''
What happens when you push the button?
+
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.
 
Now experiment around. Can you flash the LED? How fast? Make the LED read the switch.
  
 
== Analog In ==
 
== Analog In ==
The bone has eight Analog Inputs.  Several are exposed on P9.  They are labeled '''AIN''' in table 11 below.  How many do you find?
+
The bone has eight Analog Inputs.  Several are exposed on P9.  They are labeled '''AIN''' in the table below.  How many do you find?
  
 
[[File:P9PWMs.jpg|800px]]
 
[[File:P9PWMs.jpg|800px]]
Line 193: Line 188:
 
The AIN pins are sampled at 12 bits and 100k samples per second.  The input voltage is between 0 and 1.8V.  Fortunately, both voltages are available on P9.
 
The AIN pins are sampled at 12 bits and 100k samples per second.  The input voltage is between 0 and 1.8V.  Fortunately, both voltages are available on P9.
  
You've already wired these up for the AM lab. You interact with the analog in much like the gpio, but it appears in a different. We have to run a command before the AIN interface appears.  Just run them now, later I'll explain what you did if you are interested.
+
You've already wired up P9_39 for AIN0 in the AM lab. You interact with the analog in much like the gpio, but it appears in a different place. We have to run a command before the AIN interface appears.  Just run them now, later I'll explain what you did if you are interested.
  
 
  bone$ '''SLOTS=/sys/devices/bone_capemgr.*/slots'''
 
  bone$ '''SLOTS=/sys/devices/bone_capemgr.*/slots'''
Line 204: Line 199:
 
  AIN0  AIN2  AIN4  AIN6  driver@  power/      uevent
 
  AIN0  AIN2  AIN4  AIN6  driver@  power/      uevent
 
  AIN1  AIN3  AIN5  AIN7  modalias  subsystem@
 
  AIN1  AIN3  AIN5  AIN7  modalias  subsystem@
 
+
  bone$ '''cat AIN0'''
There are the various analog inputs, but watch out.  This interface starts numbering at '''1''' and Table 11 starts at '''0''', so to read AIN5 you need to look at '''AIN6'''!
 
 
 
  bone$ '''cat AIN6'''
 
 
  1185
 
  1185
  
 
Change the pot and rerun '''cat'''.  What's the min and max value you get?  Is it 12 bits?
 
Change the pot and rerun '''cat'''.  What's the min and max value you get?  Is it 12 bits?
  
You can use the following script to continuously read the input.
+
You can use the following script to continuously read the input and print it on the same line.
 +
bone$ '''cd'''
 +
bone$ '''nano ainOut.sh'''
 +
 
 +
cd /sys/devices/ocp.2/helper.14
 
  while [ 1 ]
 
  while [ 1 ]
 
   do  
 
   do  
   tr '\n' '\r' < AIN6
+
   tr '\n' '\r' < AIN0
 
  done
 
  done
 +
 +
bone$ '''chmod +x ainOut.sh'''
 +
bone$ '''./ainOut.sh'''
 +
Use '''Ctrl-C''' to quit ainOut.sh
  
 
== Pulse Width Modulation ==
 
== Pulse Width Modulation ==
Line 222: Line 222:
 
(Note: The pwm interface seems to changing. Some of this may not apply in the future.)
 
(Note: The pwm interface seems to changing. Some of this may not apply in the future.)
  
The Bone has a PWM interface at <code>/sys/class/pwm/</code>. You can see what's there by:
+
We have to run a couple of commands to make the PWM interface appear.  Try
 +
bone$ '''SLOTS=/sys/devices/bone_capemgr.*/slots'''
 +
bone$ '''echo am33xx_pwm    > $SLOTS'''
 +
bone$ '''echo bone_pwm_P9_21 > $SLOTS'''
 +
bone$ '''cd /sys/devices/ocp.2/pwm_test_P9_21.14'''
 +
bone$ '''ls'''
 +
driver  duty  modalias  period  polarity  power  run  subsystem  uevent
  
bone$ '''cd /sys/class/pwm'''
+
The units are in ns.
bone$ '''ls -F'''
 
export  unexport
 
Hmmm, there isn't much there. We have to run a command to make something appear.  Try
 
bone$ '''SLOTS=/sys/devices/bone_capemgr.*/slots'''
 
bone$ '''echo am33xx_pwm > $SLOTS'''
 
bone$ '''ls -F'''
 
export  pwmchip0@  pwmchip2@  pwmchip3@  pwmchip5@  pwmchip7@  unexport
 
Now we need to run another command to say which pwm pin we want to use.  I'm using P9_21.
 
bone$ '''echo bone_pwm_P9_21 > $SLOTS
 
Now you can export a pwm much list you export a gpio port
 
bone$ '''echo 1 > export'''
 
bone$ '''cd pwm1'''
 
bone$ '''ls -F'''
 
device@  duty_ns  period_ns  polarity  power/  run  subsystem@  uevent
 
 
Try a 1Hz frequency with a 25% duty cycle
 
Try a 1Hz frequency with a 25% duty cycle
  bone$ '''echo 1000000000 > period_ns'''
+
  bone$ '''echo 1000000000 > period'''
  bone$ '''echo  250000000 > duty_ns'''
+
  bone$ '''echo  250000000 > duty'''
 
  bone$ '''echo 1 > run'''
 
  bone$ '''echo 1 > run'''
  
Line 249: Line 241:
 
Combine the analog in and the PWM by having the pot control the frequency or the duty cycle of the LED.
 
Combine the analog in and the PWM by having the pot control the frequency or the duty cycle of the LED.
 
   
 
   
 +
== 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.
 +
 
{{YoderFoot}}
 
{{YoderFoot}}

Latest revision as of 11:22, 6 August 2013

thumb‎ Embedded Linux Class by Mark A. Yoder


Here are the labs for the afternoon Linux part of the ASEE 2013 Workshop

Warm Up

PuTTYconfiguration.jpg

Before we can interact with LEDs and switches we need to learn some simple Linux commands.

  1. On your host computer, running Windows, start up puTTY.
  2. If you get a Security Warning, click Run.
  3. Enter 192.168.7.2 in the Host Name field and click Open
  4. Login as root with no password.
  5. Enter ls to list what files you have. You shouldn't see much.
PuTTYloging.jpg

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 -F
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 -F
backlight/  firmware/     lcd/       mtd/           scsi_disk/   ubi/
bdi/        gpio/         leds/      net/           scsi_host/   udc/
block/      graphics/     mbox/      power_supply/  sound/       uio/
bluetooth/  hwmon/        mdio_bus/  regulator/     spi_master/  usbmon/
bsg/        i2c-adapter/  mem/       rfkill/        spidev/      vc/
dma/        i2c-dev/      misc/      rtc/           thermal/     vtconsole/
drm/        input/        mmc_host/  scsi_device/   tty/         watchdog/

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 -F
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 -F
brightness  device@  max_brightness  power/  subsystem@  trigger  uevent

(The "@" after the name means it's a link.) 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 -F
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

In the AM lab 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.

P9PWMs.jpg

Here's how you turn it on

bone$ cd /sys/class/gpio
bone$ ls -F
export  gpiochip0@  gpiochip32@  gpiochip64@  gpiochip96@  unexport

Presently no gpio pins are visible. You need to tell it which pin to export

bone$ echo 60 > export
bone$ ls -F
export  gpio60@  gpiochip0@  gpiochip32@  gpiochip64@  gpiochip96@  unexport

Notice gpio60 has appeared. All we need to do is tell it which direction and then turn it on.

bone$ cd gpio60
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. In the AM lab 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.

Analog In

The bone has eight Analog Inputs. Several are exposed on P9. They are labeled AIN in the table below. How many do you find?

P9PWMs.jpg

The AIN pins are sampled at 12 bits and 100k samples per second. The input voltage is between 0 and 1.8V. Fortunately, both voltages are available on P9.

You've already wired up P9_39 for AIN0 in the AM lab. You interact with the analog in much like the gpio, but it appears in a different place. We have to run a command before the AIN interface appears. Just run them now, later I'll explain what you did if you are interested.

bone$ SLOTS=/sys/devices/bone_capemgr.*/slots
bone$ echo cape-bone-iio > $SLOTS

You can now access the analog interface, let's explore.

bone$ cd /sys/devices/ocp.2/helper.14
bone$ ls -F
AIN0  AIN2  AIN4  AIN6  driver@   power/      uevent
AIN1  AIN3  AIN5  AIN7  modalias  subsystem@
bone$ cat AIN0
1185

Change the pot and rerun cat. What's the min and max value you get? Is it 12 bits?

You can use the following script to continuously read the input and print it on the same line.

bone$ cd
bone$ nano ainOut.sh
cd /sys/devices/ocp.2/helper.14
while [ 1 ]
  do 
  tr '\n' '\r' < AIN0
done
bone$ chmod +x ainOut.sh
bone$ ./ainOut.sh

Use Ctrl-C to quit ainOut.sh

Pulse Width Modulation

(Note: The pwm interface seems to changing. Some of this may not apply in the future.)

We have to run a couple of commands to make the PWM interface appear. Try

bone$ SLOTS=/sys/devices/bone_capemgr.*/slots
bone$ echo am33xx_pwm     > $SLOTS
bone$ echo bone_pwm_P9_21 > $SLOTS
bone$ cd /sys/devices/ocp.2/pwm_test_P9_21.14
bone$ ls
driver  duty  modalias  period  polarity  power  run  subsystem  uevent

The units are in ns. Try a 1Hz frequency with a 25% duty cycle

bone$ echo 1000000000 > period
bone$ echo  250000000 > duty
bone$ echo 1 > run

Connect the LED from and watch it flash. Try changing the frequency and duty cycle. You may have to set the duty cycle to 0 to change the frequency. Can you guess why?

Challenge

Combine the analog in and the PWM by having the pot control the frequency or the duty cycle of the LED.

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.




thumb‎ Embedded Linux Class by Mark A. Yoder