Skip to main content
Linas L
Senior
July 1, 2018
Question

SPI generates clocks when set to 8b mode

  • July 1, 2018
  • 4 replies
  • 871 views
Posted on July 01, 2018 at 10:57

I have strange problem, SPI is sending 16 clocks instead of 8 as configured, and my trinamic motor driver simply does not work. Same code worked for STM32F415, right now i am using STM32F051

  GPIO_InitTypeDef  GPIO_InitStructure;

  SPI_InitTypeDef   SPI_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);  

 

  SPI_I2S_DeInit(SPI1);

  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // define for it is ((uint16_t)0x0700)

  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI1, &SPI_InitStructure);

  SPI_Cmd(SPI1, ENABLE);

It looks like normal code, so i don't know what to do now. If i set to 4 bits, it works (generates 8 clocks) , if i set to 8b i always get 16 clocks.

Is it some kind error in silicon? or in SPL libraries ?

Sending is done like this:

while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));   

SPI1->DR=(uint8_t)address;

while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));

R=SPI1->DR;
    This topic has been closed for replies.

    4 replies

    Tesla DeLorean
    Guru
    July 1, 2018
    Posted on July 02, 2018 at 01:59

    >>Is it some kind error in silicon? or in SPL libraries ?

    NO

    The newer families of STM32 have a FIFO on the SPI, and queue a 16-bit write as a pair of 8-bit byte

    while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); 

    *((volatile uint8_t *)&SPI1->DR) = (uint8_t)address; // Do a 8-bit wide write at the peripheral bus

    while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));

    R=*((volatile uint8_t *)&SPI1->DR);
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Linas L
    Linas LAuthor
    Senior
    July 6, 2018
    Posted on July 06, 2018 at 11:26

    I see now, it is more complicated.

    digging to libraries i found different sending function: (Flag check is not included in send function)

      while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));

      *(__IO uint8_t *) ((uint32_t)SPI1+0x0C) = data;

      while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));

      return *(__IO uint8_t *) ((uint32_t)SPI1+0x0C);

    it always hangs on while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));

    But if i comment it, i will get one cycle delay in my SPI register.

    in other words, if i short MISO and MOSI, send 0xAA, i will get back 0, and second time, i will get 0xAA, so it i useless for my implementation.

    How can i get clock in data with no latency ?

    waclawek.jan
    Super User
    July 6, 2018
    Posted on July 06, 2018 at 12:10

    it always hangs on while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));

    Should not.

    If you observe the SPI registers using debugger, stop doing that.

    JW