Assign GPIO as ACT-led

From eLinux.org
Jump to: navigation, search

This is a guide how to assign one of the onboard GPIO:s as activity LED.

What do you need?

  • Raspberry Pi model B
  • SD-card with the latest version of Raspbian
  • Internet access on your Raspberry Pi


I prefer to use Putty to control your Raspberry Pi since you dont need a separate display to your Raspberry Pi, but it works configuring on your Raspberry Pi directly.

You can download Putty here.

Preparations

Prepare your RPi

Plug in and boot your RPi. If you are using Putty for control, make sure you have the correct IP-address to your RPi and that you have the network cable from your RPi plugged into the network. Start up Putty and connect. Don't forget to log in.


To prevent error-messages about time and date, make sure you set up the time and date on your RPi. Time will be set to the date your version of Raspbian was released. To set up time and date, use the following code:

sudo date --set [Date, for example 2013-12-25]
sudo date --set [Time, for example 12:15:00]

Make sure time and date have been changed by running:

date

To make it easier following this guide and to gather all materials in the same directory, make a new directory calles something like "kernel" in /home/pi/:

mkdir kernel

Get Kernel Source

The kernel source should be downloaded from the RPI linux section on GitHub. Although you could just compile the vanilla kernel from Kernel.org, it will not have the necessary drivers and modules for the Broadcom SoC on the RPi. You can however apply patches from the vanilla kernel to the RPi one - be prepared for potential compiler grumbles though!

At the time of writing, three branches of interest are available:

  • rpi-3.2.27 - This is the version of the kernel currently used in Raspbian, but not exactly the same - Raspbian stock kernel image (the one available from the foundation's website) has a 3.2.27+ version marking. Please see this post for more details.
  • rpi-3.6-y - This is a development branch based on the current vanilla kernel( GIT rebased ) . It replaced the 3.2 development branch end marc 2013.

At the time of writing, the exact version is 3.6.11.

  • rpi-3.8-y - This is a Alpha development branch based on the current vanilla kernel( GIT rebased) . It will eventually replace the 3.6 branch.

At the time of writing, the exact version is 3.8.8 ( 22-04-2013 ).

If you are using the kernel directory in /home/pi/kernel/, move to the kernel directory before you download.

You can download the source directly using git. For the 3.2 branch:

git init
git clone --depth 1 git://github.com/raspberrypi/linux.git
git checkout rpi-3.2.27

and for the 3.6 Stable Code branch:

git init
git fetch git://github.com/raspberrypi/linux.git rpi-3.6.y:refs/remotes/origin/rpi-3.6.y
git checkout rpi-3.6.y

and for the 3.8 Development branch:

git init
git fetch git://github.com/raspberrypi/linux.git rpi-3.8.y:refs/remotes/origin/rpi-3.8.y
git checkout rpi-3.8.y

Or you can download a tarball from the website using these links: rpi-3.2.27 rpi-3.6.yrpi-3.8.y[1]

Get a Compiler

If you are compiling directly on Rpi:

apt-get update
apt-get -y dist-upgrade
apt-get -y install gcc make bc

If you are cross-compiling on another computer, use this.

Assign LED to GPIO

Open bcm2708.c with this command: sudo nano /home/pi/kernel/arch/arm/mach-bcm2708/bcm2708.c

Find static struct gpio_led and change following:

static struct gpio_led bcm2708_leds[] = {
   [0] = {
    	.gpio = 16,
    	.name = "led0",
    	.default_trigger = "mmc0",
    	.active_low = 1,
    	},
};

to

static struct gpio_led bcm2708_leds [] = {
   [0] = {
        .gpio = 16,
        .name = "led0",
        .default_trigger = "mmc0",
        .active_low = 1,
        },
   [1] = {
   	.gpio = [The number of the GPIO you want to assign, example 17], 
   	.name = "led1",
   	.default_trigger = "mmc0",
   	.active_low = 2,
   	},
};

Save (ctrl + O -> Enter)

Exit (ctrl + X)

Perform compilation

Firstly, ensure your build directory is clean:

make mrproper

Next, in all cases, you will want to get a working kernel configuration to start from. You can get the one running on the RPi by typing the following (on the RPi):

zcat /proc/config.gz > .config

then copy .config into your build directory.

Ensure that your configuration file is up-to-date:

make oldconfig

If any configuration options have been added, you will be asked what set each option to. If you don't know the answer, just press enter to accept the default.


To make sure that loading modules is enabled by running make menuconfig. If the option Enable loadable module support is checked you are good to go and can just exit. If not, check the option and exit.


Now you are ready to build:

make


The modules will be build with the following command.

make modules


Transfer Build

First you need to make a backup copy of your original kernel image. Got to /boot/ and run the following command to copy kernel.img to /home/pi/.

sudo mv kernel.img /home/pi/

Now copy the new kernel image into Boot directory

sudo cp /home/pi/kernel/arch/arm/boot/Image /boot/kernel.img

Go back to your build directory, /home/pi/kernel/, and run the following command:

make modules_install

Update firmware

In most cases this will not be needed, but it will not harm anything to do it so I recommend updating if you're not sure. To update, simply run the following command:

sudo rpi-update


After update, reboot your RPi.

Wiring

Before you can see if your new kernel is working you have to wire up the led you want. The picture to the right shows how to wire a LED to GPIO 17 and Ground.

Wiring RPi

References