1

I am having some trouble trying to SET or RESET one of my GPIO pins on the STM32F030.

I'm using the STM32F0xx_HAL_Driver and I initialize GPIO PA12 like this:

    GPIO_InitStruct.Pin = GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

I also set my UART using the folowing code:

  huart1.Instance = USART1;
  huart1.Init.BaudRate = 9600; 
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_ENABLE;
  huart1.gState = HAL_UART_STATE_RESET;

  huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

  HAL_UART_Init(&huart1);

I know PA12 can be used as UART1_RTS pin but I'm not setting the hardware-flow-control to use RTS or CTS.

The problem I'm facing: After code initialization I can receive messages over the UART1 connection. To reply I need to set a pin of an external IC which I'm trying to set using PA12. But when I call:

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);

nothing happens..

Also calling the GPIO_PIN_RESET won't do anything..

What am I missing here??

I've checked (and measured) the PCB, PA12 is only connected to 0V with a 10k pull-down resistor, the external IC isn't pulling the PA12 output low.

Many thanks in advance!

edit:

As requested in the comments, my UART pin configuration:

  GPIO_InitStruct.Pin = GPIO_PIN_9;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF1_USART1;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

I'm using DMA to set the Rx:

    /* DMA controller clock enable */
    __HAL_RCC_DMA1_CLK_ENABLE();

    /* DMA interrupt init */
    /* DMA1_Channel2_3_IRQn interrupt configuration */
    hdma_usart1_rx.Instance = DMA1_Channel3;
    hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_usart1_rx.Init.Mode = DMA_NORMAL;
    hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_usart1_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;

    HAL_DMA_Init(&hdma_usart1_rx);
    HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);

2nd EDIT!:

I just found that re-initializing the GPIO PA12 after the UART initialization works! I can now toggle PA12 without any problems..

But this is not the way it should be! I'm looking into the STM32 HAL-lib but can't find the piece of code that sets PA12...

  • Your configuration seems to set pullup non pulldown – LPs Dec 17 '19 at 08:58
  • This is correct, I have an external pull-down (when the STM32F0 is starting up, I want PA12 to be pulled down). I added the internal pull-up because I thought the PIN was having a hard time delivering enought current. (I have experienced this with a low-current pin before). But adding the internall-pull-up doesn't do the trick :( – Ruben van Leeuwen Dec 17 '19 at 09:01
  • The Pull-Up/Dow is not necessary since the output function is "much stronger" than the weak internal pull resistors. – theSealion Dec 17 '19 at 09:10
  • 1
    Could you post your USART pin initialization ( and maybe search for any occurrence of ´GPIO_PIN_12` so see if the config is changed somewhere else). – theSealion Dec 17 '19 at 09:13
  • Could not find any other call to GPIO_PIN_12 except for my own pin-initialization – Ruben van Leeuwen Dec 17 '19 at 09:42
  • 1
    Have you tried a clean project to just toggle the pin like: `RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN; GPIOA->MODER &= ~(GPIO_MODER_MODE3_12); GPIOA->MODER |= GPIO_MODER_MODE3_12; GPIOA->ODR |= GPIO_ODR_OD12` – Damiano Dec 17 '19 at 12:35
  • @Damiano I tried your method and it worked, the pin toggles. I also tried the STM32 HAL GPIO driver and this works too.. I guess it has to do with the DMA functionality.. I noticed that I'm not setting the UART1_Rx pin but it still works by simply calling the DMA functionality.. – Ruben van Leeuwen Dec 17 '19 at 13:07
  • I just found that I can keep toggling the pun UNTILL I call: HAL_UART_Init(&huart1); – Ruben van Leeuwen Dec 17 '19 at 13:12
  • 1
    I could use some externation from Linus to describe you my consideration of the ST HAL library. – Damiano Dec 17 '19 at 13:52
  • I feel you, there are so manny little errors in this library.. – Ruben van Leeuwen Dec 17 '19 at 14:00

2 Answers2

0

Did you enable the GPIOA Clock?

__HAL_RCC_GPIOA_CLK_ENABLE();

Edit:

Please have a look at your void HAL_UART_MspInit(UART_HandleTypeDef *huart) function to see which and how the pins for the usart are configured.

theSealion
  • 1,082
  • 7
  • 13
0

I haven't found the clean solution I really wanted..

But initializing this single pin after the UART initialization seems to do the trick..

PA12 is being set in the UART initialization but I was not able to find the piece of code doing so anywhere

  • You should have an HAL_UART_MspInit implementation thats hardware dependend (outside HAL driver).... there should be the code that initializes the pins. (probably generated by your ide) – Damiano Dec 17 '19 at 14:29
  • HAL_UART_MspInit is not overridden and just has "UNUSED(huart);" – Ruben van Leeuwen Dec 18 '19 at 07:32
  • Are you sure? It cannot work this way. Maybe that one you found is just a weak function and there some other implementation around, maybe masked by some macros. Just try to debug it till it get called. – Damiano Dec 18 '19 at 07:51
  • Well, if you've initilized it yourself, it isn't needed of course – Damiano Dec 18 '19 at 08:01
  • Debugged it, only thing it is calling is the UNUSED() function. I guess the MCU itself sets the pin at UART initialisation?? – Ruben van Leeuwen Dec 18 '19 at 08:13
  • It seems really strange. Could you just dump the gpio configuration after HAL_UART_Init()? You could also put a watchpoint on the registry that been written by it. – Damiano Dec 18 '19 at 08:19