cancel
Showing results for 
Search instead for 
Did you mean: 

I generated SPI1 on CubeMX. Does CubeMX SPI configuration include GPIO functionalities for the SPI pins, or do we need to programme GPIO manually?

NAnand1
Associate III

I enabled SPI1 on CubeMX using STM32L433 on pins 11(SPI-CLK), 16(MISO), 17(MOSI). I am interfacing it to a Kionix MEMS sensor on SPI. I detect no signals on the pins. Do I need to separately write configuration for GPIO for the SPI pins?

I could not find any code generated by MX which allocates GPIO functions, incl AF to these pins.

I am wondering if it so, why is it not a part of MX code generated?

7 REPLIES 7
Houssem CHAABANI
Senior II

Dear @NAnand1​ ,

Could you please share your .ioc file ?

Thank you,

Houssem

I hope this is the correct file you wanted!

Houssem CHAABANI
Senior II

Dear @NAnand1​ ,

Thank you for your file.

The code generated by STM32CubeMX is fine because STM32CubeMX is only for configuration and you can find that SPI1 is configured in the main.c.

Otherwise in order to read/write data from/to your sensor you need to use the functions found in stm32l4xx_hal_spi.c file.

Kind regards,

Houssem

TDK
Guru

> I could not find any code generated by MX which allocates GPIO functions, incl AF to these pins.

It's going to be in stm32f4xx_hal_msp.c in the HAL_SPI_MspInit function. Probably not looking in the right spot.

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

Dear Houssem,

I understand your response. My question was if I need to specifically configure GPIO pins 11(SPI-CLK), 16(MISO), 17(MOSI) IN ADDTION TO code generated by CubeMX to see the clock activity on the SPI_CLK pin?

I did use the functions in the relevant HAL file. However, I do not see any activity with the scope on the CLK pin, which led me to this question.

Unfortunately there is no file stm32f4xx_hal_msp.c found anywhere in the project.

Houssem CHAABANI
Senior II

Dear @NAnand1​ ,

Every pin support some functionality and doesn't support others.

So you can't configure a pin that doesn't support to be SPI-CLK / MOSI / MISO to do SPI functions with STM32CubeMX.

In your case you have 3 SPI so you can choose between them or you can develop it manually to create an other SPI configuration and it's called bit bang.

Here is a document describe Virtually increasing the number of serial communication peripherals in STM32 applications .

Hope that this help you.

Kind regards,

Houssem

NAnand1
Associate III

Dear Houssem.

I am not able to communicate my issue properly to you. Let me try to explain again.

  1. I enabled SPI1 on Cube MX, which automatically allotted pins 11(SPI-CLK), 16(MISO), 17(MOSI) to the SPI1 port. My PCB design used these pins to connect to the Kionix device through its SPI port. I do not need more SPI ports - only one is required to communicate with the KX device.
  2. Code was generated by MX into my project using this configuration, as follows:

static void MX_SPI1_Init(void)

{

 /* USER CODE BEGIN SPI1_Init 0 */

 /* USER CODE END SPI1_Init 0 */

 /* USER CODE BEGIN SPI1_Init 1 */

 /* USER CODE END SPI1_Init 1 */

 /* SPI1 parameter configuration*/

 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_8;

 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_ENABLE;

 if (HAL_SPI_Init(&hspi1) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN SPI1_Init 2 */

 /* USER CODE END SPI1_Init 2 */

}

  1. Now my query is whether I need to also set these pins allotted by Cube MX SPI port as GPIO ports and assign them functionalities like SPI_CLK as OUTPUT, MISO as INPUT and MOSI as OUTPUT?
  2. Nowhere in my project I found any reference to pins 11,16 and 17 as generated by Cube MX. Maybe it is automatically implied by the pin allotment to SPI1.
  3. I have added HAL functions to write and read to SPI, as follows:

ENABLE_KX;

KX_SendBuffer[0]= 0x00;

HAL_SPI_Transmit(&hspi1, KX_SendBuffer, 1,10);

HAL_SPI_Receive(&hspi1, (uint8_t *) &KX_receiveBuffer,4,10);

DISABLE_KX;

However, I do not see any activity on any pin like SPI_CLK, MOSI or MISO at all. (The processor is working well otherwise).. Which is why I asked if I am missing something and do I need to configure ports in addition to the code provided by MX

I hope this is clear.

PS:

My doubts came from reading an instructional lesson on the web which says clearly: (reproducing relevant part here only:) The SPI has been configured and intialized already like in my program..

"

If we begin using the SPI peripheral right after calling HAL_SPI_Init(), the peripheral will appear working from the program’s point of view, but the SPI signals will not be actually be visible on the chip pins. To fix this, we need to explicitly switch pins PA5-PA7 to the SPI mode. We will leave the PA4 pin to be a generic GPIO pin to demonstrate how to control the NSS signal manually:

14

    __GPIOA_CLK_ENABLE();

    GPIO_InitTypeDef  GPIO_InitStruct;

 

    GPIO_InitStruct.Pin       = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;

    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull      = GPIO_PULLUP;

    GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;

    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    

    GPIO_InitStruct.Pin  = GPIO_PIN_4;

    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);