Assignment 3: Basic GPIO and Interrupts Solution




This assignment was originally created by Dr. Leonard Manzara for CPSC 359, Winter 2019.

In this assignment you will begin to use the memory-mapped input/output (I/O) features of the BCM2837 system on chip that is at the heart of the Raspberry Pi. You will also use interrupts so that your program responds efficiently to changes in inputs.


Your goal is to write a program, in C, that illuminates three LEDs in a given pattern (order and duration). The LED pattern should change, as specified, in response to changes in the input.

Table 1 lists the Raspberry Pi I/O pins that you will use. These are the pins corresponding to LEDs 1, 2, and 3, and inputs 1 and 2 on the ExplorerHat Pro boards in the lab. Note that all of the ExplorerHat inputs are pulled low.

GPIO pin

Input or Output













Input 1 (pulled low)



Input 2 (pulled low)

Table 1: GPIO pin usage for assignment 3.

There are two LED patterns that your program will produce. These are:

  1. The LEDs illuminate sequentially in the order: 1, 2, 3, 1, 2, 3, and so on. Each will be on for 0:5s, then off for

a delay of 0:5s.

  1. The LEDs illuminate sequentially in the order : 3, 2, 1, 3, 2, 1, and so on. Each will be on for 0:25s, then off for

a delay of 0:25s. I.e., the reverse order of the first pattern, and faster.

When your program starts, it should illuminate the LEDs in the first pattern. Upon receiving an interrupt from Switch A, the LEDs should change to the second illumination pattern. Similarly, when your program receives an interrupt from Switch B the LEDs should change to the first pattern. Although it would be possible to do this by polling, your program must use interrupts on the the two inputs to achieve the specified behaviour.

Your program must be written in the C language, with the exception of the provided assembly code that initializes the Pi and sets up the IRQ vector.


You may use code from examples provided in the course material through D2L – but be sure to note in your source code comments where the code came from.

You should use the system timer counter register to time your delays. See the Broadcom ARM Peripherals manual, p 172. It is not more efficient than than using delay loops, but it does expose you to another, easy-to-use capability of the Raspberry Pi.

As stated above, use the C language because: it will give you more practice with an important programming lan-guage, and it is much easier to work with then assembly language. In fact, this kind of low-level machine programming is precisely what C does best. See

As with any programming project, I recommend that you approach this incrementally. Start by implementing a simple subset of the required functionality, then add the remaining functionality one piece at a time. I suggest doing the interrupts last.

Debugging is hard to do in this bare metal environment. Doing incremental changes will help you to zero on the source of errors. Also, UART output may help to find some bugs, so set that up.

Good coding practices include all the good habits you should exhibit when writing your code. These include, but are not limited to:


informative variable and function names, helpful comments,

the presence additional, helpful files such as readme.txt and Makefile, and code modularity.


Turn in the following items for evaluation.

  1. All files required to build your program. This includes all of your source code files, Makefile, and a readme.txt file with instructions.

  1. A demo of your working program for your TA during a tutorial session – date to be determined by your TA.

Bonus (10%)

Use the system timer peripheral or the timer (ARM side) to trigger interrupts, to create any delays needed in your program. In other words, replace your busy loop with code that uses one of the hardware timers to delay the appropriate amount of time.

Note that this is not the same as implementing a delay loop by polling the system timer. You must specifically have interrupts triggered by a timer.


  • Program runs as specified

LED Pattern 1


LED Pattern 2


Response to buttons



Configuration of I/O pins



Configuration of interrupts



Coding practices



Timer interrupts (bonus)




There is no team work. Each students must submit individual solutions.

Late work

After the deadline and up to 24hrs late: -5pts. After 24hrs and up to 48hrs late: -10pts. Over 48hrs late: -20pts, i.e., no assignment will be accepted beyond 48hrs after the deadline.


Work must me the sole work of the individual submitting the work.

You may find that the this assignment is similar to other published code. Nevertheless, you should go through the process yourself, and submit your own work. If you do borrow from other sources, cite the source and clearly indicate what you have borrowed, keeping in mind the design must be substantially your own. If you cite your sources, worst case you may receive a reduced grade for borrowing too much. If you borrow, but do not cite, that is plagiarism and academic misconduct, and carries severe penalties as determined by the Faculty of Science. You may use code provided in the course content on D2L, but you must still cite the source.

As a guideline, consider the 20-minute rule. Talk with your colleagues and consult other sources (cite them please). Wait at least 20 minutes, then do your work to be sure that it is your own. Less then 20 minutes usually means that you are merely copying work from the original source.