/********************************************************************//** * @brief Initializes the SPIx peripheral according to the specified * parameters in the UART_ConfigStruct. * @param[in] SPIx SPI peripheral selected, should be SPI * @param[in] SPI_ConfigStruct Pointer to a SPI_CFG_Type structure * that contains the configuration information for the * specified SPI peripheral. * @return None *********************************************************************/ void SPI_Init(LPC_SPI_TypeDef *SPIx, SPI_CFG_Type *SPI_ConfigStruct) { uint32_t tmp; CHECK_PARAM(PARAM_SPIx(SPIx)); if(SPIx == LPC_SPI){ /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCSPI, ENABLE); } else { return; } // Configure SPI, interrupt is disable as default tmp = ((SPI_ConfigStruct->CPHA) | (SPI_ConfigStruct->CPOL) \ | (SPI_ConfigStruct->DataOrder) | (SPI_ConfigStruct->Databit) \ | (SPI_ConfigStruct->Mode) | SPI_SPCR_BIT_EN) & SPI_SPCR_BITMASK; tmp |= 0x800; // write back to SPI control register SPIx->SPCR = tmp; if (SPI_ConfigStruct->Databit > SPI_DATABIT_8){ spidat.dataword = 1; } else { spidat.dataword = 0; } // Set clock rate for SPI peripheral SPI_SetClock(SPIx, SPI_ConfigStruct->ClockRate); // If interrupt flag is set, Write '1' to Clear interrupt flag if (SPIx->SPINT & SPI_SPINT_INTFLAG){ SPIx->SPINT = SPI_SPINT_INTFLAG; } }
/*---------------------------------------------------------------------------- SPI Serial Device "LPC17xx" Set Baudrate *----------------------------------------------------------------------------*/ static int SpiDev_SetBaudrate (unsigned int Baudrate) { if (SpiInfo.State != _STATE_INITIALIZED_) { return (-1); /* Device not initialized */ } SPI_SetClock(SpiInfo.pSpi, Baudrate); return(0); /* Success */ }
/********************************************************************//** * @brief Initializes the SPIx peripheral according to the specified * parameters in the UART_ConfigStruct. * @param[in] SPIx SPI peripheral selected, should be SPI * @param[in] SPI_ConfigStruct Pointer to a SPI_CFG_Type structure * that contains the configuration information for the * specified SPI peripheral. * @return None *********************************************************************/ void SPI_Init(SPI_TypeDef *SPIx, SPI_CFG_Type *SPI_ConfigStruct) { SPI_PinCFG_Type defaultSPIPinCfg; uint32_t tmp; CHECK_PARAM(PARAM_SPIx(SPIx)); if(SPIx == SPI) { /* Set up clock and power for UART module */ CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCSPI, ENABLE); /* As default, peripheral clock for UART0 module * is set to FCCLK / 2 */ CLKPWR_SetPCLKDiv(CLKPWR_PCLKSEL_SPI, CLKPWR_PCLKSEL_CCLK_DIV_2); // Set UART0 function pin as default defaultSPIPinCfg.SCK_Pin = SPI_SCK_P0_15; defaultSPIPinCfg.SSEL_Pin = SPI_SSEL_P0_16; defaultSPIPinCfg.MISO_Pin = SPI_MISO_P0_17; defaultSPIPinCfg.MOSI_Pin = SPI_MOSI_P0_18; SPI_PinConfig(SPIx, &defaultSPIPinCfg, SPI_ConfigStruct->Mode); } // Configure SPI, interrupt is disable as default tmp = ((SPI_ConfigStruct->CPHA) | (SPI_ConfigStruct->CPOL) \ | (SPI_ConfigStruct->DataOrder) | (SPI_ConfigStruct->Databit) \ | (SPI_ConfigStruct->Mode) | SPI_SPCR_BIT_EN) & SPI_SPCR_BITMASK; // write back to SPI control register SPIx->SPCR = tmp; // Set clock rate for SPI peripheral SPI_SetClock(SPIx, SPI_ConfigStruct->ClockRate); // If interrupt flag is set, Write '1' to Clear interrupt flag if (SPIx->SPINT & SPI_SPINT_INTFLAG) { SPIx->SPINT = SPI_SPINT_INTFLAG; } }