Sunday, March 23, 2014

#2: Doing Something with the Beaglebone Black

Something Useful: Retroshare

I actually want to run Streamtuner and Retroshare on the BBB.   More about Retroshare here:

The BBB will make a nice static DHT reference for my small circle of friends and devices on Retroshare, to ensure that all can always connect no matter how much anyone or anything moves around.

Expand the SD Linux File System

If you followed my first BBB post, then you have probably seen this already:
grep: write error: No space left on device

It looks like the first thing to do, is expand the root partition to fill the SD card, otherwise there is no room to do anything :(

On you computer, use screen to connect to the serial console over a FTDI USB serial cable:
$ screen /dev/ttyUSB0 115200

On a Mac, screen works the same, but the device is something like /dev/tty.usbserial-AH0142BF

So, what was that about Macs having sensible device names? Sigh...

Then power the BBB up using the USB cable, or a 5V PSU.

Login and set user to root:
Login: username
$ su -

See what is there:
# fdisk /dev/mmcblk0
Disk /dev/mmcblk0: 7.5 GiB, 8010072064 bytes, 15644672 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00036e16

Device         Boot     Start       End  Blocks  Id System
/dev/mmcblk0p1 *         1953     41015   19531+  c W95 FAT32 (LBA)
/dev/mmcblk0p2          41016    291015  125000   c W95 FAT32 (LBA)
/dev/mmcblk0p3         291016  15644671 7676828  83 Linux

The above tells me that the SD card is 8GB and there are 3 partitions.  The 3rd one is the Linux partition that we got to fix.  Note that the Linux partition fills the whole device, meaning that the partition is OK, but the file system doesn't fill the whole partition.  So it is only a file system problem, not a partition problem.

Grow the File System

Grow the filesystem over the rest of the partition:
$ su -
# resize2fs /dev/mmcblk0p3

La voila!

Well, assuming that it still works, otherwise you are back at square one and need to rewrite the SD card again, as per my other BBB post...

Make a SD Backup

At this point, it may be a smart idea to make a backup of the whole SD card, so that next time you have to rewrite the stupid thing, you don't need to do the setup steps again.  So shutdown now, power off and plug the SD card into a laptop computer then use dd or cat with gzip to make a copy of the SD card:
# cat /dev/mmcblk0 | gzip -9 > bbb-f20-1.gz 

or on a Mac, which will likely do a partial automount, you got to unmount it first:
$ sudo umount /dev/disk1s1
$ sudo cat /dev/disk1 | gzip -9 > bbb-f20-1.gz

Why does Macs have sensible device names while Linux is such a mess lately?

Since at this time the SD image is still mostly zeroes, the compressed image will be manageable, but it will take a looong taaaime to do, so go and get a tall, cold one...

Accessing the BBB Hardware

The serial ports are available in /dev/ttyox (that 'o' really is a letter, not a zero), while the GPIO ports are available in /sys/class/gpio:
$ ls /dev/ttyo* 
$ ls /sys/class
ata_device  graphics     mem            ptp          scsi_generic  vc
ata_link    hidraw       misc           pwm          scsi_host     vtconsole
ata_port    hwmon        mmc_host       raw          sound         watchdog
bdi         i2c-adapter  net            regulator    spi_master
block       ieee80211    pci_bus        rfkill       thermal
bsg         input        pcmcia_socket  rtc          tty
dma         leds         power_supply   scsi_device  udc
gpio        mdio_bus     pps            scsi_disk    usbmon

$ ls -al /sys/class/gpio
total 0
drwxr-xr-x.  2 root root    0 Jan  1  2000 .
drwxr-xr-x. 45 root root    0 Jan  1  2000 ..
--w-------.  1 root root 4096 Jan  1  2000 export
lrwxrwxrwx.  1 root root    0 Jan  1  2000 gpiochip0 -> ../../devices/virtual/gpio/gpiochip0
lrwxrwxrwx.  1 root root    0 Jan  1  2000 gpiochip32 -> ../../devices/virtual/gpio/gpiochip32
lrwxrwxrwx.  1 root root    0 Jan  1  2000 gpiochip64 -> ../../devices/virtual/gpio/gpiochip64
lrwxrwxrwx.  1 root root    0 Jan  1  2000 gpiochip96 -> ../../devices/virtual/gpio/gpiochip96
--w-------.  1 root root 4096 Jan  1  2000 unexport

Feel free to poke around, don't be shy...

Hardware Information:
Using GPIOs on Linux:
It is relatively straight forward to access a GPIO from a script, using simple echo statements, but one has to configure them before one can use them.

See table 10 on page 70 for the P8 header and table 11 on page 72 for the P9 header in the reference manual

For example, P9 pin 15 is gpio[16] and is on gpiochip32, which has an offset of 32, so pin 15 is 16+32 = gpio48.

Two P9 header GPIO pin number mapping to kernel names:
  • pin 15 = gpio48
  • pin 16 = gpio51
To enable both pins:
# echo 48 > /sys/class/gpio/export
# echo 51 > /sys/class/gpio/export 

Now, there should be two new subdirectories in /sys/class/gpio representing these two pins and one can set the direction and a value for pin 15 and watch it toggle with an oscilloscope:
# echo out > /sys/class/gpio/gpio48/direction
# echo 0 > /sys/class/gpio/gpio48/value
# echo 1 > /sys/class/gpio/gpio48/value

Note that the BBB uses 3.3V logic and a pin can drive up to 5mA, so be careful with what you connect to it so you don't blow the pin drivers up.

You could use something like this opto-coupled solid state relay to control things with only 0.5mA per pin: 

A 3.3V supply minus 1.5V forward diode voltage, divide by 0.5mA, means using either a 270 or 330 Ohm series resistor on this type of opto-coupler.

La voila!

No comments:

Post a Comment

On topic comments are welcome. Junk will be deleted.