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 - the red LEDs count in binary from 0 to that number. The direction of counting can be changed using two buttons; a third button stops counting and requests an alternative input.

Implementation Advice

The following code is a partially complete answer. The main procedure reads and validates a number from a line of terminal input; 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 device in your Qsys architecture. 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. Add #import "system.h" to the list of imports.

In the BSP project, open drivers | inc | altera_avalon_pio_regs.h and pay particular attention to IORD_ALTERA_AVALON_PIO_DATA and IOWR_ALTERA_AVALON_PIO_DATA. Add #import "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 tutorial.

Previous  |  Contents  |  Next