Skip to main content
Associate II
September 23, 2024
Solved

Configuring a peripheral without all pins assigned

  • September 23, 2024
  • 2 replies
  • 1642 views

Hi Guys,

is it possible to configure a peripheral in CubeMX without assigning all the pins (usually) required for that peripheral. This is a quite typical scenario, if a peripheral is used other than originally intended.

E.g. I like to use I2S for efficiently and precisely generating the DATA signal for WS2812's or similar addressable LEDs. In this scenario only the I2S_SD line is required and I don't want to "lose" the additional pins by configuring them as I2S pins. But if I give one of the I2S pins another function, it will automatically disable the peripheral.

Is there a way around this except for configuring the peripheral manually?

Cheers,

D.Frejek

Best answer by mƎALLEm

Hello @dfrejek ,

I think it's not possible to reset one of the pins after setting the peripheral as it disables it.

One of the solution is to de-init the unused GPIOs in the HAL_I2S_MspInit() / stm32xxxx_hal_msp.c:

In the last block of /* USER CODE BEGIN SPI1_MspInit 1 */   /* USER CODE END SPI1_MspInit 1 */

 /**I2S1 GPIO Configuration
 PG10 ------> I2S1_WS
 PB4 (NJTRST) ------> I2S1_SDI
 PG11 ------> I2S1_CK
 PB5 ------> I2S1_SDO
 */
 GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
 HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

 GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 /* USER CODE BEGIN SPI1_MspInit 1 */

 /* USER CODE END SPI1_MspInit 1 */

 }

}

Hope it helps.

 

 

2 replies

mƎALLEm
mƎALLEmBest answer
Technical Moderator
September 23, 2024

Hello @dfrejek ,

I think it's not possible to reset one of the pins after setting the peripheral as it disables it.

One of the solution is to de-init the unused GPIOs in the HAL_I2S_MspInit() / stm32xxxx_hal_msp.c:

In the last block of /* USER CODE BEGIN SPI1_MspInit 1 */   /* USER CODE END SPI1_MspInit 1 */

 /**I2S1 GPIO Configuration
 PG10 ------> I2S1_WS
 PB4 (NJTRST) ------> I2S1_SDI
 PG11 ------> I2S1_CK
 PB5 ------> I2S1_SDO
 */
 GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
 HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);

 GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 /* USER CODE BEGIN SPI1_MspInit 1 */

 /* USER CODE END SPI1_MspInit 1 */

 }

}

Hope it helps.

 

 

"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."
dfrejekAuthor
Associate II
September 24, 2024

Hi,

thanks for the reply. This is not exactly really a pretty solution, since it would look a bit confusing and create "dead" / redundant code, yet it will do the job.

In addition, this does not allow to configure the pin as something else in CubeMX, but as long as these pins are only used as "dumb" I/Os this should be fine.

It's a bit of a shame, there is no nicer way to do this. My use case is quite specific though, so I totally understand, why such a thing is not implemented, as it would be hardly useful to most people.

 

Cheers,

D. Frejek

Associate III
September 23, 2024

I do not know about in special I²C, but for SPI or UART there are various transmit/receive only modes, pins for other channel side are not required. And e.g. the hand shake lines for USART are also not mandatory to use as them, as far I know you can use these pins for other stuff.

dfrejekAuthor
Associate II
September 24, 2024

In my case, that’s not an available option, since CubeMX does not know of a I2S configuration with only the data line.

Normally, this configuration would be useless. But, the I2S can be used to create a bit stream with precisely controllable timings. And this can make this configuration very useful to bitbang / emulate various serial protocols.

MasterT
Lead II
September 24, 2024

>>>> But, the I2S can be used to create a bit stream with precisely controllable timings. 

Actualy I2S has much less precision over SPI, since it has more clock dividers in structure. The only reason to use I2S is to have more bits . I2S allows 32-bits all the time, even 64 if two channels used as one. SPI has  32, or even 16.

Timer configured to run with DMA may be alternative