aboutsummaryrefslogtreecommitdiff
path: root/platform/stm32f4xx/gpio.c
blob: cdad97ed79b5219624b3f9ba3774b857614a4ab6 (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
/*
 * Copyright (c) 2012 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 <assert.h>
#include <dev/gpio.h>
#include <platform/stm32.h>
#include <platform/gpio.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_rcc.h>

static GPIO_TypeDef *port_to_pointer(unsigned int port)
{
    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;
    }
}

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

    /* happens to be the RCC ids are sequential bits, so we can start from A and shift */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA << port, ENABLE);
}

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.GPIO_Speed = GPIO_Speed_50MHz;
    init.GPIO_Pin = (1 << pin);
    init.GPIO_PuPd = GPIO_PuPd_NOPULL;

    if (flags & GPIO_INPUT) {
        init.GPIO_Mode = GPIO_Mode_IN;
    } else if  (flags & GPIO_OUTPUT) {
        init.GPIO_Mode = GPIO_Mode_OUT;
    } else if  (flags & GPIO_STM32_AF) {
        init.GPIO_Mode = GPIO_Mode_AF;
        GPIO_PinAFConfig(port_to_pointer(port), pin, GPIO_AFNUM(flags));
    }

    if (flags & GPIO_PULLUP) {
        init.GPIO_PuPd = GPIO_PuPd_UP;
    } else if (flags & GPIO_PULLDOWN) {
        init.GPIO_PuPd = GPIO_PuPd_DOWN;
    }

    if (flags & GPIO_STM32_OD) {
        init.GPIO_OType = GPIO_OType_OD;
    } else {
        init.GPIO_OType = GPIO_OType_PP;
    }

    GPIO_Init(port_to_pointer(port), &init);

    return 0;
}

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

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