void SPIClass::begin() { if(init_flag==0) init(); /* Unlock protected registers */ SYS_UnlockReg(); /* Enable IP clock */ CLK_EnableModuleClock(module); /* Select IP clock source and clock divider */ CLK_SetModuleClock(module,clksel,0); /* Lock protected registers */ SYS_LockReg(); /* Configure as a master, clock idle low, falling clock edge Tx, rising edge Rx and 8-bit transaction */ /* Set IP clock divider. SPI clock rate = 4MHz */ SPI_Open(spi, SPI_MASTER, SPI_MODE_0, 8, 4000000); #if defined(__NUC240__) | defined(__NANO100__) | defined(__NUC131__) SPI_EnableFIFO(spi,12,12); #endif setBitOrder(SS, MSBFIRST); #if defined(__M451__) SPI_ClearRxFIFO(spi); SPI_TRIGGER(spi); #endif }
/* ------------- */ 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); }