• 0

zynq c code feedback needed


Hi folks!

New to zynq and programming in C. 

I have some code that I have written trying to get an led to turn on when a switch on my arty z7-10 board is pressed. It builds okay in SDK but doesn't execute. 

Can anyone help me trouble shoot please?

#include "xparameters.h"
#include "xgpio.h"
#include "xil_printf.h"

#define LED 0x01
#define SWITCH 0x01  //connects bit 0 of gpio channels to led and switch


#define LED_CHANNEL 1
#define SWITCH_CHANNEL 2 //defines the channels they are on

XGpio Gpio,input, output;

int main(void)
	int Status;
	int SWITCH_data = 0;
	/* Initialize the GPIO driver */

	Status = XGpio_Initialize(&Gpio, GPIO_EXAMPLE_DEVICE_ID);

	if (Status != XST_SUCCESS) {

		xil_printf("Gpio Initialization Failed\r\n");

		return XST_FAILURE;
	XGpio_SetDataDirection(&input, SWITCH_CHANNEL, SWITCH);
	XGpio_SetDataDirection(&output, LED_CHANNEL, LED);

	while (1) {
		SWITCH_data = XGpio_DiscreteRead(&input, SWITCH);
		XGpio_DiscreteWrite(&output, LED, SWITCH_data);  /*i think that the third column is the data to write to output.*/
		if(SWITCH_data == 0b0000){} //do nothing
      else if(SWITCH_data == 0b0001)
         xil_printf("switch pressed\n\r");



	xil_printf("Successfully ran Gpio Example\r\n");

	return XST_SUCCESS;


Also I have my constraints file written correctly, and I have attatched a picture of my block diagram. Thanks!!


block diagram.JPG

Share this post

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Hi @dmeads_10,

It looks like the setdirection function is not correct in your attached sdk code. I believe the LED should be 0x0 and SWITCH should be 0x1. Here is the Getting started with zynq tutorial. Below is the SDK code for linking the switches to the LEDs and btns to the uart usb bridge.


Getting Started Guide for Zedboard
This demo displays the status of the switches on the
LEDs and prints a message to the serial communication
when a button is pressed.
Terminal Settings:
   -Baud: 115200
   -Data bits: 8
   -Parity: no
   -Stop bits: 1
7/25/16: Created by JonP
#include <stdio.h>
#include "platform.h"
#include <xgpio.h>
#include "xparameters.h"
#include "sleep.h"
int main()
   XGpio input, output;
   int button_data = 0;
   int switch_data = 0;
   XGpio_Initialize(&input, XPAR_AXI_GPIO_0_DEVICE_ID);    //initialize input XGpio variable
   XGpio_Initialize(&output, XPAR_AXI_GPIO_1_DEVICE_ID);    //initialize output XGpio variable
   XGpio_SetDataDirection(&input, 1, 0xF);            //set first channel tristate buffer to input
   XGpio_SetDataDirection(&input, 2, 0xF);            //set second channel tristate buffer to input
   XGpio_SetDataDirection(&output, 1, 0x0);        //set first channel tristate buffer to output
      switch_data = XGpio_DiscreteRead(&input, 2);    //get switch data
      XGpio_DiscreteWrite(&output, 1, switch_data);    //write switch data to the LEDs
      button_data = XGpio_DiscreteRead(&input, 1);    //get button data
      //print message dependent on whether one or more buttons are pressed
      if(button_data == 0b00000){} //do nothing
      else if(button_data == 0b00001)
         xil_printf("button 0 pressed\n\r");
      else if(button_data == 0b00010)
         xil_printf("button 1 pressed\n\r");
      else if(button_data == 0b00100)
         xil_printf("button 2 pressed\n\r");
      else if(button_data == 0b01000)
         xil_printf("button 3 pressed\n\r");
      else if(button_data == 0b10000)
              xil_printf("button 4 pressed\n\r");
         xil_printf("multiple buttons pressed\n\r");
      usleep(200000);            //delay
   return 0;


I have attached a screen shot of the vivado block design. 

I would suggest using the Digilent board files for the Arty-Z7-10. The installation tutorial is here. You would not need to use an xdc file and the board tab would allow you to easily add the switches,led and buttons to the design. The getting started with ZYNQ tutorial linked above is basically the same process for the arty-Z7-10 just different number of components and a different zynq board.

Best regards,





Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now