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

XTmrCtr_Config *tmr_config;
XTmrCtr tmr;

void tmr_init(){

	tmr_config = XTmrCtr_LookupConfig(XPAR_AXI_TIMER_0_DEVICE_ID);
	XTmrCtr_CfgInitialize(&tmr, tmr_config, tmr_config->BaseAddress);

	int status0 = XTmrCtr_SelfTest(&tmr,0);
	int status1 = XTmrCtr_SelfTest(&tmr,1);

	if((status0 && status1) == XST_SUCCESS)
		xil_printf("SELF TEST SUCCESSFUL\n");
	else
		xil_printf("SELF TEST FAILED\n");

}



int main()
{
    init_platform();
    tmr_init();

    int period = 10000000;
    int countup = 0;
    int countdown = 10000000;

    while(1){
    XTmrCtr_PwmDisable(&tmr);

         if(countup != 10000000) {
	     countup = countup + 50000;
         }
         else if(countdown != 0){
        		 countdown = countdown - 50000;
         }

         else {
        	 countup = 0;
        	 countdown = 10000000;
         }


       if(countup != 10000000)
    	  XTmrCtr_PwmConfigure(&tmr, period, countup);
       else
    	   XTmrCtr_PwmConfigure(&tmr, period, countdown);

    XTmrCtr_PwmEnable(&tmr);
    while(!XTmrCtr_IsExpired(&tmr, 0));
   // xil_printf("Duty Cycle : %0d\n", duty);
    usleep(30000);

    }

    cleanup_platform();
    return 0;
}