mbed-os for GR-LYCHEE

Dependents:   mbed-os-example-blinky-gr-lychee GR-Boads_Camera_sample GR-Boards_Audio_Recoder GR-Boads_Camera_DisplayApp ... more

Committer:
dkato
Date:
Fri Feb 02 05:42:23 2018 +0000
Revision:
0:f782d9c66c49
mbed-os for GR-LYCHEE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:f782d9c66c49 1 /* mbed Microcontroller Library
dkato 0:f782d9c66c49 2 * Copyright (c) 2006-2013 ARM Limited
dkato 0:f782d9c66c49 3 *
dkato 0:f782d9c66c49 4 * Licensed under the Apache License, Version 2.0 (the "License");
dkato 0:f782d9c66c49 5 * you may not use this file except in compliance with the License.
dkato 0:f782d9c66c49 6 * You may obtain a copy of the License at
dkato 0:f782d9c66c49 7 *
dkato 0:f782d9c66c49 8 * http://www.apache.org/licenses/LICENSE-2.0
dkato 0:f782d9c66c49 9 *
dkato 0:f782d9c66c49 10 * Unless required by applicable law or agreed to in writing, software
dkato 0:f782d9c66c49 11 * distributed under the License is distributed on an "AS IS" BASIS,
dkato 0:f782d9c66c49 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dkato 0:f782d9c66c49 13 * See the License for the specific language governing permissions and
dkato 0:f782d9c66c49 14 * limitations under the License.
dkato 0:f782d9c66c49 15 */
dkato 0:f782d9c66c49 16 #ifndef MBED_DIGITALIN_H
dkato 0:f782d9c66c49 17 #define MBED_DIGITALIN_H
dkato 0:f782d9c66c49 18
dkato 0:f782d9c66c49 19 #include "platform/platform.h"
dkato 0:f782d9c66c49 20
dkato 0:f782d9c66c49 21 #include "hal/gpio_api.h"
dkato 0:f782d9c66c49 22 #include "platform/mbed_critical.h"
dkato 0:f782d9c66c49 23
dkato 0:f782d9c66c49 24 namespace mbed {
dkato 0:f782d9c66c49 25 /** \addtogroup drivers */
dkato 0:f782d9c66c49 26 /** @{*/
dkato 0:f782d9c66c49 27
dkato 0:f782d9c66c49 28 /** A digital input, used for reading the state of a pin
dkato 0:f782d9c66c49 29 *
dkato 0:f782d9c66c49 30 * @Note Synchronization level: Interrupt safe
dkato 0:f782d9c66c49 31 *
dkato 0:f782d9c66c49 32 * Example:
dkato 0:f782d9c66c49 33 * @code
dkato 0:f782d9c66c49 34 * // Flash an LED while a DigitalIn is true
dkato 0:f782d9c66c49 35 *
dkato 0:f782d9c66c49 36 * #include "mbed.h"
dkato 0:f782d9c66c49 37 *
dkato 0:f782d9c66c49 38 * DigitalIn enable(p5);
dkato 0:f782d9c66c49 39 * DigitalOut led(LED1);
dkato 0:f782d9c66c49 40 *
dkato 0:f782d9c66c49 41 * int main() {
dkato 0:f782d9c66c49 42 * while(1) {
dkato 0:f782d9c66c49 43 * if(enable) {
dkato 0:f782d9c66c49 44 * led = !led;
dkato 0:f782d9c66c49 45 * }
dkato 0:f782d9c66c49 46 * wait(0.25);
dkato 0:f782d9c66c49 47 * }
dkato 0:f782d9c66c49 48 * }
dkato 0:f782d9c66c49 49 * @endcode
dkato 0:f782d9c66c49 50 */
dkato 0:f782d9c66c49 51 class DigitalIn {
dkato 0:f782d9c66c49 52
dkato 0:f782d9c66c49 53 public:
dkato 0:f782d9c66c49 54 /** Create a DigitalIn connected to the specified pin
dkato 0:f782d9c66c49 55 *
dkato 0:f782d9c66c49 56 * @param pin DigitalIn pin to connect to
dkato 0:f782d9c66c49 57 */
dkato 0:f782d9c66c49 58 DigitalIn(PinName pin) : gpio() {
dkato 0:f782d9c66c49 59 // No lock needed in the constructor
dkato 0:f782d9c66c49 60 gpio_init_in(&gpio, pin);
dkato 0:f782d9c66c49 61 }
dkato 0:f782d9c66c49 62
dkato 0:f782d9c66c49 63 /** Create a DigitalIn connected to the specified pin
dkato 0:f782d9c66c49 64 *
dkato 0:f782d9c66c49 65 * @param pin DigitalIn pin to connect to
dkato 0:f782d9c66c49 66 * @param mode the initial mode of the pin
dkato 0:f782d9c66c49 67 */
dkato 0:f782d9c66c49 68 DigitalIn(PinName pin, PinMode mode) : gpio() {
dkato 0:f782d9c66c49 69 // No lock needed in the constructor
dkato 0:f782d9c66c49 70 gpio_init_in_ex(&gpio, pin, mode);
dkato 0:f782d9c66c49 71 }
dkato 0:f782d9c66c49 72 /** Read the input, represented as 0 or 1 (int)
dkato 0:f782d9c66c49 73 *
dkato 0:f782d9c66c49 74 * @returns
dkato 0:f782d9c66c49 75 * An integer representing the state of the input pin,
dkato 0:f782d9c66c49 76 * 0 for logical 0, 1 for logical 1
dkato 0:f782d9c66c49 77 */
dkato 0:f782d9c66c49 78 int read() {
dkato 0:f782d9c66c49 79 // Thread safe / atomic HAL call
dkato 0:f782d9c66c49 80 return gpio_read(&gpio);
dkato 0:f782d9c66c49 81 }
dkato 0:f782d9c66c49 82
dkato 0:f782d9c66c49 83 /** Set the input pin mode
dkato 0:f782d9c66c49 84 *
dkato 0:f782d9c66c49 85 * @param mode PullUp, PullDown, PullNone, OpenDrain
dkato 0:f782d9c66c49 86 */
dkato 0:f782d9c66c49 87 void mode(PinMode pull) {
dkato 0:f782d9c66c49 88 core_util_critical_section_enter();
dkato 0:f782d9c66c49 89 gpio_mode(&gpio, pull);
dkato 0:f782d9c66c49 90 core_util_critical_section_exit();
dkato 0:f782d9c66c49 91 }
dkato 0:f782d9c66c49 92
dkato 0:f782d9c66c49 93 /** Return the output setting, represented as 0 or 1 (int)
dkato 0:f782d9c66c49 94 *
dkato 0:f782d9c66c49 95 * @returns
dkato 0:f782d9c66c49 96 * Non zero value if pin is connected to uc GPIO
dkato 0:f782d9c66c49 97 * 0 if gpio object was initialized with NC
dkato 0:f782d9c66c49 98 */
dkato 0:f782d9c66c49 99 int is_connected() {
dkato 0:f782d9c66c49 100 // Thread safe / atomic HAL call
dkato 0:f782d9c66c49 101 return gpio_is_connected(&gpio);
dkato 0:f782d9c66c49 102 }
dkato 0:f782d9c66c49 103
dkato 0:f782d9c66c49 104 /** An operator shorthand for read()
dkato 0:f782d9c66c49 105 */
dkato 0:f782d9c66c49 106 operator int() {
dkato 0:f782d9c66c49 107 // Underlying read is thread safe
dkato 0:f782d9c66c49 108 return read();
dkato 0:f782d9c66c49 109 }
dkato 0:f782d9c66c49 110
dkato 0:f782d9c66c49 111 protected:
dkato 0:f782d9c66c49 112 gpio_t gpio;
dkato 0:f782d9c66c49 113 };
dkato 0:f782d9c66c49 114
dkato 0:f782d9c66c49 115 } // namespace mbed
dkato 0:f782d9c66c49 116
dkato 0:f782d9c66c49 117 #endif
dkato 0:f782d9c66c49 118
dkato 0:f782d9c66c49 119 /** @}*/