2013-03-30 04:51 PM
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, -Farrell2013-04-03 07:11 AM
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? JW2013-04-05 05:20 PM
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, -Farrell2013-04-07 11:38 PM
>> 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