SPI Clock Polarity

Discussion to talk about software related topics only.
Post Reply
SeeCwriter
Posts: 606
Joined: Mon May 12, 2008 10:55 am

SPI Clock Polarity

Post by SeeCwriter »

I'm using a MOD5441X and v2.8.7 of the tools. The module interfaces with several components on the SPI bus (ADC, DAC, Serial Memory) and they each require a different SPI bus configuration. But they all use the same clock polarity.

Before accessing one of the components, I call a function that sets the bus configuration. This has worked fine for several years. Now a new ADC device has been added that is different than the current one, and it requires the opposite clock polarity from all the other devices.

The problem I am having is that when I change the clock polarity, the polarity doesn't change until after I excersize the bus. So I set the polarity and then do a read of the ADC, but the clock polarity hasn't changed. But if I read it a second time, the polarity is now correct. That is not case when setting the other SPI parameters. They take affect immediately. Is there something I can do to change that?

Code: Select all

static DSPIModule SPIM( DEFAULT_DSPI_MODULE );

void SpiSetup( bool Bitsz16, BYTE clkPol, BYTE clkPh, BYTE csToClkDelay, BYTE afterTransferDelay, DWORD baud = 2000000 );

void SpiSetup( bool Bitsz16, BYTE clkPol, BYTE clkPh, BYTE csToClkDelay, BYTE afterTransferDelay, DWORD baud )
{
  BYTE bsize = Bitsz16 ? 0x10:0x08;	// 8-bit or 16-bit transfers.

  SPIM.Init( baud,              // Baudrate
             bsize,             // BitSz
             0x00,              // CS
             0x0F,              // CSPol
             clkPol,            // ClkPol
             clkPh,             // ClkPhase
             0x01,              // DoutHiz
             csToClkDelay,      // csToClockDelay
             afterTransferDelay	// delayAfterTransfer
         );
}

void ReadAnalogs()
{
	SpiSetup( true, 0x01, 0x00, 0x00, 0x00 );
	ReadADC();

	SpiSetup( false, 0x00, 0x01, 0x00, 0x01, 500000 );
	ReadPowerSupplyADC();
}
According to the MCF54415 Reference Manual, paragraph 40.4.4.6, the clock polarity will change "one system clock before assertion of the chip select for the next frame." There has to be at least one system clock between calling SpiSetup and the function to read the ADC device.
Post Reply