cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F401VB - Clock Idle State of SPI Interface does not stay high

michalik
Associate II
Posted on July 24, 2015 at 11:09

Hey guys!

I have an issue with the SPI Interface of the STM32F401VB. To communicate with an ADC from LTC, I need to configure the SCK to stay high during idle time. Unfortunately the clock always starts low, see scope pictures. Where exactly is the problem? Do I need any pull up resistors or something ?

Thanks in advance for your help!

Best regards

David

11 REPLIES 11
Posted on July 24, 2015 at 11:22

This is what CPOL bit in SPI_CR1 is for. Read the Clock phase and clock polarity sub-chapter of SPI chapter in RM.

What confuses me on the scope-shot is that it appears you are tristating of the CS pin after the communication ends, why do you do that?

JW

michalik
Associate II
Posted on July 24, 2015 at 11:33

Hello Jan,

yes I know that CPOL and CPHA are the bits to set and I did. As you can see on the scope, the SCK is High after the transmittion is finished but it goes to low slowly and the SCK is low again when the next transmittion starts. Here my initialization code for the SPI interface:

void MX_SPI3_Init(void)
{
hspi3.Instance = SPI3;
hspi3.Init.Mode = SPI_MODE_MASTER;
hspi3.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
hspi3.Init.DataSize = SPI_DATASIZE_16BIT;
hspi3.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi3.Init.NSS = SPI_NSS_SOFT;
hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi3.Init.TIMode = SPI_TIMODE_DISABLED;
hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
hspi3.Init.CRCPolynomial = 10;
if(HAL_SPI_Init(&hspi3) != HAL_OK)
{
/* Initialization Error */
Error_Handler();
}

the CS pin is acutally the ''Convert Pin'' and it's the answer from the ADC. After it goes to low again the data can be shifted out, so I use this pin to start an interrupt at the falling edge of CS. The problem is the clock. I miss B15 because it the SCK line is not high while idle.
Posted on July 24, 2015 at 12:04

And what happens if you avoid the Cube libraries? SPI Master is trivial to write using direct register accesses; another way for a quick try is to write/read SPI registers directly in the debugger.

JW

PS. What's your location?

michalik
Associate II
Posted on July 24, 2015 at 12:25

I've checked the registers and the values are written corretly. So it seems that the HAL driver is okay and working corretly.

  PS: Location's Germany, NRW.

Posted on July 24, 2015 at 13:50

So, if you stop the program after the setup, and then in debugger write something into DR, watching on the scope, can you see the clocks and then the clock line going threestate?

JW
michalik
Associate II
Posted on July 24, 2015 at 13:54

yes. the problem is that CLK should end high and start high again, but as you can see on the scope shot it starts low, ends high but goes into some kind of tristate..

Posted on July 24, 2015 at 14:17

Once again: did you try it *without* the Cube stuff? Directly writing to DR in the debugger?

JW

michalik
Associate II
Posted on July 24, 2015 at 14:33

I'm acting as a slave, so why should I try to write anything into DR?

EDIT: Okay I've got the solution. The ST MCU support team answered and said that after the transmittion, SCK goes to Z-State. So to have a SCK line that stays high you need to use a pull-up resistor.. Wondering that nobody before had this problem, cause there is no line in the application notes or reference manuals that describes that. All pictures concerning the SPI interface let you believe that SCK stays high when set to CPOL=1...

Posted on July 24, 2015 at 14:50

> I'm acting as a slave

From your above post: >

hspi3.Init.Mode = SPI_MODE_MASTER;

So, please, restate, what is your setup and what is your problem exactly. If the STM32F401 is slave, it's the other device which drives the clock, so you should investigate that one. JW