STM32F746 SPI basic usage
Hi everybody!
I've been struggling with this monster for a few days now. I'm trying to make it do some very basic (pun not intended) things and I have little hair left... But let's cut a (too) long story short: I'd like to send with the SPI a few bytes to a device. I'm using polling (DMA and INT's is an epic drama for another time), although that's not relevant at this point. So let's just send a byte (8 bits) thru SPI. I've set up the GPIOs for the SPI normal, as alternate functions (most of this code has been generated by CubeMX) - with NSS port as normal output - like this:
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOA);
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
LL_SPI_InitTypeDef SPI_InitStruct = {0};
GPIO_InitStruct.Pin = CK_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = MOSI_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = MISO_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);And this is the interesting part, the SPI setup:
SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT; // this is like using Bytes, right?
SPI_InitStruct.ClockPolarity = LL_SPI_POLARITY_LOW;
SPI_InitStruct.ClockPhase = LL_SPI_PHASE_1EDGE;
SPI_InitStruct.NSS = LL_SPI_NSS_SOFT;
SPI_InitStruct.BaudRate = LL_SPI_BAUDRATEPRESCALER_DIV16;
SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
SPI_InitStruct.CRCPoly = 7;
LL_SPI_SetRxFIFOThreshold(SPI4, LL_SPI_RX_FIFO_TH_QUARTER); //the infamous FIFO, ignored in most STM examples,
// just like the NSS output
LL_SPI_Init(SPI4, &SPI_InitStruct);
LL_SPI_SetStandard(SPI4, LL_SPI_PROTOCOL_MOTOROLA);
// then I enable SPI:
SET_BIT(SPI4->CR1, SPI_CR1_SPE);
// so far, so good. And now I try to send a byte:
SPI1->DR = data; //data defined as a uint8_t of a whatever valuewhat I get is 8 bits of 'data' on MOSI and CLK outputs, followed by another 8 bits of CLK and MOSI=0. In total 16 bits/pulses. Why is that?! How can I convince this animal to give what is set up for - 8 bits? What do I miss?
Thank you in advance for any input!
