Exemplo n.º 1
0
int trf3795ReadBackRegs(XSpi *InstancePtr)
{
	u32 StatusReg;
	u32 ControlReg;
	u32 Index;
	u32 Delay;
	u32 Data;
	u32 NumSent = 0;
	u32 NumRecvd = 0;
	u32 RxData[] = {0,0,0};
	u8  DataWidth;
	u8  j;
	u32 read_trf_register[] = {XSP_REG0_READBACK,XSP_REG1_READBACK,XSP_REG2_READBACK,XSP_REG3_READBACK,
								XSP_REG4_READBACK,XSP_REG5_READBACK,XSP_REG6_READBACK,XSP_REG7_READBACK};

	ControlReg = XSpi_GetControlReg(InstancePtr);
	XSpi_SetControlReg(InstancePtr, ControlReg | XSP_CR_MASTER_MODE_MASK |
						XSP_CR_LSB_FIRST);

	xil_printf("ctrl reg setup done\r\n");

	/*
	 * Set the slave select zero bit to active - low
	 */
//	XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFE);

	/*
	 * We do not need interrupts for now
	 */
	XSpi_IntrGlobalDisable(InstancePtr);

	DataWidth = InstancePtr->DataWidth;

	StatusReg = XSpi_GetStatusReg(InstancePtr);

	for(Index=0; Index<8; Index++)
	{
		if (DataWidth == XSP_DATAWIDTH_BYTE) {
			/*
			 * Data Transfer Width is Byte (8 bit).
			 */
			Data = 0;
		} else if (DataWidth == XSP_DATAWIDTH_HALF_WORD) {
			/*
			 * Data Transfer Width is Half Word (16 bit).
			 */
			Data = XSP_HALF_WORD_TESTBYTE;
		} else if (DataWidth == XSP_DATAWIDTH_WORD){
			/*
			 * Data Transfer Width is Word (32 bit).
			 */
			Data = read_trf_register[Index]; // index of the register to readback here *********************
		}

		XSpi_WriteReg(InstancePtr->BaseAddr, XSP_DTR_OFFSET, Data);
		NumSent += (DataWidth >> 3);
		StatusReg = XSpi_GetStatusReg(InstancePtr);


		/*
		 * Start the transfer by not inhibiting the transmitter and
		 * enabling the device.
		 */
		ControlReg = XSpi_GetControlReg(InstancePtr) &
						 (~XSP_CR_TRANS_INHIBIT_MASK);
		XSpi_SetControlReg(InstancePtr, ControlReg |
					XSP_CR_ENABLE_MASK);					// | XSP_CR_CLK_PHASE_MASK);

		/*
		 * Wait for the transfer to be done by polling the transmit
		 * empty status bit.
		 */
		//xil_printf("enter first tx while loop \r\n");
		do {
			StatusReg = XSpi_IntrGetStatus(InstancePtr);
		} while ((StatusReg & XSP_INTR_TX_EMPTY_MASK) == 0);

		XSpi_IntrClear(InstancePtr, XSP_INTR_TX_EMPTY_MASK);

		for (Delay = 0; Delay < 10; Delay++)	// add delay
		{}

		/*
		* To create a latch enable pulse and extra read clock pulse,
		* set the slave select one SS(1) bit low
		*/
		XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFD); // creates read pulse and clock
		for (Delay = 0; Delay < 10; Delay++)	// more delay makes wider pulses
		{}
		XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFF); // removes read pulse and clock


		for (Delay = 0; Delay < 10; Delay++)	// add delay
		{}

		/*
		 * Stop the transfer (hold off automatic sending) by inhibiting
		 * the transmitter and disabling the device.
		 */
		ControlReg |= XSP_CR_TRANS_INHIBIT_MASK;
		XSpi_SetControlReg(InstancePtr ,
					ControlReg & ~ XSP_CR_ENABLE_MASK);
	//	}	// end of the for loop

		/*
		 *************  Now read-back the specific register *********************************
		 */

	//	for (Index = 0; Index < 1; Index++) {
	//		Data = 0;

			/*
			 * Fill the transmit register.
			 */
			StatusReg = XSpi_GetStatusReg(InstancePtr);
	//		while ((StatusReg & XSP_SR_TX_FULL_MASK) == 0) {	// do just a single transfer for now
				if (DataWidth == XSP_DATAWIDTH_BYTE) {
					/*
					 * Data Transfer Width is Byte (8 bit).
					 */
					Data = 0;
				} else if (DataWidth == XSP_DATAWIDTH_HALF_WORD) {
					/*
					 * Data Transfer Width is Half Word (16 bit).
					 */
					Data = XSP_HALF_WORD_TESTBYTE;
				} else if (DataWidth == XSP_DATAWIDTH_WORD){
					/*
					 * Data Transfer Width is Word (32 bit).
					 */
					Data = XSP_ZERO_WRITE;	// just leave the data bits at zero during read-back
				}

				XSpi_WriteReg(InstancePtr->BaseAddr, XSP_DTR_OFFSET,
						Data + Index);
				NumSent += (DataWidth >> 3);
				StatusReg = XSpi_GetStatusReg(InstancePtr);
	//		}

			/*
			 * Start the transfer by not inhibiting the transmitter and
			 * enabling the device.
			 */
			ControlReg = XSpi_GetControlReg(InstancePtr) &
							 (~XSP_CR_TRANS_INHIBIT_MASK);
			XSpi_SetControlReg(InstancePtr, ControlReg |
					    XSP_CR_ENABLE_MASK | XSP_CR_CLK_PHASE_MASK);	// clock data in on the second edge (falling)

			/*
			 * Wait for the transfer to be done by polling the transmit
			 * empty status bit.
			 */
			//xil_printf("Enter tx empty while loop\r\n");
			do {
				StatusReg = XSpi_IntrGetStatus(InstancePtr);
			} while ((StatusReg & XSP_INTR_TX_EMPTY_MASK) == 0);

			XSpi_IntrClear(InstancePtr, XSP_INTR_TX_EMPTY_MASK);

			/*
			 * Receive and verify the data just transmitted.
			 */
			StatusReg = XSpi_GetStatusReg(InstancePtr);
			//xil_printf("Enter rx empty while loop\r\n");
			while ((StatusReg & XSP_SR_RX_EMPTY_MASK) == 0) {


				for (j = 0; j < 3; j++) {
					RxData[j] = XSpi_ReadReg(InstancePtr->BaseAddr,
								XSP_DRR_OFFSET);
					NumRecvd += (DataWidth >> 3);
					StatusReg = XSpi_GetStatusReg(InstancePtr);
				}
			xil_printf("\r%08x\n\r", RxData[2]);
			}

			/*
			 * Stop the transfer (hold off automatic sending) by inhibiting
			 * the transmitter and disabling the device.
			 */
			ControlReg |= XSP_CR_TRANS_INHIBIT_MASK;
			XSpi_SetControlReg(InstancePtr ,
					    ControlReg & ~ XSP_CR_ENABLE_MASK);
		}	// end of the for loop



		xil_printf("\n\rTRF3765 Register Access Done\n\r");

		return XST_SUCCESS;

}
Exemplo n.º 2
0
/* Write to and read back the RF board TRF3795
*
*
******************************************************************************/
static int trf3795WriteAndRead(XSpi *InstancePtr)
{
	u32 StatusReg;
	u32 ControlReg;
	u32 Index;
	u32 Delay;
	u32 Data;
	u32 NumSent = 0;
	u32 NumRecvd = 0;
	u32 RxData[] = {0,0,0};
	u8  DataWidth;
	u8  j;
	int ctr = 0;
	u32 write_trf_register[] = {XSP_REG0_WRITE,XSP_REG1_WRITE,XSP_REG2_WRITE_ENC,XSP_REG3_WRITE,
							 XSP_REG4_WRITE,XSP_REG5_WRITE,XSP_REG6_WRITE, XSP_REG7_WRITE};
	u32 read_trf_register[] = {XSP_REG0_READBACK,XSP_REG1_READBACK,XSP_REG2_READBACK,XSP_REG3_READBACK,
							XSP_REG4_READBACK,XSP_REG5_READBACK,XSP_REG6_READBACK,XSP_REG7_READBACK,XSP_REG7_READBACK};

	/*
	 * Setup the control register to enable master mode and
	 * to send least significant bit 1st
	 */
	ControlReg = XSpi_GetControlReg(InstancePtr);
	XSpi_SetControlReg(InstancePtr, ControlReg | XSP_CR_MASTER_MODE_MASK |
						XSP_CR_LSB_FIRST);

	/*
	 * Set the slave select zero bit to active - low
	 */
//	XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFE);

	/*
	 * We do not need interrupts for now
	 */
	XSpi_IntrGlobalDisable(InstancePtr);

	DataWidth = InstancePtr->DataWidth;

	/*****************************************************************************/

	/*
	 * perform a write to a TRF3765 register
	 */

	for (Index = 0; Index < 8; Index++) {	// for loop write to all eight registers
		Data = 0;

		/*
		 * Fill the transmit register.
		 */
		StatusReg = XSpi_GetStatusReg(InstancePtr);
//		while ((StatusReg & XSP_SR_TX_FULL_MASK) == 0) {	// no loop, do just a single transfer for now
			if (DataWidth == XSP_DATAWIDTH_BYTE) {
				/*
				 * Data Transfer Width is Byte (8 bit).
				 */
				Data = 0;
			} else if (DataWidth == XSP_DATAWIDTH_HALF_WORD) {
				/*
				 * Data Transfer Width is Half Word (16 bit).
				 */
				Data = XSP_HALF_WORD_TESTBYTE;
			} else if (DataWidth == XSP_DATAWIDTH_WORD){
				/*
				 * Data Transfer Width is Word (32 bit).
				 */
				Data = write_trf_register[Index];	// choose the register index 0 to 7 ************
			}

			XSpi_WriteReg(InstancePtr->BaseAddr, XSP_DTR_OFFSET, Data);
			NumSent += (DataWidth >> 3);
			StatusReg = XSpi_GetStatusReg(InstancePtr);
//		}

		/*
		 * Start the transfer by not inhibiting the transmitter and
		 * enabling the device.
		 */
		ControlReg = XSpi_GetControlReg(InstancePtr) &
						 (~XSP_CR_TRANS_INHIBIT_MASK);
		XSpi_SetControlReg(InstancePtr, ControlReg |
				    XSP_CR_ENABLE_MASK);

		/*
		 * Wait for the transfer to be done by polling the transmit
		 * empty status bit.
		 */
		//xil_printf("status reg %d \r\n", StatusReg);
		//xil_printf("intr status reg %d \r\n", XSpi_IntrGetStatus(InstancePtr));
		ctr = 0;
		do {
			StatusReg = XSpi_IntrGetStatus(InstancePtr);
			if(ctr > SPI_TIMEOUT)
				break;
			ctr++;
		} while ((StatusReg & XSP_INTR_TX_EMPTY_MASK) == 0);

		XSpi_IntrClear(InstancePtr, XSP_INTR_TX_EMPTY_MASK);

		/*
		* To create a latch enable pulse and extra read clock pulse,
		* set the slave select one SS(1) bit low
		*/
		XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFD); // creates read pulse and clock
		for (Delay = 0; Delay < 10; Delay++)	// more delay makes wider pulses
		{}
		XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFF); // removes read pulse and clock


	/*
	* To create a latch enable pulse,
	* set the slave select one SS(0) bit low
	*/
