cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
dimpy
Adventurer
Adventurer
177 Views
Registered: ‎06-05-2020

Coding SPI_PS using SPI driver in Xilinx SDK

Hello @sabankocal 

I am new to SPI and would like to know who to proceed for it. For a start here is what i have dome.

dimpy_0-1617788461960.png

 

I have generate the bitstream for same and export it to the Xilinx SDK.

FPGA is working as Slave device.

In the SDK i have created the hello world application project. I took reference from the xspips_slave_polled_example . Please find the below code.

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xparameters.h" /* SDK generated parameters */
#include "xspips.h" /* SPI device driver */

/************************** Constant Definitions *****************************/

/*
* The following constant map to the XPAR parameters created in the
* xparameters.h file. They are defined here such that a user can easily
* change all the needed parameters in one place.
*/
#define SPI_DEVICE_ID XPAR_XSPIPS_0_DEVICE_ID

/*
* The following constant specify the max amount of data the slave is
* expecting to receive from the master.
*/
#define MAX_DATA 100

/**************************** Type Definitions *******************************/

/***************** Macros (Inline Functions) Definitions *********************/

#define SpiPs_RecvByte(BaseAddress) \
(u8)XSpiPs_In32((BaseAddress) + XSPIPS_RXD_OFFSET)

#define SpiPs_SendByte(BaseAddress, Data) \
XSpiPs_Out32((BaseAddress) + XSPIPS_TXD_OFFSET, (Data))

/************************** Function Prototypes ******************************/

void SpiSlaveRead(int ByteCount);

void SpiSlaveWrite(u8 *Sendbuffer, int ByteCount);

int SpiPsSlavePolledExample(u16 SpiDeviceId);

/************************** Variable Definitions *****************************/

/*
* The instances to support the device drivers are global such that they
* are initialized to zero each time the program runs. They could be local
* but should at least be static so they are zeroed.
*/
static XSpiPs SpiInstance;

/*
* The ReadBuffer is used to read to the data which it received from the SPI
* Bus which master has sent.
*/
u8 ReadBuffer[MAX_DATA];

/*****************************************************************************/
/**
*
* Main function to call the SPI Slave Example.
*
* @param None
*
* @return
* - XST_SUCCESS if successful
* - XST_FAILURE if not successful
*
* @note None
*
******************************************************************************/
int main(void)
{
init_platform();

int Status;

xil_printf("Running SpiPS Slave Polled Example \r\n");

/*
* Run the SpiPs Slave Polled example.
*/
Status = SpiPsSlavePolledExample(SPI_DEVICE_ID);
if (Status != XST_SUCCESS) {
xil_printf("SpiPs Slave Polled Example Failed \r\n");
return XST_FAILURE;
}

xil_printf("Successfully ran SpiPs Slave Polled Example \r\n");

cleanup_platform();
return XST_SUCCESS;
}

/*****************************************************************************/
/**
*
* The purpose of this function is to illustrate how to use the XSpiPs
* device driver in Slave mode. This function reads data from a SPI Master
* and will echo it back to the Master.
*
* @param SpiDeviceId is the Instance Id of SPI in the system.
*
* @return
* - XST_SUCCESS if successful
* - XST_FAILURE if not successful
*
* @note None
*
*
*****************************************************************************/
int SpiPsSlavePolledExample(u16 SpiDeviceId)
{
init_platform();
int Status;
u8 *BufferPtr;
XSpiPs_Config *SpiConfig;

/*
* Initialize the SPI driver so that it's ready to use
*/
SpiConfig = XSpiPs_LookupConfig(SpiDeviceId);
if (NULL == SpiConfig) {
return XST_FAILURE;
}

Status = XSpiPs_CfgInitialize((&SpiInstance), SpiConfig,
SpiConfig->BaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

/*
* The SPI device is a slave by default and the clock phase
* have to be set according to its master. In this example, CPOL is set
* to quiescent high and CPHA is set to 1.
*/
Status = XSpiPs_SetOptions((&SpiInstance), (XSPIPS_CR_CPHA_MASK) | \
(XSPIPS_CR_CPOL_MASK));
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}

memset(ReadBuffer, 0x00, sizeof(ReadBuffer));

/*
* Set the Rx FIFO Threshold to the Max Data
*/
XSpiPs_SetRXWatermark((&SpiInstance),MAX_DATA);

/*
* Enable the device.
*/
XSpiPs_Enable((&SpiInstance));

/*
* Read the contents of the Receive buffer
* Master is expected to send MAX_DATA number of bytes
*/
SpiSlaveRead(MAX_DATA);

/*
* Setup a pointer to the start of the data that was read into the read
* buffer and the same back
*/
BufferPtr = ReadBuffer;

/*
* Send the data received back to Master
* Master is expected to send MAX_DATA number of dummy bytes for
* the slave to be able to echo previously received data.
*/
SpiSlaveWrite(BufferPtr, MAX_DATA);

/*
* Disable the device.
*/
XSpiPs_Disable((&SpiInstance));

return XST_SUCCESS;
}

