Skip to content

drivers: pinctrl: wch_20x_30x_afio: fix afio remap #87397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions drivers/pinctrl/pinctrl_wch_20x_30x_afio.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt, uintp
uint8_t pcfr_id = FIELD_GET(CH32V20X_V30X_PINCTRL_PCFR_ID_MASK, pins->config);
uint8_t remap = FIELD_GET(CH32V20X_V30X_PINCTRL_RM_MASK, pins->config);
GPIO_TypeDef *regs = wch_afio_pinctrl_regs[port];
uint32_t pcfr = pcfr_id == 0 ? AFIO->PCFR1 : AFIO->PCFR2;
uint8_t cfg = 0;

if (pins->output_high || pins->output_low) {
Expand Down Expand Up @@ -69,19 +68,21 @@ int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt, uintp
}
}

pcfr |= remap << bit0;
if (remap != 0) {
RCC->APB2PCENR |= RCC_AFIOEN;

if (pcfr_id == 0) {
AFIO->PCFR1 = pcfr;
} else {
AFIO->PCFR2 = pcfr;
}

if (bit0 == CH32V20X_V30X_PINMUX_USART1_RM) {
pcfr = AFIO->PCFR2;
pcfr |= ((uint32_t)((remap >> 1) & 1)
<< (CH32V20X_V30X_PINMUX_USART1_RM1 & 0x1F));
AFIO->PCFR2 = pcfr;
if (pcfr_id == 0 && bit0 == CH32V20X_V30X_PINMUX_USART1_RM) {
AFIO->PCFR1 |= ((uint32_t)((remap >> 0) & 1)
<< (CH32V20X_V30X_PINMUX_USART1_RM & 0x1F));
AFIO->PCFR2 |= ((uint32_t)((remap >> 1) & 1)
<< (CH32V20X_V30X_PINMUX_USART1_RM1 & 0x1F));
} else {
if (pcfr_id == 0) {
AFIO->PCFR1 |= (uint32_t)remap << bit0;
} else {
AFIO->PCFR2 |= (uint32_t)remap << bit0;
}
}
}
}

Expand Down
30 changes: 16 additions & 14 deletions drivers/pinctrl/pinctrl_wch_afio.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@ int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt, uintp
uint8_t bit0 = (pins->config >> CH32V003_PINCTRL_RM_BASE_SHIFT) & 0x1F;
uint8_t remap = (pins->config >> CH32V003_PINCTRL_RM_SHIFT) & 0x3;
GPIO_TypeDef *regs = wch_afio_pinctrl_regs[port];
uint32_t pcfr1 = AFIO->PCFR1;
uint8_t cfg = 0;

if (remap != 0) {
RCC->APB2PCENR |= RCC_AFIOEN;
}

if (pins->output_high || pins->output_low) {
cfg |= (pins->slew_rate + 1);
if (pins->drive_open_drain) {
Expand Down Expand Up @@ -63,16 +58,23 @@ int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt, uintp
}
}

if (bit0 == CH32V003_PINMUX_I2C1_RM) {
pcfr1 |= ((remap & 1) << CH32V003_PINMUX_I2C1_RM) |
(((remap >> 1) & 1) << CH32V003_PINMUX_I2C1_RM1);
} else if (bit0 == CH32V003_PINMUX_USART1_RM) {
pcfr1 |= ((remap & 1) << CH32V003_PINMUX_USART1_RM) |
(((remap >> 1) & 1) << CH32V003_PINMUX_USART1_RM1);
} else {
pcfr1 |= remap << bit0;
if (remap != 0) {
RCC->APB2PCENR |= RCC_AFIOEN;

if (bit0 == CH32V003_PINMUX_I2C1_RM) {
AFIO->PCFR1 |= ((uint32_t)((remap >> 0) & 1)
<< CH32V003_PINMUX_I2C1_RM) |
((uint32_t)((remap >> 1) & 1)
<< CH32V003_PINMUX_I2C1_RM1);
} else if (bit0 == CH32V003_PINMUX_USART1_RM) {
AFIO->PCFR1 |= ((uint32_t)((remap >> 0) & 1)
<< CH32V003_PINMUX_USART1_RM) |
((uint32_t)((remap >> 1) & 1)
<< CH32V003_PINMUX_USART1_RM1);
} else {
AFIO->PCFR1 |= (uint32_t)remap << bit0;
}
}
AFIO->PCFR1 = pcfr1;
}

return 0;
Expand Down
Loading