/*
 * GPIO_Driv.c
 *
 *  Created on: 2021年5月7日
 *      Author: Administrator
 */
#include "GPIO_Driv.h"
#include "xil_printf.h"
#include "xparameters.h"

XGpioPs psGpioInstancePtr;     //端口
XGpioPs_Config* GpioConfigPtr; //设备ID
					//sck,cs,sdi,sdo,rst
u32 CPLD_U1  [11] = {54,55,56,57,58,59,60,61,62,63,64};
u32 CPLD_U2  [11] = {65,66,67,68,69,70,71,72,73,74,75};
u32 CPLD_U3  [11] = {76,77,78,79,80,81,82,83,84,85,86};

int EMIO_config(void)
{
	    int xStatus;
		GpioConfigPtr = XGpioPs_LookupConfig(XPAR_XGPIOPS_0_DEVICE_ID);
		if(GpioConfigPtr == NULL)
		{
			xil_printf("[E] PS GPIO LookupConfig failed\n\r");
			return XST_FAILURE;
		}
		else
		{
			xil_printf("[D] Successfully Find PS GPIO!\r\n");
		}

		xStatus = XGpioPs_CfgInitialize(&psGpioInstancePtr,GpioConfigPtr, GpioConfigPtr->BaseAddr);
		if(XST_SUCCESS != xStatus)
		{
			xil_printf("[E] PS GPIO INIT FAILED \n\r");
			return XST_FAILURE;
		}

		/*-------------------------------------CPLD_U1------------------------------------*/
		//--设置EMIO方向
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 54, 1);//54---sck
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 55, 1);//55---cs
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 56, 1);//56---sdi
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 57, 0);//57---sdo
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 58, 1);//58---rst_n

		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 59, 1);//59
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 60, 1);//60
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 61, 1);//61
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 62, 1);//62
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 63, 1);//63

		//使能EMIO输出 1=output 0=input
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 54, 1);//54---sck
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 55, 1);//55---cs
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 56, 1);//56---sdi
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 57, 0);//57---sdo
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 58, 1);//58---rst_n

		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 59, 1);//59
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 60, 1);//60
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 61, 1);//61
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 62, 1);//62
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 63, 1);//63

		/*-------------------------------------CPLD_U2------------------------------------*/
		//--设置EMIO方向
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 65, 1);//65---sck
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 66, 1);//66---cs
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 67, 1);//67---sdi
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 68, 0);//68---sdo
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 69, 1);//69---rst_n

		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 70, 1);//70
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 71, 1);//71
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 72, 1);//72
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 73, 1);//73
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 74, 1);//74

		//使能EMIO输出 1=output 0=input
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 65, 1);//65---sck
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 66, 1);//66---cs
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 67, 1);//67---sdi
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 68, 0);//68---sdo
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 69, 1);//69---rst_n

		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 70, 1);//70
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 71, 1);//71
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 72, 1);//72
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 73, 1);//73
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 74, 1);//74

		/*-------------------------------------CPLD_U3------------------------------------*/
				//--设置EMIO方向
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 76, 1);//76---sck
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 77, 1);//77---cs
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 78, 1);//78---sdi
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 79, 0);//79---sdo
		XGpioPs_SetDirectionPin(&psGpioInstancePtr, 80, 1);//80---rst_n

		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 81, 1);//81
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 82, 1);//82
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 83, 1);//83
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 84, 1);//84
		//XGpioPs_SetDirectionPin(&psGpioInstancePtr, 85, 1);//85

		//使能EMIO输出 1=output 0=input
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 76, 1);//76---sck
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 77, 1);//77---cs
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 78, 1);//78---sdi
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 79, 0);//79---sdo
		XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 80, 1);//80---rst_n

		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 81, 1);//81
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 82, 1);//82
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 83, 1);//83
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 84, 1);//84
		//XGpioPs_SetOutputEnablePin(&psGpioInstancePtr, 85, 1);//85
		xil_printf("[I] PS GPIO Init Done!\r\n");

		return XST_SUCCESS;
}
