cancel
Showing results for 
Search instead for 
Did you mean: 

spi error send slave

bfeixas
Associate II
Posted on April 11, 2012 at 19:48

Hi, I got two different devices with the microcontroler STM32F103RB which I'm trying to comunicate between them using the SPI protocol.

The element A, is the master :

The element B, is the slave :

When I send from A(master) to B(slave) it works fine like that, but it doens't work when I try to send form B to A. I know that B must use the time clock that gives A (SCKL) but I don't know how it should work ?

I couldn't find the same example

Here I attach the example code:

-master (A):

 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;  //SPI_BaudRatePrescaler_8;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI1, &SPI_InitStructure);

  /* Enable SPI2 RXNE interrupt */

  SPI_ITConfig(SPI1, SPI_IT_RXNE, ENABLE);

  /* Enable SPI1 */

  SPI_Cmd(SPI1, ENABLE);

main{

...

    while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);  

    SPI_SendData(SPI1, '0'); 

...

}

-slave(B):

 void init_spi(void){

SPI_InitTypeDef  SPI_InitStructure;

   NVIC_InitTypeDef NVIC_InitStructure;          

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

          /* Configure and enable SPI1 interrupt -------------------------------------*/

  NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

      SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

      SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;

      SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

      SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

      SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

      SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

      SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;

      SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

      SPI_InitStructure.SPI_CRCPolynomial = 7;

      SPI_Init(SPI1, &SPI_InitStructure);

      /* Enable SPI1 RXNE interrupt */

       SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);

      /* Enable SPI1 */

      SPI_Cmd(SPI1, ENABLE);

}

void SPI1_IRQHandler(void)

{     /* Store SPI1 received data */

    

  u8 temp,i;

  temp = SPI_I2S_ReceiveData(SPI1);

    if(temp==4){

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

        SPI_I2S_SendData(SPI1, 'R');

  }
2 REPLIES 2
Posted on April 11, 2012 at 20:17

On the Master

while(1)

{

    while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);  

    SPI_SendData(SPI1, '0'); 

    while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);  

    SPI_SendData(SPI1, '0'); 

    while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);  

    SPI_SendData(SPI1, '0'); 

    while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);  

    SPI_SendData(SPI1, 4); 

}

Where you presumably have the RXNE interrupt code to pluck incoming data from the DR register.

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

Thanks for the help, at the end I discovered that the main problem was that I had forget to set the MISO pin in the slave as output ^_^ '