cancel
Showing results for 
Search instead for 
Did you mean: 

GPIO set certain bits without affecting others

chriskuku
Senior II

BTW, what is the forum to ask general programming questions on STM32 architecture?
STM32F103C8Tx
I have two timer interrupt routines, sharing the

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)

call. Depending on which handle is passed, I process the code for TIM1 or TIM3.
When I enter TIM3 code A0,A1,A2,and A3 of port GPIOA are set to all 1. I achieved this by the statement

GPIOA->ODR=0xffff on initialization and whenever I exit the ISR. On entry I set GPIOA->ODR=0xffff&(~1)
(the 1 is a rotating pattern that sets A0-A3).

Now I want to toggle A4 in the code for TIM1 without affecting bits A0-A3 and A4 shouldn't be affected during the manipulation of A0-A3 in the code for TIM3.

How can I achieve this if at all?

1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

This can be done with the Port bit set/reset register (GPIOx_BSRR). See the Reference manual (RM0008).

hth

KnarfB

View solution in original post

6 REPLIES 6
KnarfB
Principal III

This can be done with the Port bit set/reset register (GPIOx_BSRR). See the Reference manual (RM0008).

hth

KnarfB

chriskuku
Senior II

Are there HAL functions for this, too?

 

Piranha
Chief II

What impedes you just opening the header file and looking at those functions?

Frequently expressed as Macros, suggest you Grep the source, or Find-In-Files, save you hours

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Yes, HAL_GPIO_WritePin/HAL_GPIO_TogglePin.

/**
  * @brief  Toggles the specified GPIO pins.
  * @PAram  GPIOx: Where x can be (A..K) to select the GPIO peripheral.
  * @PAram  GPIO_Pin: Specifies the pins to be toggled.
  * @retval None
  */
void HAL_GPIO_TogglePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
  uint32_t odr;

  /* Check the parameters */
  assert_param(IS_GPIO_PIN(GPIO_Pin));

  /* get current Output Data Register value */
  odr = GPIOx->ODR;

  /* Set selected pins that were at low level, and reset ones that were high */
  GPIOx->BSRR = ((odr & GPIO_Pin) << GPIO_NUMBER) | (~odr & GPIO_Pin);
}
If you feel a post has answered your question, please click "Accept as Solution".

By the way the first odr & for the shifted reset bits is useless. ST was informed about this, but still implemented the bloated version.