aboutsummaryrefslogtreecommitdiff
path: root/platform/stm32f7xx/gpio.c
blob: ac2a8a3e0bd6e3ddef06b5a2f24f015be8bf5682 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (c) 2012-2015 Travis Geiselbrecht
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files
 * (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include <debug.h>
#include <err.h>
#include <assert.h>
#include <dev/gpio.h>
#include <platform/stm32.h>
#include <platform/gpio.h>

static GPIO_TypeDef *port_to_pointer(unsigned int port)
{
    DEBUG_ASSERT(port <= GPIO_PORT_K);

    switch (port) {
        default:
        case GPIO_PORT_A:
            return GPIOA;
        case GPIO_PORT_B:
            return GPIOB;
        case GPIO_PORT_C:
            return GPIOC;
        case GPIO_PORT_D:
            return GPIOD;
        case GPIO_PORT_E:
            return GPIOE;
        case GPIO_PORT_F:
            return GPIOF;
        case GPIO_PORT_G:
            return GPIOG;
        case GPIO_PORT_H:
            return GPIOH;
        case GPIO_PORT_I:
            return GPIOI;
        case GPIO_PORT_J:
            return GPIOJ;
        case GPIO_PORT_K:
            return GPIOK;
    }
}

static void enable_port(unsigned int port)
{
    DEBUG_ASSERT(port <= GPIO_PORT_K);

    switch (port) {
        case GPIO_PORT_A:
            __HAL_RCC_GPIOA_CLK_ENABLE();
            break;
        case GPIO_PORT_B:
            __HAL_RCC_GPIOB_CLK_ENABLE();
            break;
        case GPIO_PORT_C:
            __HAL_RCC_GPIOC_CLK_ENABLE();
            break;
        case GPIO_PORT_D:
            __HAL_RCC_GPIOD_CLK_ENABLE();
            break;
        case GPIO_PORT_E:
            __HAL_RCC_GPIOE_CLK_ENABLE();
            break;
        case GPIO_PORT_F:
            __HAL_RCC_GPIOF_CLK_ENABLE();
            break;
        case GPIO_PORT_G:
            __HAL_RCC_GPIOG_CLK_ENABLE();
            break;
        case GPIO_PORT_H:
            __HAL_RCC_GPIOH_CLK_ENABLE();
            break;
        case GPIO_PORT_I:
            __HAL_RCC_GPIOI_CLK_ENABLE();
            break;
        case GPIO_PORT_J:
            __HAL_RCC_GPIOJ_CLK_ENABLE();
            break;
        case GPIO_PORT_K:
            __HAL_RCC_GPIOK_CLK_ENABLE();
            break;
    }
}

void stm32_gpio_early_init(void)
{
}

int gpio_config(unsigned nr, unsigned flags)
{
    uint port = GPIO_PORT(nr);
    uint pin = GPIO_PIN(nr);

    enable_port(port);

    GPIO_InitTypeDef init;
    init.Speed = GPIO_SPEED_HIGH;
    init.Pin = (1 << pin);
    init.Alternate = 0;

    if (flags & GPIO_INPUT) {
        init.Mode = GPIO_MODE_INPUT;
    } else if (flags & GPIO_OUTPUT) {
        if (flags & GPIO_STM32_OD) {
            init.Mode = GPIO_MODE_OUTPUT_OD;
        } else {
            init.Mode = GPIO_MODE_OUTPUT_PP;
        }
    } else if (flags & GPIO_STM32_AF) {
        if (flags & GPIO_STM32_OD) {
            init.Mode = GPIO_MODE_AF_OD;
        } else {
            init.Mode = GPIO_MODE_AF_PP;
        }
        init.Alternate = GPIO_AFNUM(flags);
    } else {
        panic("stm32f7: invalid args to gpio_config\n");
        return ERR_INVALID_ARGS;
    }

    if (flags & GPIO_PULLUP) {
        init.Pull = GPIO_PULLUP;
    } else if (flags & GPIO_PULLDOWN) {
        init.Pull = GPIO_PULLDOWN;
    } else {
        init.Pull = GPIO_NOPULL;
    }

    HAL_GPIO_Init(port_to_pointer(port), &init);

    return 0;
}

void gpio_set(unsigned nr, unsigned on)
{
    HAL_GPIO_WritePin(port_to_pointer(GPIO_PORT(nr)), 1 << GPIO_PIN(nr), on);
}

int gpio_get(unsigned nr)
{
    return HAL_GPIO_ReadPin(port_to_pointer(GPIO_PORT(nr)), 1 << GPIO_PIN(nr));
}