/* Set GPIO Alternate Function Selection. 
Note that a number of pins may be set but only with a single AF number. In practice this would rarely be useful as each pin is likely to require a different number. */
void gpio_set_af(uint32_t gpioport, uint8_t alt_func_num, uint16_t gpios)
/* Set GPIO Alternate Function Selection. 
Note that a number of pins may be set but only with a single AF number. In practice this would rarely be useful as each pin is likely to require a different number. */
void gpio_set_af(uint32_t gpioport, uint8_t alt_func_num, uint16_t gpios)
{
	uint16_t i;
	uint32_t afrl, afrh;
	afrl = GPIO_AFRL(gpioport);
	afrh = GPIO_AFRH(gpioport);
	for (i = 0; i < 8; i++) {
		if (!((1 << i) & gpios)) {
			continue;
		}
		afrl &= ~GPIO_AFR_MASK(i);
		afrl |= GPIO_AFR(i, alt_func_num);
	}
	for (i = 8; i < 16; i++) {
		if (!((1 << i) & gpios)) {
			continue;
		}
		afrh &= ~GPIO_AFR_MASK(i - 8);
		afrh |= GPIO_AFR(i - 8, alt_func_num);
	}
	GPIO_AFRL(gpioport) = afrl;
	GPIO_AFRH(gpioport) = afrh;
}
/**@}