2015-05-15 10:15 AM
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 Julien2015-05-25 03:51 AM
STM32L152RBT6
(So far, I did not realize that there are several versions of the STM32L1-DISCOVERY around) JW2015-05-25 04:55 AM
Hi Jan,
Here's the full project with a binary file. Julien2015-05-25 05:52 AM
Julien,
got it, thanks. There are two binaries, under Release and Debug subdirectories. Which one does exhibit the problem? JW2015-05-25 07:25 AM
Hum, yes 2 binaries but only one have a elf file ;) ! -> Debug.
fwi, this app uses SPI1 and UART1.2015-05-25 03:55 PM
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. JW2015-05-26 05:32 AM
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 Julien2015-05-26 09:14 AM
> 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, Jan2015-05-26 10:06 AM
2015-05-26 10:12 PM
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. JW2015-05-29 02:35 AM
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 asOSPEED
Rx ?