AnsweredAssumed Answered

SPI 8bit data length

Question asked by Turboman on Feb 28, 2017
Latest reply on Mar 2, 2017 by Turboman

Hi, I'm setting up SPI comms with a slave device. (ADXL350 accelerometer) This is on an STM32F767ZIT6. I should point out I put this on a nucleo -'F746 board but it's all working fine so far at 200MHz with other peripherals presenting no problem.

 

I've selected 8 bit data size, but am seeing 16 clocks on a scope. I use a read function to read address 0x00, the device's "who_am_I" register, and get 0xE5 back. In theory this is ok.

 

However, I see the image here:

Image showing 16bit data transfer

Yellow = MOSI

Cyan = MISO

Red = CLK

Green = _CS

 

The accelerometer is replying <0xE5> 3 times (trace 2 in cyan) because of this 16bit data size. If I set it to 4bit data size, I get 8 data clocks. But then I get a nybble in the low byte and a nybble in the high byte.

 

What worries me is that other people are saying the HAL can't transmit single bytes

 

Page 9 of your own SPI protocol document describes an SPI read operation completing in 16 clocks total

 

I use my own transfer code, but copied HAL initialisation code as follows:

 

void spiSetup(void)
{
   hspi4.Instance = SPI4;
   hspi4.Init.Mode = SPI_MODE_MASTER;
   hspi4.Init.Direction = SPI_DIRECTION_2LINES;
   hspi4.Init.DataSize = SPI_DATASIZE_8BIT;
   hspi4.Init.CLKPolarity = SPI_POLARITY_HIGH;
   hspi4.Init.CLKPhase = SPI_PHASE_2EDGE;
   hspi4.Init.NSS = SPI_NSS_SOFT;
   hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
   hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;
   hspi4.Init.TIMode = SPI_TIMODE_DISABLE;
   hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
   hspi4.Init.CRCPolynomial = 7;
   hspi4.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
   hspi4.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

   if (HAL_SPI_Init(&hspi4) != HAL_OK)
   {
      //Error_Handler();
      while(1){};
   }

   SPI4->CR1 |= SPI_CR1_SPE;

}

All I'm doing is writing to SPI4->DR and waiting for the TXNE or BUSY flags to clear.

Any help would be appreciated.

Outcomes