]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_NXP/TARGET_LPC11XX_11CXX/gpio_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_NXP / TARGET_LPC11XX_11CXX / 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 #include "reserved_pins.h"
20
21 static const PinName reserved_pins[] = TARGET_RESERVED_PINS;
22
23 uint32_t gpio_set(PinName pin) {
24     MBED_ASSERT(pin != (PinName)NC);
25     // PIO default value of following ports are not same as others
26     unsigned i;
27     int f = 0;
28
29     for (i = 0; i < sizeof(reserved_pins) / sizeof(PinName); i ++) {
30         if (pin == reserved_pins[i]) {
31             f = 1;
32             break;
33         }
34     }
35     pin_function(pin, f);
36     return ((pin & 0x0F00) >> 8);
37 }
38
39 void gpio_init(gpio_t *obj, PinName pin) {
40     obj->pin = pin;
41     if (pin == (PinName)NC)
42         return;
43
44     LPC_GPIO_TypeDef *port_reg = ((LPC_GPIO_TypeDef *) (LPC_GPIO0_BASE + (((pin & 0xF000) >> PORT_SHIFT) * 0x10000)));
45
46     obj->reg_mask_read = &port_reg->MASKED_ACCESS[1 << gpio_set(pin)];
47     obj->reg_dir       = &port_reg->DIR;
48     obj->reg_write     = &port_reg->DATA;
49 }
50
51 void gpio_mode(gpio_t *obj, PinMode mode) {
52     pin_mode(obj->pin, mode);
53 }
54
55 void gpio_dir(gpio_t *obj, PinDirection direction) {
56     MBED_ASSERT(obj->pin != (PinName)NC);
57     int pin_number = ((obj->pin & 0x0F00) >> 8);
58     switch (direction) {
59         case PIN_INPUT :
60             *obj->reg_dir &= ~(1 << pin_number);
61             break;
62         case PIN_OUTPUT:
63             *obj->reg_dir |= (1 << pin_number);
64             break;
65     }
66 }