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