Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcozecchini 0:9fca2b23d0ba 1 /* mbed Microcontroller Library
marcozecchini 0:9fca2b23d0ba 2 * Copyright (c) 2006-2013 ARM Limited
marcozecchini 0:9fca2b23d0ba 3 *
marcozecchini 0:9fca2b23d0ba 4 * Licensed under the Apache License, Version 2.0 (the "License");
marcozecchini 0:9fca2b23d0ba 5 * you may not use this file except in compliance with the License.
marcozecchini 0:9fca2b23d0ba 6 * You may obtain a copy of the License at
marcozecchini 0:9fca2b23d0ba 7 *
marcozecchini 0:9fca2b23d0ba 8 * http://www.apache.org/licenses/LICENSE-2.0
marcozecchini 0:9fca2b23d0ba 9 *
marcozecchini 0:9fca2b23d0ba 10 * Unless required by applicable law or agreed to in writing, software
marcozecchini 0:9fca2b23d0ba 11 * distributed under the License is distributed on an "AS IS" BASIS,
marcozecchini 0:9fca2b23d0ba 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
marcozecchini 0:9fca2b23d0ba 13 * See the License for the specific language governing permissions and
marcozecchini 0:9fca2b23d0ba 14 * limitations under the License.
marcozecchini 0:9fca2b23d0ba 15 */
marcozecchini 0:9fca2b23d0ba 16 #ifndef MBED_PORTINOUT_H
marcozecchini 0:9fca2b23d0ba 17 #define MBED_PORTINOUT_H
marcozecchini 0:9fca2b23d0ba 18
marcozecchini 0:9fca2b23d0ba 19 #include "platform/platform.h"
marcozecchini 0:9fca2b23d0ba 20
marcozecchini 0:9fca2b23d0ba 21 #if defined (DEVICE_PORTINOUT) || defined(DOXYGEN_ONLY)
marcozecchini 0:9fca2b23d0ba 22
marcozecchini 0:9fca2b23d0ba 23 #include "hal/port_api.h"
marcozecchini 0:9fca2b23d0ba 24 #include "platform/mbed_critical.h"
marcozecchini 0:9fca2b23d0ba 25
marcozecchini 0:9fca2b23d0ba 26 namespace mbed {
marcozecchini 0:9fca2b23d0ba 27 /** \addtogroup drivers */
marcozecchini 0:9fca2b23d0ba 28
marcozecchini 0:9fca2b23d0ba 29 /** A multiple pin digital in/out used to set/read multiple bi-directional pins
marcozecchini 0:9fca2b23d0ba 30 *
marcozecchini 0:9fca2b23d0ba 31 * @note Synchronization level: Interrupt safe
marcozecchini 0:9fca2b23d0ba 32 * @ingroup drivers
marcozecchini 0:9fca2b23d0ba 33 */
marcozecchini 0:9fca2b23d0ba 34 class PortInOut {
marcozecchini 0:9fca2b23d0ba 35 public:
marcozecchini 0:9fca2b23d0ba 36
marcozecchini 0:9fca2b23d0ba 37 /** Create an PortInOut, connected to the specified port
marcozecchini 0:9fca2b23d0ba 38 *
marcozecchini 0:9fca2b23d0ba 39 * @param port Port to connect to (Port0-Port5)
marcozecchini 0:9fca2b23d0ba 40 * @param mask A bitmask to identify which bits in the port should be included (0 - ignore)
marcozecchini 0:9fca2b23d0ba 41 */
marcozecchini 0:9fca2b23d0ba 42 PortInOut(PortName port, int mask = 0xFFFFFFFF) {
marcozecchini 0:9fca2b23d0ba 43 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 44 port_init(&_port, port, mask, PIN_INPUT);
marcozecchini 0:9fca2b23d0ba 45 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 46 }
marcozecchini 0:9fca2b23d0ba 47
marcozecchini 0:9fca2b23d0ba 48 /** Write the value to the output port
marcozecchini 0:9fca2b23d0ba 49 *
marcozecchini 0:9fca2b23d0ba 50 * @param value An integer specifying a bit to write for every corresponding port pin
marcozecchini 0:9fca2b23d0ba 51 */
marcozecchini 0:9fca2b23d0ba 52 void write(int value) {
marcozecchini 0:9fca2b23d0ba 53 port_write(&_port, value);
marcozecchini 0:9fca2b23d0ba 54 }
marcozecchini 0:9fca2b23d0ba 55
marcozecchini 0:9fca2b23d0ba 56 /** Read the value currently output on the port
marcozecchini 0:9fca2b23d0ba 57 *
marcozecchini 0:9fca2b23d0ba 58 * @returns
marcozecchini 0:9fca2b23d0ba 59 * An integer with each bit corresponding to associated port pin setting
marcozecchini 0:9fca2b23d0ba 60 */
marcozecchini 0:9fca2b23d0ba 61 int read() {
marcozecchini 0:9fca2b23d0ba 62 return port_read(&_port);
marcozecchini 0:9fca2b23d0ba 63 }
marcozecchini 0:9fca2b23d0ba 64
marcozecchini 0:9fca2b23d0ba 65 /** Set as an output
marcozecchini 0:9fca2b23d0ba 66 */
marcozecchini 0:9fca2b23d0ba 67 void output() {
marcozecchini 0:9fca2b23d0ba 68 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 69 port_dir(&_port, PIN_OUTPUT);
marcozecchini 0:9fca2b23d0ba 70 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 71 }
marcozecchini 0:9fca2b23d0ba 72
marcozecchini 0:9fca2b23d0ba 73 /** Set as an input
marcozecchini 0:9fca2b23d0ba 74 */
marcozecchini 0:9fca2b23d0ba 75 void input() {
marcozecchini 0:9fca2b23d0ba 76 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 77 port_dir(&_port, PIN_INPUT);
marcozecchini 0:9fca2b23d0ba 78 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 79 }
marcozecchini 0:9fca2b23d0ba 80
marcozecchini 0:9fca2b23d0ba 81 /** Set the input pin mode
marcozecchini 0:9fca2b23d0ba 82 *
marcozecchini 0:9fca2b23d0ba 83 * @param mode PullUp, PullDown, PullNone, OpenDrain
marcozecchini 0:9fca2b23d0ba 84 */
marcozecchini 0:9fca2b23d0ba 85 void mode(PinMode mode) {
marcozecchini 0:9fca2b23d0ba 86 core_util_critical_section_enter();
marcozecchini 0:9fca2b23d0ba 87 port_mode(&_port, mode);
marcozecchini 0:9fca2b23d0ba 88 core_util_critical_section_exit();
marcozecchini 0:9fca2b23d0ba 89 }
marcozecchini 0:9fca2b23d0ba 90
marcozecchini 0:9fca2b23d0ba 91 /** A shorthand for write()
marcozecchini 0:9fca2b23d0ba 92 * \sa PortInOut::write()
marcozecchini 0:9fca2b23d0ba 93 */
marcozecchini 0:9fca2b23d0ba 94 PortInOut& operator= (int value) {
marcozecchini 0:9fca2b23d0ba 95 write(value);
marcozecchini 0:9fca2b23d0ba 96 return *this;
marcozecchini 0:9fca2b23d0ba 97 }
marcozecchini 0:9fca2b23d0ba 98
marcozecchini 0:9fca2b23d0ba 99 /** A shorthand for write()
marcozecchini 0:9fca2b23d0ba 100 * \sa PortInOut::write()
marcozecchini 0:9fca2b23d0ba 101 */
marcozecchini 0:9fca2b23d0ba 102 PortInOut& operator= (PortInOut& rhs) {
marcozecchini 0:9fca2b23d0ba 103 write(rhs.read());
marcozecchini 0:9fca2b23d0ba 104 return *this;
marcozecchini 0:9fca2b23d0ba 105 }
marcozecchini 0:9fca2b23d0ba 106
marcozecchini 0:9fca2b23d0ba 107 /** A shorthand for read()
marcozecchini 0:9fca2b23d0ba 108 * \sa PortInOut::read()
marcozecchini 0:9fca2b23d0ba 109 */
marcozecchini 0:9fca2b23d0ba 110 operator int() {
marcozecchini 0:9fca2b23d0ba 111 return read();
marcozecchini 0:9fca2b23d0ba 112 }
marcozecchini 0:9fca2b23d0ba 113
marcozecchini 0:9fca2b23d0ba 114 private:
marcozecchini 0:9fca2b23d0ba 115 port_t _port;
marcozecchini 0:9fca2b23d0ba 116 };
marcozecchini 0:9fca2b23d0ba 117
marcozecchini 0:9fca2b23d0ba 118 } // namespace mbed
marcozecchini 0:9fca2b23d0ba 119
marcozecchini 0:9fca2b23d0ba 120 #endif
marcozecchini 0:9fca2b23d0ba 121
marcozecchini 0:9fca2b23d0ba 122 #endif