Showing results for 
Show  only  | Search instead for 
Did you mean: 

Driving the Zynq SoC's GPIO: Adam Taylor’s MicroZed Chronicles Part 10

Xilinx Employee
Xilinx Employee
0 3 171K

By Adam Taylor


My previous blog post looked at the Zynq All Programmable SoC’s MIO and EMIO. (See “Meet the Zynq MIO: Adam Taylor’s MicroZed Chronicles Part 9”). This blog looks at driving a LED connected as a GPIO. While blinking an LED may seem a very simple task, examining the steps needed to blink the LED allows us to then explore further aspects of the Zynq SoC such as its timers and interrupts. I’ll be looking at those topics in later blogs.


The Zynq SoC has a number general purpose I/O pins that combine to create a 10-bit-wide, general-purpose I/O port, as can be seen below.


Figure 1 Zynq SoC GPIO definitions.jpg 


As you can see this GPIO bank is split across both MIO banks with a mixture of voltages. For this example, our LED will be connected to MIO 47.


Xilinx provides a number of drivers to simplify use of the Zynq SoC’s GPIO. Links to supporting documentation and examples can be found linked in the system.mss file, available in your board support package:



Figure 2 Zynq SoC Board Support Package Documentation Detail.jpg



The code needed to drive the GPIO is very straightforward. The file xparameters.h contains the number of GPIO instances, the GPIO device id within the system, and the upper and lower address ranges:



Figure 3 xparameters.h GPIO definitions file.jpg


Macros and functions required to drive the IO are defined with the file xgpiops.h, which is available under the BSP include files. This file contains the needed configuration and initialization functions for the Zynq SoC’s GPIO, along with functions to support reading from and writing to the GPIO, which is after all what we are most interested in.


To blink the LED, we need to do the following:


1. Include the xgpiops.h file within the main application code



#include "xgpiops.h"



2. Define the output pin we want to toggle. In this case it is pin 8 within the bank of 10, MIO 47:



#define ledpin 47



3. Declare the driver instance:



XGpioPs Gpio;



4. Configure the GPIO and define the status and pointer variables required for initialization within the function you wish to use:



int Status;

XGpioPs_Config *GPIOConfigPtr;



5. Initialize the GPIO Driver:



//GPIO Initilization

GPIOConfigPtr = XGpioPs_LookupConfig(XPAR_PS7_GPIO_0_DEVICE_ID);

Status = XGpioPs_CfgInitialize(&Gpio, GPIOConfigPtr, GPIOConfigPtr ->BaseAddr);

if (Status != XST_SUCCESS) {





6. Set the direction of the GPIO pin and enable the output:



XGpioPs_SetDirectionPin(&Gpio, ledpin, 1);

XGpioPs_SetOutputEnablePin(&Gpio, ledpin, 1);



7. Write the desired output value to the GPIO pin:



XGpioPs_WritePin(&Gpio, ledpin, 0x0);



This write function can be used within a loop to get the LED flashing at a desired rate however you can also use this approach to drive anything connected to the GPIO pin. Reading the GPIO pins is achieved in a similar manner using the XGpioPs_ReadPin(&Gpio,INPUT_PIN) function.


It is well worth spending some time reading the documentation and examples provided because the Zynq SoC’s GPIO is a very flexible resource.


Note: Please see the previous entries in this MicroZed series by Adam Taylor:


Meet the Zynq MIO: Adam Taylor’s MicroZed Chronicles Part 9


MicroZed XADC Software: Adam Taylor’s MicroZed Chronicles Part 8


Getting the XADC Running on the MicroZed: Adam Taylor’s MicroZed Chronicles Part 7


A Boot Loader for MicroZed. Adam Taylor’s MicroZed Chronicles, Part 6 


Figuring out the MicroZed Boot Loader – Adam Taylor’s MicroZed Chronicles, Part 5


Running your programs on the MicroZed – Adam Taylor’s MicroZed Chronicles, Part 4


Zynq and MicroZed say “Hello World”-- Adam Taylor’s MicroZed Chronicles, Part 3


Adam Taylor’s MicroZed Chronicles: Setting the SW Scene


Bringing up the Avnet MicroZed with Vivado


Tags (1)