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 17:40:46 2018 +0000
Revision:
2:77d9bb2fa44e
Parent:
1:27721b0d390b
Child:
3:717258c64304
Cleanup comments

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 2:77d9bb2fa44e 41 * TMR1 | P3_1, P5_3<br>
jessexm 2:77d9bb2fa44e 42 * TMR2 | P3_2, P4_0, P5_4<br>
jessexm 2:77d9bb2fa44e 43 * TMR3 | P3_3, P5_5<br>
jessexm 2:77d9bb2fa44e 44 * TMR4 | P3_4, P5_0, P5_6<br>
jessexm 2:77d9bb2fa44e 45 * TMR5 | P3_5, P5_1<br>
jessexm 1:27721b0d390b 46 *
jessexm 1:27721b0d390b 47 * @code
jessexm 1:27721b0d390b 48 * #include "mbed.h"
jessexm 1:27721b0d390b 49 * #include "MAX32630FTHR_PwmOut.h"
jessexm 1:27721b0d390b 50 *
jessexm 1:27721b0d390b 51 * DigitalOut led(LED1, LED_OFF);
jessexm 1:27721b0d390b 52 * MAX32630FTHR_PwmOut servo(P3_4);
jessexm 1:27721b0d390b 53 *
jessexm 1:27721b0d390b 54 * int main()
jessexm 1:27721b0d390b 55 * {
jessexm 1:27721b0d390b 56 * while (1) {
jessexm 1:27721b0d390b 57 * led = !led;
jessexm 1:27721b0d390b 58 * for (float pw = 0.000f; pw <= 1.000f; pw += 0.001f) {
jessexm 1:27721b0d390b 59 * servo.write(pw);
jessexm 1:27721b0d390b 60 * wait_ms(100);
jessexm 1:27721b0d390b 61 * }
jessexm 1:27721b0d390b 62 * led = !led;
jessexm 1:27721b0d390b 63 * for (float pw = 1.000f; pw >= 0.000f; pw -= 0.001f) {
jessexm 1:27721b0d390b 64 * servo = pw;
jessexm 1:27721b0d390b 65 * wait_ms(100);
jessexm 1:27721b0d390b 66 * }
jessexm 1:27721b0d390b 67 * }
jessexm 1:27721b0d390b 68 * }
jessexm 1:27721b0d390b 69 * @endcode
jessexm 1:27721b0d390b 70 */
jessexm 1:27721b0d390b 71
jessexm 0:9df5e4328319 72 class MAX32630FTHR_PwmOut
jessexm 0:9df5e4328319 73 {
jessexm 0:9df5e4328319 74 public:
jessexm 0:9df5e4328319 75
jessexm 0:9df5e4328319 76 /** Create a PwmOut connected to the specified pin
jessexm 0:9df5e4328319 77 *
jessexm 0:9df5e4328319 78 * @param pin PwmOut pin to connect to
jessexm 0:9df5e4328319 79 */
jessexm 0:9df5e4328319 80 MAX32630FTHR_PwmOut(PinName pin) : _deep_sleep_locked(false) {
jessexm 0:9df5e4328319 81 core_util_critical_section_enter();
jessexm 0:9df5e4328319 82 pwmout_init(pin);
jessexm 0:9df5e4328319 83 core_util_critical_section_exit();
jessexm 0:9df5e4328319 84 }
jessexm 0:9df5e4328319 85
jessexm 0:9df5e4328319 86 ~MAX32630FTHR_PwmOut() {
jessexm 0:9df5e4328319 87 core_util_critical_section_enter();
jessexm 0:9df5e4328319 88 unlock_deep_sleep();
jessexm 0:9df5e4328319 89 core_util_critical_section_exit();
jessexm 0:9df5e4328319 90 }
jessexm 0:9df5e4328319 91
jessexm 0:9df5e4328319 92 /** Set the ouput duty-cycle, specified as a percentage (float)
jessexm 0:9df5e4328319 93 *
jessexm 0:9df5e4328319 94 * @param value A floating-point value representing the output duty-cycle,
jessexm 0:9df5e4328319 95 * specified as a percentage. The value should lie between
jessexm 0:9df5e4328319 96 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
jessexm 0:9df5e4328319 97 * Values outside this range will be saturated to 0.0f or 1.0f.
jessexm 0:9df5e4328319 98 */
jessexm 0:9df5e4328319 99 void write(float value) {
jessexm 0:9df5e4328319 100 core_util_critical_section_enter();
jessexm 0:9df5e4328319 101 lock_deep_sleep();
jessexm 0:9df5e4328319 102 pwmout_write(value);
jessexm 0:9df5e4328319 103 core_util_critical_section_exit();
jessexm 0:9df5e4328319 104 }
jessexm 0:9df5e4328319 105
jessexm 0:9df5e4328319 106 /** Return the current output duty-cycle setting, measured as a percentage (float)
jessexm 0:9df5e4328319 107 *
jessexm 0:9df5e4328319 108 * @returns
jessexm 0:9df5e4328319 109 * A floating-point value representing the current duty-cycle being output on the pin,
jessexm 0:9df5e4328319 110 * measured as a percentage. The returned value will lie between
jessexm 0:9df5e4328319 111 * 0.0f (representing on 0%) and 1.0f (representing on 100%).
jessexm 0:9df5e4328319 112 *
jessexm 0:9df5e4328319 113 * @note
jessexm 0:9df5e4328319 114 * This value may not match exactly the value set by a previous write().
jessexm 0:9df5e4328319 115 */
jessexm 0:9df5e4328319 116 float read() {
jessexm 0:9df5e4328319 117 core_util_critical_section_enter();
jessexm 0:9df5e4328319 118 float val = pwmout_read();
jessexm 0:9df5e4328319 119 core_util_critical_section_exit();
jessexm 0:9df5e4328319 120 return val;
jessexm 0:9df5e4328319 121 }
jessexm 0:9df5e4328319 122
jessexm 0:9df5e4328319 123 /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
jessexm 0:9df5e4328319 124 *
jessexm 0:9df5e4328319 125 * @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle
jessexm 0:9df5e4328319 126 * @note
jessexm 0:9df5e4328319 127 * The resolution is currently in microseconds; periods smaller than this
jessexm 0:9df5e4328319 128 * will be set to zero.
jessexm 0:9df5e4328319 129 */
jessexm 0:9df5e4328319 130 void period(float seconds) {
jessexm 0:9df5e4328319 131 core_util_critical_section_enter();
jessexm 0:9df5e4328319 132 pwmout_period(seconds);
jessexm 0:9df5e4328319 133 core_util_critical_section_exit();
jessexm 0:9df5e4328319 134 }
jessexm 0:9df5e4328319 135
jessexm 0:9df5e4328319 136 /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same.
jessexm 0:9df5e4328319 137 * @param ms Change the period of a PWM signal in milli-seconds without modifying the duty cycle
jessexm 0:9df5e4328319 138 */
jessexm 0:9df5e4328319 139 void period_ms(int ms) {
jessexm 0:9df5e4328319 140 core_util_critical_section_enter();
jessexm 0:9df5e4328319 141 pwmout_period_ms(ms);
jessexm 0:9df5e4328319 142 core_util_critical_section_exit();
jessexm 0:9df5e4328319 143 }
jessexm 0:9df5e4328319 144
jessexm 0:9df5e4328319 145 /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same.
jessexm 0:9df5e4328319 146 * @param us Change the period of a PWM signal in micro-seconds without modifying the duty cycle
jessexm 0:9df5e4328319 147 */
jessexm 0:9df5e4328319 148 void period_us(int us) {
jessexm 0:9df5e4328319 149 core_util_critical_section_enter();
jessexm 0:9df5e4328319 150 pwmout_period_us(us);
jessexm 0:9df5e4328319 151 core_util_critical_section_exit();
jessexm 0:9df5e4328319 152 }
jessexm 0:9df5e4328319 153
jessexm 0:9df5e4328319 154 /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same.
jessexm 0:9df5e4328319 155 * @param seconds Change the pulse width of a PWM signal specified in seconds (float)
jessexm 0:9df5e4328319 156 */
jessexm 0:9df5e4328319 157 void pulsewidth(float seconds) {
jessexm 0:9df5e4328319 158 core_util_critical_section_enter();
jessexm 0:9df5e4328319 159 pwmout_pulsewidth(seconds);
jessexm 0:9df5e4328319 160 core_util_critical_section_exit();
jessexm 0:9df5e4328319 161 }
jessexm 0:9df5e4328319 162
jessexm 0:9df5e4328319 163 /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same.
jessexm 0:9df5e4328319 164 * @param ms Change the pulse width of a PWM signal specified in milli-seconds
jessexm 0:9df5e4328319 165 */
jessexm 0:9df5e4328319 166 void pulsewidth_ms(int ms) {
jessexm 0:9df5e4328319 167 core_util_critical_section_enter();
jessexm 0:9df5e4328319 168 pwmout_pulsewidth_ms(ms);
jessexm 0:9df5e4328319 169 core_util_critical_section_exit();
jessexm 0:9df5e4328319 170 }
jessexm 0:9df5e4328319 171
jessexm 0:9df5e4328319 172 /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same.
jessexm 0:9df5e4328319 173 * @param us Change the pulse width of a PWM signal specified in micro-seconds
jessexm 0:9df5e4328319 174 */
jessexm 0:9df5e4328319 175 void pulsewidth_us(int us) {
jessexm 0:9df5e4328319 176 core_util_critical_section_enter();
jessexm 0:9df5e4328319 177 pwmout_pulsewidth_us(us);
jessexm 0:9df5e4328319 178 core_util_critical_section_exit();
jessexm 0:9df5e4328319 179 }
jessexm 0:9df5e4328319 180
jessexm 0:9df5e4328319 181 /** A operator shorthand for write()
jessexm 0:9df5e4328319 182 * \sa PwmOut::write()
jessexm 0:9df5e4328319 183 */
jessexm 0:9df5e4328319 184 MAX32630FTHR_PwmOut& operator= (float value) {
jessexm 0:9df5e4328319 185 // Underlying call is thread safe
jessexm 0:9df5e4328319 186 write(value);
jessexm 0:9df5e4328319 187 return *this;
jessexm 0:9df5e4328319 188 }
jessexm 0:9df5e4328319 189
jessexm 0:9df5e4328319 190 /** A operator shorthand for write()
jessexm 0:9df5e4328319 191 * \sa PwmOut::write()
jessexm 0:9df5e4328319 192 */
jessexm 0:9df5e4328319 193 MAX32630FTHR_PwmOut& operator= (PwmOut& rhs) {
jessexm 0:9df5e4328319 194 // Underlying call is thread safe
jessexm 0:9df5e4328319 195 write(rhs.read());
jessexm 0:9df5e4328319 196 return *this;
jessexm 0:9df5e4328319 197 }
jessexm 0:9df5e4328319 198
jessexm 0:9df5e4328319 199 /** An operator shorthand for read()
jessexm 0:9df5e4328319 200 * \sa PwmOut::read()
jessexm 0:9df5e4328319 201 */
jessexm 0:9df5e4328319 202 operator float() {
jessexm 0:9df5e4328319 203 // Underlying call is thread safe
jessexm 0:9df5e4328319 204 return read();
jessexm 0:9df5e4328319 205 }
jessexm 0:9df5e4328319 206
jessexm 0:9df5e4328319 207 protected:
jessexm 0:9df5e4328319 208 /** Lock deep sleep only if it is not yet locked */
jessexm 0:9df5e4328319 209 void lock_deep_sleep() {
jessexm 0:9df5e4328319 210 if (_deep_sleep_locked == false) {
jessexm 0:9df5e4328319 211 sleep_manager_lock_deep_sleep();
jessexm 0:9df5e4328319 212 _deep_sleep_locked = true;
jessexm 0:9df5e4328319 213 }
jessexm 0:9df5e4328319 214 }
jessexm 0:9df5e4328319 215
jessexm 0:9df5e4328319 216 /** Unlock deep sleep in case it is locked */
jessexm 0:9df5e4328319 217 void unlock_deep_sleep() {
jessexm 0:9df5e4328319 218 if (_deep_sleep_locked == true) {
jessexm 0:9df5e4328319 219 sleep_manager_unlock_deep_sleep();
jessexm 0:9df5e4328319 220 _deep_sleep_locked = false;
jessexm 0:9df5e4328319 221 }
jessexm 0:9df5e4328319 222 }
jessexm 0:9df5e4328319 223
jessexm 0:9df5e4328319 224 bool _deep_sleep_locked;
jessexm 0:9df5e4328319 225
jessexm 0:9df5e4328319 226 private:
jessexm 0:9df5e4328319 227 /** Initialize the pwm out peripheral and configure the pin
jessexm 0:9df5e4328319 228 *
jessexm 0:9df5e4328319 229 * @param obj The pwmout object to initialize
jessexm 0:9df5e4328319 230 * @param pin The pwmout pin to initialize
jessexm 0:9df5e4328319 231 */
jessexm 0:9df5e4328319 232 void pwmout_init(PinName pin);
jessexm 0:9df5e4328319 233
jessexm 0:9df5e4328319 234 /** Deinitialize the pwmout object
jessexm 0:9df5e4328319 235 *
jessexm 0:9df5e4328319 236 * @param obj The pwmout object
jessexm 0:9df5e4328319 237 */
jessexm 0:9df5e4328319 238 void pwmout_free();
jessexm 0:9df5e4328319 239
jessexm 0:9df5e4328319 240 /** Set the output duty-cycle in range <0.0f, 1.0f>
jessexm 0:9df5e4328319 241 *
jessexm 0:9df5e4328319 242 * Value 0.0f represents 0 percentage, 1.0f represents 100 percent.
jessexm 0:9df5e4328319 243 * @param obj The pwmout object
jessexm 0:9df5e4328319 244 * @param percent The floating-point percentage number
jessexm 0:9df5e4328319 245 */
jessexm 0:9df5e4328319 246 void pwmout_write(float percent);
jessexm 0:9df5e4328319 247
jessexm 0:9df5e4328319 248 /** Read the current float-point output duty-cycle
jessexm 0:9df5e4328319 249 *
jessexm 0:9df5e4328319 250 * @param obj The pwmout object
jessexm 0:9df5e4328319 251 * @return A floating-point output duty-cycle
jessexm 0:9df5e4328319 252 */
jessexm 0:9df5e4328319 253 float pwmout_read();
jessexm 0:9df5e4328319 254
jessexm 0:9df5e4328319 255 /** Set the PWM period specified in seconds, keeping the duty cycle the same
jessexm 0:9df5e4328319 256 *
jessexm 0:9df5e4328319 257 * Periods smaller than microseconds (the lowest resolution) are set to zero.
jessexm 0:9df5e4328319 258 * @param obj The pwmout object
jessexm 0:9df5e4328319 259 * @param seconds The floating-point seconds period
jessexm 0:9df5e4328319 260 */
jessexm 0:9df5e4328319 261 void pwmout_period(float seconds);
jessexm 0:9df5e4328319 262
jessexm 0:9df5e4328319 263 /** Set the PWM period specified in miliseconds, keeping the duty cycle the same
jessexm 0:9df5e4328319 264 *
jessexm 0:9df5e4328319 265 * @param obj The pwmout object
jessexm 0:9df5e4328319 266 * @param ms The milisecond period
jessexm 0:9df5e4328319 267 */
jessexm 0:9df5e4328319 268 void pwmout_period_ms(int ms);
jessexm 0:9df5e4328319 269
jessexm 0:9df5e4328319 270 /** Set the PWM period specified in microseconds, keeping the duty cycle the same
jessexm 0:9df5e4328319 271 *
jessexm 0:9df5e4328319 272 * @param obj The pwmout object
jessexm 0:9df5e4328319 273 * @param us The microsecond period
jessexm 0:9df5e4328319 274 */
jessexm 0:9df5e4328319 275 void pwmout_period_us(int us);
jessexm 0:9df5e4328319 276
jessexm 0:9df5e4328319 277 /** Set the PWM pulsewidth specified in seconds, keeping the period the same.
jessexm 0:9df5e4328319 278 *
jessexm 0:9df5e4328319 279 * @param obj The pwmout object
jessexm 0:9df5e4328319 280 * @param seconds The floating-point pulsewidth in seconds
jessexm 0:9df5e4328319 281 */
jessexm 0:9df5e4328319 282 void pwmout_pulsewidth(float seconds);
jessexm 0:9df5e4328319 283
jessexm 0:9df5e4328319 284 /** Set the PWM pulsewidth specified in miliseconds, keeping the period the same.
jessexm 0:9df5e4328319 285 *
jessexm 0:9df5e4328319 286 * @param obj The pwmout object
jessexm 0:9df5e4328319 287 * @param ms The floating-point pulsewidth in miliseconds
jessexm 0:9df5e4328319 288 */
jessexm 0:9df5e4328319 289 void pwmout_pulsewidth_ms(int ms);
jessexm 0:9df5e4328319 290
jessexm 0:9df5e4328319 291 /** Set the PWM pulsewidth specified in microseconds, keeping the period the same.
jessexm 0:9df5e4328319 292 *
jessexm 0:9df5e4328319 293 * @param obj The pwmout object
jessexm 0:9df5e4328319 294 * @param us The floating-point pulsewidth in microseconds
jessexm 0:9df5e4328319 295 */
jessexm 0:9df5e4328319 296 void pwmout_pulsewidth_us(int us);
jessexm 0:9df5e4328319 297
jessexm 0:9df5e4328319 298
jessexm 0:9df5e4328319 299 PinName pin;
jessexm 0:9df5e4328319 300 mxc_tmr_regs_t *tmr;
jessexm 0:9df5e4328319 301 int tmr_idx;
jessexm 0:9df5e4328319 302 int pwm_period;
jessexm 0:9df5e4328319 303 int pulse_width;
jessexm 0:9df5e4328319 304 int last_pulse_width;
jessexm 0:9df5e4328319 305 void pwmout_update(void);
jessexm 0:9df5e4328319 306 };
jessexm 0:9df5e4328319 307
jessexm 0:9df5e4328319 308 #endif