]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_STM/TARGET_STM32F4XX/gpio_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_STM / TARGET_STM32F4XX / gpio_api.c
1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "mbed_assert.h"
17 #include "gpio_api.h"
18 #include "pinmap.h"
19
20 uint32_t gpio_set(PinName pin) {
21 MBED_ASSERT(pin != (PinName)NC);
22 uint32_t port_index = (uint32_t) pin >> 4;
23
24 // Enable GPIO peripheral clock
25 RCC->AHB1ENR |= 1 << port_index;
26
27 pin_function(pin, STM_PIN_DATA(0, 0));
28 return 1 << ((uint32_t) pin & 0xF);
29 }
30
31 void gpio_init(gpio_t *obj, PinName pin) {
32 obj->pin = pin;
33 if (pin == (PinName)NC)
34 return;
35
36 obj->mask = gpio_set(pin);
37
38 uint32_t port_index = (uint32_t) pin >> 4;
39
40 GPIO_TypeDef *port_reg = (GPIO_TypeDef *) (GPIOA_BASE + (port_index << 10));
41 obj->reg_mode = &port_reg->MODER;
42 obj->reg_set = &port_reg->BSRRL;
43 obj->reg_clr = &port_reg->BSRRH;
44 obj->reg_in = &port_reg->IDR;
45 }
46
47 void gpio_mode(gpio_t *obj, PinMode mode) {
48 pin_mode(obj->pin, mode);
49 }
50
51 void gpio_dir(gpio_t *obj, PinDirection direction) {
52 MBED_ASSERT(obj->pin != (PinName)NC);
53 switch (direction) {
54 case PIN_INPUT :
55 pin_function(obj->pin, STM_PIN_DATA(0, 0));
56 break;
57 case PIN_OUTPUT:
58 pin_function(obj->pin, STM_PIN_DATA(1, 0));
59 break;
60 }
61 }
Imprint / Impressum