AXI quad SPI没有输出(已解决)
在使用ZYNQ的AXI quad SPI时遇到以下问题:
使用loopback可以成功,但是使用示波器测量引脚却没有输出。
问题描述:
最近在用ZYNQ的AXI quad SPI拓展接口,却遇到了这个问题:
添加AXI quad SPI核,编译完毕后。使用官方例程,loopback模式可以跑通,但是正常输出模式无法工作,且用示波器测试引脚输出没有波形。
IP核AXI quad SPI的设置如下:
程序为AXI quad SPI官方的 xspi_polled_example:
/***************************** Include Files *********************************/
#include "xparameters.h" /* XPAR parameters */
#include "xspi.h" /* SPI device driver */
#include "xspi_l.h"
#include "xil_printf.h"
/************************** Constant Definitions *****************************/
/*
* The following constants 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_SPI_0_DEVICE_ID
/*
* This is the size of the buffer to be transmitted/received in this example.
*/
#define BUFFER_SIZE 12
/**************************** Type Definitions *******************************/
/*
* The following data type is used to send and receive data on the SPI
* interface.
*/
typedef u8 DataBuffer[BUFFER_SIZE];
/***************** Macros (Inline Functions) Definitions *********************/
/************************** Function Prototypes ******************************/
int SpiPolledExample(XSpi *SpiInstancePtr, u16 SpiDeviceId);
/************************** Variable Definitions *****************************/
/*
* The instances to support the device drivers are global such that the
* are initialized to zero each time the program runs.
*/
static XSpi SpiInstance; /* The instance of the SPI device */
/*
* The following variables are used to read and write to the Spi device, they
* are global to avoid having large buffers on the stack.
*/
u8 ReadBuffer[BUFFER_SIZE];
u8 WriteBuffer[BUFFER_SIZE];
/*****************************************************************************/
/**
*
* Main function to call the Spi Polled example.
*
* @param None
*
* @return XST_SUCCESS if successful, otherwise XST_FAILURE.
*
* @note None
*
******************************************************************************/
int main(void)
{
int Status;
/*
* Run the Spi Polled example.
*/
Status = SpiPolledExample(&SpiInstance, SPI_DEVICE_ID);
if (Status != XST_SUCCESS) {
xil_printf("Spi polled Example Failed\r\n");
return XST_FAILURE;
}
xil_printf("Successfully ran Spi polled Example\r\n");
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;
}
编译下载之后,串口提示运行成功,
将loopback模式关闭,设置为禁止loopback,CPOL = 1 CPHA = 1。
Status = XSpi_SetOptions(SpiInstancePtr, XSP_MASTER_OPTION |
XSP_LOOPBACK_OPTION);
//修改为禁止loopback,CPOL = 1 CPHA = 1
Status = XSpi_SetOptions(SpiInstancePtr, XSP_MASTER_OPTION |
XSP_CLK_PHASE_1_OPTION | XSP_CLK_ACTIVE_LOW_OPTION);
然后,把mosi引脚和miso引脚短接。然后编译、下载,提示运行错误
使用示波器测量SCK、mosi引脚,发现没有波形。
原因分析:
然后在Xilinx的论坛上发现也有人反馈这个问题,最后原因好像是:
因为SS0的引脚一直输出是高电平引起的,我后来将程序修改为持续向SPI发送数据,发现确实是SS0引脚一直是高电平(刚烧写完程序,SS0也会持续一段时间的高电平)。
个人认为可能是SPI自动选择从机编号发生了问题。
解决方案:
参考论坛中的解决方法,具体解决方案:
将从机选择设置为手动选择(XSP_MANUAL_SSELECT_OPTION)
Status = XSpi_SetOptions(SpiInstancePtr, XSP_MASTER_OPTION |
XSP_CLK_PHASE_1_OPTION | XSP_CLK_ACTIVE_LOW_OPTION | XSP_MANUAL_SSELECT_OPTION);
然后再在SPI禁止中断语句之后,加入选择从机编号的语句。(1号从机的选择引脚就是SS0)
/*
* Disable Global interrupt to use polled mode operation
*/
XSpi_IntrGlobalDisable(SpiInstancePtr);
XSpi_SetSlaveSelect(SpiInstancePtr, 0x01); //选择从机编号
经过以上修改后,再次编译烧写,程序运行正确。