Computer Laboratory

ECAD and Architecture Practical Classes

Exercise 2 - Interactive Counter

Behaviour

Given a decimal integer in the range [0..99] input via the terminal, make the red LEDs count in binary from 0 up to that number and then stop. The direction of counting can be changed using two buttons (stopping at 0 if counting down); a third button stops counting and requests an alternative input.

Implementation Advice

The following code is a partially complete solution. The main function reads and validates a number from the terminal; this is then assigned to num. The direction of counting is stored by dir. The procedure usleep, in the unistd.h library, suspends the thread for the specified number of microseconds. The procedure atoi, in the stdlib.h library, converts a numeric string to an integer.

In the BSP project, open system.h. Scrolling down, each block of definitions represents a component in your Qsys SoC. In particular, there should be two blocks with the first definition ending with altera_avalon_pio. We are particularly interested in the second definition for PIO_X_BASE, which gives the base address for the memory-mapped PIO, X (where X is 0 or 1). Add #include "system.h" to the list of imports at the top of the C program.

In the BSP project, open drivers | inc | altera_avalon_pio_regs.h and pay particular attention to the functions IORD_ALTERA_AVALON_PIO_DATA and IOWR_ALTERA_AVALON_PIO_DATA. Add #include "altera_avalon_pio_regs.h" to the list of imports.

Discussion

Recall that a PIO is basically a register where data may be read or written, both by hardware (e.g. signals from the buttons) and by software (using the procedures above).

For this lab, the signals from three buttons are combined as a 3-bit value. This value is then read to determine which button (or buttons) are currently being pressed. This paradigm is known as polling. A more efficient implementation would modify the Qsys architecture to enable interrupts from the button PIO. Unfortunately, the code required to interact with the interrupt driver is too complex for this introductory lab.

Previous  |  Contents  |  Next