Initial commit
mbed-dev-master/drivers/AnalogOut.h@0:bb348c97df44, 2020-09-16 (annotated)
- Committer:
- lypinator
- Date:
- Wed Sep 16 01:11:49 2020 +0000
- Revision:
- 0:bb348c97df44
Added PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lypinator | 0:bb348c97df44 | 1 | /* mbed Microcontroller Library |
lypinator | 0:bb348c97df44 | 2 | * Copyright (c) 2006-2013 ARM Limited |
lypinator | 0:bb348c97df44 | 3 | * |
lypinator | 0:bb348c97df44 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
lypinator | 0:bb348c97df44 | 5 | * you may not use this file except in compliance with the License. |
lypinator | 0:bb348c97df44 | 6 | * You may obtain a copy of the License at |
lypinator | 0:bb348c97df44 | 7 | * |
lypinator | 0:bb348c97df44 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
lypinator | 0:bb348c97df44 | 9 | * |
lypinator | 0:bb348c97df44 | 10 | * Unless required by applicable law or agreed to in writing, software |
lypinator | 0:bb348c97df44 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
lypinator | 0:bb348c97df44 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
lypinator | 0:bb348c97df44 | 13 | * See the License for the specific language governing permissions and |
lypinator | 0:bb348c97df44 | 14 | * limitations under the License. |
lypinator | 0:bb348c97df44 | 15 | */ |
lypinator | 0:bb348c97df44 | 16 | #ifndef MBED_ANALOGOUT_H |
lypinator | 0:bb348c97df44 | 17 | #define MBED_ANALOGOUT_H |
lypinator | 0:bb348c97df44 | 18 | |
lypinator | 0:bb348c97df44 | 19 | #include "platform/platform.h" |
lypinator | 0:bb348c97df44 | 20 | |
lypinator | 0:bb348c97df44 | 21 | #if defined (DEVICE_ANALOGOUT) || defined(DOXYGEN_ONLY) |
lypinator | 0:bb348c97df44 | 22 | |
lypinator | 0:bb348c97df44 | 23 | #include "hal/analogout_api.h" |
lypinator | 0:bb348c97df44 | 24 | #include "platform/PlatformMutex.h" |
lypinator | 0:bb348c97df44 | 25 | |
lypinator | 0:bb348c97df44 | 26 | namespace mbed { |
lypinator | 0:bb348c97df44 | 27 | /** \addtogroup drivers */ |
lypinator | 0:bb348c97df44 | 28 | |
lypinator | 0:bb348c97df44 | 29 | /** An analog output, used for setting the voltage on a pin |
lypinator | 0:bb348c97df44 | 30 | * |
lypinator | 0:bb348c97df44 | 31 | * @note Synchronization level: Thread safe |
lypinator | 0:bb348c97df44 | 32 | * |
lypinator | 0:bb348c97df44 | 33 | * Example: |
lypinator | 0:bb348c97df44 | 34 | * @code |
lypinator | 0:bb348c97df44 | 35 | * // Make a sawtooth output |
lypinator | 0:bb348c97df44 | 36 | * |
lypinator | 0:bb348c97df44 | 37 | * #include "mbed.h" |
lypinator | 0:bb348c97df44 | 38 | * |
lypinator | 0:bb348c97df44 | 39 | * AnalogOut tri(p18); |
lypinator | 0:bb348c97df44 | 40 | * int main() { |
lypinator | 0:bb348c97df44 | 41 | * while(1) { |
lypinator | 0:bb348c97df44 | 42 | * tri = tri + 0.01; |
lypinator | 0:bb348c97df44 | 43 | * wait_us(1); |
lypinator | 0:bb348c97df44 | 44 | * if(tri == 1) { |
lypinator | 0:bb348c97df44 | 45 | * tri = 0; |
lypinator | 0:bb348c97df44 | 46 | * } |
lypinator | 0:bb348c97df44 | 47 | * } |
lypinator | 0:bb348c97df44 | 48 | * } |
lypinator | 0:bb348c97df44 | 49 | * @endcode |
lypinator | 0:bb348c97df44 | 50 | * @ingroup drivers |
lypinator | 0:bb348c97df44 | 51 | */ |
lypinator | 0:bb348c97df44 | 52 | class AnalogOut { |
lypinator | 0:bb348c97df44 | 53 | |
lypinator | 0:bb348c97df44 | 54 | public: |
lypinator | 0:bb348c97df44 | 55 | |
lypinator | 0:bb348c97df44 | 56 | /** Create an AnalogOut connected to the specified pin |
lypinator | 0:bb348c97df44 | 57 | * |
lypinator | 0:bb348c97df44 | 58 | * @param pin AnalogOut pin to connect to |
lypinator | 0:bb348c97df44 | 59 | */ |
lypinator | 0:bb348c97df44 | 60 | AnalogOut(PinName pin) |
lypinator | 0:bb348c97df44 | 61 | { |
lypinator | 0:bb348c97df44 | 62 | analogout_init(&_dac, pin); |
lypinator | 0:bb348c97df44 | 63 | } |
lypinator | 0:bb348c97df44 | 64 | |
lypinator | 0:bb348c97df44 | 65 | /** Set the output voltage, specified as a percentage (float) |
lypinator | 0:bb348c97df44 | 66 | * |
lypinator | 0:bb348c97df44 | 67 | * @param value A floating-point value representing the output voltage, |
lypinator | 0:bb348c97df44 | 68 | * specified as a percentage. The value should lie between |
lypinator | 0:bb348c97df44 | 69 | * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). |
lypinator | 0:bb348c97df44 | 70 | * Values outside this range will be saturated to 0.0f or 1.0f. |
lypinator | 0:bb348c97df44 | 71 | */ |
lypinator | 0:bb348c97df44 | 72 | void write(float value) |
lypinator | 0:bb348c97df44 | 73 | { |
lypinator | 0:bb348c97df44 | 74 | lock(); |
lypinator | 0:bb348c97df44 | 75 | analogout_write(&_dac, value); |
lypinator | 0:bb348c97df44 | 76 | unlock(); |
lypinator | 0:bb348c97df44 | 77 | } |
lypinator | 0:bb348c97df44 | 78 | |
lypinator | 0:bb348c97df44 | 79 | /** Set the output voltage, represented as an unsigned short in the range [0x0, 0xFFFF] |
lypinator | 0:bb348c97df44 | 80 | * |
lypinator | 0:bb348c97df44 | 81 | * @param value 16-bit unsigned short representing the output voltage, |
lypinator | 0:bb348c97df44 | 82 | * normalised to a 16-bit value (0x0000 = 0v, 0xFFFF = 3.3v) |
lypinator | 0:bb348c97df44 | 83 | */ |
lypinator | 0:bb348c97df44 | 84 | void write_u16(unsigned short value) |
lypinator | 0:bb348c97df44 | 85 | { |
lypinator | 0:bb348c97df44 | 86 | lock(); |
lypinator | 0:bb348c97df44 | 87 | analogout_write_u16(&_dac, value); |
lypinator | 0:bb348c97df44 | 88 | unlock(); |
lypinator | 0:bb348c97df44 | 89 | } |
lypinator | 0:bb348c97df44 | 90 | |
lypinator | 0:bb348c97df44 | 91 | /** Return the current output voltage setting, measured as a percentage (float) |
lypinator | 0:bb348c97df44 | 92 | * |
lypinator | 0:bb348c97df44 | 93 | * @returns |
lypinator | 0:bb348c97df44 | 94 | * A floating-point value representing the current voltage being output on the pin, |
lypinator | 0:bb348c97df44 | 95 | * measured as a percentage. The returned value will lie between |
lypinator | 0:bb348c97df44 | 96 | * 0.0f (representing 0v / 0%) and 1.0f (representing 3.3v / 100%). |
lypinator | 0:bb348c97df44 | 97 | * |
lypinator | 0:bb348c97df44 | 98 | * @note |
lypinator | 0:bb348c97df44 | 99 | * This value may not match exactly the value set by a previous write(). |
lypinator | 0:bb348c97df44 | 100 | */ |
lypinator | 0:bb348c97df44 | 101 | float read() |
lypinator | 0:bb348c97df44 | 102 | { |
lypinator | 0:bb348c97df44 | 103 | lock(); |
lypinator | 0:bb348c97df44 | 104 | float ret = analogout_read(&_dac); |
lypinator | 0:bb348c97df44 | 105 | unlock(); |
lypinator | 0:bb348c97df44 | 106 | return ret; |
lypinator | 0:bb348c97df44 | 107 | } |
lypinator | 0:bb348c97df44 | 108 | |
lypinator | 0:bb348c97df44 | 109 | /** An operator shorthand for write() |
lypinator | 0:bb348c97df44 | 110 | * \sa AnalogOut::write() |
lypinator | 0:bb348c97df44 | 111 | */ |
lypinator | 0:bb348c97df44 | 112 | AnalogOut &operator= (float percent) |
lypinator | 0:bb348c97df44 | 113 | { |
lypinator | 0:bb348c97df44 | 114 | // Underlying write call is thread safe |
lypinator | 0:bb348c97df44 | 115 | write(percent); |
lypinator | 0:bb348c97df44 | 116 | return *this; |
lypinator | 0:bb348c97df44 | 117 | } |
lypinator | 0:bb348c97df44 | 118 | |
lypinator | 0:bb348c97df44 | 119 | /** An operator shorthand for write() |
lypinator | 0:bb348c97df44 | 120 | * \sa AnalogOut::write() |
lypinator | 0:bb348c97df44 | 121 | */ |
lypinator | 0:bb348c97df44 | 122 | AnalogOut &operator= (AnalogOut &rhs) |
lypinator | 0:bb348c97df44 | 123 | { |
lypinator | 0:bb348c97df44 | 124 | // Underlying write call is thread safe |
lypinator | 0:bb348c97df44 | 125 | write(rhs.read()); |
lypinator | 0:bb348c97df44 | 126 | return *this; |
lypinator | 0:bb348c97df44 | 127 | } |
lypinator | 0:bb348c97df44 | 128 | |
lypinator | 0:bb348c97df44 | 129 | /** An operator shorthand for read() |
lypinator | 0:bb348c97df44 | 130 | * \sa AnalogOut::read() |
lypinator | 0:bb348c97df44 | 131 | */ |
lypinator | 0:bb348c97df44 | 132 | operator float() |
lypinator | 0:bb348c97df44 | 133 | { |
lypinator | 0:bb348c97df44 | 134 | // Underlying read call is thread safe |
lypinator | 0:bb348c97df44 | 135 | return read(); |
lypinator | 0:bb348c97df44 | 136 | } |
lypinator | 0:bb348c97df44 | 137 | |
lypinator | 0:bb348c97df44 | 138 | virtual ~AnalogOut() |
lypinator | 0:bb348c97df44 | 139 | { |
lypinator | 0:bb348c97df44 | 140 | // Do nothing |
lypinator | 0:bb348c97df44 | 141 | } |
lypinator | 0:bb348c97df44 | 142 | |
lypinator | 0:bb348c97df44 | 143 | protected: |
lypinator | 0:bb348c97df44 | 144 | |
lypinator | 0:bb348c97df44 | 145 | virtual void lock() |
lypinator | 0:bb348c97df44 | 146 | { |
lypinator | 0:bb348c97df44 | 147 | _mutex.lock(); |
lypinator | 0:bb348c97df44 | 148 | } |
lypinator | 0:bb348c97df44 | 149 | |
lypinator | 0:bb348c97df44 | 150 | virtual void unlock() |
lypinator | 0:bb348c97df44 | 151 | { |
lypinator | 0:bb348c97df44 | 152 | _mutex.unlock(); |
lypinator | 0:bb348c97df44 | 153 | } |
lypinator | 0:bb348c97df44 | 154 | |
lypinator | 0:bb348c97df44 | 155 | dac_t _dac; |
lypinator | 0:bb348c97df44 | 156 | PlatformMutex _mutex; |
lypinator | 0:bb348c97df44 | 157 | }; |
lypinator | 0:bb348c97df44 | 158 | |
lypinator | 0:bb348c97df44 | 159 | } // namespace mbed |
lypinator | 0:bb348c97df44 | 160 | |
lypinator | 0:bb348c97df44 | 161 | #endif |
lypinator | 0:bb348c97df44 | 162 | |
lypinator | 0:bb348c97df44 | 163 | #endif |