TUKS MCU Introductory course / TUKS-COURSE-2-LED
Committer:
elmot
Date:
Fri Feb 24 21:13:56 2017 +0000
Revision:
1:d0dfbce63a89
Ready-to-copy

Who changed what in which revision?

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