/* Set the remap function and AF function of the GPIO pin.  */
void exGPIO_PinAFConfig(GPIO_TypeDef *gpio, u16 pin, s32 remap, s8 alternate_function)
/* Set the remap function and AF function of the GPIO pin.  */
void exGPIO_PinAFConfig(GPIO_TypeDef *gpio, u16 pin, s32 remap, s8 alternate_function)
{
    u8 i;
    u8 shift;
    u32* ptr;
    if (alternate_function >= 0) {
        for (i = 0; i < 32; i++) {
            if (pin & 0x01) {
                pin = i;
                break;
            }
            pin >>= 1;
        }
        shift = (pin & 0x07) * 4;
        ptr = (pin < 8) ? (u32*)&gpio->AFRL : (u32*)&gpio->AFRH;
        *ptr = (*ptr & ~(0x0F << shift)) | (alternate_function << shift);
    }
}