/*
 * Intro to SW Design with Zynq-7000 and Vitis
 *
 */

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xparameters.h"
#include "xgpio.h"
#include "xtmrctr.h"

#define TIMER_COUNTER_0	 0

int main()
{
    XGpio GpioLeds;
    XTmrCtr Timer;
    int Status;
    uint32_t i=0;

    // Start program
	init_platform();
    printf("Intro to SW Design with Zynq-7000 and Vitis Classic\n\r");

    // Initialize LEDs GPIO
	Status = XGpio_Initialize(&GpioLeds, XPAR_GPIO_1_DEVICE_ID);
	if (Status != XST_SUCCESS) {
		printf("ERROR: Initialization failed for LEDs GPIO \n\r");
		return XST_FAILURE;
	}

	// Initialize Timer
	Status = XTmrCtr_Initialize(&Timer, XPAR_TMRCTR_0_DEVICE_ID);
	if (Status != XST_SUCCESS) {
		printf("ERROR: Initialization failed for LEDs GPIO \n\r");
		return XST_FAILURE;
	}

	// Configure timer
	XTmrCtr_SetResetValue(&Timer, TIMER_COUNTER_0, 500000);
	XTmrCtr_SetOptions(&Timer, TIMER_COUNTER_0, XTC_DOWN_COUNT_OPTION);
	// Start timer
	XTmrCtr_Start(&Timer, TIMER_COUNTER_0);

	while(1) {
		if (XTmrCtr_IsExpired(&Timer, TIMER_COUNTER_0)) {
			// Restart timer
			XTmrCtr_Start(&Timer, TIMER_COUNTER_0);
			// Update LEDs
			XGpio_DiscreteWrite(&GpioLeds, 1, i++);
			if (i>63) i=0;
		}
	}

    // Orderly end of program (never reached)
    cleanup_platform();
    return 0;
}
