/** * \file * * * \brief STM32 GPIO control interface. * * \author Andrea Righi */ #include "gpio_stm32.h" #include #include #include /** * Configure a GPIO pin * * \param base Base address of the GPIO port * \param pins Bit-packed representation of the pin(s) * \param mode Pin(s) configuration mode * \param speed Output drive speed * * Return 0 on success, otherwise a negative value. */ int stm32_gpioPinConfig(struct stm32_gpio *base, uint16_t pins, uint8_t mode, uint8_t speed) { uint32_t reg_mode = mode & 0x0f; int i; if (mode & 0x10) reg_mode |= speed; if (pins & 0xff) { uint32_t reg = base->CRL; for (i = 0; i < 8; i++) { uint32_t pos = 1 << i; if (pins & pos) { pos = i << 2; reg &= ~(0x0f << pos); reg |= reg_mode << pos; if (mode == GPIO_MODE_IPD) base->BRR = 0x01 << i; if (mode == GPIO_MODE_IPU) base->BSRR = 0x01 << i; } } base->CRL = reg; } if (pins > 0xff) { uint32_t reg = base->CRH; for (i = 0; i < 8; i++) { uint32_t pos = 1 << (i + 8); if (pins & pos) { pos = i << 2; reg &= ~(0x0f << pos); reg |= reg_mode << pos; if (mode == GPIO_MODE_IPD) base->BRR = 0x01 << (i + 8); if (mode == GPIO_MODE_IPU) base->BSRR = 0x01 << (i + 8); } } base->CRH = reg; } return 0; }