Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-src/common/gpio.c@0:638edba3adf6, 2016-01-11 (annotated)
- Committer:
- yihui
- Date:
- Mon Jan 11 02:32:24 2016 +0000
- Revision:
- 0:638edba3adf6
initial
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yihui | 0:638edba3adf6 | 1 | /* mbed Microcontroller Library |
| yihui | 0:638edba3adf6 | 2 | * Copyright (c) 2006-2013 ARM Limited |
| yihui | 0:638edba3adf6 | 3 | * |
| yihui | 0:638edba3adf6 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| yihui | 0:638edba3adf6 | 5 | * you may not use this file except in compliance with the License. |
| yihui | 0:638edba3adf6 | 6 | * You may obtain a copy of the License at |
| yihui | 0:638edba3adf6 | 7 | * |
| yihui | 0:638edba3adf6 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| yihui | 0:638edba3adf6 | 9 | * |
| yihui | 0:638edba3adf6 | 10 | * Unless required by applicable law or agreed to in writing, software |
| yihui | 0:638edba3adf6 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| yihui | 0:638edba3adf6 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| yihui | 0:638edba3adf6 | 13 | * See the License for the specific language governing permissions and |
| yihui | 0:638edba3adf6 | 14 | * limitations under the License. |
| yihui | 0:638edba3adf6 | 15 | */ |
| yihui | 0:638edba3adf6 | 16 | #include "gpio_api.h" |
| yihui | 0:638edba3adf6 | 17 | |
| yihui | 0:638edba3adf6 | 18 | static inline void _gpio_init_in(gpio_t* gpio, PinName pin, PinMode mode) |
| yihui | 0:638edba3adf6 | 19 | { |
| yihui | 0:638edba3adf6 | 20 | gpio_init(gpio, pin); |
| yihui | 0:638edba3adf6 | 21 | if (pin != NC) { |
| yihui | 0:638edba3adf6 | 22 | gpio_dir(gpio, PIN_INPUT); |
| yihui | 0:638edba3adf6 | 23 | gpio_mode(gpio, mode); |
| yihui | 0:638edba3adf6 | 24 | } |
| yihui | 0:638edba3adf6 | 25 | } |
| yihui | 0:638edba3adf6 | 26 | |
| yihui | 0:638edba3adf6 | 27 | static inline void _gpio_init_out(gpio_t* gpio, PinName pin, PinMode mode, int value) |
| yihui | 0:638edba3adf6 | 28 | { |
| yihui | 0:638edba3adf6 | 29 | gpio_init(gpio, pin); |
| yihui | 0:638edba3adf6 | 30 | if (pin != NC) { |
| yihui | 0:638edba3adf6 | 31 | gpio_write(gpio, value); |
| yihui | 0:638edba3adf6 | 32 | gpio_dir(gpio, PIN_OUTPUT); |
| yihui | 0:638edba3adf6 | 33 | gpio_mode(gpio, mode); |
| yihui | 0:638edba3adf6 | 34 | } |
| yihui | 0:638edba3adf6 | 35 | } |
| yihui | 0:638edba3adf6 | 36 | |
| yihui | 0:638edba3adf6 | 37 | void gpio_init_in(gpio_t* gpio, PinName pin) { |
| yihui | 0:638edba3adf6 | 38 | gpio_init_in_ex(gpio, pin, PullDefault); |
| yihui | 0:638edba3adf6 | 39 | } |
| yihui | 0:638edba3adf6 | 40 | |
| yihui | 0:638edba3adf6 | 41 | void gpio_init_in_ex(gpio_t* gpio, PinName pin, PinMode mode) { |
| yihui | 0:638edba3adf6 | 42 | _gpio_init_in(gpio, pin, mode); |
| yihui | 0:638edba3adf6 | 43 | } |
| yihui | 0:638edba3adf6 | 44 | |
| yihui | 0:638edba3adf6 | 45 | void gpio_init_out(gpio_t* gpio, PinName pin) { |
| yihui | 0:638edba3adf6 | 46 | gpio_init_out_ex(gpio, pin, 0); |
| yihui | 0:638edba3adf6 | 47 | } |
| yihui | 0:638edba3adf6 | 48 | |
| yihui | 0:638edba3adf6 | 49 | void gpio_init_out_ex(gpio_t* gpio, PinName pin, int value) { |
| yihui | 0:638edba3adf6 | 50 | _gpio_init_out(gpio, pin, PullNone, value); |
| yihui | 0:638edba3adf6 | 51 | } |
| yihui | 0:638edba3adf6 | 52 | |
| yihui | 0:638edba3adf6 | 53 | void gpio_init_inout(gpio_t* gpio, PinName pin, PinDirection direction, PinMode mode, int value) { |
| yihui | 0:638edba3adf6 | 54 | if (direction == PIN_INPUT) { |
| yihui | 0:638edba3adf6 | 55 | _gpio_init_in(gpio, pin, mode); |
| yihui | 0:638edba3adf6 | 56 | if (pin != NC) |
| yihui | 0:638edba3adf6 | 57 | gpio_write(gpio, value); // we prepare the value in case it is switched later |
| yihui | 0:638edba3adf6 | 58 | } else { |
| yihui | 0:638edba3adf6 | 59 | _gpio_init_out(gpio, pin, mode, value); |
| yihui | 0:638edba3adf6 | 60 | } |
| yihui | 0:638edba3adf6 | 61 | } |