//	XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFE); // drives the latch enable
//	for (Delay = 0; Delay < 10; Delay++)	// more delay makes pulse wider
//	{}
//	XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFF); // removes the latch enable

		/*
		 * Stop the transfer (hold off automatic sending) by inhibiting
		 * the transmitter and disabling the device.
		 */
		ControlReg |= XSP_CR_TRANS_INHIBIT_MASK;
		XSpi_SetControlReg(InstancePtr ,
				    ControlReg & ~ XSP_CR_ENABLE_MASK);
//	}	// end of the for loop


	/*****************************************************************************/

	/*
	 * To Read-back from the Internal Register Banks, Register 0 must be programmed
	 * with a specific command that sets the TRF3765 into read-back mode and
	 * specifies the register to be read
	 */

//	for (Index = 0; Index < 1; Index++) {
//		Data = 0;

		/*
		 * Fill the transmit register.
		 */
		StatusReg = XSpi_GetStatusReg(InstancePtr);
//		while ((StatusReg & XSP_SR_TX_FULL_MASK) == 0) {	// do just a single transfer for now
			if (DataWidth == XSP_DATAWIDTH_BYTE) {
				/*
				 * Data Transfer Width is Byte (8 bit).
				 */
				Data = 0;
			} else if (DataWidth == XSP_DATAWIDTH_HALF_WORD) {
				/*
				 * Data Transfer Width is Half Word (16 bit).
				 */
				Data = XSP_HALF_WORD_TESTBYTE;
			} else if (DataWidth == XSP_DATAWIDTH_WORD){
				/*
				 * Data Transfer Width is Word (32 bit).
				 */
				Data = read_trf_register[Index]; // index of the register to readback here *********************
			}

			XSpi_WriteReg(InstancePtr->BaseAddr, XSP_DTR_OFFSET, Data);
			NumSent += (DataWidth >> 3);
			StatusReg = XSpi_GetStatusReg(InstancePtr);
//		}

		/*
		 * Start the transfer by not inhibiting the transmitter and
		 * enabling the device.
		 */
		ControlReg = XSpi_GetControlReg(InstancePtr) &
						 (~XSP_CR_TRANS_INHIBIT_MASK);
		XSpi_SetControlReg(InstancePtr, ControlReg |
				    XSP_CR_ENABLE_MASK);					// | XSP_CR_CLK_PHASE_MASK);

		/*
		 * Wait for the transfer to be done by polling the transmit
		 * empty status bit.
		 */
		ctr = 0;
		do {
			StatusReg = XSpi_IntrGetStatus(InstancePtr);
			if(ctr > SPI_TIMEOUT)
				break;
			ctr++;
		} while ((StatusReg & XSP_INTR_TX_EMPTY_MASK) == 0);

		XSpi_IntrClear(InstancePtr, XSP_INTR_TX_EMPTY_MASK);

		for (Delay = 0; Delay < 10; Delay++)	// add delay
		{}

		/*
		* To create a latch enable pulse and extra read clock pulse,
		* set the slave select one SS(1) bit low
		*/
		XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFD); // creates read pulse and clock
		for (Delay = 0; Delay < 10; Delay++)	// more delay makes wider pulses
		{}
		XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFF); // removes read pulse and clock


		for (Delay = 0; Delay < 10; Delay++)	// add delay
		{}

		/*
		 * Stop the transfer (hold off automatic sending) by inhibiting
		 * the transmitter and disabling the device.
		 */
		ControlReg |= XSP_CR_TRANS_INHIBIT_MASK;
		XSpi_SetControlReg(InstancePtr ,
				    ControlReg & ~ XSP_CR_ENABLE_MASK);
//	}	// end of the for loop

	/*
	 *************  Now read-back the specific register *********************************
	 */

//	for (Index = 0; Index < 1; Index++) {
//		Data = 0;

		/*
		 * Fill the transmit register.
		 */
		StatusReg = XSpi_GetStatusReg(InstancePtr);
