/* * 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 Multiplex group of pins to the given alternate function * * Mux the pin or group of pins to the given alternate function. Note that a * number of pins may be set but only with a single AF number. This is useful * when one or more of a peripheral's pins are assigned to the same alternate * function. * * Because AF0 is not used on the LM4F, passing 0 as the alt_func_num parameter * will disable the alternate function of the given pins. * * @param[in] gpioport GPIO block register address base @ref gpio_reg_base * @param[in] alt_func_num Pin alternate function number or 0 to disable the * alternate function multiplexing. * @param[in] gpios @ref gpio_pin_id. Any combination of pins may be specified * by OR'ing then together */ void gpio_set_af(uint32_t gpioport, uint8_t alt_func_num, uint8_t gpios) { uint32_t pctl32; uint8_t pin_mask; int i; /* Did we mean to disable the alternate function? */ if (alt_func_num == 0) { GPIO_AFSEL(gpioport) &= ~gpios; return; } /* Enable the alternate function */ GPIO_AFSEL(gpioport) |= gpios; /* Alternate functions are digital */ GPIO_DEN(gpioport) |= gpios; /* Now take care of the actual multiplexing */ pctl32 = GPIO_PCTL(gpioport); for (i = 0; i < 8; i++) { pin_mask = (1 << i); if (!(gpios & pin_mask)) { continue; } pctl32 &= ~PCTL_MASK(i); pctl32 |= PCTL_AF(i, (alt_func_num & 0xf)); } GPIO_PCTL(gpioport) = pctl32; }
static void gpio_setup(void) { SYSTEMCONTROL_RCGC2 |= 0x04; /* Enable GPIOC in run mode. */ GPIO_DIR(GPIOC) |= (1 << 5); /* Configure PC5 as output. */ GPIO_DEN(GPIOC) |= (1 << 5); /* Enable digital function on PC5. */ }
static void uart_setup(void) { u32 pins; /* Enable GPIOA in run mode. */ periph_clock_enable(RCC_GPIOA); /* Configure PA0 and PA1 as alternate function pins */ pins = GPIO0 | GPIO1; GPIO_AFSEL(GPIOA) |= pins; GPIO_DEN(GPIOA) |= pins; /* PA0 and PA1 are muxed to UART0 during power on, by default */ /* Enable the UART clock */ periph_clock_enable(RCC_UART0); /* We need a brief delay before we can access UART config registers */ __asm__("nop"); /* Disable the UART while we mess with its setings */ uart_disable(UART0); /* Configure the UART clock source as precision internal oscillator */ uart_clock_from_piosc(UART0); /* Set communication parameters */ uart_set_baudrate(UART0, 921600); uart_set_databits(UART0, 8); uart_set_parity(UART0, UART_PARITY_NONE); uart_set_stopbits(UART0, 1); /* Now that we're done messing with the settings, enable the UART */ uart_enable(UART0); }
/** * \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; } }