cc y / mbed

Fork of mbed by mbed official

Committer:
Kojto
Date:
Fri Aug 12 13:04:35 2016 +0200
Revision:
123:b0220dba8be7
Parent:
122:f9eeca106725
Release 123 of the mbed library

Changes:
- new targets: nucleo_f207zg, beetle, nrf51_dk, hexiwear,
nuvoton nuc472, vk rz a1h
- ST - fix timer interrupt handler, sleep api fix
- NXP - lpc15xx us ticker fix
- Nordic - analogin fixes, LF clock init addition, enable i2c async

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 65:5798e58a58b1 1 /* mbed Microcontroller Library
bogdanm 65:5798e58a58b1 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 65:5798e58a58b1 3 *
bogdanm 65:5798e58a58b1 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 65:5798e58a58b1 5 * you may not use this file except in compliance with the License.
bogdanm 65:5798e58a58b1 6 * You may obtain a copy of the License at
bogdanm 65:5798e58a58b1 7 *
bogdanm 65:5798e58a58b1 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 65:5798e58a58b1 9 *
bogdanm 65:5798e58a58b1 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 65:5798e58a58b1 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 65:5798e58a58b1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 65:5798e58a58b1 13 * See the License for the specific language governing permissions and
bogdanm 65:5798e58a58b1 14 * limitations under the License.
bogdanm 65:5798e58a58b1 15 */
bogdanm 65:5798e58a58b1 16 #ifndef MBED_DIGITALINOUT_H
bogdanm 65:5798e58a58b1 17 #define MBED_DIGITALINOUT_H
bogdanm 65:5798e58a58b1 18
bogdanm 65:5798e58a58b1 19 #include "platform.h"
bogdanm 65:5798e58a58b1 20
bogdanm 65:5798e58a58b1 21 #include "gpio_api.h"
Kojto 122:f9eeca106725 22 #include "critical.h"
bogdanm 65:5798e58a58b1 23
bogdanm 65:5798e58a58b1 24 namespace mbed {
bogdanm 65:5798e58a58b1 25
bogdanm 65:5798e58a58b1 26 /** A digital input/output, used for setting or reading a bi-directional pin
Kojto 122:f9eeca106725 27 *
Kojto 122:f9eeca106725 28 * @Note Synchronization level: Interrupt safe
bogdanm 65:5798e58a58b1 29 */
bogdanm 65:5798e58a58b1 30 class DigitalInOut {
bogdanm 65:5798e58a58b1 31
bogdanm 65:5798e58a58b1 32 public:
bogdanm 65:5798e58a58b1 33 /** Create a DigitalInOut connected to the specified pin
bogdanm 65:5798e58a58b1 34 *
bogdanm 65:5798e58a58b1 35 * @param pin DigitalInOut pin to connect to
bogdanm 65:5798e58a58b1 36 */
bogdanm 85:024bf7f99721 37 DigitalInOut(PinName pin) : gpio() {
Kojto 122:f9eeca106725 38 // No lock needed in the constructor
bogdanm 81:7d30d6019079 39 gpio_init_in(&gpio, pin);
bogdanm 81:7d30d6019079 40 }
bogdanm 81:7d30d6019079 41
bogdanm 81:7d30d6019079 42 /** Create a DigitalInOut connected to the specified pin
bogdanm 81:7d30d6019079 43 *
bogdanm 81:7d30d6019079 44 * @param pin DigitalInOut pin to connect to
bogdanm 81:7d30d6019079 45 * @param direction the initial direction of the pin
bogdanm 81:7d30d6019079 46 * @param mode the initial mode of the pin
bogdanm 81:7d30d6019079 47 * @param value the initial value of the pin if is an output
bogdanm 81:7d30d6019079 48 */
bogdanm 85:024bf7f99721 49 DigitalInOut(PinName pin, PinDirection direction, PinMode mode, int value) : gpio() {
Kojto 122:f9eeca106725 50 // No lock needed in the constructor
bogdanm 81:7d30d6019079 51 gpio_init_inout(&gpio, pin, direction, mode, value);
bogdanm 65:5798e58a58b1 52 }
bogdanm 65:5798e58a58b1 53
bogdanm 65:5798e58a58b1 54 /** Set the output, specified as 0 or 1 (int)
bogdanm 65:5798e58a58b1 55 *
bogdanm 65:5798e58a58b1 56 * @param value An integer specifying the pin output value,
bogdanm 65:5798e58a58b1 57 * 0 for logical 0, 1 (or any other non-zero value) for logical 1
bogdanm 65:5798e58a58b1 58 */
bogdanm 65:5798e58a58b1 59 void write(int value) {
Kojto 122:f9eeca106725 60 // Thread safe / atomic HAL call
bogdanm 65:5798e58a58b1 61 gpio_write(&gpio, value);
bogdanm 65:5798e58a58b1 62 }
bogdanm 65:5798e58a58b1 63
bogdanm 65:5798e58a58b1 64 /** Return the output setting, represented as 0 or 1 (int)
bogdanm 65:5798e58a58b1 65 *
bogdanm 65:5798e58a58b1 66 * @returns
bogdanm 65:5798e58a58b1 67 * an integer representing the output setting of the pin if it is an output,
bogdanm 65:5798e58a58b1 68 * or read the input if set as an input
bogdanm 65:5798e58a58b1 69 */
bogdanm 65:5798e58a58b1 70 int read() {
Kojto 122:f9eeca106725 71 // Thread safe / atomic HAL call
bogdanm 65:5798e58a58b1 72 return gpio_read(&gpio);
bogdanm 65:5798e58a58b1 73 }
bogdanm 65:5798e58a58b1 74
bogdanm 65:5798e58a58b1 75 /** Set as an output
bogdanm 65:5798e58a58b1 76 */
bogdanm 65:5798e58a58b1 77 void output() {
Kojto 122:f9eeca106725 78 core_util_critical_section_enter();
bogdanm 65:5798e58a58b1 79 gpio_dir(&gpio, PIN_OUTPUT);
Kojto 122:f9eeca106725 80 core_util_critical_section_exit();
bogdanm 65:5798e58a58b1 81 }
bogdanm 65:5798e58a58b1 82
bogdanm 65:5798e58a58b1 83 /** Set as an input
bogdanm 65:5798e58a58b1 84 */
bogdanm 65:5798e58a58b1 85 void input() {
Kojto 122:f9eeca106725 86 core_util_critical_section_enter();
bogdanm 65:5798e58a58b1 87 gpio_dir(&gpio, PIN_INPUT);
Kojto 122:f9eeca106725 88 core_util_critical_section_exit();
bogdanm 65:5798e58a58b1 89 }
bogdanm 65:5798e58a58b1 90
bogdanm 65:5798e58a58b1 91 /** Set the input pin mode
bogdanm 65:5798e58a58b1 92 *
bogdanm 65:5798e58a58b1 93 * @param mode PullUp, PullDown, PullNone, OpenDrain
bogdanm 65:5798e58a58b1 94 */
bogdanm 65:5798e58a58b1 95 void mode(PinMode pull) {
Kojto 122:f9eeca106725 96 core_util_critical_section_enter();
bogdanm 65:5798e58a58b1 97 gpio_mode(&gpio, pull);
Kojto 122:f9eeca106725 98 core_util_critical_section_exit();
bogdanm 65:5798e58a58b1 99 }
bogdanm 65:5798e58a58b1 100
Kojto 93:e188a91d3eaa 101 /** Return the output setting, represented as 0 or 1 (int)
Kojto 93:e188a91d3eaa 102 *
Kojto 93:e188a91d3eaa 103 * @returns
Kojto 93:e188a91d3eaa 104 * Non zero value if pin is connected to uc GPIO
Kojto 93:e188a91d3eaa 105 * 0 if gpio object was initialized with NC
Kojto 93:e188a91d3eaa 106 */
Kojto 93:e188a91d3eaa 107 int is_connected() {
Kojto 122:f9eeca106725 108 // Thread safe / atomic HAL call
Kojto 93:e188a91d3eaa 109 return gpio_is_connected(&gpio);
Kojto 93:e188a91d3eaa 110 }
Kojto 93:e188a91d3eaa 111
bogdanm 65:5798e58a58b1 112 /** A shorthand for write()
bogdanm 65:5798e58a58b1 113 */
bogdanm 65:5798e58a58b1 114 DigitalInOut& operator= (int value) {
Kojto 122:f9eeca106725 115 // Underlying write is thread safe
bogdanm 65:5798e58a58b1 116 write(value);
bogdanm 65:5798e58a58b1 117 return *this;
bogdanm 65:5798e58a58b1 118 }
bogdanm 65:5798e58a58b1 119
bogdanm 65:5798e58a58b1 120 DigitalInOut& operator= (DigitalInOut& rhs) {
Kojto 122:f9eeca106725 121 core_util_critical_section_enter();
bogdanm 65:5798e58a58b1 122 write(rhs.read());
Kojto 122:f9eeca106725 123 core_util_critical_section_exit();
bogdanm 65:5798e58a58b1 124 return *this;
bogdanm 65:5798e58a58b1 125 }
bogdanm 65:5798e58a58b1 126
bogdanm 65:5798e58a58b1 127 /** A shorthand for read()
bogdanm 65:5798e58a58b1 128 */
bogdanm 65:5798e58a58b1 129 operator int() {
Kojto 122:f9eeca106725 130 // Underlying call is thread safe
bogdanm 65:5798e58a58b1 131 return read();
bogdanm 65:5798e58a58b1 132 }
bogdanm 65:5798e58a58b1 133
bogdanm 65:5798e58a58b1 134 protected:
bogdanm 65:5798e58a58b1 135 gpio_t gpio;
bogdanm 65:5798e58a58b1 136 };
bogdanm 65:5798e58a58b1 137
bogdanm 65:5798e58a58b1 138 } // namespace mbed
bogdanm 65:5798e58a58b1 139
bogdanm 65:5798e58a58b1 140 #endif