CH32V003: TIM2CH4_ pin mapping trouble - Page 1
Forum section: electronics / general

author: TMZulu
author_rank: Newbie
timestamp: February 07, 2025, 05:02:40 am
content: Hello All,I am struggling to try to get TIM2CH4 to output to pin PC1 for a CH32V003J4M6. According to the datasheet, PC has the alternate function of TIM2CH4_.I assume I am missing some sort of mapping config function call needed to have PC1 output TIM2CH4 there. I am confused on the meaning of the '_' designation of TIM2CH4_ here and what is needed for it.Unfortunately, I have thus far been unable to find any resources on this.Any help and information on what is needed to do so would be appreciated.Here is my code for testing this:Code:#include "ch32v00x.h"#include <system_ch32v00x.h>#include <debug.h>#include <stdint.h>void initPwm(void){GPIO_InitTypeDef GPIO_InitStructure = {0};TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = {0};TIM_OCInitTypeDef TIM_OCInitStructure = {0};RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOC, &GPIO_InitStructure);TIM_TimeBaseStructure.TIM_Prescaler = 120 - 1;TIM_TimeBaseStructure.TIM_Period = 100;TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse = 10;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;TIM_OC4Init(TIM2, &TIM_OCInitStructure); // Channel 4 for PWMTIM_CtrlPWMOutputs(TIM2, ENABLE);TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); // Channel 4 for PWMTIM_ARRPreloadConfig(TIM2, ENABLE);TIM_Cmd(TIM2, ENABLE);}int main(void){SystemCoreClockUpdate(); // Initialize system clocks and peripherals at 48MHzDelay_Init();initPwm();while (1){}}#define SYSCLK_FREQ_48MHZ_HSI 48000000 is set.

author: brucehoult
author_rank: Super Contributor
timestamp: February 07, 2025, 05:57:15 am
content: Have you checked the post from 4 days ago with a similar problem to see if the same thing is happening?https://www.eevblog.com/forum/microcontrollers/ch32v003-i2c-remap-not-working

author: HwAoRrDk
author_rank: Super Contributor
timestamp: February 07, 2025, 09:37:07 am
content: Quote from: TMZulu on February 07, 2025, 05:02:40 amDid you make a typo? There's no such part as the J4G6. I assume you mean the 8-pin CH32V003J4M6.Quote from: TMZulu on February 07, 2025, 05:02:40 amTake a look at section 7.2.11 of the Reference Manual. Table 7-9 shows what AFIO register settings need to be made to map TIM2CH4 to different pins.The pin labelling convention is a little confusing at first glance, but makes sense. On the Datasheet pinout the trailing underscore just means it is one of the alternate mappings. You need to refer to Datasheet table 2-2 for all the possible alternate function re-mappings. There, "T2CH4" (no underscore) is the default mapping on PD7, "T2CH4_1" is the first alternate mapping on PC1, "T2CH4_2" is the second alternate mapping on PD7 (yes, that's the same as the first... I'm sure it makes sense to someone), and "T2CH4_3" is the third alternate mapping on PD5.Of course, the 8-pin package has no PD7, only PD5 and PC1, so you definitely need to remap TIM2CH4. Beware that PD5 is physically commoned with PD1 and PD4 on the 8-pin chip, so if using PD5 you need to ensure the other two are configured as floating inputs.You want to set theAFIO->PCFR1register'sTIM2_RMfield to 0b01 to remap TIM2CH4 to pin PC1. I'm not exactly sure what you need to do with the HAL API to do that. Looks like you need to callGPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE). Don't forget to also enable the peripheral clock for AFIO.

author: TMZulu
author_rank: Newbie
timestamp: February 07, 2025, 04:33:16 pm
content: Thank you HwAoRrDk,Your explanation here was the information I needed to get this working. And yeah, CH32V003J4M6 is what I meant, fixed the original post.I had actually triedQuotebefore and forgot to mention it in my initial post.I had not enabled peripheral clock for AFIO though. That was the missing piece for me.QuoteI notice that AFIO doesn't need to have peripheral clock enabled if it is the default mapping. I tested it with T2CH3 on pin PC0 with a F4P6 chip (default mapping) and AFIO periph clock wasn't needed for it. Is the AFIO periph clock needed specifically in any instances where pin remapping (GPIO_PinRemapConfig) is used?For reference for anyone else down the line, my working solution as a result of this information is as follows:Code:#include "ch32v00x.h"#include <system_ch32v00x.h>#include <debug.h>void initPwm(void){GPIO_InitTypeDef GPIO_InitStructure = {0};TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure = {0};TIM_OCInitTypeDef TIM_OCInitStructure = {0};RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOC, &GPIO_InitStructure);TIM_TimeBaseStructure.TIM_Prescaler = 120 - 1;TIM_TimeBaseStructure.TIM_Period = 100;TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;TIM_OCInitStructure.TIM_Pulse = 50;TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;TIM_OC4Init(TIM2, &TIM_OCInitStructure); // Channel 4 for PWMTIM_CtrlPWMOutputs(TIM2, ENABLE);TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); // Channel 4 for PWMTIM_ARRPreloadConfig(TIM2, ENABLE);TIM_Cmd(TIM2, ENABLE);GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);}int main(void){SystemCoreClockUpdate(); // Initialize system clocks and peripherals at 48MHzDelay_Init();initPwm();while (1){}}

author: TMZulu
author_rank: Newbie
timestamp: February 07, 2025, 04:38:37 pm
content: Thank you for the link brucehoult, I had not seen that post. The information there was useful as well, though I am not writing in assembly code for my approach.

author: brucehoult
author_rank: Super Contributor
timestamp: February 07, 2025, 11:11:52 pm
content: Well, programming language is not very relevant to these questions...

author: HwAoRrDk
author_rank: Super Contributor
timestamp: February 08, 2025, 04:21:49 am
content: Quote from: TMZulu on February 07, 2025, 04:33:16 pmYes. Basically, if your code changes anything in the AFIO registers, it needs to enable the peripheral clock for AFIO, otherwise those settings won't take effect. So, that includes alternate pin mapping, or external pin-change interrupts.By the way, one might find that somehow doing remapping works without enabling AFIO sometimes, but that is because the when the HAL startup code is configured to use external crystal (HSE), it enables AFIO itself already, because it needs to disable GPIO functionality of PA1 & PA2. You can see this insystem_ch32v00x.c.