Skip to content

Commit

Permalink
drivers: gpio: implement pull up and pull down for the CH32V003
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Hope <mlhx@google.com>
  • Loading branch information
nzmichaelh committed Oct 20, 2024
1 parent 358113b commit e32d9ac
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions drivers/gpio/wch_gpio_ch32v00x.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* SPDX-License-Identifier: Apache-2.0
*/

#include <errno.h>
#include <zephyr/drivers/clock_control.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/gpio/gpio_utils.h>
#include <zephyr/dt-bindings/gpio/gpio.h>
#include <zephyr/irq.h>

#include <ch32_gpio.h>
Expand All @@ -29,21 +29,32 @@ static int gpio_ch32v00x_configure(const struct device *dev, gpio_pin_t pin, gpi
{
const struct gpio_ch32v00x_config *config = dev->config;
GPIO_TypeDef *regs = config->regs;
uint32_t cfg = regs->CFGLR & ~(0x0F << (4 * pin));
uint32_t cnf_mode;
uint32_t bshr = 0;

if ((flags & GPIO_OUTPUT) != 0) {
cfg |= (0x01 << (4 * pin));
cnf_mode = 0x01;
if ((flags & GPIO_OUTPUT_INIT_HIGH) != 0) {
regs->BSHR = 1 << pin;
bshr = 1 << pin;
} else if ((flags & GPIO_OUTPUT_INIT_LOW) != 0) {
regs->BSHR = 1 << (16 + pin);
bshr = 1 << (16 + pin);
}
} else if ((flags & GPIO_INPUT) != 0) {
cfg |= (0x10 << (4 * pin));
if ((flags & GPIO_PULL_UP) != 0) {
cnf_mode = 0x08;
bshr = 1 << pin;
} else if ((flags & GPIO_PULL_DOWN) != 0) {
cnf_mode = 0x08;
bshr = 1 << (16 + pin);
} else {
cnf_mode = 0x04;
}
} else {
cfg |= (0x00 << (4 * pin));
cnf_mode = 0x00;
}
regs->CFGLR = cfg;

regs->CFGLR = (regs->CFGLR & ~(0x0F << (4 * pin))) | (cnf_mode << (4 * pin));
regs->BSHR = bshr;

return 0;
}
Expand Down

0 comments on commit e32d9ac

Please sign in to comment.