cancel
Showing results for 
Search instead for 
Did you mean: 

Strange reading over SPI

JulienD
Senior
Posted on May 15, 2015 at 19:15

Hi,

I'm porting an implementation of SD over spi to stm2 using Cube library.

It seems that the communication micro to sd card is ok, it seems that the sd card

answers like it should but it looks like the microcontroler does not interpret correctly

the data on the spi bus.

To get this conclusion, I asked the micro to echo each byte received on SPI to an uart.

On the logic analyzer screen capture joined, you can see :

- on channel 5 (green), data micro -> SD

- on channel 7 (pink), data SD -> micro

- on channel 3 (orange), the uart echo.

Each byte on channel 3 should be the same of the preceeding byte on channel 7 which is

not always the case.

Here's the initialization code of the spi channel :

   __SPI2_CLK_ENABLE();

    GPIO_InitStructure.Pin   = GPIO_Pin_SPI_SD_SCK | GPIO_Pin_SPI_SD_MOSI;

    GPIO_InitStructure.Mode  = GPIO_MODE_AF_PP;

    GPIO_InitStructure.Pull = GPIO_NOPULL;

    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;

    GPIO_InitStructure.Alternate = GPIO_AF5_SPI2;

    HAL_GPIO_Init(GPIO_SPI_SD, &GPIO_InitStructure);

    GPIO_InitStructure.Pin   = GPIO_Pin_SPI_SD_MISO;

    GPIO_InitStructure.Mode  = GPIO_MODE_INPUT;

    GPIO_InitStructure.Pull = GPIO_PULLUP;   // obligatoire

    HAL_GPIO_Init(GPIO_SPI_SD, &GPIO_InitStructure);

    /* SPI configuration */

    hndSPI.Instance = SPI_SD;

    hndSPI.Init.Mode = SPI_MODE_MASTER;

    hndSPI.Init.Direction = SPI_DIRECTION_2LINES;

    hndSPI.Init.DataSize = SPI_DATASIZE_8BIT;

    hndSPI.Init.CLKPolarity = SPI_POLARITY_LOW;

    hndSPI.Init.CLKPhase = SPI_PHASE_1EDGE;

    hndSPI.Init.NSS = SPI_NSS_SOFT;

    hndSPI.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; // 72000kHz/256=281kHz < 400kHz

    hndSPI.Init.FirstBit = SPI_FIRSTBIT_MSB;

    hndSPI.Init.TIMode = SPI_TIMODE_DISABLED; // todo valeur au hasard = motorola, sinon TI

    hndSPI.Init.CRCPolynomial = 7;

    hndSPI.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;

    HAL_SPI_Init(&hndSPI);

Here's how I read over it :

BYTE rcvr_spi() {

    uint8_t rx;

    uint8_t tx = 0xff;

    HAL_SPI_TransmitReceive(&hndSPI, &tx, &rx, 1, 50);

    HAL_UART_Transmit(&huart4, &rx, 1, 50);

    return rx;

}

Does anybody have an idea of what could be wrong ?

Regards

Julien
22 REPLIES 22
Posted on May 25, 2015 at 12:51

STM32L152RBT6

(So far, I did not realize that there are several versions of the STM32L1-DISCOVERY around)

JW

JulienD
Senior
Posted on May 25, 2015 at 13:55

Hi Jan,

Here's the full project with a binary file.

http://we.tl/lIH0h1Odbm

Julien

Posted on May 25, 2015 at 14:52

Julien,

got it, thanks.

There are two binaries, under Release and Debug subdirectories. Which one does exhibit the problem?

JW

JulienD
Senior
Posted on May 25, 2015 at 16:25

Hum, yes 2 binaries but only one have a elf file ;) !  -> Debug.

fwi, this app uses SPI1 and UART1.

Posted on May 26, 2015 at 00:55

Julien,

I could not reproduce the symptoms you described with the binary provided.

I spent quite some time trying to understand what's going on - there was no echo at all. When I brought the baudrate down (manually overwriting the respective field in SPI1_CR1 in debugger) suddenly some echo appeared, although severely corrupted. Bringing it further down brought success. Then I realized, that you set the SCK/MOSI/MISO output drivers to weak

 GPIO_InitStruct.Speed = GPIO_SPEED_VERY_LOW;

when I set it to 0x02 (''Medium speed'' - I rewrote the constant directly in the binary), the echo is as supposed, without any error.

JW

JulienD
Senior
Posted on May 26, 2015 at 14:32

Hi Jan,

Sorry for the non working example, as I don't have this board, I could not test

the binary before.

Well, you're right about the pin speed.

I would be pleased to pay you a beer !

Thanks a lot

Julien

Posted on May 26, 2015 at 18:14

> Sorry for the non working example, as I don't have this board, I could not test

the binary before.

Oh of course, I did not complaint.

> I would be pleased to pay you a beer !

It's not beer-time yet. What exactly is your target? Could you produce a similar minimal example for your target exhibiting the problem, and post it as it is?

Thanks,

Jan

JulienD
Senior
Posted on May 26, 2015 at 19:06

By ''Well, you're right about the pin speed.'', I meant the problem is solved by increasing

the pin speed on my board too.

I have to dive in the datasheet to learn about this. It was my first try with stm32. I come from

microchip and Xmega... Lot of new things on the road.

Thanks again for your help.

Julien

0690X000006031QQAQ.jpg

Posted on May 27, 2015 at 07:12

Thanks for the beer :)

You really will find nothing specific to read - if the pins drivers are not strong enough, the rising/falling edges due to parasitic capacitances may be simply to slow for the specified baudrate.

JW
JulienD
Senior
Posted on May 29, 2015 at 11:35

I read the sheet and it raises a question for me:

- as speeds are defined only for a maximum frequency and not on a fork of frequency,

what are the drawbacks of always using 11 as

OSPEED

Rx ?