cancel
Showing results for 
Search instead for 
Did you mean: 

SPI 8bit data length

tau
Associate II
Posted on February 28, 2017 at 16:56

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:

0690X00000603hHQAQ.jpg

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.

https://community.st.com/0D50X00009XkeupSAB

http://www.st.com/content/ccc/resource/technical/document/technical_note/58/17/ad/50/fa/c9/48/07/DM00054618.pdf/files/DM00054618.pdf/jcr:content/translations/en.DM00054618.pdf

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.

null
11 REPLIES 11
Amel NASRI
ST Employee
Posted on March 02, 2017 at 15:48

Hi

Slater.Tau

‌,

Your question is marked as answered thanks to the help of

Waclawek.Jan

‌ and

Centauris.Alpha

‌. But I would like to understand the following:

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.

Other than using different chips, are there other differences for SPI communication tested with STM32F767? Were you using same code?

It is interesting to understand this because both products have same SPI features.

Thanks for sharing these details.

-Amel

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

tau
Associate II
Posted on March 02, 2017 at 17:35

To be honest this code has been developed entirely on the '767. The process has been:

1. Change '746ZG to 767ZI on Nucleo board

2. Create a project in SW4STM32 for the '767

3. Use CubeMX to generate initialisation code for a '767 chip - no board selected.

4. Set up debugging and get coding...

I do have an unadulterated '746 nucleo board, so could in theory just port/copy the code over. (assuming 746's SPI4 is of the same type) Would be a valuable step for me as I could see just how portable my code is already. I think I might even be using the SystemClock_Config() code from a 746 disco board on one of my other projects actually.

If I have any more info, I'll post back / blog it / link to it or something.

Regards