cancel
Showing results for 
Search instead for 
Did you mean: 

Reading pins of a port in one line of code

embedonix
Associate III
Posted on October 14, 2015 at 05:17

I am working on project which is generated by Cube and uses HAL drivers. I require to read states of a number of pins on a port. Currently I am doing like this:

  //read PC0 to PC7 to check for state (on or off)

  report.BUTTONS_0_7.BIT_0 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_0);

  report.BUTTONS_0_7.BIT_1 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_1);

  report.BUTTONS_0_7.BIT_2 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_2);

  report.BUTTONS_0_7.BIT_3 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_3);

  report.BUTTONS_0_7.BIT_4 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_4);

  report.BUTTONS_0_7.BIT_5 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_5);

  report.BUTTONS_0_7.BIT_6 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_6);

  report.BUTTONS_0_7.BIT_7 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7);

I did not find any code in HAL that allows reading a number of pins of a port simultaneously. Is there a way to read a given amount of pins from a port, or half of them or all of them?

If the HAL does not have this function, what alternative exist that would be compatible with HAL?

Thanks

#gpio #stm32 #hal
3 REPLIES 3
Posted on October 14, 2015 at 09:05

Something along the lines of

uint16_t pins = GPIOC->IDR;

if (pins & GPIO_PIN_7)

  puts(''Pin 7 High'');

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

If I have a port that has more pins (e.g. STM32L552ZET6Q port E has pins 0 to 15), would something like this cover all of them, or would I have to run it for both bytes contained within the port?

uint16_t pins = GPIOE->IDR; // would read a bit vector of all 16 pins in the bank, Read the RM

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