/******************************************************************************* * Function Name: SPI_ReadStatus ******************************************************************************** * * Summary: * Read the status register for the component. * * Parameters: * None. * * Return: * Contents of the status register. * * Global variables: * SPI_swStatus - used to store in software status register, * modified every function call - resets to zero. * * Theory: * Allows the user and the API to read the status register for error detection * and flow control. * * Side Effects: * Clear status register of the component. * * Reentrant: * No. * *******************************************************************************/ uint8 SPI_ReadStatus(void) { uint8 tmpStatus; #if(SPI_TX_SOFTWARE_BUF_ENABLED || SPI_RX_SOFTWARE_BUF_ENABLED) SPI_DisableInt(); tmpStatus = SPI_GET_STATUS_RX(SPI_swStatusRx); tmpStatus |= SPI_GET_STATUS_TX(SPI_swStatusTx); tmpStatus &= ((uint8) ~SPI_STS_SPI_IDLE); SPI_swStatusTx = 0u; SPI_swStatusRx = 0u; SPI_EnableInt(); #else tmpStatus = SPI_RX_STATUS_REG; tmpStatus |= SPI_TX_STATUS_REG; tmpStatus &= ((uint8) ~SPI_STS_SPI_IDLE); #endif /* (SPI_TX_SOFTWARE_BUF_ENABLED || SPI_RX_SOFTWARE_BUF_ENABLED) */ return(tmpStatus); }
void SPI0_IRQHandler(void) { /* Check RX EMPTY flag */ while(SPI_GET_RX_FIFO_EMPTY_FLAG(SPI0) == 0) { /* Read RX FIFO */ g_au32DestinationData[g_u32RxDataCount++] = SPI_READ_RX(SPI0); } /* Check TX FULL flag and TX data count */ while((SPI_GET_TX_FIFO_FULL_FLAG(SPI0) == 0) && (g_u32TxDataCount < TEST_COUNT)) { /* Write to TX FIFO */ SPI_WRITE_TX(SPI0, g_au32SourceData[g_u32TxDataCount++]); } if(g_u32TxDataCount >= TEST_COUNT) SPI_DisableInt(SPI0, SPI_FIFO_TX_INT_MASK); /* Disable TX FIFO threshold interrupt */ /* Check the RX FIFO time-out interrupt flag */ if(SPI_GetIntFlag(SPI0, SPI_FIFO_TIMEOUT_INT_MASK)) { /* If RX FIFO is not empty, read RX FIFO. */ while((SPI0->STATUS & SPI_STATUS_RX_EMPTY_Msk) == 0) g_au32DestinationData[g_u32RxDataCount++] = SPI_READ_RX(SPI0); } }
int main(void) { uint32_t u32DataCount, u32TestCount, u32Err; /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Init UART to 115200-8n1 for print message */ UART_Open(UART0, 115200); /* Configure SPI0 as a master, MSB first, 32-bit transaction, SPI Mode-0 timing, clock is 2MHz */ SPI_Open(SPI0, SPI_MASTER, SPI_MODE_0, 32, 2000000); /* Enable the automatic hardware slave select function. Select the SPI0_SS0 pin and configure as low-active. */ SPI_EnableAutoSS(SPI0, SPI_SS0, SPI_SS_ACTIVE_LOW); /* Configure SPI1 as a slave, MSB first, 32-bit transaction, SPI Mode-0 timing, clock is 4Mhz */ SPI_Open(SPI1, SPI_SLAVE, SPI_MODE_0, 32, 4000000); /* Configure SPI1 as a low level active device. */ SPI_SET_SS0_LOW(SPI1); printf("\n\n"); printf("+----------------------------------------------------------------------+\n"); printf("| SPI Driver Sample Code |\n"); printf("+----------------------------------------------------------------------+\n"); printf("\n"); printf("Configure SPI0 as a master and SPI1 as a slave.\n"); printf("Data width of a transaction: 32\n"); printf("SPI clock rate: %d Hz\n", SPI_GetBusClock(SPI0)); printf("The I/O connection for SPI0/SPI1 loopback:\n"); printf(" SPI0_SS (PE.4) <--> SPI1_SS(PC.12)\n SPI0_CLK(PE.5) <--> SPI1_CLK(PD.1)\n"); printf(" SPI0_MISO(PE.2) <--> SPI1_MISO(PD.0)\n SPI0_MOSI(PE.3) <--> SPI1_MOSI(PC.15)\n\n"); printf("Please connect SPI0 with SPI1, and press any key to start transmission ..."); getchar(); printf("\n"); printf("\nSPI0/1 Loopback test "); /* Enable the SPI1 unit transfer interrupt. */ SPI_EnableInt(SPI1, SPI_UNITIEN_MASK); NVIC_EnableIRQ(SPI1_IRQn); SPI_TRIGGER(SPI1); u32Err = 0; for(u32TestCount=0; u32TestCount<10000; u32TestCount++) { /* set the source data and clear the destination buffer */ for(u32DataCount=0; u32DataCount<TEST_COUNT; u32DataCount++) { g_au32SourceData[u32DataCount] = u32DataCount; g_au32DestinationData[u32DataCount] = 0; } u32DataCount=0; SPI1_INT_Flag = 0; if((u32TestCount&0x1FF) == 0) { putchar('.'); } SPI_TRIGGER(SPI0); /* write the first data of source buffer to Tx register of SPI0. And start transmission. */ SPI_WRITE_TX(SPI0, g_au32SourceData[0]); while(1) { if(SPI1_INT_Flag==1) { SPI1_INT_Flag = 0; if(u32DataCount<(TEST_COUNT-1)) { /* Read the previous retrieved data and trigger next transfer. */ g_au32DestinationData[u32DataCount] = SPI_READ_RX(SPI1); u32DataCount++; /* Write data to SPI0 Tx buffer and trigger the transfer */ SPI_WRITE_TX(SPI0, g_au32SourceData[u32DataCount]); } else { /* Just read the previous retrieved data but trigger next transfer, because this is the last transfer. */ g_au32DestinationData[u32DataCount] = SPI_READ_RX(SPI1); break; } } } /* Check the received data */ for(u32DataCount=0; u32DataCount<TEST_COUNT; u32DataCount++) { if(g_au32DestinationData[u32DataCount]!=g_au32SourceData[u32DataCount]) u32Err = 1; } if(u32Err) break; } /* Disable the SPI1 unit transfer interrupt. */ SPI_DisableInt(SPI1, SPI_UNITIEN_MASK); NVIC_DisableIRQ(SPI1_IRQn); if(u32Err) printf(" [FAIL]\n\n"); else printf(" [PASS]\n\n"); printf("\n\nExit SPI driver sample code.\n"); while(1); }
/* ------------- */ int main(void) { uint32_t u32DataCount; /* Unlock protected registers */ SYS_UnlockReg(); /* Init system, IP clock and multi-function I/O. */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */ UART_Open(UART0, 115200); /* Init SPI */ SPI_Init(); printf("\n\n"); printf("+-----------------------------------------------------+\n"); printf("| SPI Master Mode Sample Code |\n"); printf("+-----------------------------------------------------+\n"); printf("\n"); printf("Configure SPI0 as a master.\n"); printf("Bit length of a transaction: 32\n"); printf("The I/O connection for SPI0:\n"); printf(" SPI0_SS0 (PC.0)\n SPI0_CLK (PC.1)\n"); printf(" SPI0_MISO0 (PC.2)\n SPI0_MOSI0 (PC.3)\n\n"); printf("SPI controller will enable FIFO mode and transfer %d data to a off-chip slave device.\n", TEST_COUNT); printf("In the meanwhile the SPI controller will receive %d data from the off-chip slave device.\n", TEST_COUNT); printf("After the transfer is done, the %d received data will be printed out.\n", TEST_COUNT); printf("The SPI master configuration is ready.\n"); for(u32DataCount = 0; u32DataCount < TEST_COUNT; u32DataCount++) { /* Write the initial value to source buffer */ g_au32SourceData[u32DataCount] = 0x00550000 + u32DataCount; /* Clear destination buffer */ g_au32DestinationData[u32DataCount] = 0; } printf("Before starting the data transfer, make sure the slave device is ready. Press any key to start the transfer.\n"); getchar(); printf("\n"); /* Set TX FIFO threshold, enable TX FIFO threshold interrupt and RX FIFO time-out interrupt */ SPI_EnableFIFO(SPI0, 4, 4); SPI_EnableInt(SPI0, SPI_FIFO_TX_INT_MASK | SPI_FIFO_TIMEOUT_INT_MASK); g_u32TxDataCount = 0; g_u32RxDataCount = 0; NVIC_EnableIRQ(SPI0_IRQn); /* Wait for transfer done */ while(g_u32RxDataCount < TEST_COUNT); /* Print the received data */ printf("Received data:\n"); for(u32DataCount = 0; u32DataCount < TEST_COUNT; u32DataCount++) { printf("%d:\t0x%X\n", u32DataCount, g_au32DestinationData[u32DataCount]); } /* Disable TX FIFO threshold interrupt and RX FIFO time-out interrupt */ SPI_DisableInt(SPI0, SPI_FIFO_TX_INT_MASK | SPI_FIFO_TIMEOUT_INT_MASK); NVIC_DisableIRQ(SPI0_IRQn); printf("The data transfer was done.\n"); printf("\n\nExit SPI driver sample code.\n"); /* Reset SPI0 */ SPI_Close(SPI0); while(1); }