cancel
Showing results for 
Search instead for 
Did you mean: 

Hi Everyone, For the past 9 months approximately, we have been working on migrating our custom mcu (stm32L496vg) from a PIC mcu. Currently, we have 2 of our custom boards connected via SPI with one as the master and the other being the slave.

Hiram Hyman
Associate III

The connections are quite simple Master-CLK , Master->MOSI to Slave->MISO and we only have one Slave so, we aren't using CS. We're only trying to receive simple data but, we're only getting various trash.

We have experienced crashing and the clock signal being distorted before but, this went away as soon as we disabled the nss pulse. However, we're still unable to receive useful data.

In debug and monitoring the the rxreceive buffer, it's as if there is no connections at all. Just trash.

The master clock and mosi signals are doing what we expected though.

This is usually a simple fix but, if anyone has insight where we should start looking to fix the issue it would be greatly appreciated!

Our test code is below:

#include "main.h"

/* Private variables ---------------------------------------------------------*/

SPI_HandleTypeDef hspi1;

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_SPI1_Init(void);

/* USER CODE BEGIN PFP */

uint8_t SpiData_TX [7] = "BONE";

uint8_t SpiData_RX [7];

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_SPI1_Init();

 /* USER CODE BEGIN 2 */

 /* USER CODE END 2 */

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

    HAL_SPI_Receive(&hspi1, SpiData_RX, 8,1000);

    HAL_Delay(10);

 }

 /* USER CODE END 3 */

}

/**

 * @brief System Clock Configuration

 * @retval None

 */

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Initializes the CPU, AHB and APB busses clocks

 */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;

 RCC_OscInitStruct.MSIState = RCC_MSI_ON;

 RCC_OscInitStruct.MSICalibrationValue = 0;

 RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;

 RCC_OscInitStruct.PLL.PLLM = 1;

 RCC_OscInitStruct.PLL.PLLN = 40;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

 RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;

 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

   Error_Handler();

 }

 /** Initializes the CPU, AHB and APB busses clocks

 */

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

                             |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

 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_4) != HAL_OK)

 {

   Error_Handler();

 }

 /** Configure the main internal regulator output voltage

 */

 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)

 {

   Error_Handler();

 }

}

/**

 * @brief SPI1 Initialization Function

 * @param None

 * @retval None

 */

static void MX_SPI1_Init(void)

{

 /* SPI1 parameter configuration*/

 hspi1.Instance = SPI1;

 hspi1.Init.Mode = SPI_MODE_SLAVE;

 hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;   //SPI_DIRECTION_2LINES_RXONLY   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_HARD_INPUT;                  // SPI_NSS_SOFT SPI_NSS_HARD_INPUT

 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

 hspi1.Init.CRCPolynomial = 7;

 hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

 hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

 if (HAL_SPI_Init(&hspi1) != HAL_OK)

 {

   Error_Handler();

 }

 /* USER CODE BEGIN SPI1_Init 2 */

 /* USER CODE END SPI1_Init 2 */

}

/**

 * @brief GPIO Initialization Function

 * @param None

 * @retval None

 */

static void MX_GPIO_Init(void)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */

 __HAL_RCC_GPIOA_CLK_ENABLE();

 __HAL_RCC_GPIOE_CLK_ENABLE();

 /*Configure GPIO pin Output Level */

 /*Configure GPIO pins : PDN_PE10_Pin CSN_PE11_Pin */

 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

 GPIO_InitStruct.Pull = GPIO_PULLUP;

 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

 HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**

 * @brief This function is executed in case of error occurrence.

 * @retval None

 */

void Error_Handler(void)

{

 /* USER CODE BEGIN Error_Handler_Debug */

 /* User can add his own implementation to report the HAL error return state */

 /* USER CODE END Error_Handler_Debug */

}

#ifdef USE_FULL_ASSERT

/**

 * @brief Reports the name of the source file and the source line number

 *        where the assert_param error has occurred.

 * @param file: pointer to the source file name

 * @param line: assert_param error line source number

 * @retval None

 */

void assert_failed(char *file, uint32_t line)

{

 /* USER CODE BEGIN 6 */

 /* User can add his own implementation to report the file name and line number,

    tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

 /* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT */

1 ACCEPTED SOLUTION

Accepted Solutions
Hiram Hyman
Associate III

Solved: To all. We found the necessary manuals to hammer out the hiccup!

We will configure spi ourselves instead if using CubeMX.

Thanks for attempting to help.

View solution in original post

1 REPLY 1
Hiram Hyman
Associate III

Solved: To all. We found the necessary manuals to hammer out the hiccup!

We will configure spi ourselves instead if using CubeMX.

Thanks for attempting to help.