Example #1
0
int init_spi(XSpi *SpiInstancePtr){
	int Status;
	XSpi_Config *ConfigPtr;	/* Pointer to Configuration data */

	ConfigPtr = XSpi_LookupConfig(SPI_DEVICE_ID);
	if (ConfigPtr == NULL) {
		return XST_DEVICE_NOT_FOUND;
	}
	Status = XSpi_CfgInitialize(SpiInstancePtr, ConfigPtr,
				  ConfigPtr->BaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	Status = XSpi_SelfTest(SpiInstancePtr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	if (SpiInstancePtr->SpiMode != XSP_STANDARD_MODE) {
		return XST_SUCCESS;
	}
	Status = XSpi_SetOptions(SpiInstancePtr, XSP_MASTER_OPTION);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}
	XSpi_Start(SpiInstancePtr);
	XSpi_IntrGlobalDisable(SpiInstancePtr);
	return XST_SUCCESS;
}
Example #2
0
/******************************************************************************
 * This function sets the SPI interrupt handler and SPI options. This function
 * shall be used after the interrupts are registered.
 *
 * @param	psSpi is the pointer to the SPI driver structure.
 *
 * @return	XST_SUCCESS - Everything went well
 * 			XST_FAILURE - Failure
 *****************************************************************************/
XStatus fnOledDriverOptions(XSpi *psSpi) {

	XStatus Status;

	// Set the handler for the SPI that will be called from the interrupt
	// context
	XSpi_SetStatusHandler(psSpi, psSpi, (XSpi_StatusHandler)fnOledSpiIsr);

	// Set SPI device as master
	Status = XSpi_SetOptions(psSpi, XSP_MASTER_OPTION |
			XSP_MANUAL_SSELECT_OPTION);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	// Select the slave on the SPI bus (although the SS pin is not connected)
	Status = XSpi_SetSlaveSelect(psSpi, 0x01);
	if(Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	// Start the SPI driver
	XSpi_Start(psSpi);

	return XST_SUCCESS;
}
Example #3
0
int AD1_SPIInit(XSpi *SpiInstancePtr){
	int Status;

	Status = XSpi_CfgInitialize(SpiInstancePtr, &AD1Config, AD1Config.BaseAddress);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}



	Status = XSpi_SetOptions(SpiInstancePtr, (XSP_MASTER_OPTION | XSP_CLK_ACTIVE_LOW_OPTION | XSP_CLK_PHASE_1_OPTION) | XSP_MANUAL_SSELECT_OPTION);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	Status = XSpi_SetSlaveSelect(SpiInstancePtr, 1);
	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);

	return XST_SUCCESS;

}
Example #4
0
static int
xspi_open(struct inode *inode, struct file *filp)
{
	int retval = 0;
	struct xspi_instance *dev;

	func_enter();

	dev = container_of(inode->i_cdev, struct xspi_instance, cdev);
	filp->private_data = dev; /* for other methods */

	if (dev == NULL)
		return -ENODEV;

	if (down_interruptible(&dev->sem))
		return -EINTR;

	while (dev->use_count++ == 0) {
		/*
		 * This was the first opener; we need  to get the IRQ,
		 * and to setup the device as master.
		 */
		retval = request_irq(dev->irq, xspi_isr, 0, XSPI_NAME,
				     &dev->Spi);
		if (retval != 0) {
			printk(KERN_ERR XSPI_NAME
			       "%d: Could not allocate interrupt %d.\n",
			       dev->device_id, dev->irq);
			break;
		}

		if (XSpi_SetOptions(&dev->Spi, XSPI_DEFAULT_OPTIONS) !=
		    XST_SUCCESS) {
			printk(KERN_ERR XSPI_NAME
			       "%d: Could not set device options.\n",
			       dev->device_id);
			free_irq(dev->irq, &dev->Spi);
			retval = -EIO;
			break;
		}

		if (XSpi_Start(&dev->Spi) != XST_SUCCESS) {
			printk(KERN_ERR XSPI_NAME
			       "%d: Could not start the device.\n",
			       dev->device_id);
			free_irq(dev->irq, &dev->Spi);
			retval = -EIO;
			break;
		}

		break;
	}

	if (retval != 0)
		--dev->use_count;

	up(&dev->sem);
	return retval;
}
/*****
 * PmodCtlSys_init() - initialize the PmodCtlsys
 * 
 * This function initializes the PmodCtlSys ADC (an MSP3202).  Sine
 * the MSP3202 is a SPI device the primary purpose of the function
 * is to configure and start the Xilinx SPI peripheral.  It checks
 * basic functionality of the SPI peripheral by running the self-test.
 * It also writes the correct data to the global send buffer PmodCtlSys_SndBuf[]
 * because its contents never change.  This code is based on the Xilinx SPI
 * driver "spi_polled_example.c" example included in the EDK.
 *****/
