cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L433 LL SPI sending too much data (16 instead 8)

Linas L
Senior II

Hello. I need ability to send only 8 bit at the time. And I can't figure-out why I can send it, it keeps generating 16clocks no matter what

    LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_4);
    Delay(0x8);
    while((SPI1->SR&SPI_SR_TXE)!=SPI_SR_TXE);
    SPI1->DR =  i;
    while((SPI1->SR&SPI_SR_RXNE)!=SPI_SR_RXNE);
    b = SPI1->DR;
    i++;
    Delay(0x2F);
    LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_4);

By reading reference manual i know it has something to do with FIFO, but i can't chane anything for only 8bit communication

Any simple solution ?

SPI INIT code:

static void MX_SPI1_Init(void)
{
  LL_SPI_InitTypeDef SPI_InitStruct = {0};
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
  /**  PA4   ------> SPI1_CS#
       PA5   ------> SPI1_SCK
       PA6   ------> SPI1_MISO
       PA7   ------> SPI1_MOSI  */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_5|LL_GPIO_PIN_6|LL_GPIO_PIN_7;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_5;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  SPI_InitStruct.TransferDirection = LL_SPI_FULL_DUPLEX;
  SPI_InitStruct.Mode = LL_SPI_MODE_MASTER;
  SPI_InitStruct.DataWidth = LL_SPI_DATAWIDTH_8BIT;
  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_DIV64;
  SPI_InitStruct.BitOrder = LL_SPI_MSB_FIRST;
  SPI_InitStruct.CRCCalculation = LL_SPI_CRCCALCULATION_DISABLE;
  SPI_InitStruct.CRCPoly = 7;
  LL_SPI_Init(SPI1, &SPI_InitStruct);
  //SPI1->CR2 |= SPI_CR2_FRXTH; try that, does not efect anything
  LL_SPI_SetStandard(SPI1, LL_SPI_PROTOCOL_MOTOROLA);
  LL_SPI_EnableNSSPulseMgt(SPI1);
  LL_SPI_Enable(SPI1);
}

1 ACCEPTED SOLUTION

Accepted Solutions

Sensitive to the width of the write access on the bus, SPI1->DR is 16-bit wide per structure

*(volatile uint8_t *)&SPI1->DR = i;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

4 REPLIES 4

Use

*(uint8_t *)&SPI1->DR = i; [EDIT] yes, add volatile, as Clive wrote below [/EDIT]

to write a byte.

Read "data packing" subchapter in SPI chapter in RM.

JW

Sensitive to the width of the write access on the bus, SPI1->DR is 16-bit wide per structure

*(volatile uint8_t *)&SPI1->DR = i;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Yes, perfect result ! Thank you so much :)

Your was working also, but ok, will change it as Clive1 writen