cancel
Showing results for 
Search instead for 
Did you mean: 

Simple SPI problem

llombardini
Associate II
Posted on July 11, 2012 at 16:36

Hi all,

I have a simple routine called in TIMER_IRQ event that set GPIO and than set a DAC on SPI BUS. This routine must be quick as possible.

Regarding the GPIO setting there is no particular problem, while SPI has some trouble:

1) Obviously this code doesn't work because CS is rising during SPI Trasmission

 

SPI1_CS1_LOW();

    SPI_I2S_SendData(SPI1, DacValue);

  SPI1_CS1_HIGH();

2) Using a simple delay make things working but the routine in not quick now

 

SPI1_CS1_LOW();

    SPI_I2S_SendData(SPI1, DacValue);

  int i = 100;

  while(i--);

  SPI1_CS1_HIGH();

 3) I tried to use SPI IRQ but unfortunately the routine doesn't work. Could enyone give me some code sample to rise the CS after the SPI trasmission and without using delay cicle?

For your information below the code that doens't work:

  SPI1_CS1_LOW();

if (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) != RESET)

  SPI_I2S_SendData(SPI1, DacValue);

  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);

  void SPI1_IRQHandler(void){

  if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE) != RESET){

//      SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_TXE);

      if(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY) == RESET){

        SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);

        SPI1_CS1_HIGH();

      }

    }

Thanks in advance

Luca Lombardini

2 REPLIES 2
Posted on July 11, 2012 at 16:43

Wait on RXNE, it will assert after last bit has left the shift register, TXE indicates the holding register is empty, which occurs proximately when the holding register moves to the shift register, and the first bit leaves. You'll need to do a scratch read of the data register to clear RXNE.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
llombardini
Associate II
Posted on July 12, 2012 at 10:11

Hi Clive,

thanks for your suggestion. I modified the code waiting for RXNE before to rise the CS and up to now seems that the routine working:

Here my IRQ Handler code:

  void SPI1_IRQHandler(void){

  if (State == 0){

  SPI2_CS1_LOW();

    if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE) != RESET){

      //!< Send byte through the SPI1 peripheral //

      SPI_I2S_SendData(SPI1, 67);

  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);

      State = 1;

   }

  }   

 if (State == 1){

   if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_RXNE) != RESET){

      SPI_I2S_ReceiveData(SPI1);

      State = 0;

      SPI2_CS1_HIGH();

   }

 }    

  }

In the routine where I want to write on the DAC I simple enable the TXE IRQ and I write the value in the IRQ Routine.

 

SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);

many thanks

Luca Lombardini