A Raspberry Pi 3B+ is talking to an STM32 chip via UART over pins 8 (TXD) & 10 (RXD). When I take control of those pins using GPIO.setup(8, GPIO.OUTPUT); GPIO.output(8, GPIO.LOW); and later release them by calling GPIO.cleanup(), UART no longer works. To restore UART, I have to reboot the Pi. /boot/config.txt contains enable_uart=1.

How do I restore UART pins to the way the kernel sets them without rebooting the Pi?

The reason why I need to pull down the UART pins, is because the Pi and a the STM32 have a common ground, but the STM32's board has a safety diode that seems to be turned on by the Pi's UART pins when it's power source is off. Pulling down the pins to zero when the STM32 is off seems to prevent this current from flowing. This is obviously not ideal, but due to time constraints, I can't add an opto-coupler yet.


I doubt the logic of the deductions you have drawn, but GPIO.cleanup() should restore the pins to their previous state. (I suspect you are putting the Microcontroller into its programming mode.)

NOTE ANY interconnection to an external device must have a common ground

You should add the output of gpio readall before and after running your code (which you should include).

PS GPIO.setup(8, GPIO.OUTPUT); GPIO.output(8, GPIO.LOW); will NOT have any effect on serial pins.

| improve this answer | |
  • GPIO.cleanup() does not restore previous modes, but instead places pins in INPUT mode. I've added a detailed answer below. Boot mode on the STM32 controller is controlled via its BOOT and RESET pins, which I am also doing, but those are connected to different GPIO pins on the RPi. – Petrus Theron Nov 12 '18 at 10:42

UART requires pins 8 and 10 to be in the ALT0 mode.

As per line 134 in py_gpio.c of the raspberry-pi-gpio-python library I'm using, touched pins are placed in INPUT mode when calling GPIO.cleanup() instead of restoring them to their previous modes. This is a blatant bug in my view, which others have also encountered before.

To restore UART, set pins 8 and 10 to mode ALT0 instead of using the GPIO library and do your own cleanup, or using wiringpi's gpio command:

gpio -1 mode 8 alt0
gpio -1 mode 10 alt0

Under the hood, all these tools read or write to the internal GPIO files (or sockets) located at /sys/class/gpio/gpioN/*.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.