[352] | 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 "hal/gpio_api.h"
|
---|
| 17 |
|
---|
| 18 | static inline void _gpio_init_in(gpio_t* gpio, PinName pin, PinMode mode)
|
---|
| 19 | {
|
---|
| 20 | gpio_init(gpio, pin);
|
---|
| 21 | if (pin != NC) {
|
---|
| 22 | gpio_dir(gpio, PIN_INPUT);
|
---|
| 23 | gpio_mode(gpio, mode);
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | static inline void _gpio_init_out(gpio_t* gpio, PinName pin, PinMode mode, int value)
|
---|
| 28 | {
|
---|
| 29 | gpio_init(gpio, pin);
|
---|
| 30 | if (pin != NC) {
|
---|
| 31 | gpio_write(gpio, value);
|
---|
| 32 | gpio_dir(gpio, PIN_OUTPUT);
|
---|
| 33 | gpio_mode(gpio, mode);
|
---|
| 34 | }
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | void gpio_init_in(gpio_t* gpio, PinName pin) {
|
---|
| 38 | gpio_init_in_ex(gpio, pin, PullDefault);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | void gpio_init_in_ex(gpio_t* gpio, PinName pin, PinMode mode) {
|
---|
| 42 | _gpio_init_in(gpio, pin, mode);
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | void gpio_init_out(gpio_t* gpio, PinName pin) {
|
---|
| 46 | gpio_init_out_ex(gpio, pin, 0);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | void gpio_init_out_ex(gpio_t* gpio, PinName pin, int value) {
|
---|
| 50 | _gpio_init_out(gpio, pin, PullNone, value);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | void gpio_init_inout(gpio_t* gpio, PinName pin, PinDirection direction, PinMode mode, int value) {
|
---|
| 54 | if (direction == PIN_INPUT) {
|
---|
| 55 | _gpio_init_in(gpio, pin, mode);
|
---|
| 56 | if (pin != NC)
|
---|
| 57 | gpio_write(gpio, value); // we prepare the value in case it is switched later
|
---|
| 58 | } else {
|
---|
| 59 | _gpio_init_out(gpio, pin, mode, value);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|