stm32-GPIO-Pin-Configuration

by Marwen Maghrebi
void Input_Pin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin) {
    uint32_t position = 0;
    uint32_t register_offset;
    volatile uint32_t *config_register;
    uint32_t temp;

    while (GPIO_Pin) {
        if (GPIO_Pin & 0x01) {
            register_offset = (position < 8) ? 0 : 1;
            config_register = &GPIOx->CRL + register_offset;
            temp = *config_register;
            temp &= ~(0x0F << ((position & 0x07) * 4));
            temp |= (0x04 << ((position & 0x07) * 4));  // Input floating
            *config_register = temp;
        }
        position++;
        GPIO_Pin >>= 1;
    }
}

void Output_Pin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin) {
    uint32_t position = 0;
    uint32_t register_offset;
    volatile uint32_t *config_register;
    uint32_t temp;

    while (GPIO_Pin) {
        if (GPIO_Pin & 0x01) {
            register_offset = (position < 8) ? 0 : 1;
            config_register = &GPIOx->CRL + register_offset;
            temp = *config_register;
            temp &= ~(0x0F << ((position & 0x07) * 4));
            temp |= (0x01 << ((position & 0x07) * 4));  // Output push-pull
            *config_register = temp;
        }
        position++;
        GPIO_Pin >>= 1;
    }
}
Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
-
00:00
00:00
Update Required Flash plugin
-
00:00
00:00