Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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