cancel
Showing results for 
Search instead for 
Did you mean: 

Synchronizing SPI in STM32F107

parisa
Senior

Hello,

In my project, I connected two MCUs to each other (MISO->MISO, MOSI->MOSI, SCK->SCK). I define a new pin as the CS pin (PORTC.10). But in my receiver the sequence is wrong. Here is my code( For example I send 1234 but I get something else, sometimes works many times doesn't)

void SPI1_IRQHandler(void){
 
    unsigned long Input;
            if (SPI_I2S_GetITStatus(SPI1,SPI_I2S_IT_RXNE)== SET) 
                     {
 
                    Input=SPI_I2S_ReceiveData(SPI1);
 
                    MSG[Position]=Input;Position=Position+1;
 
                    if(Position>=(PosMax+1)){Position=0;SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,DISABLE);}
                }   
 
            if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE)== SET )  
                {       
                        SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);
                }
 
 
 
}
 
 
void EXTI15_10_IRQHandler(void){
 
 
if (EXTI_GetITStatus(EXTI_Line10) != RESET) {
 
            if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_10)==0)
            {
                        SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);
            }   
            else
            {
                            SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,DISABLE);
                            if(Position>=PosMax)
                                {
                                    Position=0;     
                                    SendTest=MSG[2];
                                }               
            }
 
             EXTI_ClearITPendingBit(EXTI_Line10);
}

I checked the signal on oscilloscope and all transmitted signals are correct. However, my receiver MCU lose the position of incoming data and lost my data(sometimes when it is synchronized by transmitter works fine but other times it loses my data).

5 REPLIES 5
berendi
Principal

Can you connect the CS line to the SPI NSS pin on the slave? Handling it in a software interrupt could be too slow.

parisa
Senior

Thank you berendi,

For this circuit (PCB is manufactured), I don't think so

Not good.

Processing the EXTI interrupt might take too long, and SPI can miss a couple of SCK cycles.

If it's possible to change the master, insert a delay between activating CS and starting the transmission.

The delay should be long enough to cover the worst case interrupt latency on the slave. Sum of the longest possible execution time of the EXTI10 handler and all slave side interrupts that have priorities equal to or higher than EXTI10 plus a microsecond for good measure.

parisa
Senior

Thank you so much for your complete answer. I try to make another PCB to check this situation again.

If this is the only slave connected to the master, you can try the following.

Set up EXTI to trigger only on the rising edge, disable SPI and enable it immediately again. If the gap between the data packets is long enough, it might actually work.