1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
| #include "xparameters.h" #include "xspips.h" #include "xil_printf.h" #include "sleep.h" XSpiPs Spi0;
#define SpiPs_RecvByte(BaseAddress) \ (u8)XSpiPs_In32((BaseAddress) + XSPIPS_RXD_OFFSET)
#define SpiPs_SendByte(BaseAddress, Data) \ XSpiPs_Out32((BaseAddress) + XSPIPS_TXD_OFFSET, (Data))
int spi0_init(); void SpiRead(int ByteCount); void SpiWrite(u8 *Sendbuffer, int ByteCount);
unsigned char ReadBuffer[1024]; unsigned char WriteBuffer[1024]={1,2,3,4,5,6,7,8,9,0}; int main(void) { int Status; int i,j;
xil_printf("SPI Selftest Example \r\n");
Status = spi0_init(); if (Status != XST_SUCCESS) { xil_printf("SPI Selftest Example Failed\r\n"); return XST_FAILURE; }
for (i = 0; i < 6; i++) { SpiWrite(WriteBuffer,10); SpiRead(10); for (j = 0; j < 10; j++ ) { xil_printf("%d,",ReadBuffer[j]); } xil_printf("\n"); memset(ReadBuffer, 0x00, 1024); sleep(1); } xil_printf("Successfully ran SPI Selftest Example\r\n"); return XST_SUCCESS; }
void SpiRead(int ByteCount) { int Count; u32 StatusReg;
StatusReg = XSpiPs_ReadReg(Spi0.Config.BaseAddress, XSPIPS_SR_OFFSET);
do{ StatusReg = XSpiPs_ReadReg(Spi0.Config.BaseAddress, XSPIPS_SR_OFFSET); }while(!(StatusReg & XSPIPS_IXR_RXNEMPTY_MASK));
for(Count = 0; Count < ByteCount; Count++){ ReadBuffer[Count] = SpiPs_RecvByte( Spi0.Config.BaseAddress); }
}
void SpiWrite(u8 *Sendbuffer, int ByteCount) { u32 StatusReg; int TransCount = 0;
StatusReg = XSpiPs_ReadReg(Spi0.Config.BaseAddress, XSPIPS_SR_OFFSET);
while ((ByteCount > 0) && (TransCount < XSPIPS_FIFO_DEPTH)) { SpiPs_SendByte(Spi0.Config.BaseAddress, *Sendbuffer); Sendbuffer++; ++TransCount; ByteCount--; }
do { StatusReg = XSpiPs_ReadReg( Spi0.Config.BaseAddress, XSPIPS_SR_OFFSET); } while ((StatusReg & XSPIPS_IXR_TXOW_MASK) == 0);
}
int spi0_init() { int Status; XSpiPs_Config *SpiConfig;
SpiConfig = XSpiPs_LookupConfig(XPAR_XSPIPS_0_DEVICE_ID); if (NULL == SpiConfig) { return XST_FAILURE; }
Status = XSpiPs_CfgInitialize(&Spi0, SpiConfig, SpiConfig->BaseAddress); if (Status != XST_SUCCESS) { return XST_FAILURE; }
Status = XSpiPs_SelfTest(&Spi0); if (Status != XST_SUCCESS) { return XST_FAILURE; } xil_printf("%s self test succ\r\n", __func__);
Status = XSpiPs_SetOptions(&Spi0, XSPIPS_MASTER_OPTION); if (Status != XST_SUCCESS) { xil_printf("%s XSpiPs_SetOptions fail\n", __func__); return XST_FAILURE; } Status = XSpiPs_SetClkPrescaler(&Spi0, XSPIPS_CLK_PRESCALE_64); if (Status != XST_SUCCESS) { xil_printf("%s XSpiPs_SetClkPrescaler fail\n", __func__); return XST_FAILURE; } XSpiPs_Enable(&Spi0); xil_printf("spi 0 config finish\n"); return XST_SUCCESS; }
|