summaryrefslogtreecommitdiffstats
path: root/firmware/target/arm/stm32/gpio-stm32h7.c
blob: 020c3cb86f2c012ac656620c57c816d59e2986ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2025 Aidan MacDonald
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include "gpio-stm32h7.h"

void gpio_configure_single(int gpio, int confbits)
{
    int mode = GPIO_F_GETMODE(confbits);

    if (mode == GPIO_MODE_OUTPUT)
        gpio_set_level(gpio, GPIO_F_GETINITLEVEL(confbits));
    else if (mode == GPIO_MODE_FUNCTION)
        gpio_set_function(gpio, GPIO_F_GETAFNUM(confbits));

    gpio_set_pull(gpio, GPIO_F_GETPULL(confbits));
    gpio_set_type(gpio, GPIO_F_GETTYPE(confbits));
    gpio_set_speed(gpio, GPIO_F_GETSPEED(confbits));
    gpio_set_mode(gpio, mode);
}

void gpio_configure_port(int port, uint16_t mask, int confbits)
{
    for (int pin = 0; pin < 16; ++pin)
    {
        if (mask & BIT_N(pin))
        {
            int gpio = GPION_CREATE(port, pin);

            gpio_configure_single(gpio, confbits);
        }
    }
}

void gpio_configure_all(
    const struct gpio_setting *gpios, size_t ngpios,
    const struct pingroup_setting *pingroups, size_t ngroups)
{
    for (size_t i = 0; i < ngpios; ++i)
    {
        gpio_configure_single(gpios[i].gpio, gpios[i].func);
    }

    for (size_t i = 0; i < ngroups; ++i)
    {
        gpio_configure_port(pingroups[i].port,
                            pingroups[i].pins,
                            pingroups[i].func);
    }
}