Difference between revisions of "Minnowboard:GPIO Programming"

From eLinux.org
Jump to: navigation, search
m (Changed the figure numbering)
m (Added back home button)
 
Line 1: Line 1:
[[File:Mlogo.png|right|295px|Minnow]]
+
{{Template:Minnowboard_home}}
 +
 
 +
[[File:Mlogo.png|right|180px|Minnow]]
 +
 
 
=Summary=
 
=Summary=
  

Latest revision as of 19:28, 11 August 2013

Mlogo.png Back to the MinnowBoard home page


Minnow

Summary

In this guide, I will describe how to do GPIO Programming on the MinnowBoard step by step. This guide is for new users who are just getting started with the MinnowBoard.

Getting Started

For information on 'Setting up a microSD card', 'Booting Angstrom' etc, please refer here

GPIO on the MinnowBoard

The MinnowBoard provides a variety of GPIO(General Purpose Input Output), some with a dedicated purpose. For the purpose of this guide, I will be concentrating only on GPIO(s) which are on the J9 expansion header as shown in Figure - 1 . Please refer the table below to find out how are the GPIO(s) actually referenced in the kernel and their default modes as well as values. Unless otherwise stated the GPIO(s) on J9 expansion header are rated at 3.3V and can source/sink a maximum of 10mA. Also, it can be seen from the table that the GPIO(s) are by default set to be used in INPUT mode and have PULL-UP resistors enabled.

GPIO on the MinnowBoard
Sr. No. GPIO Reference Number in the kernel* Default Mode Default Value
1 1 N.A. N.A. 3.3V
2 2 N.A. N.A. GND
3 3 244 INPUT HIGH
4 4 245 INPUT HIGH
5 5 246 INPUT HIGH
6 6 247 INPUT HIGH
7 7 248 INPUT HIGH
8 8 249 INPUT HIGH
9 9 250 INPUT HIGH
10 10 251 INPUT HIGH
*For example GPIO-3 on J9 header will be referenced as gpio-244 in kernel


Figure-1: GPIO on the MinnowBoard


As you can see from Figure -1, there are a total of ten pins on the J9 header out of which 8 pins can be used as GPIO. The remaining two pins are GND and 3.3V as mentioned in the table.

The GPIO(s) are accessible via the user space in Linux at the location below on the filesystem:

/sys/class/gpio

There is one directory per GPIO, named as shown below(as an example only two GPIO(s) are shown here):

/sys/class/gpio/gpio245

Inside each one of those directories, there are files named "direction” and “value” as shown in Figure - 2. The former is for configuring the mode of GPIO as input(“in”) or output(“out”) while the latter is for the value('1' for HIGH and '0' for LOW) if used in output mode.


Figure-2: GPIO attributes


Selecting the correct LED

• To make sure that you do not damage the GPIO pins on the MinnowBoard, please use a LED whose rating should not exceed 3.3V/10mA:

http://www.digikey.com/product-detail/en/HLMP-4700-C0002/516-2483-2-ND/1234840

• If you have an LED that is higher than the above mentioned ratings, please refer the refer the link below to select an appropriate current limiting resistor:

http://www.cmiyc.com/tutorials/led-basics

Extra Credit

This is an optional section which you can read for further understanding. In a nutshell, we are trying to access the on board USER LED(s) via userspace in Linux. To be more precise we are using the sysfs interface. sysfs is a virtual filesystem which translates the hardware devices and busses attached to the system(board in our case) into a file system hierarchy that can be accessed from userspace. sysfs is generated by the kernel and always mounted at /sys. As discussed earlier, trigger for D11 and D12 LEDs has to be changed to make them function as USER LEDs. I would describe 'trigger' as the API used to link a LED to an 'event' in kernel space. Here, 'event' could be microSD card or Ethernet activity, heartbeat, power etc. To understand this better, take LED D11 for example. The trigger for D11 has been set by default to heartbeat. So, to use it as an USER LED, we have to change the trigger for it to 'none' as shown in Figure - 3 below:


Figure-3: Change LED Trigger


Setup

Please refer the Figure - 4 below to see the connections:


Figure-4: Connection Diagram


Only the pins on the J9 expansion header are used in this case. Connect the anode of the LED to the resistor(I am using a 330 ohms resistor) which in turn is connected to pin 3(GPIO) and connect the cathode of the LED to pin 2(GND) . You can purchase the male-female type hookup/jumper wires used for connections from the link below:

https://www.sparkfun.com/products/9140


Steps

• Now let us toggle that LED!

Step-1:

GPIO - 3 is referenced as 244 in the kernel. Since, it is set to be used in INPUT mode by default, we need to change its mode to OUTPUT. Type the following command in your terminal to accomplish this:

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

Step-2:

Now we are ready to toggle the LED. Type the following commands in your terminal as shown in Figure - 5(First one is for turning ON and latter for OFF):

echo 1 > /sys/class/gpio/gpio244/value
echo 0 > /sys/class/gpio/gpio244/value


Figure-5: Toggle the LED


Output

You should get an output as shown below:


Figure-6: Led ON


Figure-7: Led OFF