void SPI_Init(SPI_Type *pSPI, SPI_ConfigType *pConfig)
{
/* enable SPI clock gating on */
if( pSPI == SPI0) {
SIM->SCGC |= SIM_SCGC_SPI0_MASK;
} else {
SIM->SCGC |= SIM_SCGC_SPI1_MASK;
}
/* configure other control bits */
if( pConfig->sSettings.bIntEn) {
SPI_IntEnable(pSPI);
if( pSPI == SPI0 ) {
NVIC_EnableIRQ(SPI0_IRQn);
} else {
NVIC_EnableIRQ(SPI1_IRQn);
}
}
if( pConfig->sSettings.bTxIntEn) {
SPI_TxIntEnable(pSPI);
if( pSPI == SPI0 ) {
NVIC_EnableIRQ(SPI0_IRQn);
} else {
NVIC_EnableIRQ(SPI1_IRQn);
}
}
if( pConfig->sSettings.bMasterMode) {
SPI_SetMasterMode(pSPI);
} else {
SPI_SetSlaveMode(pSPI);
}
if( pConfig->sSettings.bClkPolarityLow) {
SPI_SetClockPol(pSPI,1);
}
if( pConfig->sSettings.bClkPhase1) {
SPI_SetClockPhase(pSPI,1);
}
if( pConfig->sSettings.bShiftLSBFirst) {
SPI_SetLSBFirst(pSPI);
}
if( pConfig->sSettings.bMatchIntEn) {
SPI_MatchIntEnable(pSPI);
}
if( pConfig->sSettings.bModeFaultEn) {
SPI_ModfEnable(pSPI);
}
if( pConfig->sSettings.bMasterAutoDriveSS) {
/* set both SSOE and MODFEN bits when auto drive slave SS is enabled */
SPI_SSOutputEnable(pSPI);
SPI_ModfEnable(pSPI);
}
if( pConfig->sSettings.bPinAsOuput) {
SPI_BidirPinEnable(pSPI);
}
if( pConfig->sSettings.bBidirectionModeEn) {
SPI_BidirOutEnable(pSPI);
}
if( pConfig->sSettings.bStopInWaitMode) {
SPI_ClockStopEnable(pSPI);
}
if(pConfig->sSettings.bMasterMode) {
SPI_SetBaudRate(pSPI,pConfig->u32BusClkHz,pConfig->u32BitRate);
}
/* enable SPI module */
if( pConfig->sSettings.bModuleEn) {
SPI_Enable(pSPI);
}
}
|