mbed-os
Fork of mbed-os by
drivers/PortOut.h@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2006-2013 ARM Limited |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | #ifndef MBED_PORTOUT_H |
elessair | 0:f269e3021894 | 17 | #define MBED_PORTOUT_H |
elessair | 0:f269e3021894 | 18 | |
elessair | 0:f269e3021894 | 19 | #include "platform/platform.h" |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | #if DEVICE_PORTOUT |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | #include "hal/port_api.h" |
elessair | 0:f269e3021894 | 24 | #include "platform/critical.h" |
elessair | 0:f269e3021894 | 25 | |
elessair | 0:f269e3021894 | 26 | namespace mbed { |
elessair | 0:f269e3021894 | 27 | /** \addtogroup drivers */ |
elessair | 0:f269e3021894 | 28 | /** @{*/ |
elessair | 0:f269e3021894 | 29 | /** A multiple pin digital out |
elessair | 0:f269e3021894 | 30 | * |
elessair | 0:f269e3021894 | 31 | * @Note Synchronization level: Interrupt safe |
elessair | 0:f269e3021894 | 32 | * |
elessair | 0:f269e3021894 | 33 | * Example: |
elessair | 0:f269e3021894 | 34 | * @code |
elessair | 0:f269e3021894 | 35 | * // Toggle all four LEDs |
elessair | 0:f269e3021894 | 36 | * |
elessair | 0:f269e3021894 | 37 | * #include "mbed.h" |
elessair | 0:f269e3021894 | 38 | * |
elessair | 0:f269e3021894 | 39 | * // LED1 = P1.18 LED2 = P1.20 LED3 = P1.21 LED4 = P1.23 |
elessair | 0:f269e3021894 | 40 | * #define LED_MASK 0x00B40000 |
elessair | 0:f269e3021894 | 41 | * |
elessair | 0:f269e3021894 | 42 | * PortOut ledport(Port1, LED_MASK); |
elessair | 0:f269e3021894 | 43 | * |
elessair | 0:f269e3021894 | 44 | * int main() { |
elessair | 0:f269e3021894 | 45 | * while(1) { |
elessair | 0:f269e3021894 | 46 | * ledport = LED_MASK; |
elessair | 0:f269e3021894 | 47 | * wait(1); |
elessair | 0:f269e3021894 | 48 | * ledport = 0; |
elessair | 0:f269e3021894 | 49 | * wait(1); |
elessair | 0:f269e3021894 | 50 | * } |
elessair | 0:f269e3021894 | 51 | * } |
elessair | 0:f269e3021894 | 52 | * @endcode |
elessair | 0:f269e3021894 | 53 | */ |
elessair | 0:f269e3021894 | 54 | class PortOut { |
elessair | 0:f269e3021894 | 55 | public: |
elessair | 0:f269e3021894 | 56 | |
elessair | 0:f269e3021894 | 57 | /** Create an PortOut, connected to the specified port |
elessair | 0:f269e3021894 | 58 | * |
elessair | 0:f269e3021894 | 59 | * @param port Port to connect to (Port0-Port5) |
elessair | 0:f269e3021894 | 60 | * @param mask A bitmask to identify which bits in the port should be included (0 - ignore) |
elessair | 0:f269e3021894 | 61 | */ |
elessair | 0:f269e3021894 | 62 | PortOut(PortName port, int mask = 0xFFFFFFFF) { |
elessair | 0:f269e3021894 | 63 | core_util_critical_section_enter(); |
elessair | 0:f269e3021894 | 64 | port_init(&_port, port, mask, PIN_OUTPUT); |
elessair | 0:f269e3021894 | 65 | core_util_critical_section_exit(); |
elessair | 0:f269e3021894 | 66 | } |
elessair | 0:f269e3021894 | 67 | |
elessair | 0:f269e3021894 | 68 | /** Write the value to the output port |
elessair | 0:f269e3021894 | 69 | * |
elessair | 0:f269e3021894 | 70 | * @param value An integer specifying a bit to write for every corresponding PortOut pin |
elessair | 0:f269e3021894 | 71 | */ |
elessair | 0:f269e3021894 | 72 | void write(int value) { |
elessair | 0:f269e3021894 | 73 | port_write(&_port, value); |
elessair | 0:f269e3021894 | 74 | } |
elessair | 0:f269e3021894 | 75 | |
elessair | 0:f269e3021894 | 76 | /** Read the value currently output on the port |
elessair | 0:f269e3021894 | 77 | * |
elessair | 0:f269e3021894 | 78 | * @returns |
elessair | 0:f269e3021894 | 79 | * An integer with each bit corresponding to associated PortOut pin setting |
elessair | 0:f269e3021894 | 80 | */ |
elessair | 0:f269e3021894 | 81 | int read() { |
elessair | 0:f269e3021894 | 82 | return port_read(&_port); |
elessair | 0:f269e3021894 | 83 | } |
elessair | 0:f269e3021894 | 84 | |
elessair | 0:f269e3021894 | 85 | /** A shorthand for write() |
elessair | 0:f269e3021894 | 86 | */ |
elessair | 0:f269e3021894 | 87 | PortOut& operator= (int value) { |
elessair | 0:f269e3021894 | 88 | write(value); |
elessair | 0:f269e3021894 | 89 | return *this; |
elessair | 0:f269e3021894 | 90 | } |
elessair | 0:f269e3021894 | 91 | |
elessair | 0:f269e3021894 | 92 | PortOut& operator= (PortOut& rhs) { |
elessair | 0:f269e3021894 | 93 | write(rhs.read()); |
elessair | 0:f269e3021894 | 94 | return *this; |
elessair | 0:f269e3021894 | 95 | } |
elessair | 0:f269e3021894 | 96 | |
elessair | 0:f269e3021894 | 97 | /** A shorthand for read() |
elessair | 0:f269e3021894 | 98 | */ |
elessair | 0:f269e3021894 | 99 | operator int() { |
elessair | 0:f269e3021894 | 100 | return read(); |
elessair | 0:f269e3021894 | 101 | } |
elessair | 0:f269e3021894 | 102 | |
elessair | 0:f269e3021894 | 103 | private: |
elessair | 0:f269e3021894 | 104 | port_t _port; |
elessair | 0:f269e3021894 | 105 | }; |
elessair | 0:f269e3021894 | 106 | |
elessair | 0:f269e3021894 | 107 | } // namespace mbed |
elessair | 0:f269e3021894 | 108 | |
elessair | 0:f269e3021894 | 109 | #endif |
elessair | 0:f269e3021894 | 110 | |
elessair | 0:f269e3021894 | 111 | #endif |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | /** @}*/ |