Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

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