CH32V003: Unexpected pin behavior (alternate function pin remapping) - Page 1
Forum section: electronics / general

author: rhodges
author_rank: Frequent Contributor
timestamp: November 01, 2023, 10:29:25 pm
content: I had set up PWM on my CH32V003 -A3 (SOP16) using channel 2 and pin C0. This was driving a small piezo buzzer. I was hoping to get it louder.I chose timer 2, because I prefer to save timer1 for more advanced uses (if I get into motor control, for instance.) Timer 2 has four channels, but they do not have complementary outputs. But why not use two channels and make one the other polarity?So I chose pin remapping #1, so channels 2 and 4 would come out on pins C2 and C1. No output. They were floating. I set up both C1 and C2 as alternate, push-pull, and 2mhz. According to the GPIO pin diagram, the input from the GPIO output control comes from the GPIO data register, or the alternate function. Even if my alternate output from timer2 is not set up, the output control should still drive the pin. Right? But they are floating.So I spend some quality time with my scope before giving up for the night. Then, WHILE DREAMING last night, I remembered, "maybe the alternate function mapping has its own clock." After waking up and some coffee, I see that there is indeed a clock for that. After adding "RCC->APB2PCENR |= RCC_AFIOEN;", my PWM works. Yay.But why were pins C1 and C2 floating until I enabled the alternate function clock? In the GPIO diagram, the output control should have driven the pins, even if the alternate function was not working? I did set those pins to push-pull outputs.

author: wek
author_rank: Frequent Contributor
timestamp: November 02, 2023, 09:53:42 am
content: The STM32 Timer (copied ruthlessly by the various chinesiums) Channels are more than just PWM (e.g. Capture, or Trigger, among other things), so they need to have control over the connected pins' output enable. That is controlled by - again among other things - the given channels' enable bit. This bit (and, maybe more importantly, also the MOE bit, which too influences if given pin output is enabled) may be changed by hardware for certain functionality modes, so this can't be left to the user to be changed in software.This is less obvious in conjunction with the immature STM32F1xx GPIO - which again is the one most copied by the chinesiums - as they don't have the AF matrix found in all other STM32 families, peripherals' input pads are connected hard to the pins input buffers, so it appears that only pins connected to peripheral outputs pads would need to be switched to AF (it may quite well be that some peripherals don't have the Output Enable control connected from their IO to GPIO, as the Timers do). This is then coupled with inadequate ST documentation which tries to hide the "ugly details", and "libraries" like SPL and Cube just double down on this effort. The copycats then just copy this inadequate documentation, too, or not even that. OK I misread the question: I though the question is "why are PC1/PC2 floating until timer channels get enabled, even if it's set to AF". I see the question is "why are PC1/PC2 floating until they don't get remapped in AFIO". The reason is similar/the same: without remapping, they are mapped to I2C, and if that is not enabled and set up to drive the pins (which in I2C is quite a complex issue as it turns around SDA's direction, and SCL's depends on whether it's set to master or slave), they remain to be not driven.JW

author: rhodges
author_rank: Frequent Contributor
timestamp: November 02, 2023, 12:32:24 pm
content: Thank you, those are good points.

author: DavidAlfa
author_rank: Super Contributor
timestamp: November 02, 2023, 01:38:55 pm
content: In stm32 you must set the remapping ,but also set the pin mode to AF, probably this is the same thing and you're missing out.Hard to know with no code (Which you should be posting), "So I chose pin remapping #1" means nothing if you made a mistake in the code.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: November 02, 2023, 04:46:20 pm
content: I'm just as confused as you are.If I'm interpreting the reference manual correctly - particularly figures 7-2, 7-3, 7-4 - it seems the only way you should get a floating pin is when it is still configured as an input. That is, as per figure 7-2 where the push-pull output drivers are completely disconnected (and there is no pull-up/down enabled). Are you sure you actually set the MODE1 & MODE2 bits of GPIOC->CFGLR to an output mode (i.e. not 0b00)? It's perfectly possible to set the CNF bits to an alternate-function/multiplexed output configuration, but still have the MODE as input.The only other thing I can imagine is if perhaps when you've set a pin to alternate-function output mode, but the AFIO peripheral clock isn't enabled, that because the regular GPIO output registers are disconnected and the alternate function isn't controlling state of the pin's push-pull output drivers (i.e. they're both off) either, the pin is then floating because of that.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: November 02, 2023, 04:57:27 pm
content: Quote from: wek on November 02, 2023, 09:53:42 amThat's not strictly true. The I2C pins should always be configured as open-drain outputs, even when used only in I2C slave mode. This is because when a pin is in alternate-function output mode, it's schmitt-trigger input remains always enabled. Thus the I2C peripheral has no need to "turn around" the direction of the pin; nor, indeed, is it capable of doing so - that can only be done by changing the GPIOx->CFGLR register.

author: rhodges
author_rank: Frequent Contributor
timestamp: November 02, 2023, 10:54:52 pm
content: Quote from: DavidAlfa on November 02, 2023, 01:38:55 pmNo, the pins were set up correctly, and I did write:QuoteAnd both pins worked correctly after I added:Code:RCC->APB2PCENR |= RCC_AFIOENJust to be perfectly clear, enabling the clock for alternate mapping solved my problem. I started this thread to say that I expected the pins to be driven after setting to outputs, and not floating. And to note that I was surprised that alternate mapping had a clock.