//		while ((StatusReg & XSP_SR_TX_FULL_MASK) == 0) {	// do just a single transfer for now
			if (DataWidth == XSP_DATAWIDTH_BYTE) {
				/*
				 * Data Transfer Width is Byte (8 bit).
				 */
				Data = 0;
			} else if (DataWidth == XSP_DATAWIDTH_HALF_WORD) {
				/*
				 * Data Transfer Width is Half Word (16 bit).
				 */
				Data = XSP_HALF_WORD_TESTBYTE;
			} else if (DataWidth == XSP_DATAWIDTH_WORD){
				/*
				 * Data Transfer Width is Word (32 bit).
				 */
				Data = XSP_ZERO_WRITE;	// just leave the data bits at zero during read-back
			}

			XSpi_WriteReg(InstancePtr->BaseAddr, XSP_DTR_OFFSET,
					Data + Index);
			NumSent += (DataWidth >> 3);
			StatusReg = XSpi_GetStatusReg(InstancePtr);
//		}

		/*
		 * Start the transfer by not inhibiting the transmitter and
		 * enabling the device.
		 */
		ControlReg = XSpi_GetControlReg(InstancePtr) &
						 (~XSP_CR_TRANS_INHIBIT_MASK);
		XSpi_SetControlReg(InstancePtr, ControlReg |
				    XSP_CR_ENABLE_MASK | XSP_CR_CLK_PHASE_MASK);	// clock data in on the second edge (falling)

		/*
		 * Wait for the transfer to be done by polling the transmit
		 * empty status bit.
		 */
		ctr = 0;
		do {
			StatusReg = XSpi_IntrGetStatus(InstancePtr);
			if(ctr > SPI_TIMEOUT)
				break;
			ctr++;
		} while ((StatusReg & XSP_INTR_TX_EMPTY_MASK) == 0);

		XSpi_IntrClear(InstancePtr, XSP_INTR_TX_EMPTY_MASK);

		/*
		 * Receive and verify the data just transmitted.
		 */
		StatusReg = XSpi_GetStatusReg(InstancePtr);
		while ((StatusReg & XSP_SR_RX_EMPTY_MASK) == 0) {


			for (j = 0; j < 3; j++) {
				RxData[j] = XSpi_ReadReg(InstancePtr->BaseAddr,
							XSP_DRR_OFFSET);
				NumRecvd += (DataWidth >> 3);
				StatusReg = XSpi_GetStatusReg(InstancePtr);
			}
		xil_printf("\r%08x\n\r", RxData[2]);
		}

		/*
		 * Stop the transfer (hold off automatic sending) by inhibiting
		 * the transmitter and disabling the device.
		 */
		ControlReg |= XSP_CR_TRANS_INHIBIT_MASK;
		XSpi_SetControlReg(InstancePtr ,
				    ControlReg & ~ XSP_CR_ENABLE_MASK);
	}	// end of the for loop

	/*
	 * One final check to make sure the total number of bytes sent equals
	 * the total number of bytes received.
	 */