/*****************************************************************************/
/**
*
* This function reads from the Rx buffer
*
* @param ByteCount is the number of bytes to be read from Rx buffer.
*
* @return None.
*
* @note None.
*
******************************************************************************/
void SpiSlaveRead(int ByteCount)
{
int Count;
u32 StatusReg;

StatusReg = XSpiPs_ReadReg(SpiInstance.Config.BaseAddress,
XSPIPS_SR_OFFSET);

/*
* Polling the Rx Buffer for Data
*/
do{
StatusReg = XSpiPs_ReadReg(SpiInstance.Config.BaseAddress,
XSPIPS_SR_OFFSET);
}while(!(StatusReg & XSPIPS_IXR_RXNEMPTY_MASK));

/*
* Reading the Rx Buffer
*/
for(Count = 0; Count < ByteCount; Count++){
ReadBuffer[Count] = SpiPs_RecvByte(
SpiInstance.Config.BaseAddress);
}

}

/*****************************************************************************/
/**
*
* This function writes Data into the Tx buffer
*
* @param Sendbuffer is the buffer whose data is to be sent onto the
* Tx FIFO.
* @param ByteCount is the number of bytes to be read from Rx buffer.
*
* @return None.
*
* @note None.
*
******************************************************************************/
void SpiSlaveWrite(u8 *Sendbuffer, int ByteCount)
{
u32 StatusReg;
int TransCount = 0;

StatusReg = XSpiPs_ReadReg(SpiInstance.Config.BaseAddress,
XSPIPS_SR_OFFSET);

/*
* Fill the TXFIFO with as many bytes as it will take (or as
* many as we have to send).
*/
while ((ByteCount > 0) &&
(TransCount < XSPIPS_FIFO_DEPTH)) {
SpiPs_SendByte(SpiInstance.Config.BaseAddress,
*Sendbuffer);
Sendbuffer++;
++TransCount;
ByteCount--;
}

/*
* Wait for the transfer to finish by polling Tx fifo status.
*/
do {
StatusReg = XSpiPs_ReadReg(
SpiInstance.Config.BaseAddress,
XSPIPS_SR_OFFSET);
} while ((StatusReg & XSPIPS_IXR_TXOW_MASK) == 0);

}

 

After doing this, programmed the FPGA and run the configuration. But i am not getting any output on the console. Kindly help me to solve this.

Thank you.

 

Tags (2)
0 Kudos
2 Replies
abommera
Xilinx Employee
Xilinx Employee
124 Views
Registered: ‎10-12-2018

Hi @dimpy,

If you are not seeing anything on UART, please double-check the baud rate. Try to do "Debug as" your application project and check whether the breakpoint hitting at print function (xil_printf("Running SpiPS Slave Polled Example \r\n");) or not.

Thanks & Regards
Anil B
-------------------------------------------------------------------------
Don’t forget to reply, kudo, and accept as solution.
-------------------------------------------------------------------------
0 Kudos
dimpy
Adventurer
Adventurer
107 Views
Registered: ‎06-05-2020

Hello @abommera 

Please find the attached Screenshot of my console where i am only getting "Running SpiPS Slave Polled Example ".

After it i am not getting anything. Also i am not getting any clk.

Please find the attached zip file of my design and help me to getting spi clk.

Thank you.

image.png
0 Kudos