Timer based PWM output for MAX32630FTHR platform

This library provides PWM output using the MAX32630 32-bit timers.

The mbed PwmOut API implementation uses the MAX32630 Pulse Train peripherals.

The table below contains the available GPIO pins that can be connected to the six 32-bit timers (TMR0-5). Timer 0 is used for the microsecond ticker API and is not available for PWM output. Timer 5 is used by the BLE API and will not be available for PWM output if the BLE API is used.

TimerGPIO Port and Pin
TMR1P3_1, P5_3
TMR2P2_4, P3_2, P4_0, P5_4
TMR3P2_5, P3_3, P5_5
TMR4P2_6, P3_4, P5_0, P5_6
TMR5P3_5, P5_1

Note GPIO P2_4, P2_5 and P2_6 are connected to onboard LEDs 1, 2 and 3.

Committer:
jessexm
Date:
Fri Apr 20 22:36:02 2018 +0000
Revision:
3:717258c64304
Parent:
2:77d9bb2fa44e
Child:
4:bd1c27ca31e8
Add LEDs pins to PWM pin list; update example to use LEDs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jessexm 2:77d9bb2fa44e 1 /* Original files PwmOut.h and pwmout_api.h contained the following copyright */
jessexm 0:9df5e4328319 2 /* mbed Microcontroller Library
jessexm 0:9df5e4328319 3 * Copyright (c) 2006-2013 ARM Limited
jessexm 0:9df5e4328319 4 *
jessexm 0:9df5e4328319 5 * Licensed under the Apache License, Version 2.0 (the "License");
jessexm 0:9df5e4328319 6 * you may not use this file except in compliance with the License.
jessexm 0:9df5e4328319 7 * You may obtain a copy of the License at
jessexm 0:9df5e4328319 8 *
jessexm 0:9df5e4328319 9 * http://www.apache.org/licenses/LICENSE-2.0
jessexm 0:9df5e4328319 10 *
jessexm 0:9df5e4328319 11 * Unless required by applicable law or agreed to in writing, software
jessexm 0:9df5e4328319 12 * distributed under the License is distributed on an "AS IS" BASIS,
jessexm 0:9df5e4328319 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jessexm 0:9df5e4328319 14 * See the License for the specific language governing permissions and
jessexm 0:9df5e4328319 15 * limitations under the License.
jessexm 0:9df5e4328319 16 */
jessexm 0:9df5e4328319 17 /* Original files PwmOut.h and pwmout_api.h combined and modified:
jessexm 0:9df5e4328319 18 19 April 2018 Maxim Integrated Products, Inc.
jessexm 0:9df5e4328319 19 */
jessexm 0:9df5e4328319 20
jessexm 0:9df5e4328319 21 #ifndef _MAX32630FTHR_PWMOUT_H_
jessexm 0:9df5e4328319 22 #define _MAX32630FTHR_PWMOUT_H_
jessexm 0:9df5e4328319 23
jessexm 0:9df5e4328319 24 #include "mbed.h"
jessexm 0:9df5e4328319 25 #include "PinNames.h"
jessexm 0:9df5e4328319 26 #include "platform/mbed_critical.h"
jessexm 0:9df5e4328319 27 #include "platform/mbed_power_mgmt.h"
jessexm 0:9df5e4328319 28 #include "tmr_regs.h"
jessexm 0:9df5e4328319 29
jessexm 1:27721b0d390b 30 /**
jessexm 1:27721b0d390b 31 * @brief Timer based PWM output for the MAX32630FTHR platform
jessexm 1:27721b0d390b 32 *
jessexm 1:27721b0d390b 33 * @details This library provides PWM output using the MAX32630 32-bit timers.
jessexm 1:27721b0d390b 34 * The mbed PwmOut API implementation uses the MAX32630 Pulse Train peripherals.
jessexm 1:27721b0d390b 35 * The table below contains the available GPIO pins that can be connected to the
jessexm 1:27721b0d390b 36 * six 32-bit timers (TMR0-5). Timer 0 is used for the microsecond ticker API
jessexm 1:27721b0d390b 37 * and is not available for PWM output. Timer 5 is used by the BLE API and will
jessexm 1:27721b0d390b 38 * not be available for PWM output if the BLE API is used.
jessexm 1:27721b0d390b 39 *
jessexm 1:27721b0d390b 40 * Timer | GPIO Port and Pin<br>
jessexm 3:717258c64304 41 * TMR1 | P3_1, P5_3<br>
jessexm 3:717258c64304 42 * TMR2 | P2_4, P3_2, P4_0, P5_4<br>
jessexm 3:717258c64304 43 * TMR3 | P2_5, P3_3, P5_5<br>
jessexm 3:717258c64304 44 * TMR4 | P2_6, P3_4, P5_0, P5_6<br>
jessexm 3:717258c64304 45 * TMR5 | P3_5, P5_1<br>
jessexm 3:717258c64304 46 *
jessexm 3:717258c64304 47 * Note GPIO P2_4, P2_5 and P2_6 are connected to onboard LEDs 1, 2 and 3.
jessexm 1:27721b0d390b 48 *
jessexm 1:27721b0d390b 49 * @code
jessexm 1:27721b0d390b 50 * #include "mbed.h"
jessexm 1:27721b0d390b 51 * #include "MAX32630FTHR_PwmOut.h"
jessexm 3:717258c64304 52 *
jessexm 3:717258c64304 53 * MAX32630FTHR_PwmOut led[] = {
jessexm 3:717258c64304 54 * MAX32630FTHR_PwmOut(LED1),
jessexm 3:717258c64304 55 * MAX32630FTHR_PwmOut(LED2),
jessexm 3:717258c64304 56 * MAX32630FTHR_PwmOut(LED3)
jessexm 3:717258c64304 57 * };
jessexm 3:717258c64304 58 *
jessexm 1:27721b0d390b 59 * int main()
jessexm 1:27721b0d390b 60 * {
jessexm 3:717258c64304 61 * float dc;
jessexm 3:717258c64304 62 * unsigned int idx = 0;
jessexm 3:717258c64304 63 *
jessexm 1:27721b0d390b 64 * while (1) {
jessexm 3:717258c64304 65 * for (dc = 0.0f; dc <= 1.0f; dc += 0.01f) {
jessexm 3:717258c64304 66 * led[idx % 3].write(dc);
jessexm 3:717258c64304 67 * led[(idx + 1) % 3].write(1.0f - dc);
jessexm 3:717258c64304 68 * wait_ms(20);
jessexm 1:27721b0d390b 69 * }
jessexm 3:717258c64304 70 * idx++;
jessexm 1:27721b0d390b 71 * }
jessexm 1:27721b0d390b 72 * }
jessexm 1:27721b0d390b 73 * @endcode
jessexm 1:27721b0d390b 74 */
jessexm 1:27721b0d390b 75
jessexm 0:9df5e4328319 76 class MAX32630FTHR_PwmOut
jessexm 0:9df5e4328319 77 {
jessexm 0:9df5e4328319 78 public:
jessexm 0:9df5e4328319 79
jessexm 0:9df5e4328319 80 /** Create a PwmOut connected to the specified pin
jessexm 0:9df5e4328319 81 *
jessexm 0:9df5e4328319 82 * @param pin PwmOut pin to connect to
jessexm 0:9df5e4328319 83 */
jessexm 0:9df5e4328319 84 MAX32630FTHR_PwmOut(PinName pin) : _deep_sleep_locked(false) {
jessexm 0:9df5e4328319 85 core_util_critical_section_enter();
jessexm 0:9df5e4328319 86 pwmout_init(pin);
jessexm 0:9df5e4328319 87 core_util_critical_section_exit();
jessexm 0:9df5e4328319 88 }
jessexm 0:9df5e4328319 89
jessexm 0:9df5e4328319 90 ~MAX32630FTHR_PwmOut() {
jessexm 0:9df5e4328319 91 core_util_critical_section_enter();
jessexm 0:9df5e4328319 92 unlock_deep_sleep();
jessexm 0:9df5e4328319 93 core_util_critical_section_exit();
jessexm 0:9df5e4328319 94 }
jessexm 0:9df5e4328319 95
jessexm 0:9df5e4328319 96 /** Set the ouput duty-cycle, specified as a percentage (float)
jessexm 0:9df5e4328319 97 *
jessexm 0:9df5e4328319 98 * @param value A floating-point value representing the output duty-cycle,
jessexm 0:9df5e4328319 99 * specified as a percentage. The value should lie between
jessexm 0:9df5e4328319 100 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
jessexm 0:9df5e4328319 101 * Values outside this range will be saturated to 0.0f or 1.0f.
jessexm 0:9df5e4328319 102 */
jessexm 0:9df5e4328319 103 void write(float value) {
jessexm 0:9df5e4328319 104 core_util_critical_section_enter();
jessexm 0:9df5e4328319 105 lock_deep_sleep();
jessexm 0:9df5e4328319 106 pwmout_write(value);
jessexm 0:9df5e4328319 107 core_util_critical_section_exit();
jessexm 0:9df5e4328319 108 }
jessexm 0:9df5e4328319 109
jessexm 0:9df5e4328319 110 /** Return the current output duty-cycle setting, measured as a percentage (float)
jessexm 0:9df5e4328319 111 *
jessexm 0:9df5e4328319 112 * @returns
jessexm 0:9df5e4328319 113 * A floating-point value representing the current duty-cycle being output on the pin,
jessexm 0:9df5e4328319 114 * measured as a percentage. The returned value will lie between
jessexm 0:9df5e4328319 115 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
jessexm 0:9df5e4328319 116 *
jessexm 0:9df5e4328319 117 * @note
jessexm 0:9df5e4328319 118 * This value may not match exactly the value set by a previous write().
jessexm 0:9df5e4328319 119 */
jessexm 0:9df5e4328319 120 float read() {
jessexm 0:9df5e4328319 121 core_util_critical_section_enter();
jessexm 0:9df5e4328319 122 float val = pwmout_read();
jessexm 0:9df5e4328319 123 core_util_critical_section_exit();
jessexm 0:9df5e4328319 124 return val;
jessexm 0:9df5e4328319 125 }
jessexm 0:9df5e4328319 126
jessexm 0:9df5e4328319 127 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
jessexm 0:9df5e4328319 128 *
jessexm 0:9df5e4328319 129 * @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
jessexm 0:9df5e4328319 130 * @note
jessexm 0:9df5e4328319 131 * The resolution is currently in microseconds; periods smaller than this
jessexm 0:9df5e4328319 132 * will be set to zero.
jessexm 0:9df5e4328319 133 */
jessexm 0:9df5e4328319 134 void period(float seconds) {
jessexm 0:9df5e4328319 135 core_util_critical_section_enter();
jessexm 0:9df5e4328319 136 pwmout_period(seconds);
jessexm 0:9df5e4328319 137 core_util_critical_section_exit();
jessexm 0:9df5e4328319 138 }
jessexm 0:9df5e4328319 139
jessexm 0:9df5e4328319 140 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
jessexm 0:9df5e4328319 141 * @param ms Change the period of a PWM signal in milli-seconds without modifying the duty cycle
jessexm 0:9df5e4328319 142 */
jessexm 0:9df5e4328319 143 void period_ms(int ms) {
jessexm 0:9df5e4328319 144 core_util_critical_section_enter();
jessexm 0:9df5e4328319 145 pwmout_period_ms(ms);
jessexm 0:9df5e4328319 146 core_util_critical_section_exit();
jessexm 0:9df5e4328319 147 }
jessexm 0:9df5e4328319 148
jessexm 0:9df5e4328319 149 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
jessexm 0:9df5e4328319 150 * @param us Change the period of a PWM signal in micro-seconds without modifying the duty cycle
jessexm 0:9df5e4328319 151 */
jessexm 0:9df5e4328319 152 void period_us(int us) {
jessexm 0:9df5e4328319 153 core_util_critical_section_enter();
jessexm 0:9df5e4328319 154 pwmout_period_us(us);
jessexm 0:9df5e4328319 155 core_util_critical_section_exit();
jessexm 0:9df5e4328319 156 }
jessexm 0:9df5e4328319 157
jessexm 0:9df5e4328319 158 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
jessexm 0:9df5e4328319 159 * @param seconds Change the pulse width of a PWM signal specified in seconds (float)
jessexm 0:9df5e4328319 160 */
jessexm 0:9df5e4328319 161 void pulsewidth(float seconds) {
jessexm 0:9df5e4328319 162 core_util_critical_section_enter();
jessexm 0:9df5e4328319 163 pwmout_pulsewidth(seconds);
jessexm 0:9df5e4328319 164 core_util_critical_section_exit();
jessexm 0:9df5e4328319 165 }
jessexm 0:9df5e4328319 166
jessexm 0:9df5e4328319 167 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
jessexm 0:9df5e4328319 168 * @param ms Change the pulse width of a PWM signal specified in milli-seconds
jessexm 0:9df5e4328319 169 */
jessexm 0:9df5e4328319 170 void pulsewidth_ms(int ms) {
jessexm 0:9df5e4328319 171 core_util_critical_section_enter();
jessexm 0:9df5e4328319 172 pwmout_pulsewidth_ms(ms);
jessexm 0:9df5e4328319 173 core_util_critical_section_exit();
jessexm 0:9df5e4328319 174 }
jessexm 0:9df5e4328319 175
jessexm 0:9df5e4328319 176 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
jessexm 0:9df5e4328319 177 * @param us Change the pulse width of a PWM signal specified in micro-seconds
jessexm 0:9df5e4328319 178 */
jessexm 0:9df5e4328319 179 void pulsewidth_us(int us) {
jessexm 0:9df5e4328319 180 core_util_critical_section_enter();
jessexm 0:9df5e4328319 181 pwmout_pulsewidth_us(us);
jessexm 0:9df5e4328319 182 core_util_critical_section_exit();
jessexm 0:9df5e4328319 183 }
jessexm 0:9df5e4328319 184
jessexm 0:9df5e4328319 185 /** A operator shorthand for write()
jessexm 0:9df5e4328319 186 * \sa PwmOut::write()
jessexm 0:9df5e4328319 187 */
jessexm 0:9df5e4328319 188 MAX32630FTHR_PwmOut& operator= (float value) {
jessexm 0:9df5e4328319 189 // Underlying call is thread safe
jessexm 0:9df5e4328319 190 write(value);
jessexm 0:9df5e4328319 191 return *this;
jessexm 0:9df5e4328319 192 }
jessexm 0:9df5e4328319 193
jessexm 0:9df5e4328319 194 /** A operator shorthand for write()
jessexm 0:9df5e4328319 195 * \sa PwmOut::write()
jessexm 0:9df5e4328319 196 */
jessexm 0:9df5e4328319 197 MAX32630FTHR_PwmOut& operator= (PwmOut& rhs) {
jessexm 0:9df5e4328319 198 // Underlying call is thread safe
jessexm 0:9df5e4328319 199 write(rhs.read());
jessexm 0:9df5e4328319 200 return *this;
jessexm 0:9df5e4328319 201 }
jessexm 0:9df5e4328319 202
jessexm 0:9df5e4328319 203 /** An operator shorthand for read()
jessexm 0:9df5e4328319 204 * \sa PwmOut::read()
jessexm 0:9df5e4328319 205 */
jessexm 0:9df5e4328319 206 operator float() {
jessexm 0:9df5e4328319 207 // Underlying call is thread safe
jessexm 0:9df5e4328319 208 return read();
jessexm 0:9df5e4328319 209 }
jessexm 0:9df5e4328319 210
jessexm 0:9df5e4328319 211 protected:
jessexm 0:9df5e4328319 212 /** Lock deep sleep only if it is not yet locked */
jessexm 0:9df5e4328319 213 void lock_deep_sleep() {
jessexm 0:9df5e4328319 214 if (_deep_sleep_locked == false) {
jessexm 0:9df5e4328319 215 sleep_manager_lock_deep_sleep();
jessexm 0:9df5e4328319 216 _deep_sleep_locked = true;
jessexm 0:9df5e4328319 217 }
jessexm 0:9df5e4328319 218 }
jessexm 0:9df5e4328319 219
jessexm 0:9df5e4328319 220 /** Unlock deep sleep in case it is locked */
jessexm 0:9df5e4328319 221 void unlock_deep_sleep() {
jessexm 0:9df5e4328319 222 if (_deep_sleep_locked == true) {
jessexm 0:9df5e4328319 223 sleep_manager_unlock_deep_sleep();
jessexm 0:9df5e4328319 224 _deep_sleep_locked = false;
jessexm 0:9df5e4328319 225 }
jessexm 0:9df5e4328319 226 }
jessexm 0:9df5e4328319 227
jessexm 0:9df5e4328319 228 bool _deep_sleep_locked;
jessexm 0:9df5e4328319 229
jessexm 0:9df5e4328319 230 private:
jessexm 0:9df5e4328319 231 /** Initialize the pwm out peripheral and configure the pin
jessexm 0:9df5e4328319 232 *
jessexm 0:9df5e4328319 233 * @param obj The pwmout object to initialize
jessexm 0:9df5e4328319 234 * @param pin The pwmout pin to initialize
jessexm 0:9df5e4328319 235 */
jessexm 0:9df5e4328319 236 void pwmout_init(PinName pin);
jessexm 0:9df5e4328319 237
jessexm 0:9df5e4328319 238 /** Deinitialize the pwmout object
jessexm 0:9df5e4328319 239 *
jessexm 0:9df5e4328319 240 * @param obj The pwmout object
jessexm 0:9df5e4328319 241 */
jessexm 0:9df5e4328319 242 void pwmout_free();
jessexm 0:9df5e4328319 243
jessexm 0:9df5e4328319 244 /** Set the output duty-cycle in range <0.0f, 1.0f>
jessexm 0:9df5e4328319 245 *
jessexm 0:9df5e4328319 246 * Value 0.0f represents 0 percentage, 1.0f represents 100 percent.
jessexm 0:9df5e4328319 247 * @param obj The pwmout object
jessexm 0:9df5e4328319 248 * @param percent The floating-point percentage number
jessexm 0:9df5e4328319 249 */
jessexm 0:9df5e4328319 250 void pwmout_write(float percent);
jessexm 0:9df5e4328319 251
jessexm 0:9df5e4328319 252 /** Read the current float-point output duty-cycle
jessexm 0:9df5e4328319 253 *
jessexm 0:9df5e4328319 254 * @param obj The pwmout object
jessexm 0:9df5e4328319 255 * @return A floating-point output duty-cycle
jessexm 0:9df5e4328319 256 */
jessexm 0:9df5e4328319 257 float pwmout_read();
jessexm 0:9df5e4328319 258
jessexm 0:9df5e4328319 259 /** Set the PWM period specified in seconds, keeping the duty cycle the same
jessexm 0:9df5e4328319 260 *
jessexm 0:9df5e4328319 261 * Periods smaller than microseconds (the lowest resolution) are set to zero.
jessexm 0:9df5e4328319 262 * @param obj The pwmout object
jessexm 0:9df5e4328319 263 * @param seconds The floating-point seconds period
jessexm 0:9df5e4328319 264 */
jessexm 0:9df5e4328319 265 void pwmout_period(float seconds);
jessexm 0:9df5e4328319 266
jessexm 0:9df5e4328319 267 /** Set the PWM period specified in miliseconds, keeping the duty cycle the same
jessexm 0:9df5e4328319 268 *
jessexm 0:9df5e4328319 269 * @param obj The pwmout object
jessexm 0:9df5e4328319 270 * @param ms The milisecond period
jessexm 0:9df5e4328319 271 */
jessexm 0:9df5e4328319 272 void pwmout_period_ms(int ms);
jessexm 0:9df5e4328319 273
jessexm 0:9df5e4328319 274 /** Set the PWM period specified in microseconds, keeping the duty cycle the same
jessexm 0:9df5e4328319 275 *
jessexm 0:9df5e4328319 276 * @param obj The pwmout object
jessexm 0:9df5e4328319 277 * @param us The microsecond period
jessexm 0:9df5e4328319 278 */
jessexm 0:9df5e4328319 279 void pwmout_period_us(int us);
jessexm 0:9df5e4328319 280
jessexm 0:9df5e4328319 281 /** Set the PWM pulsewidth specified in seconds, keeping the period the same.
jessexm 0:9df5e4328319 282 *
jessexm 0:9df5e4328319 283 * @param obj The pwmout object
jessexm 0:9df5e4328319 284 * @param seconds The floating-point pulsewidth in seconds
jessexm 0:9df5e4328319 285 */
jessexm 0:9df5e4328319 286 void pwmout_pulsewidth(float seconds);
jessexm 0:9df5e4328319 287
jessexm 0:9df5e4328319 288 /** Set the PWM pulsewidth specified in miliseconds, keeping the period the same.
jessexm 0:9df5e4328319 289 *
jessexm 0:9df5e4328319 290 * @param obj The pwmout object
jessexm 0:9df5e4328319 291 * @param ms The floating-point pulsewidth in miliseconds
jessexm 0:9df5e4328319 292 */
jessexm 0:9df5e4328319 293 void pwmout_pulsewidth_ms(int ms);
jessexm 0:9df5e4328319 294
jessexm 0:9df5e4328319 295 /** Set the PWM pulsewidth specified in microseconds, keeping the period the same.
jessexm 0:9df5e4328319 296 *
jessexm 0:9df5e4328319 297 * @param obj The pwmout object
jessexm 0:9df5e4328319 298 * @param us The floating-point pulsewidth in microseconds
jessexm 0:9df5e4328319 299 */
jessexm 0:9df5e4328319 300 void pwmout_pulsewidth_us(int us);
jessexm 0:9df5e4328319 301
jessexm 0:9df5e4328319 302
jessexm 0:9df5e4328319 303 PinName pin;
jessexm 0:9df5e4328319 304 mxc_tmr_regs_t *tmr;
jessexm 0:9df5e4328319 305 int tmr_idx;
jessexm 0:9df5e4328319 306 int pwm_period;
jessexm 0:9df5e4328319 307 int pulse_width;
jessexm 0:9df5e4328319 308 int last_pulse_width;
jessexm 0:9df5e4328319 309 void pwmout_update(void);
jessexm 0:9df5e4328319 310 };
jessexm 0:9df5e4328319 311
jessexm 0:9df5e4328319 312 #endif