Saturday 27 April 2013

HiPi Perl Modules and Interrupts

HiPi::Perl modules version 0.32 have been released containing modules that support GPIO interrupt handling in Perl.

Edit: These interrupt modules were removed from HiPi in release 0.66

Threads are created to handle interrupt response and pin value polling. True interrupt handling is implemented via the kernel sysfs gpio filespace and HiPi::Device::GPIO. Direct polling of the bcm2835 registers is implemented using the HiPi:: BCM2835 module.

Documentation for the interrupt handling modules is available in the online application help pages.

There are also three example scripts showing usage of the interrupt modules:

  • Derived Class Example shows handling interrupts by implementing your own derived class and overriding the base class methods.
  • Callback Example shows handling interrupts by  registering callbacks with the base class.
  • Mixed Handler Example shows handling interrupts for pins managed by HiPi::Device::GPIO and HiPi::BCM2835 with the same process.

As with all releases, an existing installation can be upgraded by calling:

hipi-upgrade

Full installation instructions are available on the install page.

Tuesday 9 April 2013

Second I2C Bus

Warning - note that on all recent versions of Raspbian the second I2C bus pins are used by default for several functions so you may not want to get access to it in the manner described here.

HiPi Perl modules version 0.27 has been released including tested support for using a second I2C bus on Model B Revision 2 and Model A boards. The modules also install a utility command that you can use from any scripting language or the command line to enable this second I2C bus. (Install or Upgrade Instructions).

To get access to this second I2C bus you need to solder some pins or a connector to the P5 header on your Raspberry Pi.



The eight through holes for the P5 pad are next to the GPIO Pad 1 pins but are offset slightly because they are designed for a connector to be attached on the reverse side of the board. There isn't actually room for a connector on the same side as the GPIO Pad 1 pins.  However, as demonstrated elsewhere in various blog posts, it is possible to solder pins at a slight angle so that there is room for a ribbon connector on the standard GPIO Pad 1 pins and individual connectors on each of the PAD 5 pins. I found Alex Eames' article and video at RasPi TV helped me make up my mind how to approach this.

The layout table for the P5 pins is:

Wiring GPIO Name Raspberry Pi Name GPIO Wiring
-------- ------ ---------- ---- ---- ---------- ------ --------
- - 5V0 1 2 3V3 - -
17 28 GPIO_28 3 4 GPIO_29 29 18
19 30 GPIO_30 5 6 GPIO_31 31 20
- - GND 7 8 GND - -

You can see from the picture above  that I have soldered pins next to the GPIO 1 Pad rather than in the manner the P5 header was designed for, on the reverse of the board. If you do this too, remember that the pins are effectively the 'wrong way around' from left to right as shown in the picture on the left.

At the raw level, the way to enable the second i2c bus is to set the function of GPIO pins 28 and 29 to ALT0 and then activate internal pull up resistors on each pin. However, there is one slight caveat. By default and during load of the i2c kernel device driver, the Broadcom I2C0 peripheral is assigned to pins on the S5 connector of the Raspberry Pi by setting the ALT0 function on GPIO Pins 0 and 1. You may note that on Model B revision 1 boards GPIO Pins 0 and 1 were broken out on the main GPIO Header Pad 1 for use as the default i2c pins used by /dev/i2c-0. The new arrangement on Model B Revision 2 and Model A boards is to use the I2C0 peripheral to support the Raspsberry Pi Camera Module that will plug into the connector on S5 and to break out peripheral I2C1 as the default i2c bus on the GPIO Pad 1 header. To use the I2C0 peripheral on the pins 28 and 29 broken out by P5 you must first remove the existing assignment to pins 0 and 1 on S5. This is achieved by setting pins 0 and 1 to simple inputs before assigning pins 28 and 29 to ALT0. Of course this means you can have either S5 supporting a Raspberry Pi Camera or P5 supporting a second i2c bus, but not both at the same time. You can switch back and forth at any time.

The HiPi modules give you a number of ways to set up your i2c bus which conveniently wrap the necessary settings for S5, P5 and P1 where appropriate. You can set up the second I2C0 bus directly in code with:

use HiPi::BCM2835;
my $dev = HiPi::BCM2835->new;
$dev->set_I2C0(1);

Similarly you can disable the I2C0 bus and restore its operation on the S5 camera connector with:

$dev->set_I2C0(0);

If you are running as an unprivileged user or you want to make a command line call or a call for some other language you can enable the I2C0 bus using the hipi-i2c utility:

hipi-i2c e 0 1
   or to disable
hipi-i2c e 0 0

This may prove useful if you want to access the I2C0 bus using the standard kernel i2c device drivers. After the kernel module is loaded you can call "hipi-i2c e 0 1" to make the I2C0 bus available on the P5 header pins. Finally, you can also use the HiPi Control GUI application to enable and disable the I2C0 bus on the P5 header manually.

Soldering Is Easy?

It is worth making a few points about the actual soldering for any reader new to this. I have 50 year old eyes and hands and use soldering equipment that cost lest than the Raspberry Pi itself. I am a novice in it its usage. I would count myself as reasonably adept at manual tasks. Where precision is required I find I've naturally developed techniques to compensate for the declining precision of my fingertip touch and movement. So I thought I'd report on just how easy soldering actually is in general and for connecting to the P5 pad in particular.

In general, many breakout boards sold by electronics hobby suppliers come with pre-tinned pads and are simplicity itself to solder. Just wave the soldering iron vaguely in the right place and solder flows just where it is meant to. Perfect volcanoes every time. (Bizarrely although I have no prior experience of actually soldering something, I do have a pretty good idea of what a good connection should look like.)

I found soldering pins to the Raspberry Pi P5 header a different experience altogether. I'm fully aware that the cause of my problem was cheap / old soldering tips - but I reckon that is what a huge number of Raspberry Pi owners will end up using - initially at least. Given the proximity of other pin-outs and surface mounted components I found it all rather difficult to get right. However, it turns out that removing quantities of misplaced solder is not so difficult and the Pi is quite robust given any reasonable level of care with the soldering iron. I only ever achieved the 'perfect volcano' shape on pin 1 which I would guess is down to the much greater surface area of the square pin 1 pad but visual inspection under the eyeglass told me that the other seven connections were probably good enough for my purposes and a check with the multimeter confirmed that continuity was where continuity should be.

I decided that overall it is a bit pointless having a Raspberry Pi unless you are willing to take a soldering iron to it and encourage others to do the same. Be aware of what might happen when you grab the bare uncased Pi with your sweaty hand dressed in your polyester and nylon fleecy. Then grab it anyway. Earth yourself first if you're not adventurous. I can assure that the current financial austerity in place at chez Wallasey would not allow the automatic replacement of a fried Pi. But you have to do stuff if you are going to learn and that's the reason for me having a couple of Raspberry Pi's in the first place.

Raspberry Pi - concept priceless, implementation $25. Get soldering and have fun.