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; }
/** * * This function does a selftest and loopback test on the SPI device and * XSpi driver as an example. The purpose of this function is to illustrate * how to use the XSpi component. * * * @param DeviceId is the XPAR_<SPI_instance>_DEVICE_ID value from * xparameters.h * * @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful * * @note None * ****************************************************************************/ int SpiSelfTestExample(u16 DeviceId) { int Status; XSpi_Config *ConfigPtr; /* Pointer to Configuration data */ /* * Initialize the SPI driver so that it is ready to use. */ ConfigPtr = XSpi_LookupConfig(DeviceId); 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; } return XST_SUCCESS; }
/***** * 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; }
/** * * 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; }
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; }
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; }*/ }
/** * * 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; }