cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0: Trying to use SPI2 in 8-bit mode, but each transfer causes 16 clock cycles

upgrdman
Associate II
Posted on March 31, 2013 at 00:51

I want to use SPI2 of an F0 in 8-bit mode. My stripped-down code is below. Looking at the SPI2_CLK line on a scope shows 16 cycles each time I write to SPI2->DR.

// reset and enable spi2

RCC->APB1RSTR |= RCC_APB1RSTR_SPI2RST;

RCC->APB1RSTR &= ~RCC_APB1RSTR_SPI2RST;

RCC->APB1ENR |= RCC_APB1ENR_SPI2EN;

// setup spi2 registers

SPI2->CR1 |= SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_BR_0;     // br = 111 = fpclk/256

SPI2->CR2 |= SPI_CR2_DS_0 | SPI_CR2_DS_1 | SPI_CR2_DS_2;     // ds = 0111 = 8bit mode

SPI2->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI;      // software slave management, software slave select

SPI2->CR1 |= SPI_CR1_MSTR;     // master

SPI2->CR1 |= SPI_CR1_SPE;     // spi enable

// wait for empty TX buffer

while((SPI2->SR & SPI_SR_TXE) == 0);

// send one byte of data ... but 16 bits get sent

SPI2->DR = 0x01;

Any ideas?

Thanks,

-Farrell
3 REPLIES 3
Posted on April 03, 2013 at 16:11

And what happens if you try a completely different bit number, e.g. 11?

Also, what happens if you don't use |= but = when writing to _CR2?

JW

upgrdman
Associate II
Posted on April 06, 2013 at 02:20

Writing (1 << 11) to the DR will send a 16bit number with bit 11 set. SPI2->DR is defined as a uint16_t, and if I try to write to the register as a uint8_t the F0 stops working, presumably sitting in a fault handler.

Wrting to CR2 with = has the same effect as my |= code. I have checked the value of CR2 and CR1 to verify both methods.

Thanks,

-Farrell

Posted on April 08, 2013 at 08:38

>> And what happens if you try a completely different bit number, e.g. 11?

> Writing (1 << 11) to the DR will send a 16bit number with bit 11 set.

No, I meant to set the DS field in SPI_CR2 register to 1010.

JW