/** @brief Set GPIO Pin Mode Sets the Pin Direction and Analog/Digital Mode, and Output Pin Pullup, for a set of GPIO pins on a given GPIO port. @param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id @param[in] mode Unsigned int8. Pin mode @ref gpio_mode @param[in] pull_up_down Unsigned int8. Pin pullup/pulldown configuration @ref gpio_pup @param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id If multiple pins are to be set, use bitwise OR '|' to separate them. */ void gpio_mode_setup(uint32_t gpioport, uint8_t mode, uint8_t pull_up_down, uint16_t gpios) { uint16_t i; uint32_t moder, pupd; /* * We want to set the config only for the pins mentioned in gpios, * but keeping the others, so read out the actual config first. */ moder = GPIO_MODER(gpioport); pupd = GPIO_PUPDR(gpioport); for (i = 0; i < 16; i++) { if (!((1 << i) & gpios)) { continue; } moder &= ~GPIO_MODE_MASK(i); moder |= GPIO_MODE(i, mode); pupd &= ~GPIO_PUPD_MASK(i); pupd |= GPIO_PUPD(i, pull_up_down); } /* Set mode and pull up/down control registers. */ GPIO_MODER(gpioport) = moder; GPIO_PUPDR(gpioport) = pupd; }
inline static void gpio_pupdr(uint8_t port, uint8_t pin, uint8_t mode) { uint32_t reg = *GPIO_PUPDR(port); reg &= ~(GPIO_PUPDR_M(pin)); reg |= (mode << GPIO_PUPDR_PIN(pin)); *GPIO_PUPDR(port) = reg; }