/* * GPIO setup: * Enable the pins driving the RGB LED as outputs. */ static void gpio_setup(void) { /* * Configure GPIOF * This port is used to control the RGB LED */ periph_clock_enable(RCC_GPIOF); const u32 outpins = (LED_R | LED_G | LED_B); GPIO_DIR(RGB_PORT) |= outpins; /* Configure outputs. */ GPIO_DEN(RGB_PORT) |= outpins; /* Enable digital function on outputs. */ /* * Now take care of our buttons */ const u32 btnpins = USR_SW1 | USR_SW2; /* * PF0 is locked by default. We need to unlock the GPIO_CR register, * then enable PF0 commit. After we do this, we can setup PF0. If we * don't do this, any configuration done to PF0 is lost, and we will not * have a PF0 interrupt. */ GPIO_LOCK(GPIOF) = 0x4C4F434B; GPIO_CR(GPIOF) |= USR_SW2; /* Configure pins as inputs. */ GPIO_DIR(GPIOF) &= ~btnpins; /* Enable digital function on the pins. */ GPIO_DEN(GPIOF) |= btnpins; /* Pull-up the pins. We don't have an external pull-up */ GPIO_PUR(GPIOF) |= btnpins; }
/** * \brief Configure a group of pins * * Sets the Pin direction, analog/digital mode, and pull-up configuration of * or a set of GPIO pins on a given GPIO port. * * @param[in] gpioport GPIO block register address base @ref gpio_reg_base * @param[in] mode Pin mode (@ref gpio_mode) \n * - GPIO_MODE_OUTPUT -- Configure pin as output \n * - GPIO_MODE_INPUT -- Configure pin as input \n * - GPIO_MODE_ANALOG -- Configure pin as analog function * @param[in] pullup Pin pullup/pulldown configuration (@ref gpio_pullup) \n * - GPIO_PUPD_NONE -- Do not pull the pin high or low \n * - GPIO_PUPD_PULLUP -- Pull the pin high \n * - GPIO_PUPD_PULLDOWN -- Pull the pin low * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified * by OR'ing then together */ void gpio_mode_setup(uint32_t gpioport, enum gpio_mode mode, enum gpio_pullup pullup, uint8_t gpios) { switch (mode) { case GPIO_MODE_OUTPUT: GPIO_DIR(gpioport) |= gpios; GPIO_DEN(gpioport) |= gpios; GPIO_AMSEL(gpioport) &= ~gpios; break; case GPIO_MODE_INPUT: GPIO_DIR(gpioport) &= ~gpios; GPIO_DEN(gpioport) |= gpios; GPIO_AMSEL(gpioport) &= ~gpios; break; case GPIO_MODE_ANALOG: GPIO_DEN(gpioport) &= ~gpios; GPIO_AMSEL(gpioport) |= gpios; break; default: /* Don't do anything */ break; } /* * Setting a bit in the GPIO_PDR register clears the corresponding bit * in the GPIO_PUR register, and vice-versa. */ switch (pullup) { case GPIO_PUPD_PULLUP: GPIO_PUR(gpioport) |= gpios; break; case GPIO_PUPD_PULLDOWN: GPIO_PDR(gpioport) |= gpios; break; case GPIO_PUPD_NONE: /* Fall through */ default: GPIO_PUR(gpioport) &= ~gpios; GPIO_PDR(gpioport) &= ~gpios; break; } }