Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

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_PWMOUT_H
nexpaq 0:6c56fb4bc5f0 17 #define MBED_PWMOUT_H
nexpaq 0:6c56fb4bc5f0 18
nexpaq 0:6c56fb4bc5f0 19 #include "platform.h"
nexpaq 0:6c56fb4bc5f0 20
nexpaq 0:6c56fb4bc5f0 21 #if DEVICE_PWMOUT
nexpaq 0:6c56fb4bc5f0 22 #include "pwmout_api.h"
nexpaq 0:6c56fb4bc5f0 23 #include "critical.h"
nexpaq 0:6c56fb4bc5f0 24
nexpaq 0:6c56fb4bc5f0 25 namespace mbed {
nexpaq 0:6c56fb4bc5f0 26
nexpaq 0:6c56fb4bc5f0 27 /** A pulse-width modulation digital output
nexpaq 0:6c56fb4bc5f0 28 *
nexpaq 0:6c56fb4bc5f0 29 * @Note Synchronization level: Interrupt safe
nexpaq 0:6c56fb4bc5f0 30 *
nexpaq 0:6c56fb4bc5f0 31 * Example
nexpaq 0:6c56fb4bc5f0 32 * @code
nexpaq 0:6c56fb4bc5f0 33 * // Fade a led on.
nexpaq 0:6c56fb4bc5f0 34 * #include "mbed.h"
nexpaq 0:6c56fb4bc5f0 35 *
nexpaq 0:6c56fb4bc5f0 36 * PwmOut led(LED1);
nexpaq 0:6c56fb4bc5f0 37 *
nexpaq 0:6c56fb4bc5f0 38 * int main() {
nexpaq 0:6c56fb4bc5f0 39 * while(1) {
nexpaq 0:6c56fb4bc5f0 40 * led = led + 0.01;
nexpaq 0:6c56fb4bc5f0 41 * wait(0.2);
nexpaq 0:6c56fb4bc5f0 42 * if(led == 1.0) {
nexpaq 0:6c56fb4bc5f0 43 * led = 0;
nexpaq 0:6c56fb4bc5f0 44 * }
nexpaq 0:6c56fb4bc5f0 45 * }
nexpaq 0:6c56fb4bc5f0 46 * }
nexpaq 0:6c56fb4bc5f0 47 * @endcode
nexpaq 0:6c56fb4bc5f0 48 *
nexpaq 0:6c56fb4bc5f0 49 * @note
nexpaq 0:6c56fb4bc5f0 50 * On the LPC1768 and LPC2368, the PWMs all share the same
nexpaq 0:6c56fb4bc5f0 51 * period - if you change the period for one, you change it for all.
nexpaq 0:6c56fb4bc5f0 52 * Although routines that change the period maintain the duty cycle
nexpaq 0:6c56fb4bc5f0 53 * for its PWM, all other PWMs will require their duty cycle to be
nexpaq 0:6c56fb4bc5f0 54 * refreshed.
nexpaq 0:6c56fb4bc5f0 55 */
nexpaq 0:6c56fb4bc5f0 56 class PwmOut {
nexpaq 0:6c56fb4bc5f0 57
nexpaq 0:6c56fb4bc5f0 58 public:
nexpaq 0:6c56fb4bc5f0 59
nexpaq 0:6c56fb4bc5f0 60 /** Create a PwmOut connected to the specified pin
nexpaq 0:6c56fb4bc5f0 61 *
nexpaq 0:6c56fb4bc5f0 62 * @param pin PwmOut pin to connect to
nexpaq 0:6c56fb4bc5f0 63 */
nexpaq 0:6c56fb4bc5f0 64 PwmOut(PinName pin) {
nexpaq 0:6c56fb4bc5f0 65 core_util_critical_section_enter();
nexpaq 0:6c56fb4bc5f0 66 pwmout_init(&_pwm, pin);
nexpaq 0:6c56fb4bc5f0 67 core_util_critical_section_exit();
nexpaq 0:6c56fb4bc5f0 68 }
nexpaq 0:6c56fb4bc5f0 69
nexpaq 0:6c56fb4bc5f0 70 /** Set the ouput duty-cycle, specified as a percentage (float)
nexpaq 0:6c56fb4bc5f0 71 *
nexpaq 0:6c56fb4bc5f0 72 * @param value A floating-point value representing the output duty-cycle,
nexpaq 0:6c56fb4bc5f0 73 * specified as a percentage. The value should lie between
nexpaq 0:6c56fb4bc5f0 74 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
nexpaq 0:6c56fb4bc5f0 75 * Values outside this range will be saturated to 0.0f or 1.0f.
nexpaq 0:6c56fb4bc5f0 76 */
nexpaq 0:6c56fb4bc5f0 77 void write(float value) {
nexpaq 0:6c56fb4bc5f0 78 core_util_critical_section_enter();
nexpaq 0:6c56fb4bc5f0 79 pwmout_write(&_pwm, value);
nexpaq 0:6c56fb4bc5f0 80 core_util_critical_section_exit();
nexpaq 0:6c56fb4bc5f0 81 }
nexpaq 0:6c56fb4bc5f0 82
nexpaq 0:6c56fb4bc5f0 83 /** Return the current output duty-cycle setting, measured as a percentage (float)
nexpaq 0:6c56fb4bc5f0 84 *
nexpaq 0:6c56fb4bc5f0 85 * @returns
nexpaq 0:6c56fb4bc5f0 86 * A floating-point value representing the current duty-cycle being output on the pin,
nexpaq 0:6c56fb4bc5f0 87 * measured as a percentage. The returned value will lie between
nexpaq 0:6c56fb4bc5f0 88 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
nexpaq 0:6c56fb4bc5f0 89 *
nexpaq 0:6c56fb4bc5f0 90 * @note
nexpaq 0:6c56fb4bc5f0 91 * This value may not match exactly the value set by a previous <write>.
nexpaq 0:6c56fb4bc5f0 92 */
nexpaq 0:6c56fb4bc5f0 93 float read() {
nexpaq 0:6c56fb4bc5f0 94 core_util_critical_section_enter();
nexpaq 0:6c56fb4bc5f0 95 float val = pwmout_read(&_pwm);
nexpaq 0:6c56fb4bc5f0 96 core_util_critical_section_exit();
nexpaq 0:6c56fb4bc5f0 97 return val;
nexpaq 0:6c56fb4bc5f0 98 }
nexpaq 0:6c56fb4bc5f0 99
nexpaq 0:6c56fb4bc5f0 100 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
nexpaq 0:6c56fb4bc5f0 101 *
nexpaq 0:6c56fb4bc5f0 102 * @note
nexpaq 0:6c56fb4bc5f0 103 * The resolution is currently in microseconds; periods smaller than this
nexpaq 0:6c56fb4bc5f0 104 * will be set to zero.
nexpaq 0:6c56fb4bc5f0 105 */
nexpaq 0:6c56fb4bc5f0 106 void period(float seconds) {
nexpaq 0:6c56fb4bc5f0 107 core_util_critical_section_enter();
nexpaq 0:6c56fb4bc5f0 108 pwmout_period(&_pwm, seconds);
nexpaq 0:6c56fb4bc5f0 109 core_util_critical_section_exit();
nexpaq 0:6c56fb4bc5f0 110 }
nexpaq 0:6c56fb4bc5f0 111
nexpaq 0:6c56fb4bc5f0 112 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
nexpaq 0:6c56fb4bc5f0 113 */
nexpaq 0:6c56fb4bc5f0 114 void period_ms(int ms) {
nexpaq 0:6c56fb4bc5f0 115 core_util_critical_section_enter();
nexpaq 0:6c56fb4bc5f0 116 pwmout_period_ms(&_pwm, ms);
nexpaq 0:6c56fb4bc5f0 117 core_util_critical_section_exit();
nexpaq 0:6c56fb4bc5f0 118 }
nexpaq 0:6c56fb4bc5f0 119
nexpaq 0:6c56fb4bc5f0 120 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
nexpaq 0:6c56fb4bc5f0 121 */
nexpaq 0:6c56fb4bc5f0 122 void period_us(int us) {
nexpaq 0:6c56fb4bc5f0 123 core_util_critical_section_enter();
nexpaq 0:6c56fb4bc5f0 124 pwmout_period_us(&_pwm, us);
nexpaq 0:6c56fb4bc5f0 125 core_util_critical_section_exit();
nexpaq 0:6c56fb4bc5f0 126 }
nexpaq 0:6c56fb4bc5f0 127
nexpaq 0:6c56fb4bc5f0 128 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
nexpaq 0:6c56fb4bc5f0 129 */
nexpaq 0:6c56fb4bc5f0 130 void pulsewidth(float seconds) {
nexpaq 0:6c56fb4bc5f0 131 core_util_critical_section_enter();
nexpaq 0:6c56fb4bc5f0 132 pwmout_pulsewidth(&_pwm, seconds);
nexpaq 0:6c56fb4bc5f0 133 core_util_critical_section_exit();
nexpaq 0:6c56fb4bc5f0 134 }
nexpaq 0:6c56fb4bc5f0 135
nexpaq 0:6c56fb4bc5f0 136 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
nexpaq 0:6c56fb4bc5f0 137 */
nexpaq 0:6c56fb4bc5f0 138 void pulsewidth_ms(int ms) {
nexpaq 0:6c56fb4bc5f0 139 core_util_critical_section_enter();
nexpaq 0:6c56fb4bc5f0 140 pwmout_pulsewidth_ms(&_pwm, ms);
nexpaq 0:6c56fb4bc5f0 141 core_util_critical_section_exit();
nexpaq 0:6c56fb4bc5f0 142 }
nexpaq 0:6c56fb4bc5f0 143
nexpaq 0:6c56fb4bc5f0 144 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
nexpaq 0:6c56fb4bc5f0 145 */
nexpaq 0:6c56fb4bc5f0 146 void pulsewidth_us(int us) {
nexpaq 0:6c56fb4bc5f0 147 core_util_critical_section_enter();
nexpaq 0:6c56fb4bc5f0 148 pwmout_pulsewidth_us(&_pwm, us);
nexpaq 0:6c56fb4bc5f0 149 core_util_critical_section_exit();
nexpaq 0:6c56fb4bc5f0 150 }
nexpaq 0:6c56fb4bc5f0 151
nexpaq 0:6c56fb4bc5f0 152 /** A operator shorthand for write()
nexpaq 0:6c56fb4bc5f0 153 */
nexpaq 0:6c56fb4bc5f0 154 PwmOut& operator= (float value) {
nexpaq 0:6c56fb4bc5f0 155 // Underlying call is thread safe
nexpaq 0:6c56fb4bc5f0 156 write(value);
nexpaq 0:6c56fb4bc5f0 157 return *this;
nexpaq 0:6c56fb4bc5f0 158 }
nexpaq 0:6c56fb4bc5f0 159
nexpaq 0:6c56fb4bc5f0 160 PwmOut& operator= (PwmOut& rhs) {
nexpaq 0:6c56fb4bc5f0 161 // Underlying call is thread safe
nexpaq 0:6c56fb4bc5f0 162 write(rhs.read());
nexpaq 0:6c56fb4bc5f0 163 return *this;
nexpaq 0:6c56fb4bc5f0 164 }
nexpaq 0:6c56fb4bc5f0 165
nexpaq 0:6c56fb4bc5f0 166 /** An operator shorthand for read()
nexpaq 0:6c56fb4bc5f0 167 */
nexpaq 0:6c56fb4bc5f0 168 operator float() {
nexpaq 0:6c56fb4bc5f0 169 // Underlying call is thread safe
nexpaq 0:6c56fb4bc5f0 170 return read();
nexpaq 0:6c56fb4bc5f0 171 }
nexpaq 0:6c56fb4bc5f0 172
nexpaq 0:6c56fb4bc5f0 173 protected:
nexpaq 0:6c56fb4bc5f0 174 pwmout_t _pwm;
nexpaq 0:6c56fb4bc5f0 175 };
nexpaq 0:6c56fb4bc5f0 176
nexpaq 0:6c56fb4bc5f0 177 } // namespace mbed
nexpaq 0:6c56fb4bc5f0 178
nexpaq 0:6c56fb4bc5f0 179 #endif
nexpaq 0:6c56fb4bc5f0 180
nexpaq 0:6c56fb4bc5f0 181 #endif