Skip to main content
NSemrHomeInit
Associate III
Oct 19, 2020 9:19 AM UTC
Solved

My SPI analyzer is different from what I send.

Dear ST Hello,

I am coming back to you because the my spi frame I am sending is not the same I am watching on the oscilloscope,

My spi configuration is:

 /* USER CODE BEGIN SPI3_Init 0 */
 
 /* USER CODE END SPI3_Init 0 */
 
 /* USER CODE BEGIN SPI3_Init 1 */
 
 /* USER CODE END SPI3_Init 1 */
 /* SPI3 parameter configuration*/
 hspi3.Instance = SPI3;
 hspi3.Init.Mode = SPI_MODE_MASTER;
 hspi3.Init.Direction = SPI_DIRECTION_2LINES;
 hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
 hspi3.Init.NSS = SPI_NSS_SOFT;
 hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
 hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi3.Init.CRCPolynomial = 10;
 if (HAL_SPI_Init(&hspi3) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN SPI3_Init 2 */

I am sending this trame of CAFE :p

/* Buffer used for transmission */
uint8_t aTxBuffer[] = {	0xCA,0xFE,0xCA,0xFE,0xCA,0xFE,0xCA,0xFE,
						0xCA,0xFE,0xCA,0xFE,0xCA,0xFE,0xCA,0xFE};

In my analyzer I receive this:

0693W000004KDFjQAO.pngThis is the code for sending and receiving,

 HAL_GPIO_WritePin(SPI3_CS_GPIO_Port, SPI3_CS_Pin, GPIO_PIN_RESET);
	status = HAL_SPI_TransmitReceive(&hspi3, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, 16, 5000);
	HAL_GPIO_WritePin(SPI3_CS_GPIO_Port, SPI3_CS_Pin, GPIO_PIN_SET);

Could you please tell me we why I read 95 FD *16 instead of CA FA *16,

Thank you in advance,

S.Tarik,

This topic has been closed for replies.
waclawek.jan
waclawek.janBest answer
Super User
Oct 19, 2020 12:16 PM UTC

I don't know what the Cube configurations mean, but from the picture it's clear that you have set CPOL=0 and CPHA=0, i.e. SPI Mode 0.

Similarly, I have no idea what the settings on your analyzer mean - you should consult the analyzer's manual - but from what it indicates it appears that it samples on the second, downgoing edge, i.e. CPOL=0 but CPHA=1.

JW

5 replies

waclawek.jan
Super User
Oct 19, 2020 10:57 AM UTC

Your analyzer is probably set to different SPI mode (CPOL, CPHA) than the SPI in STM32.

JW

NSemrHomeInit
Associate III
Oct 19, 2020 11:42 AM UTC

If you take a look to scopy picture, you could see clock polarity LOW, and clock phase = 1,

in the SPI configuration:

 hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;

Tarik

waclawek.jan
waclawek.janBest answer
Super User
Oct 19, 2020 12:16 PM UTC

I don't know what the Cube configurations mean, but from the picture it's clear that you have set CPOL=0 and CPHA=0, i.e. SPI Mode 0.

Similarly, I have no idea what the settings on your analyzer mean - you should consult the analyzer's manual - but from what it indicates it appears that it samples on the second, downgoing edge, i.e. CPOL=0 but CPHA=1.

JW

NSemrHomeInit
Associate III
Oct 19, 2020 12:35 PM UTC

0693W000004KFHrQAO.pngYour absolutely right,

I don't know what is the problem with CubeMx,

Any idea about this issue?

waclawek.jan
Super User
Oct 19, 2020 1:50 PM UTC

Why would there be any problem?

It's just question of definition. Instead of standard CPOL/CPHA notation, Cube authors decided for:

/** @defgroup SPI_Clock_Phase SPI Clock Phase
 * @{
 */
#define SPI_PHASE_1EDGE 0x00000000U
#define SPI_PHASE_2EDGE SPI_CR1_CPHA

If you use Cube, you have to learn both Cube's definitions and their meaning in RM.

JW