mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_gpio.c Source File

mbed_gpio.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "hal/gpio_api.h"
00018 
00019 static inline void _gpio_init_in(gpio_t *gpio, PinName pin, PinMode mode)
00020 {
00021     gpio_init(gpio, pin);
00022     if (pin != NC) {
00023         gpio_dir(gpio, PIN_INPUT);
00024         gpio_mode(gpio, mode);
00025     }
00026 }
00027 
00028 static inline void _gpio_init_out(gpio_t *gpio, PinName pin, PinMode mode, int value)
00029 {
00030     gpio_init(gpio, pin);
00031     if (pin != NC) {
00032         gpio_write(gpio, value);
00033         gpio_dir(gpio, PIN_OUTPUT);
00034         gpio_mode(gpio, mode);
00035     }
00036 }
00037 
00038 void gpio_init_in(gpio_t *gpio, PinName pin)
00039 {
00040     gpio_init_in_ex(gpio, pin, PullDefault);
00041 }
00042 
00043 void gpio_init_in_ex(gpio_t *gpio, PinName pin, PinMode mode)
00044 {
00045     _gpio_init_in(gpio, pin, mode);
00046 }
00047 
00048 void gpio_init_out(gpio_t *gpio, PinName pin)
00049 {
00050     gpio_init_out_ex(gpio, pin, 0);
00051 }
00052 
00053 void gpio_init_out_ex(gpio_t *gpio, PinName pin, int value)
00054 {
00055     _gpio_init_out(gpio, pin, PullNone, value);
00056 }
00057 
00058 void gpio_init_inout(gpio_t *gpio, PinName pin, PinDirection direction, PinMode mode, int value)
00059 {
00060     if (direction == PIN_INPUT) {
00061         _gpio_init_in(gpio, pin, mode);
00062         if (pin != NC) {
00063             gpio_write(gpio, value);    // we prepare the value in case it is switched later
00064         }
00065     } else {
00066         _gpio_init_out(gpio, pin, mode, value);
00067     }
00068 }