Fork of mbed for KL05Z based smart sensor which has no crystal on the board, so MCG FEI mode is required.

Dependents:   smart-sensor-KL05Z-debug

Fork of mbed-src by Ermanno Brusadin

Committer:
r14793
Date:
Mon Jan 29 15:38:37 2018 +0000
Revision:
1:da408b460382
Parent:
0:0a673c671a56
changed KL05 MCG mode to FEI for smart sensor boards (no crystal)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ebrus 0:0a673c671a56 1 /* mbed Microcontroller Library
ebrus 0:0a673c671a56 2 * Copyright (c) 2006-2013 ARM Limited
ebrus 0:0a673c671a56 3 *
ebrus 0:0a673c671a56 4 * Licensed under the Apache License, Version 2.0 (the "License");
ebrus 0:0a673c671a56 5 * you may not use this file except in compliance with the License.
ebrus 0:0a673c671a56 6 * You may obtain a copy of the License at
ebrus 0:0a673c671a56 7 *
ebrus 0:0a673c671a56 8 * http://www.apache.org/licenses/LICENSE-2.0
ebrus 0:0a673c671a56 9 *
ebrus 0:0a673c671a56 10 * Unless required by applicable law or agreed to in writing, software
ebrus 0:0a673c671a56 11 * distributed under the License is distributed on an "AS IS" BASIS,
ebrus 0:0a673c671a56 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ebrus 0:0a673c671a56 13 * See the License for the specific language governing permissions and
ebrus 0:0a673c671a56 14 * limitations under the License.
ebrus 0:0a673c671a56 15 */
ebrus 0:0a673c671a56 16 #ifndef MBED_PWMOUT_H
ebrus 0:0a673c671a56 17 #define MBED_PWMOUT_H
ebrus 0:0a673c671a56 18
ebrus 0:0a673c671a56 19 #include "platform.h"
ebrus 0:0a673c671a56 20
ebrus 0:0a673c671a56 21 #if DEVICE_PWMOUT
ebrus 0:0a673c671a56 22 #include "pwmout_api.h"
ebrus 0:0a673c671a56 23
ebrus 0:0a673c671a56 24 namespace mbed {
ebrus 0:0a673c671a56 25
ebrus 0:0a673c671a56 26 /** A pulse-width modulation digital output
ebrus 0:0a673c671a56 27 *
ebrus 0:0a673c671a56 28 * Example
ebrus 0:0a673c671a56 29 * @code
ebrus 0:0a673c671a56 30 * // Fade a led on.
ebrus 0:0a673c671a56 31 * #include "mbed.h"
ebrus 0:0a673c671a56 32 *
ebrus 0:0a673c671a56 33 * PwmOut led(LED1);
ebrus 0:0a673c671a56 34 *
ebrus 0:0a673c671a56 35 * int main() {
ebrus 0:0a673c671a56 36 * while(1) {
ebrus 0:0a673c671a56 37 * led = led + 0.01;
ebrus 0:0a673c671a56 38 * wait(0.2);
ebrus 0:0a673c671a56 39 * if(led == 1.0) {
ebrus 0:0a673c671a56 40 * led = 0;
ebrus 0:0a673c671a56 41 * }
ebrus 0:0a673c671a56 42 * }
ebrus 0:0a673c671a56 43 * }
ebrus 0:0a673c671a56 44 * @endcode
ebrus 0:0a673c671a56 45 *
ebrus 0:0a673c671a56 46 * @note
ebrus 0:0a673c671a56 47 * On the LPC1768 and LPC2368, the PWMs all share the same
ebrus 0:0a673c671a56 48 * period - if you change the period for one, you change it for all.
ebrus 0:0a673c671a56 49 * Although routines that change the period maintain the duty cycle
ebrus 0:0a673c671a56 50 * for its PWM, all other PWMs will require their duty cycle to be
ebrus 0:0a673c671a56 51 * refreshed.
ebrus 0:0a673c671a56 52 */
ebrus 0:0a673c671a56 53 class PwmOut {
ebrus 0:0a673c671a56 54
ebrus 0:0a673c671a56 55 public:
ebrus 0:0a673c671a56 56
ebrus 0:0a673c671a56 57 /** Create a PwmOut connected to the specified pin
ebrus 0:0a673c671a56 58 *
ebrus 0:0a673c671a56 59 * @param pin PwmOut pin to connect to
ebrus 0:0a673c671a56 60 */
ebrus 0:0a673c671a56 61 PwmOut(PinName pin) {
ebrus 0:0a673c671a56 62 pwmout_init(&_pwm, pin);
ebrus 0:0a673c671a56 63 }
ebrus 0:0a673c671a56 64
ebrus 0:0a673c671a56 65 /** Set the ouput duty-cycle, specified as a percentage (float)
ebrus 0:0a673c671a56 66 *
ebrus 0:0a673c671a56 67 * @param value A floating-point value representing the output duty-cycle,
ebrus 0:0a673c671a56 68 * specified as a percentage. The value should lie between
ebrus 0:0a673c671a56 69 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
ebrus 0:0a673c671a56 70 * Values outside this range will be saturated to 0.0f or 1.0f.
ebrus 0:0a673c671a56 71 */
ebrus 0:0a673c671a56 72 void write(float value) {
ebrus 0:0a673c671a56 73 pwmout_write(&_pwm, value);
ebrus 0:0a673c671a56 74 }
ebrus 0:0a673c671a56 75
ebrus 0:0a673c671a56 76 /** Return the current output duty-cycle setting, measured as a percentage (float)
ebrus 0:0a673c671a56 77 *
ebrus 0:0a673c671a56 78 * @returns
ebrus 0:0a673c671a56 79 * A floating-point value representing the current duty-cycle being output on the pin,
ebrus 0:0a673c671a56 80 * measured as a percentage. The returned value will lie between
ebrus 0:0a673c671a56 81 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
ebrus 0:0a673c671a56 82 *
ebrus 0:0a673c671a56 83 * @note
ebrus 0:0a673c671a56 84 * This value may not match exactly the value set by a previous <write>.
ebrus 0:0a673c671a56 85 */
ebrus 0:0a673c671a56 86 float read() {
ebrus 0:0a673c671a56 87 return pwmout_read(&_pwm);
ebrus 0:0a673c671a56 88 }
ebrus 0:0a673c671a56 89
ebrus 0:0a673c671a56 90 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
ebrus 0:0a673c671a56 91 *
ebrus 0:0a673c671a56 92 * @note
ebrus 0:0a673c671a56 93 * The resolution is currently in microseconds; periods smaller than this
ebrus 0:0a673c671a56 94 * will be set to zero.
ebrus 0:0a673c671a56 95 */
ebrus 0:0a673c671a56 96 void period(float seconds) {
ebrus 0:0a673c671a56 97 pwmout_period(&_pwm, seconds);
ebrus 0:0a673c671a56 98 }
ebrus 0:0a673c671a56 99
ebrus 0:0a673c671a56 100 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
ebrus 0:0a673c671a56 101 */
ebrus 0:0a673c671a56 102 void period_ms(int ms) {
ebrus 0:0a673c671a56 103 pwmout_period_ms(&_pwm, ms);
ebrus 0:0a673c671a56 104 }
ebrus 0:0a673c671a56 105
ebrus 0:0a673c671a56 106 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
ebrus 0:0a673c671a56 107 */
ebrus 0:0a673c671a56 108 void period_us(int us) {
ebrus 0:0a673c671a56 109 pwmout_period_us(&_pwm, us);
ebrus 0:0a673c671a56 110 }
ebrus 0:0a673c671a56 111
ebrus 0:0a673c671a56 112 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
ebrus 0:0a673c671a56 113 */
ebrus 0:0a673c671a56 114 void pulsewidth(float seconds) {
ebrus 0:0a673c671a56 115 pwmout_pulsewidth(&_pwm, seconds);
ebrus 0:0a673c671a56 116 }
ebrus 0:0a673c671a56 117
ebrus 0:0a673c671a56 118 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
ebrus 0:0a673c671a56 119 */
ebrus 0:0a673c671a56 120 void pulsewidth_ms(int ms) {
ebrus 0:0a673c671a56 121 pwmout_pulsewidth_ms(&_pwm, ms);
ebrus 0:0a673c671a56 122 }
ebrus 0:0a673c671a56 123
ebrus 0:0a673c671a56 124 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
ebrus 0:0a673c671a56 125 */
ebrus 0:0a673c671a56 126 void pulsewidth_us(int us) {
ebrus 0:0a673c671a56 127 pwmout_pulsewidth_us(&_pwm, us);
ebrus 0:0a673c671a56 128 }
ebrus 0:0a673c671a56 129
ebrus 0:0a673c671a56 130 #ifdef MBED_OPERATORS
ebrus 0:0a673c671a56 131 /** A operator shorthand for write()
ebrus 0:0a673c671a56 132 */
ebrus 0:0a673c671a56 133 PwmOut& operator= (float value) {
ebrus 0:0a673c671a56 134 write(value);
ebrus 0:0a673c671a56 135 return *this;
ebrus 0:0a673c671a56 136 }
ebrus 0:0a673c671a56 137
ebrus 0:0a673c671a56 138 PwmOut& operator= (PwmOut& rhs) {
ebrus 0:0a673c671a56 139 write(rhs.read());
ebrus 0:0a673c671a56 140 return *this;
ebrus 0:0a673c671a56 141 }
ebrus 0:0a673c671a56 142
ebrus 0:0a673c671a56 143 /** An operator shorthand for read()
ebrus 0:0a673c671a56 144 */
ebrus 0:0a673c671a56 145 operator float() {
ebrus 0:0a673c671a56 146 return read();
ebrus 0:0a673c671a56 147 }
ebrus 0:0a673c671a56 148 #endif
ebrus 0:0a673c671a56 149
ebrus 0:0a673c671a56 150 protected:
ebrus 0:0a673c671a56 151 pwmout_t _pwm;
ebrus 0:0a673c671a56 152 };
ebrus 0:0a673c671a56 153
ebrus 0:0a673c671a56 154 } // namespace mbed
ebrus 0:0a673c671a56 155
ebrus 0:0a673c671a56 156 #endif
ebrus 0:0a673c671a56 157
ebrus 0:0a673c671a56 158 #endif