Read DIP switches from a Microblaze application


In the previous tutorial on using the SDK, we exported our base project from EDK to SDK and then we ran a simple software application that printed “Hello World” in the terminal window. In this tutorial, we will do something more useful than saying hello, instead we will illustrate the concept of communicating with a peripheral from C code running on the Microblaze. More specifically, we will read the DIP switch settings and display them on the terminal screen using printfs. Our C code will poll the switches constantly so that any change will be reflected in the terminal window.

Requirements

To perform this tutorial, you will need:

  • Xilinx ISE Design Suite 13.1
  • ML50x or XUPV5 development board
  • Serial to USB converter

You will also need to go through the previous tutorial about using SDK. You can continue here once you have the base system EDK project exported to SDK and a workspace already defined.

Start SDK

  1. Open SDK by selecting “Xilinx ISE Design Suite 13.1->EDK->Xilinx Software Development Kit”.
  2. The first thing you will be asked by SDK is what workspace to open. Select C:\ML509\Projects\base-system-v13-1\sdk for your SDK workspace and click OK.
  3. When SDK opens you should see the hello_world C project in the Project Explorer. In SDK you can have multiple software projects (or applications) in the one workspace. In other words, one workspace can be used to manage several software applications for running on the same hardware platform. We want to create a new software application and write the code for it. Select “File->New->Xilinx C project”.
  4. The wizard that follows will allow us to create a template software application for our project. The default is the “hello world” example that we used in the previous tutorial. For this tutorial we want to use the “Empty Application” template, because we want to write our own code. Select “Empty Application” and click Next to accept the defaults as shown in the image below.
  5. Click Finish to accept the defaults of the second page.
  6. If you did everything correctly, you should have the SDK window looking like the image below.

Write the C code

  1. In the Project Explorer, if you open the tree empty_application_0->src, you will see that there are no .c source files for the application. We have to write our own .c source file with the main function.
  2. Select “File->New->Source File”.
  3. In the dialog box that opens, you must specify the name of the source file to create, and the folder in which to put it. Follow the screenshot below and click “Finish”.
  4. The readdip.c file should have been created and opened in SDK. Notice that SDK fills it with a comment showing the file name, the date it was created and the name of the author. Copy and paste the following code into the file, after the comment, and select “File->Save” to save the file. When you save, the C project will automatically be compiled.

#include "xparameters.h"
#include "xbasic_types.h"
#include "xgpio.h"
#include "xstatus.h"

XGpio GpioOutput;
XGpio GpioInput;

int main (void) {

  Xuint32 status;
  Xuint32 DataRead;
  Xuint32 OldData;

  // Clear the screen
  xil_printf("%c[2J",27);

  // Initialize the GPIO driver so that it's ready to use,
  status = XGpio_Initialize(&GpioOutput,
                                 XPAR_LEDS_8BIT_DEVICE_ID);
  if (status != XST_SUCCESS)
    return XST_FAILURE;
  // Set the direction for all signals to be outputs
  XGpio_SetDataDirection(&GpioOutput, 1, 0x0);

  // Initialize the GPIO driver so that it's ready to use,
  status = XGpio_Initialize(&GpioInput,
                         XPAR_DIP_SWITCHES_8BIT_DEVICE_ID);
  if (status != XST_SUCCESS)
    return XST_FAILURE;
  // Set the direction for all signals to be inputs
  XGpio_SetDataDirection(&GpioInput, 1, 0xFFFFFFFF);

  OldData = 0xFFFFFFFF;
  while(1){
    // Read the state of the DIP switches
    DataRead = XGpio_DiscreteRead(&GpioInput, 1);

    // Send the data to the UART if the settings change
    if(DataRead != OldData){
      xil_printf("DIP Switch settings: 0x%X\r\n", DataRead);
      // Set the GPIO outputs to the DIP switch values
      XGpio_DiscreteWrite(&GpioOutput, 1, DataRead);
      // Record the DIP switch settings
      OldData = DataRead;
    }
  }
  return 0;
}

Load the FPGA with the bitstream

  1. Turn on your hardware platform (ML50x or XUPV5 or whatever you have).
  2. Connect the Serial to USB device to your board’s RS232 port and your computer’s USB port.
  3. Open your terminal program (eg. Hyperterminal or Putty) and connect to the COM port that corresponds to your Serial to USB device.
  4. From the SDK menu, select “Xilinx Tools->Program FPGA”.
  5. In the “Program FPGA” dialog box, the defaults should already specify the correct bitstream for the hardware project. Make sure they correspond to the image above and click Program.

Run the Software Application

  1. In the Project Explorer, select the empty_application_0 and from the menu, select “Run->Run”.
  2. In the “Run As” dialog box, select “Launch on Hardware”.
  3. SDK will build the software application, load it into the memory on the FPGA and trigger the Microblaze to run the code. You should see “DIP Switch settings: 0x0” written in your terminal window. If you change the DIP switches by turning them on and off, you will see the terminal display change to reflect the new settings. The settings are displayed as a hexadecimal number, where 0x0 means zero (all switches are OFF) and 0xFF means all switches are ON. The least significant bit corresponds to switch 8 while the most significant bit corresponds to switch 1. In the screen shot below, I gradually turned all the switches ON.

Summary

Now you should have a good idea about the SDK and how to use it to write software applications and run them on your FPGA board. In future tutorials, I will show you different C applications and go into detail about what the application actually does, rather than going through every step covered in this tutorial.