mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
StevieWray
Date:
Wed Sep 28 08:45:18 2016 +0000
Revision:
148:e70627d019e9
Parent:
144:ef7eb2e8f9f7
Fixed DAC output on STM32F3 boards to allow 3 outputs

Who changed what in which revision?

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