cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 SPI Issue

harikk09
Associate II
Posted on August 03, 2011 at 08:24

Hai,

I am using STM32F103ZE

I am not getting SPI data correctly.

Master is transmitting correctly.

But always read as zero where a non zero value has been sent.

Master config: (MSP430)

The master configuration is correct. (I tested it.)

Master Mode, MSB First, 8-bit SPI, Inactive state is high., ss grounded, 1 MHz clock, no dividers

Slave Config (STM32F103ZE)

Using SPI2.

  SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;

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

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

Anybody have an answer,

Thanks

Hari

#stm32 #versaloon #stm32f103ze-spi #spi
11 REPLIES 11
harikk09
Associate II
Posted on August 04, 2011 at 04:57

I got when I change SPI direction to two line rx only. I don't know what this means.

But now the data read is neither sent data nor zero.

I don't know why

Can anybody tell me the reason

Posted on August 04, 2011 at 14:00

Knowing how you have this wired might be helpful to understand the issue.

In 1Line_Rx/Slave, The input is SPI2 MISO PB.14

The GPIOB clock needs to be enabled, and the PB.13 and PB.14 configured AF_PP. Along with of course the SPI2 clock.

There is insufficient initialization code presented to see if that is complete/correct.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
whitney
Associate II
Posted on August 04, 2011 at 14:55

hello clive1. you seem to know these microcontrollers pretty well so i have a stumper for you. 

i am using the stm32l152vb and i am trying to read data from an external ic. i have probed all the spi lines with an oscilloscope we have in the lab. the o-scope has an spi bus interpreter on it. if i trigger on the chip select pin, it is clear as day that 0x08 is coming across on the MISO pin. yet i keep reading 0x00. i know my spi initiliazation is good or otherwise i wouldn't see the data on the scope. also, when i tie the MISO pin high, the spi data register reads as 0xFF. which is good. but why on earth can it read 0xFF and not 0x08.

Posted on August 04, 2011 at 15:13

The SPI Data Read just pulls the content of the STM's register. It does not cause a data transfer to occur.

You should read the data register (clears RXNE), send some data to the SPI slave to generate the clocks, and then read the data register again once the send has completed (RXNE set). As it shifts data out it shifts data back in.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
harikk09
Associate II
Posted on August 04, 2011 at 15:27

Thanks for your reply

clive1

My wiring is as follows.

STM32 Ground to MSP Ground.

STM32 PB13 (SCK) to MSP SPI Clock

STM32 PB14 (MISO) to MSP MISO

STM32 PB15 (MOSI) to MSP MOSI

PB13, PB14, PB15 => Configured as AF_PP.

GPIOB clock is enabled.

SPI2 clock enabled.

I am sending 0 back to MASTER. because I don't need any other communication to master.

One interesting thing is that when I changed the slave to accept 16 bit data, after MASTER sends two bytes, the slave read the data but only the last bit is rotated to first bit. for example,

expected data

11111011 01010111

received data

11110110 10101111

I can't use this method. this is just to show that I got something like this,

Can you please identify the problem??

Posted on August 04, 2011 at 16:52

You should check your clock phase/polarity settings.

PB.15 only makes sense if you are using 2Lines_Duplex mode, in 1Line_Rx the data sink is PB.14, at least in all the examples I've looked at.

STM32 Receive Only Slave, in 1 Line Rx Mode

STM32 Ground to MSP Ground.

STM32 PB13 (SCK) to MSP SPI Clock

STM32 PB14 (MISO) to MSP MOSI

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
whitney
Associate II
Posted on August 04, 2011 at 19:29

Thank you clive1. My problem was I was not clearing the RXNE flag after sending out the first byte. When I send my first byte out on the SPI line, the RX buffer is being filled with a 'garbage byte' from the external IC. Therefore, the RXNE flag is set. So, I need to read this byte out of the rx buff to clear the flag, then send my garbage byte. Here is my code for anybody trying to write a simple SPI byte read fcn without using interrupts.

uint8_t MicsRead(uint8_t Addr)

{

  uint8_t RxData;

  

  // put a '1' in front of value to change format of address to a read

  Addr |= 0x80;

  

  // disable ints

  

  // bring chip select low

  _MICS_CS_LOW;

  // send Addr over SPI

  SPI_I2S_SendData(SPI1,Addr);

  // wait until TXE = 1 

  while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) != SET);

  // wait until RXNE = 1

  while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) != SET);

  // read the rx buff to clear the RXNE flag (garbage)

  RxData = (uint8_t)(SPI_I2S_ReceiveData(SPI1));

  // send garbage byte over SPI

  SPI_I2S_SendData(SPI1,0x00);

  // wait until TXE = 1 

  while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) != SET);

  // wait for RXNE = 1

  while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE) != SET);

  // read in rx byte

  RxData = (uint8_t)(SPI_I2S_ReceiveData(SPI1));

  // wait until TXE = 1 

  while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) != SET);

  // wait until BSY = 0 

  while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY) != RESET);

  // bring chip select high

  _MICS_CS_HI;

  

  // enable ints

  

  return(RxData);

}

harikk09
Associate II
Posted on August 05, 2011 at 06:43

Thank you for your reply.

I connected the pins as you said.

But then the value is always 0xff.

When I changed the direction to SPI_Direction_2Lines_RxOnly, the value read is not always 0xff or 0x0, but some junk values.

One interesting thing is that when I changed the slave to accept 16 bit data, after MASTER sends two bytes, the slave read the data but only the last bit is rotated to first bit. for example,

expected data

11111011 01010111

received data

11110110 10101111

I cant use this method since I need to read a gpio line just before the 8 bit data from master.

Can you tell me why this happens??

Posted on August 05, 2011 at 15:36

Can you tell me why this happens??

Like I said, look at you clock phase/polarity settings. You're getting a bit shift because you're using the wrong clock edge. Consider what you're doing on the master.

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..