//	if (NumSent != NumRecvd) {
//		return XST_LOOPBACK_ERROR;
//	}

	xil_printf("\n\rTRF3765 Register Access Done\n\r");

	return XST_SUCCESS;
}
Exemplo n.º 3
0
/**
*
* This function does a minimal test on the Spi device and driver as a
* design example. The purpose of this function is to illustrate how to use
* the XSpi component using the polled mode.
*
* This function sends data and expects to receive the same data.
*
*
* @param	SpiInstancePtr is a pointer to the instance of Spi component.
* @param	SpiDeviceId is the Device ID of the Spi Device and is the
*		XPAR_<SPI_instance>_DEVICE_ID value from xparameters.h.
*
* @return	XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note
*
* This function contains an infinite loop such that if the Spi device is not
* working it may never return.
*
******************************************************************************/
int SpiPolledExample(XSpi *SpiInstancePtr, u16 SpiDeviceId)
{
	int Status;
	u32 Count;

	XSpi_Config *ConfigPtr;	/* Pointer to Configuration data */

	/*
	 * Initialize the SPI driver so that it is  ready to use.
	 */
	ConfigPtr = XSpi_LookupConfig(SpiDeviceId);
	if (ConfigPtr == NULL) {
		return XST_DEVICE_NOT_FOUND;
	}

	Status = XSpi_CfgInitialize(SpiInstancePtr, ConfigPtr,
				  ConfigPtr->BaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Perform a self-test to ensure that the hardware was built correctly.
	 */
	Status = XSpi_SelfTest(SpiInstancePtr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Run loopback test only in case of standard SPI mode.
	 */
	if (SpiInstancePtr->SpiMode != XSP_STANDARD_MODE) {
		return XST_SUCCESS;
	}

	/*
	 * Set the Spi device as a master and in loopback mode.
	 */
	Status = XSpi_SetOptions(SpiInstancePtr, XSP_MASTER_OPTION | XSP_MANUAL_SSELECT_OPTION);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}


	/*
	 * Start the SPI driver so that the device is enabled.
	 */
	XSpi_Start(SpiInstancePtr);

	/*
	 * Disable Global interrupt to use polled mode operation
	 */
	XSpi_IntrGlobalDisable(SpiInstancePtr);

	/*
	 * Initialize the write buffer with pattern to write, initialize the
	 * read buffer to zero so it can be verified after the read, the
	 * Test value that is added to the unique value allows the value to be
	 * changed in a debug environment.
	 */





	/*
	 * Transmit the data.
	 */

	/*Init ADCL*/
	SpiInstancePtr->SlaveSelectReg = 0x00000000;

	ADXL362_Init(SpiInstancePtr);

	DelayMs(1);

	while(1)
	{
		int x = 0, y=0, z=0, temp;

		x= ADXL362_ReadX(SpiInstancePtr);
		y= ADXL362_ReadY(SpiInstancePtr);
		z= ADXL362_ReadZ(SpiInstancePtr);
		temp = ADXL362_ReadTemp(SpiInstancePtr);

		DelayMs(5);

		xil_printf("X: %d\r\n", x);
		xil_printf("Y: %d\r\n", y);
		xil_printf("Z: %d\r\n", z);
		xil_printf("temperature: %d\r\n", temp);
	}

	SpiInstancePtr->SlaveSelectReg = 0x0000001;




	return XST_SUCCESS;
}
Exemplo n.º 4
0
/*
 * Change LO frequency in fractional mode.
 * Must call trf3795EnableFrac() first. 
 */
int trf3795changeFreqFrac(XSpi *InstancePtr, double freq)
{
	u32 StatusReg;
	u32 ControlReg;
	u32 Index;
	u32 Delay;
	u32 Data;
	u32 NumSent = 0;
	u32 NumRecvd = 0;
	u32 RxData[] = {0,0,0};
	u32 reg1Val, reg2Val, reg2ENCVal, reg3Val, reg6Val;
	u8  DataWidth;
	u8  j;
	int ctr;
    
    u8 loDivSel, loDiv;
    u8 pllDiv;
    u8 pllDivSel;
    u8 prscSel;
    u16 rDiv;
    u16 nInt;
    u32 nFrac;

    // Calculate parameter and register values
    
    freq = freq/2; // Output frequency is doubled
    
    if(freq>2400)
	{
		loDiv = 1;
		loDivSel = 0;

	}
	else if(freq>1200)
	{
		loDiv = 2;
		loDivSel = 1;

	}
	else if(freq>600)
	{
		loDiv = 4;
		loDivSel = 2;

	}
	else if(freq>300)
	{
		loDiv = 8;
		loDivSel = 3;

	}
    pllDiv = (u8)ceil((double)loDiv*freq/3000.0);
    //xil_printf("Pll Div %d\r\n", pllDiv);
    rDiv = 1;
    nInt = (u16)(loDiv*freq*rDiv/(10*pllDiv));
    nFrac = (u32)(((loDiv*freq*rDiv/(10*pllDiv))-nInt)*(1<<25));
    
    if(pllDiv==1)
        pllDivSel = 0;
    else if(pllDiv==2)
        pllDivSel = 1;
    else if(pllDiv==4)
        pllDivSel = 2;    
    else
    {
        xil_printf("Invalid PLL_DIV!\r\n");
        return XST_FAILURE;
        
    }
    
    if(nInt>=75)      // 72 in integer mode, 75 in frac
        prscSel = 1;
    else
        prscSel = 0;

    //xil_printf("NInt %d\r\n", nInt);
    //xil_printf("prscSel %d\r\n", prscSel);
    //xil_printf("loDiv %d\r\n", loDiv);
    //xil_printf("NFrac %x\r\n", nFrac);
    
    reg1Val = XSP_REG1_WRITE_CF + (rDiv<<5);
    reg2Val = XSP_REG2_WRITE_CF + (nInt<<5) + (pllDivSel<<21) + (prscSel<<23);
    reg2ENCVal = XSP_REG2_WRITE_CF_ENC + (nInt<<5) + (pllDivSel<<21) + (prscSel<<23);
    reg3Val = XSP_REG3_WRITE_CF + (nFrac<<5);
    reg6Val = XSP_REG6_WRITE_CF + (loDivSel<<23);
    u32 write_trf_register[] = {XSP_REG0_WRITE, reg1Val, reg3Val, XSP_REG4_WRITE_FRAC,
    			XSP_REG5_WRITE_FRAC, reg6Val, reg2ENCVal, XSP_REG7_WRITE};
    
    xil_printf("reg1: %x\r\n", reg1Val);
    xil_printf("reg2: %x\r\n", reg2ENCVal);
    xil_printf("reg3: %x\r\n", reg3Val);
    xil_printf("reg6: %x\r\n", reg6Val);


    
    /*
	 * Setup the control reogister to enable master mode and
	 * to send least significant bit 1st
	 */
    
	ControlReg = XSpi_GetControlReg(InstancePtr);
	XSpi_SetControlReg(InstancePtr, ControlReg | XSP_CR_MASTER_MODE_MASK |
						XSP_CR_LSB_FIRST);

	//xil_printf("ctrl reg setup done\r\n");

	/*
	 * Set the slave select zero bit to active - low
	 */
//	XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFE);

	/*
	 * We do not need interrupts for now
	 */
	XSpi_IntrGlobalDisable(InstancePtr);

	DataWidth = InstancePtr->DataWidth;

	/*****************************************************************************/

	/*
	 * perform a write to a TRF3765 register
	 */

	for (Index = 0; Index < 8; Index++) {	// for loop write to three registers
		//mdelay(5);
		Data = 0;
		//xil_printf("start transfer %d\r\n", Index);
		/*
		 * Fill the transmit register.
		 */
		StatusReg = XSpi_GetStatusReg(InstancePtr);
		//xil_printf("got status reg\r\n");
//		while ((StatusReg & XSP_SR_TX_FULL_MASK) == 0) {	// no loop, do just a single transfer for now
			if (DataWidth == XSP_DATAWIDTH_BYTE) {
				/*
				 * Data Transfer Width is Byte (8 bit).
				 */
				Data = 0;
			} else if (DataWidth == XSP_DATAWIDTH_HALF_WORD) {
				/*
				 * Data Transfer Width is Half Word (16 bit).
				 */
				Data = XSP_HALF_WORD_TESTBYTE;
			} else if (DataWidth == XSP_DATAWIDTH_WORD){
				/*
				 * Data Transfer Width is Word (32 bit).
				 */
				Data = write_trf_register[Index];	// choose the register index 0 to 7 ************
				//xil_printf("selected register \r\n");
			}

			XSpi_WriteReg(InstancePtr->BaseAddr, XSP_DTR_OFFSET, Data);
			//xil_printf("wrote data \r\n");
			NumSent += (DataWidth >> 3);
			//xil_printf("Numsent %d\r\n", NumSent);
			StatusReg = XSpi_GetStatusReg(InstancePtr);
			//xil_printf("status reg %d \r\n", StatusReg);
			mdelay(5);
//		}

		/*
		 * Start the transfer by not inhibiting the transmitter and
		 * enabling the device.
		 */
		ControlReg = XSpi_GetControlReg(InstancePtr) &
						 (~XSP_CR_TRANS_INHIBIT_MASK);
		XSpi_SetControlReg(InstancePtr, ControlReg |
				    XSP_CR_ENABLE_MASK);

		/*
		 * Wait for the transfer to be done by polling the transmit
		 * empty status bit.
		 */
		//xil_printf("start while loop\r\n");
		//xil_printf("intr status reg %d\r\n", XSpi_IntrGetStatus(InstancePtr));
		ctr = 0;
		do {
			StatusReg = XSpi_IntrGetStatus(InstancePtr);
			if(ctr>SPI_TIMEOUT)
				break;
			ctr++;
		} while ((StatusReg & XSP_INTR_TX_EMPTY_MASK) == 0);
		//mdelay(5);
		//xil_printf("done with while loop \r\n");

		XSpi_IntrClear(InstancePtr, XSP_INTR_TX_EMPTY_MASK);

		/*
		* To create a latch enable pulse and extra read clock pulse,
		* set the slave select one SS(1) bit low
		*/
		XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFD); // creates read pulse and clock
		for (Delay = 0; Delay < 10; Delay++)	// more delay makes wider pulses
		{}
		XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFF); // removes read pulse and clock


	/*
	* To create a latch enable pulse,
	* set the slave select one SS(0) bit low
	*/
//	XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFE); // drives the latch enable
//	for (Delay = 0; Delay < 10; Delay++)	// more delay makes pulse wider
//	{}
//	XSpi_SetSlaveSelectReg(InstancePtr, 0xFFFF); // removes the latch enable

		/*
		 * Stop the transfer (hold off automatic sending) by inhibiting
		 * the transmitter and disabling the device.
		 */
		ControlReg |= XSP_CR_TRANS_INHIBIT_MASK;
		XSpi_SetControlReg(InstancePtr ,
				    ControlReg & ~ XSP_CR_ENABLE_MASK);

		//xil_printf("Done transfer %d\r\n", Index);
 
    }

	XSpi_Reset(InstancePtr);


	return XST_SUCCESS;

}
Exemplo n.º 5
0
/**
*
* Transfers the specified data on the SPI bus. If the SPI device is configured
* to be a master, this function initiates bus communication and sends/receives
* the data to/from the selected SPI slave. If the SPI device is configured to
* be a slave, this function prepares the data to be sent/received when selected
* by a master. For every byte sent, a byte is received.
*
* This function/driver operates in interrupt mode and polled mode.
*  - In interrupt mode this function is non-blocking and the transfer is
*    initiated by this function and completed by the interrupt service routine.
*  - In polled mode this function is blocking and the control exits this
*    function only after all the requested data is transferred.
*
* The caller has the option of providing two different buffers for send and
* receive, or one buffer for both send and receive, or no buffer for receive.
* The receive buffer must be at least as big as the send buffer to prevent
* unwanted memory writes. This implies that the byte count passed in as an
* argument must be the smaller of the two buffers if they differ in size.
* Here are some sample usages:
* <pre>
*	XSpi_Transfer(InstancePtr, SendBuf, RecvBuf, ByteCount)
*	The caller wishes to send and receive, and provides two different
*	buffers for send and receive.
*
*	XSpi_Transfer(InstancePtr, SendBuf, NULL, ByteCount)
*	The caller wishes only to send and does not care about the received
*	data. The driver ignores the received data in this case.
*
*	XSpi_Transfer(InstancePtr, SendBuf, SendBuf, ByteCount)
*	The caller wishes to send and receive, but provides the same buffer
*	for doing both. The driver sends the data and overwrites the send
*	buffer with received data as it transfers the data.
*
*	XSpi_Transfer(InstancePtr, RecvBuf, RecvBuf, ByteCount)
*	The caller wishes to only receive and does not care about sending
*	data.  In this case, the caller must still provide a send buffer, but
*	it can be the same as the receive buffer if the caller does not care
*	what it sends. The device must send N bytes of data if it wishes to
*	receive N bytes of data.
* </pre>
* In interrupt mode, though this function takes a buffer as an argument, the
* driver can only transfer a limited number of bytes at time. It transfers only
* one byte at a time if there are no FIFOs, or it can transfer the number of
* bytes up to the size of the FIFO if FIFOs exist.
*  - In interrupt mode a call to this function only starts the transfer, the
*    subsequent transfer of the data is performed by the interrupt service
*    routine until the entire buffer has been transferred.The status callback
*    function is called when the entire buffer has been sent/received.
*  - In polled mode this function is blocking and the control exits this
*    function only after all the requested data is transferred.
*
* As a master, the SetSlaveSelect function must be called prior to this
* function.
*
* @param	InstancePtr is a pointer to the XSpi instance to be worked on.
* @param	SendBufPtr is a pointer to a buffer of data which is to be sent.
*		This buffer must not be NULL.
* @param	RecvBufPtr is a pointer to a buffer which will be filled with
*		received data. This argument can be NULL if the caller does not
*		wish to receive data.
* @param	ByteCount contains the number of bytes to send/receive. The
*		number of bytes received always equals the number of bytes sent.
*
* @return
*		-XST_SUCCESS if the buffers are successfully handed off to the
*		driver for transfer. Otherwise, returns:
*		- XST_DEVICE_IS_STOPPED if the device must be started before
*		transferring data.
*		- XST_DEVICE_BUSY indicates that a data transfer is already in
*		progress. This is determined by the driver.
*		- XST_SPI_NO_SLAVE indicates the device is configured as a
*		master and a slave has not yet been selected.
*
* @notes
*
* This function is not thread-safe.  The higher layer software must ensure that
* no two threads are transferring data on the SPI bus at the same time.
*
******************************************************************************/
int XSpi_Transfer(XSpi *InstancePtr, u8 *SendBufPtr,
		  u8 *RecvBufPtr, unsigned int ByteCount)
{
	u32 ControlReg;
	u32 GlobalIntrReg;
	u32 StatusReg;
	u32 Data = 0;
	u8  DataWidth;

	/*
	 * The RecvBufPtr argument can be NULL.
	 */
	Xil_AssertNonvoid(InstancePtr != NULL);
	Xil_AssertNonvoid(SendBufPtr != NULL);
	Xil_AssertNonvoid(ByteCount > 0);
	Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY);

	if (InstancePtr->IsStarted != XIL_COMPONENT_IS_STARTED) {
		return XST_DEVICE_IS_STOPPED;
	}

	/*
	 * Make sure there is not a transfer already in progress. No need to
	 * worry about a critical section here. Even if the Isr changes the bus
	 * flag just after we read it, a busy error is returned and the caller
	 * can retry when it gets the status handler callback indicating the
	 * transfer is done.
	 */
	if (InstancePtr->IsBusy) {
		return XST_DEVICE_BUSY;
	}

	/*
	 * Save the Global Interrupt Enable Register.
	 */
	GlobalIntrReg = XSpi_IsIntrGlobalEnabled(InstancePtr);

	/*
	 * Enter a critical section from here to the end of the function since
	 * state is modified, an interrupt is enabled, and the control register
	 * is modified (r/m/w).
	 */
	XSpi_IntrGlobalDisable(InstancePtr);

	ControlReg = XSpi_GetControlReg(InstancePtr);

	/*
	 * If configured as a master, be sure there is a slave select bit set
	 * in the slave select register. If no slaves have been selected, the
	 * value of the register will equal the mask.  When the device is in
	 * loopback mode, however, no slave selects need be set.
	 */
	if (ControlReg & XSP_CR_MASTER_MODE_MASK) {
		if ((ControlReg & XSP_CR_LOOPBACK_MASK) == 0) {
			if (InstancePtr->SlaveSelectReg ==
				InstancePtr->SlaveSelectMask) {
				if (GlobalIntrReg == TRUE) {
					/* Interrupt Mode of operation */
					XSpi_IntrGlobalEnable(InstancePtr);
				}
				return XST_SPI_NO_SLAVE;
			}
		}
	}

	/*
	 * Set the slave select register to select the device on the SPI before
	 * starting the transfer of data.
	 */
	XSpi_SetSlaveSelectReg(InstancePtr,
				InstancePtr->SlaveSelectReg);
	/*
	 * Set the busy flag, which will be cleared when the transfer
	 * is completely done.
	 */
	InstancePtr->IsBusy = TRUE;

	/*
	 * Set up buffer pointers.
	 */
	InstancePtr->SendBufferPtr = SendBufPtr;
	InstancePtr->RecvBufferPtr = RecvBufPtr;

	InstancePtr->RequestedBytes = ByteCount;
	InstancePtr->RemainingBytes = ByteCount;

	DataWidth = InstancePtr->DataWidth;

	/*
	 * Fill the DTR/FIFO with as many bytes as it will take (or as many as
	 * we have to send). We use the tx full status bit to know if the device
	 * can take more data. By doing this, the driver does not need to know
	 * the size of the FIFO or that there even is a FIFO. The downside is
	 * that the status register must be read each loop iteration.
	 */
	StatusReg = XSpi_GetStatusReg(InstancePtr);

	while (((StatusReg & XSP_SR_TX_FULL_MASK) == 0) &&
		(InstancePtr->RemainingBytes > 0)) {
		if (DataWidth == XSP_DATAWIDTH_BYTE) {
			/*
			 * Data Transfer Width is Byte (8 bit).
			 */
			Data = *InstancePtr->SendBufferPtr;
		} else if (DataWidth == XSP_DATAWIDTH_HALF_WORD) {
			/*
			 * Data Transfer Width is Half Word (16 bit).
			 */
			Data = *(u16 *)InstancePtr->SendBufferPtr;
		} else if (DataWidth == XSP_DATAWIDTH_WORD){
			/*
			 * Data Transfer Width is Word (32 bit).
			 */
			Data = *(u32 *)InstancePtr->SendBufferPtr;
		}

		XSpi_WriteReg(InstancePtr->BaseAddr, XSP_DTR_OFFSET, Data);
		InstancePtr->SendBufferPtr += (DataWidth >> 3);
		InstancePtr->RemainingBytes -= (DataWidth >> 3);
		StatusReg = XSpi_GetStatusReg(InstancePtr);
	}

	/*
	 * Start the transfer by no longer inhibiting the transmitter and
	 * enabling the device. For a master, this will in fact start the
	 * transfer, but for a slave it only prepares the device for a transfer
	 * that must be initiated by a master.
	 */
	ControlReg = XSpi_GetControlReg(InstancePtr);
	ControlReg &= ~XSP_CR_TRANS_INHIBIT_MASK;
	XSpi_SetControlReg(InstancePtr, ControlReg);

	/*
	 * If the interrupts are enabled as indicated by Global Interrupt
	 * Enable Register, then enable the transmit empty interrupt to operate
	 * in Interrupt mode of operation.
	 */
	if (GlobalIntrReg == TRUE) { /* Interrupt Mode of operation */

		/*
		 * Enable the transmit empty interrupt, which we use to
		 * determine progress on the transmission.
		 */
		XSpi_IntrEnable(InstancePtr, XSP_INTR_TX_EMPTY_MASK);

		/*
		 * End critical section.
		 */
		XSpi_IntrGlobalEnable(InstancePtr);

	} else { /* Polled mode of operation */

		/*
		 * If interrupts are not enabled, poll the status register to
		 * Transmit/Receive SPI data.
		 */
		while(ByteCount > 0) {

			/*
			 * Wait for the transfer to be done by polling the
			 * Transmit empty status bit
			 */
			do {
				StatusReg = XSpi_GetStatusReg(InstancePtr);
			} while ((StatusReg & XSP_SR_TX_EMPTY_MASK) == 0);

			/*
			 * A transmit has just completed. Process received data
			 * and check for more data to transmit. Always inhibit
			 * the transmitter while the transmit register/FIFO is
			 * being filled, or make sure it is stopped if we're
			 * done.
			 */
			ControlReg = XSpi_GetControlReg(InstancePtr);
			XSpi_SetControlReg(InstancePtr, ControlReg |
						XSP_CR_TRANS_INHIBIT_MASK);

			/*
			 * First get the data received as a result of the
			 * transmit that just completed. We get all the data
			 * available by reading the status register to determine
			 * when the Receive register/FIFO is empty. Always get
			 * the received data, but only fill the receive
			 * buffer if it points to something (the upper layer
			 * software may not care to receive data).
			 */
			StatusReg = XSpi_GetStatusReg(InstancePtr);

			while ((StatusReg & XSP_SR_RX_EMPTY_MASK) == 0) {

				Data = XSpi_ReadReg(InstancePtr->BaseAddr,
								XSP_DRR_OFFSET);
				if (DataWidth == XSP_DATAWIDTH_BYTE) {
					/*
					 * Data Transfer Width is Byte (8 bit).
					 */
					if(InstancePtr->RecvBufferPtr != NULL) {
						*InstancePtr->RecvBufferPtr++ =
							(u8)Data;
					}
				} else if (DataWidth ==
						XSP_DATAWIDTH_HALF_WORD) {
					/*
					 * Data Transfer Width is Half Word
					 * (16 bit).
					 */
					if (InstancePtr->RecvBufferPtr != NULL){
					    *(u16 *)InstancePtr->RecvBufferPtr =
							(u16)Data;
						InstancePtr->RecvBufferPtr += 2;
					}
				} else if (DataWidth == XSP_DATAWIDTH_WORD) {
					/*
					 * Data Transfer Width is Word (32 bit).
					 */
					if (InstancePtr->RecvBufferPtr != NULL){
					    *(u32 *)InstancePtr->RecvBufferPtr =
							Data;
						InstancePtr->RecvBufferPtr += 4;
					}
				}
				InstancePtr->Stats.BytesTransferred +=
						(DataWidth >> 3);
				ByteCount -= (DataWidth >> 3);
				StatusReg = XSpi_GetStatusReg(InstancePtr);
			}

			if (InstancePtr->RemainingBytes > 0) {

				/*
				 * Fill the DTR/FIFO with as many bytes as it
				 * will take (or as many as we have to send).
				 * We use the Tx full status bit to know if the
				 * device can take more data.
				 * By doing this, the driver does not need to
				 * know the size of the FIFO or that there even
				 * is a FIFO.
				 * The downside is that the status must be read
				 * each loop iteration.
				 */
				StatusReg = XSpi_GetStatusReg(InstancePtr);

				while(((StatusReg & XSP_SR_TX_FULL_MASK)== 0) &&
					(InstancePtr->RemainingBytes > 0)) {
					if (DataWidth == XSP_DATAWIDTH_BYTE) {
						/*
						 * Data Transfer Width is Byte
						 * (8 bit).
						 */
						Data = *InstancePtr->
								SendBufferPtr;

					} else if (DataWidth ==
						XSP_DATAWIDTH_HALF_WORD) {

						/*
						 * Data Transfer Width is Half
						 * Word (16 bit).
			 			 */
						Data = *(u16 *)InstancePtr->
								SendBufferPtr;
					} else if (DataWidth ==
							XSP_DATAWIDTH_WORD) {
						/*
						 * Data Transfer Width is Word
						 * (32 bit).
			 			 */
						Data = *(u32 *)InstancePtr->
								SendBufferPtr;
					}
					XSpi_WriteReg(InstancePtr->BaseAddr,
							XSP_DTR_OFFSET, Data);
					InstancePtr->SendBufferPtr +=
							(DataWidth >> 3);
					InstancePtr->RemainingBytes -=
							(DataWidth >> 3);
					StatusReg = XSpi_GetStatusReg(
							InstancePtr);
				}

				/*
				 * Start the transfer by not inhibiting the
				 * transmitter any longer.
				 */
				ControlReg = XSpi_GetControlReg(InstancePtr);
				ControlReg &= ~XSP_CR_TRANS_INHIBIT_MASK;
				XSpi_SetControlReg(InstancePtr, ControlReg);
			}
		}

		/*
		 * Stop the transfer (hold off automatic sending) by inhibiting
		 * the transmitter.
		 */
		ControlReg = XSpi_GetControlReg(InstancePtr);
		XSpi_SetControlReg(InstancePtr,
				    ControlReg | XSP_CR_TRANS_INHIBIT_MASK);

		/*
		 * Select the slave on the SPI bus when the transfer is
		 * complete, this is necessary for some SPI devices,
		 * such as serial EEPROMs work correctly as chip enable
		 * may be connected to slave select
		 */
		XSpi_SetSlaveSelectReg(InstancePtr,
					InstancePtr->SlaveSelectMask);
		InstancePtr->IsBusy = FALSE;
	}
