cancel
Showing results for 
Search instead for 
Did you mean: 

Hello everyone,could u please help me with a problem I got in learning the SPI of Stm32,thank you very much

970253146
Associate II
Posted on June 05, 2015 at 21:16

I am a Stm32 new learner.Recently I have had two Stm32 chips to communicated with eachother by using it wardware SPI.After I wrote the master and slave program and downloaded it into both stm32 chips.I used a logic analytical device to observe the wave of MOSI,MISO and clock.I found that the wave(the master date) from the Master was correct,but the data the master received from the slave was wrong.For example I want to send 0xff to the slave and the slave feedback 0x32 to the master(I can see 11111111 coming out from the MOSI,but from the MISO I saw 11001000 not 00110010).I don't know why,I checked both of my program,the polarity are the same and the mode setting is right.

here is my master program

int main(void)

{

SPI_InitTypeDef  SPI_InitStructure;

GPIO_InitTypeDef  GPIO_InitStructure;

 ErrorStatus HSEStartUpStatus;

RCC_DeInit();                                                //ÅäÖÃRCCʱÖÓ//

 RCC_HSEConfig(RCC_HSE_ON);

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)

  {

    RCC_HCLKConfig(RCC_SYSCLK_Div1); 

    RCC_PCLK2Config(RCC_HCLK_Div1); 

    RCC_PCLK1Config(RCC_HCLK_Div2);

    FLASH_SetLatency(FLASH_Latency_2);

    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    RCC_PLLCmd(ENABLE);

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)

    {

    }

  }

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE ); //³õʼ»¯GPIO¡¡//

GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);  

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE ); //³õʼ»¯SPI

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_High;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SPI1, &SPI_InitStructure); 

 SPI_Cmd(SPI1, ENABLE); 

SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);

SPI1_ReadWriteByte(0xff);

while(1);

}

the slave program

int main(void)

{

SPI_InitTypeDef  SPI_InitStructure;

GPIO_InitTypeDef  GPIO_InitStructure;

 ErrorStatus HSEStartUpStatus;

RCC_DeInit();                                                //ÅäÖÃRCCʱÖÓ//

 RCC_HSEConfig(RCC_HSE_ON);

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS)

  {

    RCC_HCLKConfig(RCC_SYSCLK_Div1); 

    RCC_PCLK2Config(RCC_HCLK_Div1); 

    RCC_PCLK1Config(RCC_HCLK_Div2);

    FLASH_SetLatency(FLASH_Latency_2);

    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

    RCC_PLLCmd(ENABLE);

    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)

    {

    }

  }

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE ); //³õʼ»¯GPIO¡¡//

GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);  

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE ); //³õʼ»¯SPI

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_High;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SPI1, &SPI_InitStructure); 

 SPI_Cmd(SPI1, ENABLE); 

SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);

SPI1->DR=0x32;

while(1);

}

I want to explain why I only write 

SPI1->DR=0x32 in slave program.cos I readed some documents about SPI,It tells me that SPI slave will communicate with the master automatically as the clock signal coming from the master.I just want them exchange one byte data,So I gave one date to SPI->DR directively.What's wrong with my program or did I get the SPI process wrong???I am curious if SPI->DR=0xdata works,it can be worng,and I see the sample of ST,they also use 

SPI->DR=0xdata,but now I got a wrong MISO wave....why???

I sincerely want to get some help from you.Thank you very much

 best regards
4 REPLIES 4
Posted on June 05, 2015 at 21:53

You probably shouldn't enable interrupts unless you have code to configure and handle them.

You should pay attention to the state of the status register *before* randomly outputting data. Consider if you need to be looking for a chip-select, and how you expect the two STM32 to be synchronized before you start sending data. ie at the very least wait until the slave signals that it's ready before sending data to it.

Are you using CooCox? Shouldn't the SPL/CMSIS SystemInit() code get the PLL running?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
970253146
Associate II
Posted on June 07, 2015 at 11:51

Hello clive1,I use Keil MDK.I understand I shouldn't send a data to the slave randomly,but I actually don't know the correct approach.I readed STM32 data sheet,but I couldn't understand completely.I refer to the ST template but in that template it is about two chips using DMA by SPI,and use interrupts too.But mu goal is easier,just master giving A to the slave and getting one from slave I am so helpless now,could u please give me some ideas about how to write a program that can make the slave give correct data to the master.I just want them exchange one byte data.Thank you very much

970253146
Associate II
Posted on June 07, 2015 at 13:29

And I also readed some article saying that when Stm32 act as the slave, its baud rate should be lower than the master.But I couldn't understand.In my opinion,the baud rate of the slave has nothing to do with the SPI process.because spi commucation is under the control of the master clock,so once I configurate the master,the slave have nothing to do but receiving datas from the master whild giving datas back to the master based on the master speed.Could u tell me if what that article mentioned is wrong?Thank you very much

danilo239955
Associate II
Posted on June 09, 2015 at 14:34

The management of the GPIO and SPI interfaces in not the same for al members of the STM32 family.

If you are using a STM32F1XX MCU pin PA5 (SCK) and PA7(MOSI)  of the slave and pin PA6 (MISO) of the master  should be initialized in INPUT MODE and not as Alternate Function PP.

B.R.

Danilo