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