cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-L4R5ZI-P: Modifying Code without ioc file

_godsonthomas
Associate II

Hi,

I already have a codebase that uses the STM32L4R5ZI microcontroller. The codebase does not include an .ioc file, which we normally use to configure the pins and other related parameters. But the codebase seems to be compiling fine. I am working with a development board for our customer project (Nucleo-L4R5ZI-P).

We are trying to establish SPI communication with an external chip via SPI1. The codebase currently configures the SPI1 interface, but the SPI_CLK function is assigned to PA1. I need to change this to PA5. Since I don’t have an .ioc file, how can I make this modification?

1 ACCEPTED SOLUTION

Accepted Solutions
Gyessine
ST Employee

hello @_godsonthomas 
You can check the STM32L4xx_hal_msp.c file under the Core/Src folder.

Gyessine_0-1764322681258.png

You can make changes to the SPI configuration.
Here is an example of an SPI configuration that you should find in that file:

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hspi->Instance==SPI1)
  {
    /* USER CODE BEGIN SPI1_MspInit 0 */

    /* USER CODE END SPI1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**SPI1 GPIO Configuration
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* SPI1 interrupt Init */
    HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(SPI1_IRQn);
    /* USER CODE BEGIN SPI1_MspInit 1 */

    /* USER CODE END SPI1_MspInit 1 */

  }

}

Hope that helps
Gyessine

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.

View solution in original post

5 REPLIES 5
Gyessine
ST Employee

hello @_godsonthomas 
You can check the STM32L4xx_hal_msp.c file under the Core/Src folder.

Gyessine_0-1764322681258.png

You can make changes to the SPI configuration.
Here is an example of an SPI configuration that you should find in that file:

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hspi->Instance==SPI1)
  {
    /* USER CODE BEGIN SPI1_MspInit 0 */

    /* USER CODE END SPI1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**SPI1 GPIO Configuration
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* SPI1 interrupt Init */
    HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(SPI1_IRQn);
    /* USER CODE BEGIN SPI1_MspInit 1 */

    /* USER CODE END SPI1_MspInit 1 */

  }

}

Hope that helps
Gyessine

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.

Andrew Neil
Super User

@_godsonthomas wrote:

The codebase currently configures the SPI1 interface, 


So find the place in your code where this happens, and change it from using PA1 to use PA5 instead.

Is your codebase using HAL ?

 

This is an example of why it's always a good idea to abstract stuff like this:

Instead of directly using "PA5" like a "magic number" in your code, have some symbolic definitions; eg,

#define MY_SPI_PERIPHERAL SPI1
#define MY_SPI_CLK_PORT   GPIOA
#define MY_SPI_CLK_PIN    GPIO_PIN_1

Then it's simply a matter of changing those definitions!

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

@Gyessine wrote:

You can check the STM32L4xx_hal_msp.c file 


But that file is generated by CubeMX.

@_godsonthomas says the project doesn't have a .ioc file - so it's not using CubeMX code generation ?

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

Hi @Gyessine ,

I will do this modification. Thanks for the input.

Hi @Andrew Neil,

My codebase is using HAL. The PA5 pin has been defined as a macro.