A
A
A
L
L
L
T
T
T
E
E
E
R
R
R
A
A
A
D
D
D
E
E
E
2
2
2
F
F
F
u
u
u
n
n
n
c
c
c
t
t
t
i
i
i
o
o
o
n
n
n
L
L
L
i
i
i
b
b
b
r
r
r
a
a
a
r
r
r
y
y
y
V
V
V
e
e
e
r
r
r
s
s
s
i
i
i
o
o
o
n
n
n
1
1
1
.
.
.
0
0
0
A L T E R A D E 2 D E V E L O P M E N T B O A R D
DE2 Function Library Manual
Celoxica, the Celoxica logo and Handel-C are trademarks of Celoxica Limited. Altera, Quartus are trademarks and/or service marks of Altera Corp.
All other products or services mentioned herein may be trademarks of their respective owners. Developed by CF Lai
Table of Contents
Introduction ... 1
About This Manual ... 1
Using the board support library ... 2
Installing the Library ... 2
Setting up DK... 2
Specifying header files ... 3
Setting up Quartus II ... 4
Compiling and Running the design ... 5
Reference... 6
Flash Memory Driver ... 7
Overview ... 7
Setting up the Flash Memory Driver ... 7
Reading from and Writing to Flash Memory ... 7
Reading from Flash Memory ... 8
Writing to Flash Memory... 8
SRAM... 9
Overview ... 9
Setting up the SRAM Driver ... 9
Reading from and Writing to SRAM ... 9
Reading from SRAM ... 9
Writing to SRAM... 10
SDRAM ... 11
Overview ... 11
Setting up the SDRAM Driver... 11
Reading from and Writing to SDRAM... 11
Reading from SDRAM ... 12
Writing to SDRAM ... 12
PS/2 Mouse Driver ... 13
Overview ... 13
Setting up a Mouse Driver ... 13
VGA Display Generation ... 15
Overview ... 15
VGA Video Driver ... 15
Setting up the Video Driver ... 15
LCD Display Module... 18
Overview ... 18
Writing to the LCD Module ... 18
Displaying Hexadecimal number ... 19
Useful Marco expressions ... 19
Switches... 20
Accessing the Toggle Switches... 20
Accessing the Pushbuttons Switches... 20
7-Segment Displays & LEDs ... 21
Overview ... 21
Using the 7-Segment Displays ... 21
Writing a specified bit pattern ... 21
Displaying hexadecimal numbers ... 22
Disabling a particular display ... 22
Driving the LEDs ... 23
Expansion Header ... 24
Overview ... 24
Assessing the Expansion Headers... 24
Introduction
About This Manual
The DE2 Function Library de2lib.zip, is a library of Handel-C macros and functions designs that enable you to produce Handel-C design on the DE2 board easily. The instructions on this manual are based on Quartus II and DK4.
The DE2 Library contains the following functionality: Memory
Macros to access the 8 Mbyte, 8 bit wide Flash memory. Macros to access the 512 Kbyte, 16 bit wide SRAM. Macros to access the 8 Mbyte, 16 bit wide SDRAM. PS/2 port
Generic PS/2 mouse driver Video
Driver for the Video DAC on the board for generating VGA display on a standard monitor.
LCD Display
Driver for the 16x2 LCD module for displaying ASCII characters. Switches
Driver for the 18 toggle switches and the 4 pushbutton switches. 7-Segment Displays & LEDs
Macros to display hexadecimal characters on the 7-Segment Displays and drive the LEDs on the board.
Chapter
Chapter 1: Introduction
Expansion Header
Driver for the two 40-pin expansion headers on the board.
Further information about the DE2 board can be found in the DE2 User Manual1.
Using the board support library
Installing the Library
1. Download the DE2 library de2lib.zip
2. Unzip the contents into your working directory (e.g. H:\de2project).
Setting up DK
1. Start DK (Select Start>Programs>Celoxica>DK Design Suite>DK) 2. Create a new project (Select File>New) specifying the device (chip
name), project name and location as shown below:
3. Include the de2lib library files (Select Tools>Options, under the
Directories tab add the unzipped de2lib folder, e.g. H:\de2project\de2lib and click OK).
Chapter 1: Introduction
4. Add the DE2 library (Select Project>Settings, under the Linker tab
add de2.hcl to the Object/library modules, path: de2lib\de2.hcl)
5. Set the file type for the Handel-C output files which will be used in
Quartus II. (Select Build>Set Active Configuration, highlight EDIF and click OK)
Specifying header files
You need to create a new source file for your project. (Click on and select Handel-C Source File, give it a file name of your choice) You should now see the following in your File View window.
Figure 1 – screenshot of the object window in DK after the initial setup
In the new source file (xxx.hcc) that you have just created, add the following to specify the clock rate and include the DE2 library.
1. Specify the clock rate: set clock = external "N2"; 2. Include the DE2 library: #include "DE2.hch"
The DE2 provides two fixed clocks that are connected as follows:
FUNCTION FPGA PIN
Fixed clock 27 MHz D13 Fixed clock 50 MHz N2
Example library instantiation
set clock = external "N2"; //use the 50 MHz clock #include "DE2.hch" //include the DE2 library void main(void)
{
//some code }
Chapter 1: Introduction
Your code does not do anything yet, but compile the project and check that it returns with no error and a folder EDIF is created under your working directory.
Setting up Quartus II
In the last section, you have created the EDIF file which contains the hardware descriptions of your design. This section will explain how you can synthesis this design and load it on to the DE2 board.
1. Locate the .edf file which was created by DK under the EDIF folder.
(e.g. H:\de2project\EDIF\de2project.edf)
2. Start Quartus II (Select Start>Programs>Altera>Quartus II) 3. Start a new project (Select File>New Project Wizard)
4. Select your working directory, it is recommended that you choose the
EDIF folder created by DK i.e. H:\de2project\EDIF, otherwise you will need to copy the .edf file to your working directory every time you make changes to your Handel-C project.
5. After you click next, select the .edf file and add that to your project.
Also add the de2lib folder using the User Libraries button.
6. Specify the device: Choose Family>Cyclone II and EP2C35F672C6
and leave the other settings as default. You can find the EP2C number on the FPGA chip on the DE2 board.
7. Click Finish and you should see the following under your Project
Chapter 1: Introduction
8. Specify options for synthesis (Select Assignments>Settings, under
EDA Tool Settings click on Design Entry/Synthesis). Set the options as shown in the following screenshot.
Figure 2 – Setting for Design Entry/Synthesis for a Handel-C design
9. Setting unused pins to tri-stated: (Select Assignments>Device, click
on the Device & Pin Options button and select the Unused Pins tab). Set the unused pins As inputs, tri-stated as shown in Figure 3 and click the OK button to confirm.
Figure 3 – Settings for unused pins
Compiling and Running the design
You only need to perform the steps mentioned above once which set up both DK and Quartus. The steps mentioned in this section are essential and need to be run every time you change your Handel-C design in DK.
1. As the Handel-C source code does not contain any function, replace
the void main section of your code with the following if you want to see action on the DE2 board.
Chapter 1: Introduction
void main(void) {
DE2_SW_MASK SW; //declare variable SW as type DE2_SW_MASK do
{
DE2SW(SW); //read input from the toggle switches DE2SetLEDRs(SW); //shows the switches input on LEDs
} while(1); }
2. Assign pins (Select Tools>Tcl Scripts, highlight the one under Project
e.g. de2project and click Run). This .tcl file stores the pin assignments of your design to the actual FPGA pins.
3. Start Compilation , this builds everything necessary for loading your
design on to the DE2 board.
4. Turn on the DE2 board and make sure that the RUN/PROG switch is
switched to RUN. (Please refer to the DE2 user menu for more details)
5. Program the FPGA – Click on the programmer button to invoke the
programmer dialogue box. Make sure it is connected and choose JTAG mode, add the .sof file and click on Program/Configure check box then click Start.
6. When the programming finishes, you can toggle the switches on the
DE2 board and you should see the red LEDs are set by the switch right below it.
Note
If everything compiled but the board does not function as expected, it is very likely that you forgot to run the tcl script. This script must be run before compiling the project.
Reference
DE2 Development and Education Board User Manual Handel-C Language Reference Manual
Flash Memory Driver
Overview
The DE2 board has one 8-bit wide, 1-Mbyte Flash Memory, it is ideally for storing data after powering off. To write to the Flash memory, you must erase it first. Erasing sets all the bits to 1, this is necessary because during programming, 1 can be set to 0 but 0 cannot be set to 1.
Setting up the Flash Memory Driver
In order to use the Flash memory, you must first set up the driver by:
1. Initialize the Flash Memory Driver by calling the DE2FLASHDriver()
macro.
2. Pass a pointer of type DE2FLASH to the macro as a parameter.
Example: Setting up the Flash memory Driver
set clock = external "N2"; #include "DE2.hch"
void main(void) {
DE2_FLASH FLASH; //Create a FLASH Driver structure DE2FLASHDriver(&FLASH); //Passing a pointer - type DE2_FLASH }
Reading from and Writing to Flash Memory
There are two macros for reading from and writing to the Flash memory. You must ensure that you do not read and write to the Flash memory at the same time.
Chapter
Chapter 2: Flash Memory
Reading from Flash Memory
macro proc DE2ReadFLASH(Address, Data, FLASH)
PARAMETERS DESCRIPTION TYPE
Address Contains the address of the memory location unsigned 22 Data Value at the location specify but the address unsigned 8 FLASH Pointer for the FLASH Memory Driver DE2_FLASH
Writing to Flash Memory
macro proc DE2WriteFLASH(Address, Data, FLASH)
PARAMETERS DESCRIPTION TYPE
Address Contains the address of the memory location unsigned 22 Data Value at the location specify but the address unsigned 8 FLASH Pointer for the FLASH Memory Driver DE2_FLASH
Note
It is the user responsibility to ensure that the Flash memory is not being written to and reading from at the same time. The Flash memory is 1-Mbyte in size which is enough to store a 640x480 24-bit RGB image (921600-byte), however it is not fast enough to be used for displaying the image in a pixel by pixel basis.
SRAM
Overview
The DE2 board has one 16-bit wide, 512-Kbyte SRAM, it is ideally for caching data for fast access.
Setting up the SRAM Driver
In order to use the SRAM, you must first set up the SRAM Driver by:
3. Initialize the SRAM Driver by calling the DE2SRAMDriver() macro. 4. Pass a pointer of type DE2SRAM to the macro as a parameter.
Example: Setting up the SRAM Driver
set clock = external "N2"; #include "DE2.hch"
void main(void) {
DE2_SRAM SRAM; //Create a SRAM Driver structure DE2SRAMDriver(&SRAM); //Passing a pointer of type DE2_SRAM }
Reading from and Writing to SRAM
There are in total three separate macros for reading from and writing to the SRAM. You must ensure that you use the correct one and you must not read and write to the SRAM at the same time.
Reading from SRAM
macro proc DE2ReadSRAM(Address, Data, SRAM)
Chapter
Chapter 3: SRAM
PARAMETERS DESCRIPTION TYPE
Address Contains the address of the RAM location unsigned 18 Data Value at the location specify but the address unsigned 16 SRAM Pointer for the SRAM Driver DE2_SRAM
Writing to SRAM
There are two types of macros for writing to the SRAM, the first type writes Data to all 16-bit of the SRAM:
macro proc DE2ReadSRAM(Address, Data, SRAM)
With the second type macro, individual bytes of the SRAM can be written indicated by the type DE2_SRAM_MASK.
macro proc DE2ReadMaskSRAM(Address, Data, Mask, SRAM)
PARAMETERS DESCRIPTION TYPE
Address Contains the address of the RAM location unsigned 18 Data Value at the location specify but the address unsigned 16 Mask 2-bit mask indicating which bytes should be
updated
DE2_SRAM_MASK
SRAM Pointer for the SRAM Driver DE2_SRAM
Note
It is the user responsibility to ensure that the SRAM is not being written to and reading from at the same time. The SRAM is 512-Kbyte in size which is enough to store an 800x600 8-bit grayscale image (480-Kbyte) for display, consider using the SDRAM for displaying a color image.
SDRAM
Overview
The DE2 board has one 16-bit wide, 8-Mbyte SDRAM, which can be used for frame buffer. The SDRAM is clocked at 100 MHz and this library support using the SDRAM as a SRAM.
Setting up the SDRAM Driver
In order to use the SDRAM, you must first set up the driver by:
5. Initialize the Flash Memory Driver by calling the DE2SDRAMDriver()
macro.
6. Pass a pointer of type DE2SDRAM to the macro as a parameter.
Example: Setting up the SDRAM Driver
set clock = external "N2"; #include "DE2.hch"
void main(void) {
DE2_SDRAM SDRAM; //Create a SDRAM Driver structure DE2SDRAMDriver(&SDRAM); //Passing a pointer of type DE2_SRAM }
Reading from and Writing to SDRAM
There are two macros for reading from and writing to the SDRAM. You must ensure that you do not read and write to the SDRAM at the same time.
Chapter
Chapter 4: SDRAM
Reading from SDRAM
macro proc DE2ReadSDRAM(Address, Data, SDRAM)
PARAMETERS DESCRIPTION TYPE
Address Contains the address of the memory location unsigned 22 Data Value at the location specify but the address unsigned 16 SDRAM Pointer for the SDRAM Driver DE2_SDRAM
Writing to SDRAM
macro proc DE2WriteSDRAM(Address, Data, SDRAM)
PARAMETERS DESCRIPTION TYPE
Address Contains the address of the memory location unsigned 22 Data Value at the location specify but the address unsigned 16 SDRAM Pointer for the SDRAM Driver DE2_SDRAM
Note
It is the user responsibility to ensure that the SDRAM is not being written to and reading from at the same time.
PS/2 Mouse Driver
Overview
The DE2 library contains driver for PS/2 compatible mouse which can be connected to the PS/2 port on the board. A Genius NetScroll + mouse (a three button mouse) was used to test the driver. The driver does not support the scrolling wheel on mouse device with wheels.
Setting up a Mouse Driver
To set up a mouse driver, you need to:1. Install the mouse driver by calling the macro procedure
DE2MouseDriver().
2. Pass a pointer of type DE2_PS2_MOUSE to the macro as a parameter.
The type DE2_PS2_MOUSE has the following structure:
typedef struct {
signal unsigned 10 PointerX;
signal unsigned 10 PointerY; signal unsigned 1 LeftButton; signal unsigned 1 MiddleButton; signal unsigned 1 RightButton; signal unsigned 10 MaxX; signal unsigned 10 MaxY; }DE2_PS2_MOUSE;
PARAMETERS DESCRIPTION
PointerX Current X coordinates of the mouse pointer PointerY Current X coordinates of the mouse pointer
Chapter
Chapter 5: PS/2 Mouse Driver
LeftButton State of the left button on the mouse (1 = pressed, 0 = released) MiddleButton State of the middle button on the mouse (1 = pressed, 0 = released) RightButton State of the right button on the mouse (1 = pressed, 0 = released) MaxX Maximum value of the mouse pointer on the screen in the X-direction MaxY Maximum value of the mouse pointer on the screen in the Y-direction
The default position of the mouse pointer (PointerX,PointerY) are being set to the middle of the screen on the default resolution 800 x 600 pixels. And the MaxX and MaxY are by default set to 799 and 599 respectively.
Example – Displaying a mouse pointer
set clock = external "N2"; #include "DE2.hch"
macro proc Pointer(VideoPtr, MousePtr) {
… //find the code in the file called pointer.hcc2
, this //code uses the mouse button to switch between two backgrounds } void main(void) { DE2_PS2_MOUSE Mouse; DE2_VGA_DRIVER Video; par { DE2PS2MouseDriver(&Mouse); DE2VideoDriver800x600(&Video); Pointer(&Video,&Mouse); } }
VGA Display Generation
Overview
The DE2 board can generate a VGA display on LCD / CRT monitor, the DE2 library uses the 30-bit color video DAC to generate the required signals. The DE2 library contains:
DE2VideoDriver800x600, a video driver macro Macro expressions to provide information of the display
VGA Video Driver
The DE2VideoDriver800x600 macro generates all the signals required for a monitor using the VGA 800x600 video standard, including the horizontal and vertical sync signals.
The driver output the current x and y coordinates of the scan on the screen and you must assign the desire RGB (30bits) value on a pixel by pixel basis.
The screen scan from the top left pixel, starting from (0,0) and finishes at (799,599) for the visible region. The invisible part of the scan is called the blanking region, you can find out if you are in the visible region by check the parameter Visible. The pixel clock for this driver runs at 50MHz yielding 800 horizontal pixels and 600 vertical pixels. Therefore your design must be run at 50MHz or faster for the RGB value to be loaded correctly.
Setting up the Video Driver
Steps for setting up the video driver:Call DE2VideoDriver800x600 macro procedure to install the video driver.
Chapter
Chapter 6: VGA Display Generation
Pass a pointer of type DE2_VGA_DRIVER to macro as a parameter.
The type DE2_VGA_DRIVER has the following structure:
typedef struct {
signal unsigned 30 Output; signal unsigned 10 ScanX; signal unsigned 10 ScanY; signal unsigned 1 Visible; } DE2_VGA_DRIVER;
PARAMETERS DESCRIPTION
Output The 30 bits RGB signals to be displayed. ScanX Current horizontal pixel being display. ScanY Current vertical pixel being display.
Visible Stating whether the current scans coordinate is visible.
screensaver Indicate whether the screensaver is on or off. (1 for on, 0 for off)
Useful Marco expressions
There are three macro expressions that provide information of the display that comes with the DE2 library including:
DE2VisibleCols – number of visible columns. DE2VisibleLines – number of visible lines. Example – Installing the Video Driver
set clock = external "N2"; //use the 50 MHz clock #include "DE2.hch" //include the DE2 library macro proc ColourScreen(VideoPtr)
{
unsigned 10 temp;
macro expr sx = VideoPtr->ScanX; macro expr sy = VideoPtr->ScanY;
Chapter 6: VGA Display Generation { if (VideoPtr->Visible != 0) { VideoPtr->Output = sx @ sy @ temp; } else { delay; } } while(1); } void main(void) { DE2_VGA_DRIVER Video; par { DE2VideoDriver800x600(&Video); ColourScreen(&Video); } }
This sample code (colourscreen.hcc) generate a 800x600 VGA display with colour channel Red and Green set to Current X and Y resulting in a colour change from the left of the screen to the right of the screen.
LCD Display Module
Overview
The DE2 board has a 2 x 16 digits LCD Module which can be used to display text. The DE2 library contains macro that allows you to:
Write ASCII characters to the LCD Module.
Function to convert hexadecimal number into ASCII. Macro expressions for common characters, e.g. A-Z.
Writing to the LCD Module
You can write to the 7-segment display using the macro procedure: DE2LCDDriver(line)
PARAMETERS DESCRIPTION TYPE
line Line of characters in ASCII DE2_LCD_LINE
Please refer a ASCII table for the ASCII code for characters, the characters which are supported include:
!"#$%&'()*+,-./0123456789:;<=>? @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ `abcdefghijklmnopqrstuvwxyz{|}~
Please note that you do not need to find the code for characters A-Z, a-z and space as the library contains macro expressions for these characters.
Chapter
Chapter 7: LCD Display Module
Displaying Hexadecimal number
The DE2 library contains a macro expr for converting Hex number into ASCII: macro expr hex2ascii(hex)
PARAMETERS DESCRIPTION TYPE
hex A hexadecimal for converting into ASCII. unsigned 4
This function returns the ASCII code for the hexadecimal number that is inputted.
Useful Marco expressions
The DE2 library contains a few macro expressions that are useful for displaying text on the LCD module, these include:
Characters A – Z and a – z sp for Space
blank_line – for displaying a blank screen on the LCD module Example – displaying a line of text on the LCD module
set clock = external "N2"; //use the 50 MHz clock #include "DE2.hch" //include the DE2 library void main(void) { DE2_LCD_LINE line; line = hex2ascii(0x1<-4) @ sp @ H @ e @ l @ l @ o @ sp @ w @ o @ r @ l @ d @ blank_line<-152; DE2LCDDriver(line); }
Switches
Accessing the Toggle Switches
You can obtain inputs from the 18 toggle switches with the macro procedure: DE2SW(mask)
PARAMETERS DESCRIPTION TYPE
mask Each bit corresponds to each toggle switches. unsigned 18
For example, if only SW0 is set to low (closest to the edge of the board), variable SW will return the following bit pattern: 111111111111111110 when you call DE2SW(SW).
Accessing the Pushbuttons Switches
There are 4 pushbuttons switches on the DE2 board marked as KEY0 to KEY3. Each switch provides a high logic level “1” when it is not pressed, and provides a low logic level “0” when depressed.
You can get the status of these pushbuttons switches with macro procedure: DE2Key(mask)
PARAMETERS DESCRIPTION TYPE
mask Each bit corresponds to each pushbutton switches. unsigned 4
For example, if only KEY3 is pressed, variable KEY will return the bit pattern 0111 when DE2Key(mask) is being called.
Chapter
7-Segment Displays & LEDs
Overview
The DE2 board has eight 7-segment displays, eighteen red LEDs and nine green LEDs. The DE2 library contains macros that allow you to:
Write to the 7-segment displays.
Display hexadecimal numbers on the 7-segment displays. Access eighteen red LEDs and nine green LEDs on the board.
Using the 7-Segment Displays
To use a 7-segment display, you can use macros to: Write a specified bit pattern to it.
Write hexadecimal digits to it.
Writing a specified bit pattern
You can write to the 7-segment display using the macro procedure: DE2Set7Seg(SegN, Pattern)
PARAMETERS DESCRIPTION TYPE
SegN The display number (integer 0-8). unsigned 3 Pattern The pattern to be displayed on the display. unsigned 7
For the map of the bits pattern, please refer to Figure 4:
Chapter
Chapter 9: 7-Segment Displays & LEDs
Figure 4 – position and index map of the 7-segment displays
The library also include a pre-define type DE2_7SEG for the pattern of the displays which is useful if you are writing a pattern to the displays.
Displaying hexadecimal numbers
The library also supports display of hexadecimal numbers, you can use the following macro procedure to display a hexadecimal number on the displays.
DE2Set7SegDigit(SegN, hexdigit)
PARAMETERS DESCRIPTION TYPE
SegN The display number (integer 0-8). unsigned 3 hexdigit The hexadecimal number that you want to display. unsigned 4
Disabling a particular display
It is likely that you do not need to use all display and would like disable some of them, you may do this by calling the following macro procedure:
DE2Disable7Seg(SegN)
PARAMETERS DESCRIPTION TYPE
SegN The display number (integer 0-8). unsigned 3
Note
The display numbers are marked on the board from HEX0 to HEX7. You must not write and display a hex number on the same display at the
Chapter 9: 7-Segment Displays & LEDs
Example – displaying numbers on the 7-Segment displays3
set clock = external "N2"; //use the 50 MHz clock #include "DE2.hch" //include the DE2 library void main(void)
{ do {
par //run in parallel {
DE2Set7SegDigit(0,0x1); //display digit “1” on HEX0 DE2Set7SegDigit(1,0x2); //display digit “2” on HEX1 DE2Set7SegDigit(2,0x3); //display digit “3” on HEX2 DE2Disable7Seg(3); //disable display HEX3 }
} while(1); }
Driving the LEDs
The green and red LEDs on the DE2 board can be driven using different macro procedures described below:
macro proc DE2SetLEDRs(mask)
PARAMETERS DESCRIPTION TYPE
mask Each bit corresponds to each red LEDs. unsigned 18
macro proc DE2SetLEDGs(mask)
PARAMETERS DESCRIPTION TYPE
mask Each bit corresponds to each green LEDs. unsigned 9
The bits of the mask corresponds directly to the LEDs, e.g. to turning on only LEDG6, call DE2SetLEDGs(001000000).
Expansion Header
Overview
The DE2 board has two 40-pin expansion headers / GPIO (General Purpose I/O). The DE2 Library includes macros that allow you to
Write to or read from the expansion headers
Assessing the Expansion Headers
You can access the expansion headers using the macro procedure: DE2GPIO_0(PinN,IO,Data)
DE2GPIO_1(PinN,IO,Data)
PARAMETERS DESCRIPTION TYPE
PinN Pin number of the expansion header (0-40) unsigned 6 IO Set the particular pin to input or output taking value
1 or 0. 1 for input and 0 for output.
unsigned 1
Data Data to be written to or read from the particular pin unsigned 1
For example, to read a value from pin 0 on the expansion header GPIO_0 (JP1), you would call DE2GPIO_0(0,1,data).