cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Peripheral data bit size on STM32F072

bjerman
Associate II
Posted on June 22, 2015 at 22:59

Hi.

So I'm having a peculiar problem with the SPI on the STM32F072.

I'm trying to configure SPI1 and it seems to work, but with one cave-eat. I can't seem to configure the bit size (SCLK) down to 8b from 16b. I've been trying to do it through the CMSIS initialization structure but the changes aren't having any effect, so I've also tried it directly in the configuration registers. Still no luck.

The part where I tried configuring the registers manually:

SPI1->CR2 &= ~(1<<11);

 

SPI1->CR2 |= 0x700;

 

SPI1->I2SCFGR &= ~(1<<10);

 

SPI1->I2SCFGR &= ~(1<<11);

As you can see I've also thought about having the SPI configured in the I2S mode, but this doesn't seem to be the case.

0690X00000605HfQAI.png

The inserted picture is the oscillograph of the SPI Clock line (single transfer). No matter what I do, it clocks 16 bits.

The initialization routine is below:

SPI_Cmd(SPI1, DISABLE);

 

 

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

 

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

 

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

 

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

 

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

 

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

 

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

 

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set;

 

 

SPI_Init(SPI1, &SPI_InitStructure);

 

 

SPI1->CR2 &= ~(1<<11); // 

 

SPI1->CR2 |= 0x700; // Set databyte size number to 0111

 

 

SPI1->I2SCFGR &= ~(1<<10); // Choose SPI mode

 

SPI1->I2SCFGR &= ~(1<<11); // Disable I2S Peripheral

 

 

SPI_Cmd(SPI1, ENABLE);

I'd be very grateful for any help or ideas.

#stm32f072-spi-bytesize
5 REPLIES 5
Posted on June 22, 2015 at 23:06

And what are the content of CR2 and I2SCFGR before you enable the SPI peripheral?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bjerman
Associate II
Posted on June 22, 2015 at 23:35

Huh, nice catch.

The value of CR2 (*40013004) is 0x700 and the value of I2SCFGR (*400131C) is 0x00.

These are the reset values (read after the supposed initialization) which means that my initialization code doesn't have any effect.

I need to figure out why is that. Tell me, is it obvious? :)

[Edit]

Stupid me, the values are exactly like they're supposed to be. I'm going to check on the other setup bits in CR2.

Checks out. I'm stumped. Gonna go check the other configuration registers. I'd be grateful for a nudge in a right direction.
bjerman
Associate II
Posted on June 23, 2015 at 00:03

I don't know the rules about doubleposting on these forums, but I'm guessing it helps bring out new information more readily than an edit to the old post.

So just for kicks, I initialized the structure with

SPI_InitStructure.SPI_DataSize = SPI_DataSize_4b;

And this is the result (SPI clock)

0690X00000605HkQAI.png

The value of the CR2 register is properly initialized for the 4-bit transfer, but from the oscillograph, we can see that there are 8 clock cycles there.

Now I'm really interested in what exactly is happening. Did I completely misunderstood SPI, or is there something fishy going on?

Posted on June 24, 2015 at 17:32

Well there's really no point starting a new thread if you're just following on from the original narrative.

May be it's how you're sending the data?

This is really hard to know here remotely as your code snippet really doesn't provide adequate context to see both how you're configuring, and how you're sending. Please provide a complete/concise example that configures the clock, pins and peripherals, and sends the test data.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ilias
Associate II
Posted on September 06, 2015 at 16:00

I have exactly the same problem with the STm32F072!!! Can you please provide an explanation and how overcome this problem?