STM32F103 SPI Master Slave Receive problem

0

I try to make master and slave stm32f103 (bluepills) communicate. But I am having trouble with receiving. When I connect my master to logic analyser I can see MOSI as in the picture

Logic Analyser

In the picture, MOSI is sending "Y" letter. But not all clock pulses same.(I don't know if this is the reason of communication fail)

here is my schematics and my code I simplified as much as I can.

Master Code:

    int i;


RCC ->APB2ENR |= 0x00001004; //SPI1,GPIOA clock en

GPIOA ->CRL &= 0x00000000;
GPIOA ->CRL |= 0xb0b33333;


SPI1->CR1  = SPI_CR1_SSM| SPI_CR1_SSI| SPI_CR1_MSTR|SPI_CR1_BR_2;  

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

    while(1){

SPI1 -> DR  = 'A';

for(int i = 0 ;i<400000;i++);

while( !(SPI1->SR & SPI_SR_TXE) );  // wait until transmit buffer empty

    }

and Slave

    int i;
RCC ->APB2ENR |= 0x0000100c; //SPI1,GPIOA,GPIOB clock en

GPIOB ->CRH &= 0x00000000;
GPIOB ->CRH |= 0x33333333;
GPIOA ->CRL &= 0x00000000;
GPIOA ->CRL |= 0x4b443333;
GPIOA ->CRH &= 0x00000000;
GPIOA ->CRH |= 0x33333333;


SPI1->CR1  = SPI_CR1_SSM| SPI_CR1_SSI| SPI_CR1_BR_2;
SPI1->CR1  |= SPI_CR1_SPE;  // enable SPI
SPI1->CR1 &=~SPI_CR1_MSTR;  //disable master

 for(int c=0;c<5;c++){
LCD_INIT(cmd[c]);
}

while(1){




     while( !(SPI1->SR & SPI_SR_RXNE));

        char a = SPI1 ->DR;

 for (i=0;i<400000;i++);

 LCD_DATA(a);


 for (i=0;i<400000;i++);

 }
 }

My Schematic: Schematic

The problem is slave is not receiving any data. It stucks in the loop while( !(SPI1->SR & SPI_SR_RXNE));

stm32
spi
asked on Stack Overflow Feb 11, 2020 by iraquois • edited Feb 12, 2020 by glts

1 Answer

1

First, what are your HCLK and APB2 bus frequencies? If I'm not mistaken, you seem to use (fPLCK / 32) for SPI clock and your logic analyzer shows ~2 or 3 MHz clock. If your APB2 frequency is higher than the 72 MHz limit, you may experience clock problems.

In the slave, you use SSM (software slave management) and activate SSI (internal slave select). The name of the SSI bit is misleading: It mimics the physical NSS pin. So when SSI = 1, the slave is not selected. This is probably the reason why the slave ignores the incoming bytes.

answered on Stack Overflow Feb 12, 2020 by Tagli • edited Feb 15, 2020 by Tagli

User contributions licensed under CC BY-SA 3.0