cancel
Showing results for 
Search instead for 
Did you mean: 

SPI slave configuration

freya17365
Associate II

Hi to all,

I'm dealing with SPI interface in slave mode and I am experience some problem in communication.

I am using a ST32F030 MCU and I enabled RXNEIE interrupt for receiving data. Hereafter is the code:

CONFIGURATION

RCC->APB2ENR |= RCC_APB2ENR_SPI1EN;      // Enable SPI1 clock

SPI1->CR1 = 0x0000;    // Disable and reset SPI

SPI1->CR1 |= 0x02<<3;   // Bidirectional | Slave | CPOL = 0 | CPHA = 0

SPI1->CR2 = 0x0700;    // Reset CR2

SPI1->CR2 |= SPI_CR2_FRXTH | 0x07<<8 | SPI_CR2_RXNEIE;   // RX full = 1/4 (8bit) | 8 bit | enable RXNE interrupt

SPI1->CR1 |= SPI_CR1_SPE;   // Enable SPI

INTERRUPT

uint16_t spi1cnt = 0;

extern "C" void SPI1_IRQHandler()

{

   uint8_t c, *k;

   k = (uint8_t *) &(SPI1->DR);   // consider register as 8 bit

   if ((SPI1->SR & SPI_SR_RXNE) == SPI_SR_RXNE)

   {

      c = *k;       // Store received byte

      SPI1->DR = (uint8_t) spi1cnt;   // Send a number sequence

      if(spi1cnt < MAXBUF - 10)

      {

         spibuf[spi1cnt++] = c;

         spibuf[spi1cnt] = 0;

      }

      else

      {

         spibuf[spi1cnt] = 0;

         spi1cnt = 0;

      }

   }

   else print("ERROR");

   return;

}

Here is what I received

2 0 3 0 6 0 7 0 10 0 11       should be 2 3 4 5 6 7 8 9 10 11 12

0 14 0 15 0 18 0 19 0 22 0   should be 12 14 15 16 17 18 19 20 21 22 23

23 0 26 0 27 0 30 0 0 0 3    should be 24 25 26 27 28 29 30 0 1 2 3

0 4 0 7 0 8 0 11 0 12 0

0 16 0 19 0 20 0 23 0 24

Do I forgot something in configuration?

I checked on scope all signals and they changed their state (I can check only one channel per time).

Any hint?

Thank you

Freya

0 REPLIES 0