XStatus PmodCtlSys_init(XSpi *SpiInstancePtr, u16 SpiDeviceID)
{
	XStatus Status;				// return status from SPI driver functions
	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;
	}

	// Set the Spi device as a master,
	// SS goes low for entire transaction (does not toggle every 8 bits)
	// All other bits are OK as defaults
	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
	// and then disable the Global interrupt because we
	// are using the peripheral in polled mode
	XSpi_Start(SpiInstancePtr);
	XSpi_IntrGlobalDisable(SpiInstancePtr);
	
	// initialize the SPI send buffer.  Since the PmodCtlSys only
	// uses the channel 0 ADC in the MPS3202 the send buffer
	// contents will always be the same.
	//	Command Byte 1 - Start bit is in the LSB, others are don't care
	//	Command Byte 2 - single-ended input, Select Channel 0, send data most-significant bit first	
	//	Command Byte 2 - needed to have ADC return all the counts bits.  All bits are don't care
	PmodCtlSys_SndBuf[0] = MSP3202_START_MSK;
	PmodCtlSys_SndBuf[1] = (MSP3202_SGL_MSK << 7) | (MSP3202_SEL_CHNL0_MSK << 6) | (MSP3202_SEL_MSBF_MSK << 5);
	PmodCtlSys_SndBuf[2] = 0x55;
	
	return XST_SUCCESS;
}
Example #6
0
/***************************************************************************//**
* @brief spi_init
*******************************************************************************/
int32_t spi_init(uint32_t device_id,
				 uint8_t clk_pha,
				 uint8_t clk_pol)
{
	uint32_t base_addr	 = 0;
	uint32_t spi_options = 0;

#ifdef _XPARAMETERS_PS_H_
	spi_config = XSpiPs_LookupConfig(device_id);

	base_addr = spi_config->BaseAddress;
	XSpiPs_CfgInitialize(&spi_instance, spi_config, base_addr);

	spi_options = XSPIPS_MASTER_OPTION |
			(clk_pol ? XSPIPS_CLK_ACTIVE_LOW_OPTION : 0) |
			(clk_pha ? XSPIPS_CLK_PHASE_1_OPTION : 0) |
			(spi_decoded_cs ? XSPIPS_DECODE_SSELECT_OPTION : 0) |
			XSPIPS_FORCE_SSELECT_OPTION;
	XSpiPs_SetOptions(&spi_instance, spi_options);

	XSpiPs_SetClkPrescaler(&spi_instance, XSPIPS_CLK_PRESCALE_32);

	/* FIXME: Temporary 15.2 Fix */
	XSpiPs_CfgInitialize(&spi_instance, spi_config, base_addr);

	XSpiPs_SetOptions(&spi_instance, spi_options);

	XSpiPs_SetClkPrescaler(&spi_instance, XSPIPS_CLK_PRESCALE_32);
#else
	XSpi_Initialize(&spi_instance, device_id);

	XSpi_Stop(&spi_instance);

	spi_config = XSpi_LookupConfig(device_id);

	base_addr = spi_config->BaseAddress;
	XSpi_CfgInitialize(&spi_instance, spi_config, base_addr);

	spi_options = XSP_MASTER_OPTION |
				  (clk_pol ? XSP_CLK_ACTIVE_LOW_OPTION : 0) |
				  (clk_pha ? XSP_CLK_PHASE_1_OPTION : 0) |
				  XSP_MANUAL_SSELECT_OPTION;
	XSpi_SetOptions(&spi_instance, spi_options);

	XSpi_Start(&spi_instance);

	XSpi_IntrGlobalDisable(&spi_instance);
#endif

	return 0;
}
Example #7
0
/***************************************************************************//**
 * @brief spi_init
*******************************************************************************/
int32_t spi_init(uint32_t device_id,
				 uint8_t  clk_pha,
				 uint8_t  clk_pol)
{

	uint32_t base_addr	 = 0;
	uint32_t control_val = 0;
#ifdef _XPARAMETERS_PS_H_
	uint8_t  byte		 = 0;

	spi_config = XSpiPs_LookupConfig(device_id);
	base_addr = spi_config->BaseAddress;
	XSpiPs_CfgInitialize(&spi_instance, spi_config, base_addr);

	control_val = XSPIPS_CR_SSFORCE_MASK |
				  XSPIPS_CR_SSCTRL_MASK |
				  4 << XSPIPS_CR_PRESC_SHIFT |
				  (clk_pha ? XSPIPS_CR_CPHA_MASK : 0) |
				  (clk_pol ? XSPIPS_CR_CPOL_MASK : 0) |
				  XSPIPS_CR_MSTREN_MASK;

	XSpiPs_WriteReg(base_addr, XSPIPS_CR_OFFSET, control_val);

	for(byte = 0; byte < 128; byte++)
	{
		XSpiPs_ReadReg(base_addr, XSPIPS_RXD_OFFSET);
	}
#else
	XSpi_Initialize(&spi_instance, device_id);
	XSpi_Stop(&spi_instance);
	spi_config = XSpi_LookupConfig(device_id);
	base_addr = spi_config->BaseAddress;
	XSpi_CfgInitialize(&spi_instance, spi_config, base_addr);
	control_val = XSP_MASTER_OPTION |
				  XSP_CLK_PHASE_1_OPTION |
				  XSP_MANUAL_SSELECT_OPTION;
	XSpi_SetOptions(&spi_instance, control_val);
	XSpi_Start(&spi_instance);
	XSpi_IntrGlobalDisable(&spi_instance);
	XSpi_SetSlaveSelect(&spi_instance, 1);
#endif
	return SUCCESS;
}
int ACL_SPIInit(XSpi *SpiInstancePtr){
	int Status;
	XSpi_Config *ConfigPtr; /* Pointer to Configuration data */

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

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



	Status = XSpi_SetOptions(SpiInstancePtr, (XSP_MASTER_OPTION | XSP_CLK_ACTIVE_LOW_OPTION | XSP_CLK_PHASE_1_OPTION) | XSP_MANUAL_SSELECT_OPTION); //Manual SS off
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	Status = XSpi_SetSlaveSelect(SpiInstancePtr, 1);
	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);

	return XST_SUCCESS;

}
/**
*
* 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;
}
Example #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;
}
Example #11
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;
}
/**
*
* 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;
}
/**
*
* 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 interrupt mode.
*
* This function sends data and expects to receive the same data.
*
*
* @param	IntcInstancePtr is a pointer to the instance of the INTC
* 		component.
* @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.
* @param	SpiIntrId is the interrupt Id and is typically
*		XPAR_<INTC_instance>_<SPI_instance>_VEC_ID value from
*		xparameters.h .
*
* @return	XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note
*
* This function contains an infinite loop such that if interrupts are not
* working it may never return.
*
******************************************************************************/
int SpiIntrExample(XIntc *IntcInstancePtr, XSpi *SpiInstancePtr,
			u16 SpiDeviceId, u16 SpiIntrId)
{
	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;
	}

	/*
	 * Connect the Spi device to the interrupt subsystem such that
	 * interrupts can occur. This function is application specific.
	 */
	Status = SpiSetupIntrSystem(IntcInstancePtr,
					SpiInstancePtr,
				 	SpiIntrId);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Setup the handler for the SPI that will be called from the interrupt
	 * context when an SPI status occurs, specify a pointer to the SPI
	 * driver instance as the callback reference so the handler is able to
	 * access the instance data.
	 */
	XSpi_SetStatusHandler(SpiInstancePtr, SpiInstancePtr,
		 		(XSpi_StatusHandler) SpiIntrHandler);

	/*
	 * 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 interrupts and the device are enabled.
	 */
	XSpi_Start(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.
	 */
	TransferInProgress = TRUE;
	XSpi_Transfer(SpiInstancePtr, WriteBuffer, ReadBuffer, BUFFER_SIZE);

	/*
	 * Wait for the transmission to be complete.
	 */
	while (TransferInProgress);


	/*
	 * Disable the Spi interrupt.
	 */
	SpiDisableIntrSystem(IntcInstancePtr, SpiIntrId);

	/*
	 * 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;
}
/**
*
* 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 interrupt 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 or if the interrupts are not
*		working, it may never return.
*
******************************************************************************/
static int SpiSlaveIntrExample(XSpi *SpiInstancePtr, u16 SpiDeviceId)
{
	XSpi_Config *ConfigPtr;
	int Status;
	u32 Count;

	xil_printf("\r\nEntering the Spi Slave Interrupt 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;
	}

	/*
	 * Connect the SPI driver to the interrupt subsystem such that
	 * interrupts can occur. This function is application specific.
	 */
	Status = SetupInterruptSystem(SpiInstancePtr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Setup the handler for the SPI that will be called from the interrupt
	 * context when an SPI status occurs, specify a pointer to the SPI
	 * driver instance as the callback reference so the handler is able to
	 * access the instance data.
	 */
	XSpi_SetStatusHandler(SpiInstancePtr,SpiInstancePtr,(XSpi_StatusHandler)
			      SpiHandler);

	/*
	 * 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);

	/*
	 * Enable the DTR half-empty interrupt while transfering more than
	 * FIFO_DEPTH number of bytes in slave mode, so that the Tx FIFO
	 * is never empty during a transfer. If the Tx FIFO is empty during
	 * a transfer, it results in master receiving invalid data.
	 */
	XSpi_IntrEnable(SpiInstancePtr, XSP_INTR_TX_HALF_EMPTY_MASK);

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

	/*
	 * Transmit data as a slave, when the master starts sending data.
	 */
	TransferInProgress = TRUE;
	Status = XSpi_Transfer(SpiInstancePtr, WriteBuffer, ReadBuffer,
				BUFFER_SIZE);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Wait till the transfer is complete.
	 */
	while (TransferInProgress == TRUE);

	/*
	 * Print all the data received from 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 Interrupt Example.\r\n");

	return XST_SUCCESS;
}
Example #15
0
int main() {

  xil_printf("Master SPI oRSC echo test\n");

  // initialize stdout.
  init_platform();

  tx_buffer = cbuffer_new();
  rx_buffer = cbuffer_new();

  spi_stream = spi_stream_init(
      tx_buffer, rx_buffer, 
      DoSpiTransfer, // callback which triggers a SPI transfer
      0);

  int Status;
  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) {
    xil_printf ("Error: could not lookup SPI configuration\n");
    return XST_DEVICE_NOT_FOUND;
  }

  Status = XSpi_CfgInitialize(&SpiInstance, ConfigPtr,
      ConfigPtr->BaseAddress);
  if (Status != XST_SUCCESS) {
    xil_printf("Error: could not initialize the SPI device\n");
    return XST_FAILURE;
  }

  Status = XSpi_SelfTest(&SpiInstance);
  if (Status != XST_SUCCESS) {
    xil_printf("Error: The SPI self test failed.\n");
    return XST_FAILURE;
  }

  /*
   * Connect the Spi device to the interrupt subsystem such that
   * interrupts can occur. This function is application specific.
   */
  Status = SpiSetupIntrSystem(&IntcInstance, &SpiInstance, SPI_IRPT_INTR);
  if (Status != XST_SUCCESS) {
    xil_printf("Error: Could not setup interrupt system.\n");
    return XST_FAILURE;
  }

  /*
   * Set the Spi device as a master.
   */
  Status = XSpi_SetOptions(&SpiInstance, XSP_MASTER_OPTION);
  if (Status != XST_SUCCESS) {
    xil_printf("Error: Could not set as master\n");
    return XST_FAILURE;
  }

  // Go!
  XSpi_Start(&SpiInstance);

  // Note: to disable interrupt, do: XIntc_Disconnect(&IntcInstance,
  // SPI_IRPT_INTR);

  u32 expected_rx = 0;
  u32 current_tx = 0;

  while (1) {
    // fill up the transmit buffer
    while (cbuffer_freespace(tx_buffer)) {
      cbuffer_push_back(tx_buffer, current_tx++);
    }
    // check to make sure the received buffer is what we expect
    while (cbuffer_size(rx_buffer)) {
      u32 front = cbuffer_value_at(rx_buffer, 0);
      if (front != expected_rx) {
        //xil_printf("Error: expected %lx, got %lx!\n", expected_rx, front);
        xil_printf("Error: data value\n");
      }
      expected_rx++;
      cbuffer_deletefront(rx_buffer, 1);
    }
  }

  return 0;
}
Example #16
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;
				}*/


}
Example #17
0
static int
xspi_ioctl(struct inode *inode, struct file *filp,
	   unsigned int cmd, unsigned long arg)
{
	struct xspi_instance *dev = filp->private_data;

	/* paranoia check */
	if (!dev)
		return -ENODEV;

	switch (cmd) {
	case XSPI_IOC_GETSLAVESELECT:
		{
			int i;

			i = ffs(XSpi_GetSlaveSelect(&dev->Spi)) - 1;
			return put_user(i, (int *) arg);	/* -1 means nothing selected */
		}
		break;
	case XSPI_IOC_SETSLAVESELECT:
		{
			int i;
			int retval;

			if (get_user(i, (int *) arg) != 0)
				return -EFAULT;

			if (i < -1 || i > 31)
				return -EINVAL;

			/* Lock the device. */
			if (down_interruptible(&dev->sem))
				return -ERESTARTSYS;

			if (i == -1)
				retval =
				    convert_status(XSpi_SetSlaveSelect
						   (&dev->Spi, 0));
			else
				retval =
				    convert_status(XSpi_SetSlaveSelect
						   (&dev->Spi, (u32) 1 << i));

			/* Unlock the device. */
			up(&dev->sem);

			return retval;
		}
		break;
	case XSPI_IOC_GETOPTS:
		{
			struct xspi_ioc_options xspi_opts;
			u32 xspi_options;

			xspi_options = XSpi_GetOptions(&dev->Spi);

			memset(&xspi_opts, 0, sizeof (xspi_opts));
			if (dev->Spi.HasFifos)
				xspi_opts.has_fifo = 1;
			if (xspi_options & XSP_CLK_ACTIVE_LOW_OPTION)
				xspi_opts.clk_level = 1;
			if (xspi_options & XSP_CLK_PHASE_1_OPTION)
				xspi_opts.clk_phase = 1;
			if (xspi_options & XSP_LOOPBACK_OPTION)
				xspi_opts.loopback = 1;
			xspi_opts.slave_selects = dev->Spi.NumSlaveBits;

			return put_user(xspi_opts,
					(struct xspi_ioc_options *) arg);
		}
		break;
	case XSPI_IOC_SETOPTS:
		{
			struct xspi_ioc_options xspi_opts;
			u32 xspi_options;
			int retval;

			if (copy_from_user(&xspi_opts,
					   (struct xspi_ioc_options *) arg,
					   sizeof (struct xspi_ioc_options)) !=
			    0)
				return -EFAULT;

			/* Lock the device. */
			if (down_interruptible(&dev->sem))
				return -ERESTARTSYS;

			/* Read current settings and set the changeable ones. */
			xspi_options = XSpi_GetOptions(&dev->Spi)
			    & ~XSPI_CHANGEABLE_OPTIONS;
			if (xspi_opts.clk_level != 0)
				xspi_options |= XSP_CLK_ACTIVE_LOW_OPTION;
			if (xspi_opts.clk_phase != 0)
				xspi_options |= XSP_CLK_PHASE_1_OPTION;
			if (xspi_opts.loopback != 0)
				xspi_options |= XSP_LOOPBACK_OPTION;

			retval =
			    convert_status(XSpi_SetOptions
					   (&dev->Spi, xspi_options));

			/* Unlock the device. */
			up(&dev->sem);

			return retval;
		}
		break;
	case XSPI_IOC_TRANSFER:
		{
			struct xspi_ioc_transfer_data trans_data;
			int retval;

			if (copy_from_user(&trans_data,
					   (struct xspi_ioc_transfer_data *)
					   arg,
					   sizeof (struct
						   xspi_ioc_transfer_data)) !=
			    0)
				return -EFAULT;

			/* Transfer the data. */
			retval = xspi_transfer(dev, trans_data.write_buf,
					       trans_data.read_buf,
					       trans_data.count,
					       trans_data.slave_index);
			if (retval > 0)
				return 0;
			else
				return retval;
		}
		break;
	default:
		return -ENOTTY;	/* redundant */
	}			/* switch(cmd) */

	return -ENOTTY;
}
Example #18
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;
}
Example #19
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);

}