Exemplo n.º 6
0
/**
*
* This function does a minimal test on the Spi device and driver as a
* design example. The purpose of this function is to illustrate how to use
* the XSpi component using the polled mode.
*
* This function sends data and expects to receive the same data.
*
*
* @param	SpiInstancePtr is a pointer to the instance of Spi component.
* @param	SpiDeviceId is the Device ID of the Spi Device and is the
*		XPAR_<SPI_instance>_DEVICE_ID value from xparameters.h.
*
* @return	XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note
*
* This function contains an infinite loop such that if the Spi device is not
* working it may never return.
*
******************************************************************************/
int SpiPolledExample(XSpi *SpiInstancePtr, u16 SpiDeviceId)
{
	int Status;
	u32 Count;
	u8 Test;
	XSpi_Config *ConfigPtr;	/* Pointer to Configuration data */

	/*
	 * Initialize the SPI driver so that it is  ready to use.
	 */
	ConfigPtr = XSpi_LookupConfig(SpiDeviceId);
	if (ConfigPtr == NULL) {
		return XST_DEVICE_NOT_FOUND;
	}

	Status = XSpi_CfgInitialize(SpiInstancePtr, ConfigPtr,
				  ConfigPtr->BaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Perform a self-test to ensure that the hardware was built correctly.
	 */
	Status = XSpi_SelfTest(SpiInstancePtr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Run loopback test only in case of standard SPI mode.
	 */
	if (SpiInstancePtr->SpiMode != XSP_STANDARD_MODE) {
		return XST_SUCCESS;
	}

	/*
	 * Set the Spi device as a master and in loopback mode.
	 */
	Status = XSpi_SetOptions(SpiInstancePtr, XSP_MASTER_OPTION |
 					XSP_LOOPBACK_OPTION);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}


	/*
	 * Start the SPI driver so that the device is enabled.
	 */
	XSpi_Start(SpiInstancePtr);

	/*
	 * Disable Global interrupt to use polled mode operation
	 */
	XSpi_IntrGlobalDisable(SpiInstancePtr);

	/*
	 * Initialize the write buffer with pattern to write, initialize the
	 * read buffer to zero so it can be verified after the read, the
	 * Test value that is added to the unique value allows the value to be
	 * changed in a debug environment.
	 */
	Test = 0x10;
	for (Count = 0; Count < BUFFER_SIZE; Count++) {
		WriteBuffer[Count] = (u8)(Count + Test);
		ReadBuffer[Count] = 0;
	}


	/*
	 * Transmit the data.
	 */
	XSpi_Transfer(SpiInstancePtr, WriteBuffer, ReadBuffer, BUFFER_SIZE);

	/*
	 * Compare the data received with the data that was transmitted.
	 */
	for (Count = 0; Count < BUFFER_SIZE; Count++) {
		if (WriteBuffer[Count] != ReadBuffer[Count]) {
			return XST_FAILURE;
		}
	}

	return XST_SUCCESS;
}
Exemplo n.º 7
0
DSTATUS disk_initialize (void)
{
	BYTE n, cmd, ty, buf[4];
	UINT tmr;


#if AXI_SPI
	/*
	 * Initialize the SPI driver so that it's ready to use,
	 * specify the device ID that is generated in xparameters.h.
	 */
	XStatus Status = XSpi_Initialize(&Spi, XPAR_SDCARD_SPI_DEVICE_ID);
	if(Status != XST_SUCCESS) {\
   		xil_printf("Failure INIT\r\n");
		return XST_FAILURE;
	}
	/*
	 * Set the SPI device as a master and in manual slave select mode such
	 * that the slave select signal does not toggle for every byte of a
	 * transfer, this must be done before the slave select is set.
	 */
	Status = XSpi_SetOptions(&Spi, XSP_MASTER_OPTION |
						XSP_MANUAL_SSELECT_OPTION);
	if(Status != XST_SUCCESS) {
   		xil_printf("Failure Options\r\n");
		return XST_FAILURE;
	}

	Status = XSpi_SetSlaveSelect(&Spi, 1);
	if(Status != XST_SUCCESS) {
   		xil_printf("Failure Slave Select\r\n");
		return XST_FAILURE;
	}

	XSpi_Start(&Spi);
	XSpi_IntrGlobalDisable(&Spi);

	DLY_US(1);
#else
	INIT_PORT();

	CS_H();
#endif
	skip_mmc(10);			/* Dummy clocks */

	ty = 0;
	if (send_cmd(CMD0, 0) == 1) {			/* Enter Idle state */
		if (send_cmd(CMD8, 0x1AA) == 1) {	/* SDv2 */
			for (n = 0; n < 4; n++) buf[n] = rcvr_mmc();	/* Get trailing return value of R7 resp */
			if (buf[2] == 0x01 && buf[3] == 0xAA) {			/* The card can work at vdd range of 2.7-3.6V */
				for (tmr = 1000; tmr; tmr--) {				/* Wait for leaving idle state (ACMD41 with HCS bit) */
					if (send_cmd(ACMD41, 1UL << 30) == 0) break;
					DLY_US(1000);
				}
				if (tmr && send_cmd(CMD58, 0) == 0) {		/* Check CCS bit in the OCR */
					for (n = 0; n < 4; n++) buf[n] = rcvr_mmc();
					ty = (buf[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;	/* SDv2 (HC or SC) */
				}
			}
		} else {							/* SDv1 or MMCv3 */
			if (send_cmd(ACMD41, 0) <= 1) 	{
				ty = CT_SD1; cmd = ACMD41;	/* SDv1 */
			} else {
				ty = CT_MMC; cmd = CMD1;	/* MMCv3 */
			}
			for (tmr = 1000; tmr; tmr--) {			/* Wait for leaving idle state */
				if (send_cmd(cmd, 0) == 0) break;
				DLY_US(1000);
			}
			if (!tmr || send_cmd(CMD16, 512) != 0)			/* Set R/W block length to 512 */
				ty = 0;
		}
	}
	CardType = ty;
	release_spi();

	return ty ? 0 : STA_NOINIT;
}
/**
*
* This function does a minimal test on the Spi device and driver as a design
* example. The purpose of this function is to illustrate the device slave
* functionality in polled mode. This function receives data from a master and
* prints the received data.
*
* @param	SpiInstancePtr is a pointer to the instance of Spi component.
*
* @param	SpiDeviceId is the Device ID of the Spi Device and is the
*		XPAR_<SPI_instance>_DEVICE_ID value from xparameters.h.
*
* @return	XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note		This function contains an infinite loop such that if the Spi
*		device doesn't receive any data, it may never return.
*
******************************************************************************/
int SpiSlavePolledExample(XSpi *SpiInstancePtr, u16 SpiDeviceId)
{
	XSpi_Config *ConfigPtr;
	int Status;
	u32 Count;

	xil_printf("\r\nEntering the Spi Slave Polled Example.\r\n");
	xil_printf("Waiting for data from SPI master\r\n");

	/*
	 * Initialize the SPI driver so that it's ready to use, specify the
	 * device ID that is generated in xparameters.h.
	 */
	ConfigPtr = XSpi_LookupConfig(SpiDeviceId);
	if (ConfigPtr == NULL) {
		return XST_FAILURE;
	}
	Status = XSpi_CfgInitialize(SpiInstancePtr, ConfigPtr,
				ConfigPtr->BaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * The SPI device is a slave by default and the clock phase and polarity
	 * have to be set according to its master. In this example, CPOL is set
	 * to active low and CPHA is set to 1.
	 */
	Status = XSpi_SetOptions(SpiInstancePtr, XSP_CLK_PHASE_1_OPTION |
				 XSP_CLK_ACTIVE_LOW_OPTION);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Start the SPI driver so that the device is enabled.
	 */
	XSpi_Start(SpiInstancePtr);

	/*
	 * Disable Global interrupt to use polled mode operation.
	 */
	XSpi_IntrGlobalDisable(SpiInstancePtr);

	/*
	 * Initialize the write buffer with pattern to write, initialize the
	 * read buffer to zero so that it can be verified after the read.
	 */
	Test = 0xF0;
	for (Count = 0; Count < BUFFER_SIZE; Count++) {
		WriteBuffer[Count] = (u8)(Count + Test);
		ReadBuffer[Count] = 0;
	}

	/*
	 * Prepare the data buffers for transmission and to send/receive data
	 * when the SPI device is selected by a master.
	 */
	XSpi_Transfer(SpiInstancePtr, WriteBuffer, ReadBuffer, BUFFER_SIZE);

	/*
	 * Print all the data received from the master so that it can be
	 * compared with the data sent by the master.
	 */
	xil_printf("\r\nReceived data is:\r\n");
	for (Count = 0; Count < BUFFER_SIZE; Count++) {
		xil_printf("0x%x \r\n", ReadBuffer[Count]);
	}

	xil_printf("\r\nExiting the Spi Slave Polled Example.\r\n");

	return XST_SUCCESS;
}
Exemplo n.º 9
0
void LEDPIN_Init(void)
{
	int Status;
	Status = XGpio_Initialize(&dc, XPAR_AXI_GPIO_0_DEVICE_ID);
		 if (Status != XST_SUCCESS)  {
			  return XST_FAILURE;
		 }

	Status = XGpio_Initialize(&reset, XPAR_AXI_GPIO_1_DEVICE_ID);
		 if (Status != XST_SUCCESS)  {
		 		return XST_FAILURE;
		 }

		 Status = XGpio_Initialize(&led1, XPAR_AXI_GPIO_2_DEVICE_ID);
		 		 if (Status != XST_SUCCESS)  {
		 		 		return XST_FAILURE;
		 		 }

		 		Status = XGpio_Initialize(&led2, XPAR_AXI_GPIO_3_DEVICE_ID);
		 				 if (Status != XST_SUCCESS)  {
		 				 		return XST_FAILURE;
		 				 }
	XGpio_SetDataDirection(&dc, DC_CHANNEL, 0x0);
	XGpio_DiscreteWrite(&dc, DC_CHANNEL, 0x0);

	XGpio_SetDataDirection(&reset, reset_CHANNEL, 0x0);
	XGpio_DiscreteWrite(&reset, reset_CHANNEL, 0x0);

	XGpio_SetDataDirection(&led1, reset_CHANNEL, 0x0);
	XGpio_DiscreteWrite(&led1, reset_CHANNEL, 0x0);

	XGpio_SetDataDirection(&led2, reset_CHANNEL, 0x0);
	XGpio_DiscreteWrite(&led2, reset_CHANNEL, 0x0);

	XGpio_DiscreteWrite(&led2, reset_CHANNEL, 0x1);
	XSpi_Config *ConfigPtr;	/* Pointer to Configuration data */

		/*
		 * Initialize the SPI driver so that it is  ready to use.
		 */
		ConfigPtr = XSpi_LookupConfig(SPI_DEVICE_ID);
		if (ConfigPtr == NULL) {
			return XST_DEVICE_NOT_FOUND;

		}

		Status = XSpi_CfgInitialize(&Spi, ConfigPtr,
					  ConfigPtr->BaseAddress);
		if (Status != XST_SUCCESS) {
			return XST_FAILURE;

		}

		/*
		 * Perform a self-test to ensure that the hardware was built correctly
		 */
		Status = XSpi_SelfTest(&Spi);
		if (Status != XST_SUCCESS) {
			return XST_FAILURE;

		}

		Status = XSpi_SetOptions(&Spi, XSP_MASTER_OPTION | XSP_CLK_ACTIVE_LOW_OPTION );//|  XSP_CR_CLK_POLARITY_MASK | XSP_CR_CLK_PHASE_MASK);
						if (Status != XST_SUCCESS) {
							return XST_FAILURE;
							XGpio_DiscreteWrite(&led2, reset_CHANNEL, 0x0);
						}

				// | XSP_CR_CPOL_MASK|XSP_CR_CPHA_MASK);
		//XSpiPs_SetClkPrescaler(&Spi, XSPIPS_CLK_PRESCALE_256);

		XSpi_SetSlaveSelect(&Spi, 0x1);

		//XSpi_Start(&Spi);

		Status = XSpi_Start(&Spi);
				if (Status != XST_SUCCESS) {
					return XST_FAILURE;

				}
		XSpi_IntrGlobalDisable(&Spi);
		/*Status = XSpi_SetSlaveSelect(&Spi, 0x1);
				if (Status != XST_SUCCESS) {
					return XST_FAILURE;
				}*/


}
Exemplo n.º 10
0
int Mrf24j::initDrivers(void)
{
	// TODO: check pin direction
	/*pinMode(_pin_reset, OUTPUT);
	pinMode(_pin_cs, OUTPUT);
	pinMode(_pin_int, INPUT);*/

	XSpi_Config *SPIConfigPtr;
	XGpio_Config *GPIOConfigPtr;

	int status;

	/*
	* Initialize the SPI driver so that it is  ready to use.
	*/
	SPIConfigPtr = XSpi_LookupConfig(XPAR_SPI_0_DEVICE_ID);
	if (SPIConfigPtr == NULL)
	{
		return XST_DEVICE_NOT_FOUND;
	}

	status = XSpi_CfgInitialize(&MSpiInstance, SPIConfigPtr, SPIConfigPtr->BaseAddress);
	if (status != XST_SUCCESS)
	{
		return XST_FAILURE;
	}

	/*
	 * Set the Spi device as a master.
	 */
	status = XSpi_SetOptions( &MSpiInstance, XSP_MASTER_OPTION | XSP_MANUAL_SSELECT_OPTION ); //TO DO, add msb first config
	if (status != XST_SUCCESS)
	{
		return XST_FAILURE;
	}

	/*
	* Start the SPI driver so that the device is enabled.
	*/
	XSpi_Start(&MSpiInstance);

	/*
	* Disable Global interrupt to use polled mode operation
	*/
	XSpi_IntrGlobalDisable(&MSpiInstance);

	//GPIO config
	GPIOConfigPtr = XGpio_LookupConfig(XPAR_GPIO_1_DEVICE_ID);

	status = XGpio_CfgInitialize(&gpioInstance, GPIOConfigPtr, GPIOConfigPtr->BaseAddress);
	if (status != XST_SUCCESS)
	{
		return XST_FAILURE;
	}

	XGpio_SetDataDirection(&gpioInstance, 1, 0x1); //check data direction

	XGpio_DiscreteWrite(&gpioInstance, 1, 0x6); //check register, check wake pin if it is ok high


	//for(int i = 0; i < 20; i++)
	//{
	//u32 intr = XGpio_InterruptGetEnabled(&gpioInstance);

	//u32 sts = XGpio_InterruptGetStatus(&gpioInstance);

	//XGpio_InterruptClear(&gpioInstance, XGPIO_IR_CH1_MASK);

	 ///sts = XGpio_InterruptGetStatus(&gpioInstance);

	//u32 usts = sts;
	//}
	/*SPI.setBitOrder(MSBFIRST) ;
	SPI.setDataMode(SPI_MODE0);
	SPI.begin();*/
	return XST_SUCCESS;
}
Exemplo n.º 11
0
void OledHostInit()
{
	XSpi_Config *SPIConfigPtr;

	XGpio_Config *GPIOConfigPtr;
	int status;
	/*
	* Initialize the SPI driver so that it is  ready to use.
	*/
	SPIConfigPtr = XSpi_LookupConfig(XPAR_SPI_1_DEVICE_ID);
	if (SPIConfigPtr == NULL)
	{
		printf("OledHostInit: ERROR: SPI device not found");
	}

	status = XSpi_CfgInitialize(&SpiInstance, SPIConfigPtr, SPIConfigPtr->BaseAddress);
	if (status != XST_SUCCESS)
	{
		printf("OledHostInit: ERROR: cannot init SPI");
	}

	/*
	 * Set the Spi device as a master.
	 */
	status = XSpi_SetOptions( &SpiInstance, XSP_MASTER_OPTION | XSP_MANUAL_SSELECT_OPTION |
			XSP_CLK_ACTIVE_LOW_OPTION | XSP_CLK_PHASE_1_OPTION);
	if (status != XST_SUCCESS)
	{
		printf("OledHostInit: ERROR: set SPI options");
	}


	/*
	* Start the SPI driver so that the device is enabled.
	*/
	XSpi_Start(&SpiInstance);

	/*
	* Disable Global interrupt to use polled mode operation
	*/
	XSpi_IntrGlobalDisable(&SpiInstance);

	u32 slaveReg = XSpi_GetSlaveSelectReg(&SpiInstance);

	XSpi_SetSlaveSelectReg(&SpiInstance, 0x00);

	slaveReg = XSpi_GetSlaveSelectReg(&SpiInstance);

	//GPIO config
	GPIOConfigPtr = XGpio_LookupConfig(XPAR_GPIO_0_DEVICE_ID);
	if (GPIOConfigPtr == NULL)
	{
		printf("OledHostInit: ERROR: GPIO device not found");
	}

	status = XGpio_CfgInitialize(&gpioInstance, GPIOConfigPtr, GPIOConfigPtr->BaseAddress);
	if (status != XST_SUCCESS)
	{
		printf("OledHostInit: ERROR: cannot init GPIO");
	}

	XGpio_SetDataDirection(&gpioInstance, 1, 0xf0);

	XGpio_DiscreteWrite(&gpioInstance, 1, 0x0F);

}