/* Setup SPI handle and parameters */ static void setupSpiMaster() { SPI_CFG_T spiCfg; SPI_DELAY_CONFIG_T spiDelayCfg; /* Initialize SPI Block */ Chip_SPI_Init(LPC_SPI0); /* Set SPI Config register */ spiCfg.ClkDiv = 0xFFFF; /* Set Clock divider to maximum */ spiCfg.Mode = SPI_MODE_MASTER; /* Enable Master Mode */ spiCfg.ClockMode = SPI_CLOCK_MODE0; /* Enable Mode 0 */ spiCfg.DataOrder = SPI_DATA_MSB_FIRST; /* Transmit MSB first */ /* Slave select polarity is active low */ spiCfg.SSELPol = (SPI_CFG_SPOL0_LO | SPI_CFG_SPOL1_LO | SPI_CFG_SPOL2_LO | SPI_CFG_SPOL3_LO); Chip_SPI_SetConfig(LPC_SPI0, &spiCfg); /* Set Delay register */ spiDelayCfg.PreDelay = 2; spiDelayCfg.PostDelay = 2; spiDelayCfg.FrameDelay = 2; spiDelayCfg.TransferDelay = 2; Chip_SPI_DelayConfig(LPC_SPI0, &spiDelayCfg); /* Enable Loopback mode for this example */ Chip_SPI_EnableLoopBack(LPC_SPI0); /* Enable SPI0 */ Chip_SPI_Enable(LPC_SPI0); }
uint32_t SPI_ReadCh0(void){ /* Disable clocks to SWM and IOCON to save power */ Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_SWM); Chip_Clock_EnablePeriphClock(SYSCTL_CLOCK_IOCON); // initialize pins Chip_SWM_DisableFixedPin(SWM_FIXED_XTALIN); Chip_SWM_MovablePinAssign(SWM_SPI1_SCK_IO, 8); Chip_SWM_MovablePinAssign(SWM_SPI1_MOSI_IO, 17); Chip_SWM_MovablePinAssign(SWM_SPI1_MISO_IO, 16); //TODO: do we need to set slave select if it's tied to ground? Chip_SWM_MovablePinAssign(SWM_SPI1_SSEL_IO, 4); ConfigStruct.Mode = SPI_MODE_MASTER; ConfigStruct.ClkDiv = Chip_SPI_CalClkRateDivider(LPC_SPI1, 100000); ConfigStruct.ClockMode = SPI_CLOCK_CPHA0_CPOL0; ConfigStruct.DataOrder = SPI_DATA_MSB_FIRST; //enable MSB first option ConfigStruct.SSELPol = SPI_CFG_SPOL_LO; Chip_SPI_Init(LPC_SPI1, &ConfigStruct); DelayConfigStruct.FrameDelay = 0; DelayConfigStruct.PostDelay = 0; DelayConfigStruct.PreDelay = 0; DelayConfigStruct.TransferDelay = 0; Chip_SPI_DelayConfig(LPC_SPI1, &DelayConfigStruct); Chip_SPI_Enable(LPC_SPI1); //Chip_SPI_EnableLoopBack(LPC_SPI1); // init transmit buffer TxBuf[0] = 0x68; TxBuf[1] = 0x00; XfSetup.Length = BUFFER_SIZE; XfSetup.pTx = TxBuf; XfSetup.RxCnt = XfSetup.TxCnt = 0; XfSetup.DataSize = 8; XfSetup.pRx = RxBuf; Chip_SPI_RWFrames_Blocking(LPC_SPI1, &XfSetup); uint16_t value = ((RxBuf[0] & 0x03) << 8) + (RxBuf[1] & 0xFF); //Chip_SPI_DisableLoopBack(LPC_SPI1); /* DeInitialize SPI peripheral */ Chip_SPI_DeInit(LPC_SPI1); /* Disable clocks to SWM and IOCON to save power */ Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_SWM); Chip_Clock_DisablePeriphClock(SYSCTL_CLOCK_IOCON); return value; }