Using the Oscillator pins (OSC) and SWD pins as GPIO on STM32

Help us grow. Share with your friends!

his post is going to be unusually short. Sometime ago I created a small electronics module where I had to use almost all of the GPIO pins on the STM32F103, except the oscillator pins. I was short of exactly two pins and I needed them badly. The design had come a long way and the option to change microcontroller for a larger one wasn’t an option. Fortunately, STM32 has the option to unlock certain GPIO pins. That is exactly what we will be looking at.

STM32 AFIO MAPR

OSC Pins

The AFIO MAPR register gives the information about the alternate functions on locked pins. For example, in my case, I needed the PD0 and the PD01 which happen to be connected to the OSC_IN and OSC_OUT functions. The PD0 being configurable the usual way through GPIO registers. On the other hand, PD01 requires a remap through AFIO_MAPR register before you go and configure the GPIO control registers.

When using STM32 HAL, the remapping is extremely simple.

SWD Pins

There are three pins required for serial wire debug port, also known as the SWD port. You can configure all three of them as either JTAG pins or GPIO. In order to do this, the AFIO_MAPR register gives you all the options you need.

stm32 afio mapr

STM32 SWD AFIO configuration

Using HAL, all you need to do is call this function.

This little line should disable both, jtag as well as SWD. Now, with this done, you opened up 3 GPIO pins for your use.

For further reading, you should refer the STM32 reference manual page 184. Depending on the STM32 microcontroller you are using, there are loads of unlockable or rather, remappable pins available. Information for all those can be found in the reference manual. I hope you found this information useful. Don’t forget to leave me a comment.

nuclearrambo

Salil is an electronics enthusiast working on various RF and Microwave systems. In his free time he writes on the blog, talks over ham radio or builds circuits. He has Yaesu FT2900R VHF transceiver, FT450D HF transceiver and a TYT UV8000E Handheld transceiver.

You may also like...

1 Response

  1. Nathanael Kusanda says:

    Hi! I’m trying to do this for my microprocessor and I’m a bit stuck. Where is AFIO_REMAP_ENABLE() defined, and can I call it in the main.c file? I’m also unsure of where the bit sequence AFIO_MAPR_PD01_REMAP is defined, or what that bit sequence looks like. Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.