void spi_format(spi_t *obj, int bits, int mode, int slave) { dspi_master_config_t master_config; dspi_slave_config_t slave_config; /* Bits: values between 4 and 16 are valid */ MBED_ASSERT(bits >= 4 && bits <= 16); obj->spi.bits = bits; if (slave) { /* Slave config */ DSPI_SlaveGetDefaultConfig(&slave_config); slave_config.whichCtar = kDSPI_Ctar0; slave_config.ctarConfig.bitsPerFrame = (uint32_t)bits;; slave_config.ctarConfig.cpol = (mode & 0x2) ? kDSPI_ClockPolarityActiveLow : kDSPI_ClockPolarityActiveHigh; slave_config.ctarConfig.cpha = (mode & 0x1) ? kDSPI_ClockPhaseSecondEdge : kDSPI_ClockPhaseFirstEdge; DSPI_SlaveInit(spi_address[obj->spi.instance], &slave_config); } else { /* Master config */ DSPI_MasterGetDefaultConfig(&master_config); master_config.ctarConfig.bitsPerFrame = (uint32_t)bits;; master_config.ctarConfig.cpol = (mode & 0x2) ? kDSPI_ClockPolarityActiveLow : kDSPI_ClockPolarityActiveHigh; master_config.ctarConfig.cpha = (mode & 0x1) ? kDSPI_ClockPhaseSecondEdge : kDSPI_ClockPhaseFirstEdge; master_config.ctarConfig.direction = kDSPI_MsbFirst; master_config.ctarConfig.pcsToSckDelayInNanoSec = 0; DSPI_MasterInit(spi_address[obj->spi.instance], &master_config, CLOCK_GetFreq(spi_clocks[obj->spi.instance])); } }
status_t DSPI_RTOS_Init(dspi_rtos_handle_t *handle, SPI_Type *base, const dspi_master_config_t *masterConfig, uint32_t srcClock_Hz) { OS_ERR err; if (handle == NULL) { return kStatus_InvalidArgument; } if (base == NULL) { return kStatus_InvalidArgument; } memset(handle, 0, sizeof(dspi_rtos_handle_t)); OSSemCreate(&handle->mutex, "DSPI", (OS_SEM_CTR)1, &err); if (OS_ERR_NONE != err) { return kStatus_Fail; } OSFlagCreate(&handle->event, "DSPI", (OS_FLAGS)0, &err); if (OS_ERR_NONE != err) { OSSemDel(&handle->mutex, OS_OPT_DEL_ALWAYS, &err); return kStatus_Fail; } handle->base = base; DSPI_MasterInit(handle->base, masterConfig, srcClock_Hz); DSPI_MasterTransferCreateHandle(handle->base, &handle->drv_handle, DSPI_RTOS_Callback, (void *)handle); return kStatus_Success; }
/*! * @brief Main function */ int main(void) { BOARD_InitPins(); BOARD_BootClockRUN(); BOARD_InitDebugConsole(); PRINTF("DSPI edma example start.\r\n"); PRINTF("This example use one dspi instance as master and another as slave on one board.\r\n"); PRINTF("Master use edma way , slave uses interrupt.\r\n"); PRINTF("Please make sure you make the correct line connection. Basically, the connection is: \r\n"); PRINTF("DSPI_master -- DSPI_slave \r\n"); PRINTF(" CLK -- CLK \r\n"); PRINTF(" PCS -- PCS \r\n"); PRINTF(" SOUT -- SIN \r\n"); PRINTF(" SIN -- SOUT \r\n"); /* DMA Mux setting and EDMA init */ uint32_t masterRxChannel, masterIntermediaryChannel, masterTxChannel; uint32_t slaveRxChannel, slaveTxChannel; edma_config_t userConfig; masterRxChannel = 0U; masterIntermediaryChannel = 1U; masterTxChannel = 2U; slaveRxChannel = 3U; slaveTxChannel = 4U; /* DMA MUX init */ DMAMUX_Init(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR); #if (defined EXAMPLE_DSPI_MASTER_DMA_MUX_BASE) && (defined EXAMPLE_DSPI_SLAVE_DMA_MUX_BASE) && \ (EXAMPLE_DSPI_MASTER_DMA_MUX_BASE != EXAMPLE_DSPI_SLAVE_DMA_MUX_BASE) DMAMUX_Init(EXAMPLE_DSPI_SLAVE_DMA_MUX_BASEADDR); #endif DMAMUX_SetSource(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterRxChannel, EXAMPLE_DSPI_MASTER_DMA_RX_REQUEST_SOURCE); DMAMUX_EnableChannel(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterRxChannel); #if (defined EXAMPLE_DSPI_MASTER_DMA_TX_REQUEST_SOURCE) DMAMUX_SetSource(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterTxChannel, EXAMPLE_DSPI_MASTER_DMA_TX_REQUEST_SOURCE); DMAMUX_EnableChannel(EXAMPLE_DSPI_MASTER_DMA_MUX_BASEADDR, masterTxChannel); #endif DMAMUX_SetSource(EXAMPLE_DSPI_SLAVE_DMA_MUX_BASEADDR, slaveRxChannel, EXAMPLE_DSPI_SLAVE_DMA_RX_REQUEST_SOURCE); DMAMUX_EnableChannel(EXAMPLE_DSPI_SLAVE_DMA_MUX_BASEADDR, slaveRxChannel); #if (defined EXAMPLE_DSPI_SLAVE_DMA_TX_REQUEST_SOURCE) DMAMUX_SetSource(EXAMPLE_DSPI_SLAVE_DMA_MUX_BASEADDR, slaveTxChannel, EXAMPLE_DSPI_SLAVE_DMA_TX_REQUEST_SOURCE); DMAMUX_EnableChannel(EXAMPLE_DSPI_SLAVE_DMA_MUX_BASEADDR, slaveTxChannel); #endif /* EDMA init*/ /* * userConfig.enableRoundRobinArbitration = false; * userConfig.enableHaltOnError = true; * userConfig.enableContinuousLinkMode = false; * userConfig.enableDebugMode = false; */ EDMA_GetDefaultConfig(&userConfig); EDMA_Init(EXAMPLE_DSPI_MASTER_DMA_BASEADDR, &userConfig); #if (defined EXAMPLE_DSPI_SLAVE_DMA_BASE) && (defined EXAMPLE_DSPI_MASTER_DMA_BASE) && \ (EXAMPLE_DSPI_SLAVE_DMA_BASE != EXAMPLE_DSPI_MASTER_DMA_BASE) EDMA_Init(EXAMPLE_DSPI_SLAVE_DMA_BASEADDR, &userConfig); #endif /*DSPI init*/ uint32_t srcClock_Hz; uint32_t errorCount; uint32_t i; dspi_master_config_t masterConfig; dspi_slave_config_t slaveConfig; dspi_transfer_t masterXfer; dspi_transfer_t slaveXfer; /*Master config*/ masterConfig.whichCtar = kDSPI_Ctar0; masterConfig.ctarConfig.baudRate = TRANSFER_BAUDRATE; masterConfig.ctarConfig.bitsPerFrame = 8U; masterConfig.ctarConfig.cpol = kDSPI_ClockPolarityActiveHigh; masterConfig.ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge; masterConfig.ctarConfig.direction = kDSPI_MsbFirst; masterConfig.ctarConfig.pcsToSckDelayInNanoSec = 1000000000U / TRANSFER_BAUDRATE; masterConfig.ctarConfig.lastSckToPcsDelayInNanoSec = 1000000000U / TRANSFER_BAUDRATE; masterConfig.ctarConfig.betweenTransferDelayInNanoSec = 1000000000U / TRANSFER_BAUDRATE; masterConfig.whichPcs = EXAMPLE_DSPI_MASTER_PCS_FOR_INIT; masterConfig.pcsActiveHighOrLow = kDSPI_PcsActiveLow; masterConfig.enableContinuousSCK = false; masterConfig.enableRxFifoOverWrite = false; masterConfig.enableModifiedTimingFormat = false; masterConfig.samplePoint = kDSPI_SckToSin0Clock; srcClock_Hz = CLOCK_GetFreq(DSPI_MASTER_CLK_SRC); DSPI_MasterInit(EXAMPLE_DSPI_MASTER_BASEADDR, &masterConfig, srcClock_Hz); /*Slave config*/ slaveConfig.whichCtar = kDSPI_Ctar0; slaveConfig.ctarConfig.bitsPerFrame = masterConfig.ctarConfig.bitsPerFrame; slaveConfig.ctarConfig.cpol = masterConfig.ctarConfig.cpol; slaveConfig.ctarConfig.cpha = masterConfig.ctarConfig.cpha; slaveConfig.enableContinuousSCK = masterConfig.enableContinuousSCK; slaveConfig.enableRxFifoOverWrite = masterConfig.enableRxFifoOverWrite; slaveConfig.enableModifiedTimingFormat = masterConfig.enableModifiedTimingFormat; slaveConfig.samplePoint = masterConfig.samplePoint; DSPI_SlaveInit(EXAMPLE_DSPI_SLAVE_BASEADDR, &slaveConfig); /* Set up the transfer data */ for (i = 0U; i < TRANSFER_SIZE; i++) { masterTxData[i] = i % 256U; masterRxData[i] = 0U; slaveTxData[i] = ~masterTxData[i]; slaveRxData[i] = 0U; } /* Set up dspi slave first */ memset(&(dspiEdmaSlaveRxRegToRxDataHandle), 0, sizeof(dspiEdmaSlaveRxRegToRxDataHandle)); memset(&(dspiEdmaSlaveTxDataToTxRegHandle), 0, sizeof(dspiEdmaSlaveTxDataToTxRegHandle)); EDMA_CreateHandle(&(dspiEdmaSlaveRxRegToRxDataHandle), EXAMPLE_DSPI_SLAVE_DMA_BASEADDR, slaveRxChannel); EDMA_CreateHandle(&(dspiEdmaSlaveTxDataToTxRegHandle), EXAMPLE_DSPI_SLAVE_DMA_BASEADDR, slaveTxChannel); isTransferCompleted = false; DSPI_SlaveTransferCreateHandleEDMA(EXAMPLE_DSPI_SLAVE_BASEADDR, &g_dspi_edma_s_handle, DSPI_SlaveUserCallback, NULL, &dspiEdmaSlaveRxRegToRxDataHandle, &dspiEdmaSlaveTxDataToTxRegHandle); slaveXfer.txData = slaveTxData; slaveXfer.rxData = slaveRxData; slaveXfer.dataSize = TRANSFER_SIZE; slaveXfer.configFlags = kDSPI_SlaveCtar0; if (kStatus_Success != DSPI_SlaveTransferEDMA(EXAMPLE_DSPI_SLAVE_BASEADDR, &g_dspi_edma_s_handle, &slaveXfer)) { PRINTF("There is error when start DSPI_SlaveTransferEDMA \r\n"); } /* Set up dspi master */ memset(&(dspiEdmaMasterRxRegToRxDataHandle), 0, sizeof(dspiEdmaMasterRxRegToRxDataHandle)); memset(&(dspiEdmaMasterTxDataToIntermediaryHandle), 0, sizeof(dspiEdmaMasterTxDataToIntermediaryHandle)); memset(&(dspiEdmaMasterIntermediaryToTxRegHandle), 0, sizeof(dspiEdmaMasterIntermediaryToTxRegHandle)); EDMA_CreateHandle(&(dspiEdmaMasterRxRegToRxDataHandle), EXAMPLE_DSPI_MASTER_DMA_BASEADDR, masterRxChannel); EDMA_CreateHandle(&(dspiEdmaMasterTxDataToIntermediaryHandle), EXAMPLE_DSPI_MASTER_DMA_BASEADDR, masterIntermediaryChannel); EDMA_CreateHandle(&(dspiEdmaMasterIntermediaryToTxRegHandle), EXAMPLE_DSPI_MASTER_DMA_BASEADDR, masterTxChannel); DSPI_MasterTransferCreateHandleEDMA(EXAMPLE_DSPI_MASTER_BASEADDR, &g_dspi_edma_m_handle, DSPI_MasterUserCallback, NULL, &dspiEdmaMasterRxRegToRxDataHandle, &dspiEdmaMasterTxDataToIntermediaryHandle, &dspiEdmaMasterIntermediaryToTxRegHandle); /* Start master transfer */ masterXfer.txData = masterTxData; masterXfer.rxData = masterRxData; masterXfer.dataSize = TRANSFER_SIZE; masterXfer.configFlags = kDSPI_MasterCtar0 | EXAMPLE_DSPI_MASTER_PCS_FOR_TRANSFER | kDSPI_MasterPcsContinuous; if (kStatus_Success != DSPI_MasterTransferEDMA(EXAMPLE_DSPI_MASTER_BASEADDR, &g_dspi_edma_m_handle, &masterXfer)) { PRINTF("There is error when start DSPI_MasterTransferEDMA \r\n "); } /* Wait until transfer completed */ while (!isTransferCompleted) { } /* Check the data */ errorCount = 0U; for (i = 0U; i < TRANSFER_SIZE; i++) { if (masterTxData[i] != slaveRxData[i]) { errorCount++; } if (slaveTxData[i] != masterRxData[i]) { errorCount++; } } if (errorCount == 0) { PRINTF("DSPI transfer all data matched! \r\n"); } else { PRINTF("Error occured in DSPI transfer ! \r\n"); } DSPI_Deinit(EXAMPLE_DSPI_MASTER_BASEADDR); DSPI_Deinit(EXAMPLE_DSPI_SLAVE_BASEADDR); while (1) { } }