UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

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

by Xilinx Employee ‎12-09-2013 09:25 AM - edited ‎05-26-2015 04:32 PM (55,510 Views)

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) {

return XST_FAILURE;

}

 

 

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

 

Comments
by Visitor samuelcotard
on ‎06-12-2015 01:57 AM

Hello,

 

I tested this code and the led stayed off.

 

I had to check the box  "Run ps7_init" and "Run ps7_post_config" in the "Target Setup" tab of the "debug Configurations" window.

 

Now the led switches on. whouah!

 

Regards

by Visitor prasanna_mn
on ‎02-07-2017 06:19 AM

Hi,

 

Hi,

 

I am using zynq7000 in Amp mode, Petalinux(cpu0) and freertos(cpu1), I am able to boot both the cpu's successfully.

I am trying to control gpio from cpu1. I am not able to write it.if i write it from cpu0 it works. What could be the problem

 

Thanks

Prasanna

 

by Observer taylo_ap
on ‎02-07-2017 02:41 PM

prasanna_mn

 

AMP can be a complex area to ensure no contention of shared resources is occuring. Did you develop the application yourself or base it off a app note e.g. UG978 ?

 

I know that Linux may remove the clocks from unused blocks that may be the issue, have a look a the links below it seems people may have had similat issues 

 

https://forums.xilinx.com/t5/OpenAMP/I-O-resource-on-cpu1/td-p/669122

 

https://forums.xilinx.com/t5/Zynq-All-Programmable-SoC/AMP-spi-drivers-not-working-on-cpu1/td-p/642930 

 

Adam 

 

Labels
About the Author
  • Be sure to join the Xilinx LinkedIn group to get an update for every new Xcell Daily post! ******************** Steve Leibson is the Director of Strategic Marketing and Business Planning at Xilinx. He started as a system design engineer at HP in the early days of desktop computing, then switched to EDA at Cadnetix, and subsequently became a technical editor for EDN Magazine. He's served as Editor in Chief of EDN Magazine, Embedded Developers Journal, and Microprocessor Report. He has extensive experience in computing, microprocessors, microcontrollers, embedded systems design, design IP, EDA, and programmable logic.