linux gpio: how to handle bi-directional pins?

81 Views Asked by At

I am investigating the use of linux/gpio.h (operating GPIOs using ioctl calls). Can not find information or examples/recommendations how to handle pins which are inputs at some time, and outputs at other times - bi-directional pins.

Do I understand correctly that if there's a need to change pin direction, I must close previous line file descriptor, and request another file descriptor for another direction? If pin needs direction back, I must request another file handle again? If that will happen often, is there any risk of something adverse happening - e.g. file descriptor pool depletion - when the application start getting the request error, or if pin had been requested successfully at least once (for respective direction), and its file descriptor closed in time, it is guaranteed that this pin will keep opening/requesting without problems (or course unless hardware problem occurs)? In other words, what are the risks?

Why to bother - simple use case: if GPIO will fail to initialize in input mode after it was functioning in output mode - when another device on the bus switches to output mode, therefore two devices will appear outputting active level onto the same (set of) wires possibly causing physical damage to the system.

1

There are 1 best solutions below

0
Kent Gibson On

You can reconfigure the line(s) in a request from one configuration state to another, including switching direction, without releasing the line using gpiod_line_request_reconfigure_lines(), as per this example.