cancel
Showing results for 
Search instead for 
Did you mean: 

Inconsistencies with every HAL_SPI_Transmit() of the same message

malehakim
Associate III

Am getting inconsistencies with every HAL_SPI_Transmit() of the same message in a for-loop. Viewed the result with a logic analyzer and the results are frustrating.

Am just trying to send a Hello World! but on the Logic Analyzer, am getting dummy data.

Am using the STM32F303k8.

Here is my initialized code

char* myTxData = "Hello World!";
 
int main(void)
{
	HAL_Init();
 
	SystemClock_Config();
 
	MX_GPIO_Init();
	MX_SPI1_Init();
 
	// CSN PIN
	HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
 
	while (1)
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
		HAL_SPI_Transmit(&hspi1, (uint8_t*)myTxData, 12, HAL_MAX_DELAY);
		HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);
 
		HAL_Delay(100);
	}
}
 
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
}
 
static void MX_SPI1_Init(void)
{
	hspi1.Instance = SPI1;
	hspi1.Init.Mode = SPI_MODE_MASTER;
	hspi1.Init.Direction = SPI_DIRECTION_2LINES;
	hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
	hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
	hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
	hspi1.Init.NSS = SPI_NSS_SOFT;
	hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
	hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
	hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
	hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
	hspi1.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
}

0693W00000BbUG7QAN.png

1 ACCEPTED SOLUTION

Accepted Solutions
malehakim
Associate III

This was a tough but one but i finally got it solved. It turned out that my saleae logic devices' ground pin corresponding to the one connected to the CSN pin was not inserted in ground. So all i did was to insert it and the glitches on the CSN line disappeared like a fart i the wind

View solution in original post

10 REPLIES 10

Zoom in to one of the messages so that individual clocks can be seen.

JW

malehakim
Associate III

Snap view of the individual clocks generated when i send. Am expecting Hello World! in form of ascii but it seems to pick up dummy data that probably from other registers or the data is corrupted in some way

0693W00000BbXKDQA3.png

TDK
Guru

Looks like your CSN line is not behaving correctly. so your bytes are not synchronized to where they should be. I bet an analog capture of that line would show considerable noise, since there are 3 spikes that correspond with a negative edge on MOSI. Are you initializing it as output?

If you feel a post has answered your question, please click "Accept as Solution".

The transmitted characters as you show them are exactly those corresponding to Hello World!

48 65 6C 6C 6F 20 57 6F 72 6C 64 21

so it's just the LA which is not synchronized - if you set it to synchronize to CSN, then it's as TDK said - either incorrectly initialized pin, incorrectly connected (bad solder joint), or if it is a board with more circuitry on it, it conflicts with some other output.

JW

malehakim
Associate III

0693W00000BbiADQAZ.pngOk, thanks for all the replies. Am now starting to get it working. However, my CSN line is still not behaving well. It spikes up as shown in the snap above. One thing i've seen from all the tutorials is that they never initialize the CSN pin. Its always the MISO, MOSI, SCLK and the GPIO pins as alternate functions. I tried to initialize the GPIO where the CSN pin is so that i try with either pull-up or pull-down resistors but still not getting it right. How can i get a more consistent CSN link acting only to the right configuration.

TDK
Guru

> How can i get a more consistent CSN link acting only to the right configuration.

CSN should be configured as a GPIO output pin and toggled manually before and after each transfer.

You appear to be toggling it with HAL_GPIO_WritePin, but not sure if you're initialized it correctly as you didn't include MX_GPIO_Init.

If you feel a post has answered your question, please click "Accept as Solution".
malehakim
Associate III

this is how am initializing the GPIO ports

static void MX_GPIO_Init(void)
{
 
	GPIO_InitTypeDef GPIO_InitStruct;
	/* GPIO Ports Clock Enable */
	__HAL_RCC_GPIOA_CLK_ENABLE();
 
	GPIO_InitStruct.Pin = GPIO_PIN_4;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
}

But still getting the glitch on the CSN line thats messing up all the data. It seems to happen when the clock is going down and MOSI is going low.....although this happens sometimes. Gash, thisz so frustrating. What on earth am i missing.

TDK
Guru

Should work. Remove the SPI part and just toggle PA4 until you get it working correctly. Double check connections. Make sure nothing else is competing for PA4.

If you feel a post has answered your question, please click "Accept as Solution".

either incorrectly initialized pin (read out and check the relevant GPIO registers content), incorrectly connected (bad solder joint), or if it is a board with more circuitry on it, it conflicts with some other output.

JW