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.
Timer | GPIO Port and Pin |
TMR1 | P3_1, P5_3 |
TMR2 | P2_4, P3_2, P4_0, P5_4 |
TMR3 | P2_5, P3_3, P5_5 |
TMR4 | P2_6, P3_4, P5_0, P5_6 |
TMR5 | P3_5, P5_1 |
Note GPIO P2_4, P2_5 and P2_6 are connected to onboard LEDs 1, 2 and 3.
MAX32630FTHR_PwmOut.h@0:9df5e4328319, 2018-04-20 (annotated)
- Committer:
- jessexm
- Date:
- Fri Apr 20 16:39:48 2018 +0000
- Revision:
- 0:9df5e4328319
- Child:
- 1:27721b0d390b
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jessexm | 0:9df5e4328319 | 1 | /* Original files PwmOut.h and pwmout_api.h contined 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 | 0:9df5e4328319 | 30 | class MAX32630FTHR_PwmOut |
jessexm | 0:9df5e4328319 | 31 | { |
jessexm | 0:9df5e4328319 | 32 | public: |
jessexm | 0:9df5e4328319 | 33 | |
jessexm | 0:9df5e4328319 | 34 | /** Create a PwmOut connected to the specified pin |
jessexm | 0:9df5e4328319 | 35 | * |
jessexm | 0:9df5e4328319 | 36 | * @param pin PwmOut pin to connect to |
jessexm | 0:9df5e4328319 | 37 | */ |
jessexm | 0:9df5e4328319 | 38 | MAX32630FTHR_PwmOut(PinName pin) : _deep_sleep_locked(false) { |
jessexm | 0:9df5e4328319 | 39 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 40 | pwmout_init(pin); |
jessexm | 0:9df5e4328319 | 41 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 42 | } |
jessexm | 0:9df5e4328319 | 43 | |
jessexm | 0:9df5e4328319 | 44 | ~MAX32630FTHR_PwmOut() { |
jessexm | 0:9df5e4328319 | 45 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 46 | unlock_deep_sleep(); |
jessexm | 0:9df5e4328319 | 47 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 48 | } |
jessexm | 0:9df5e4328319 | 49 | |
jessexm | 0:9df5e4328319 | 50 | /** Set the ouput duty-cycle, specified as a percentage (float) |
jessexm | 0:9df5e4328319 | 51 | * |
jessexm | 0:9df5e4328319 | 52 | * @param value A floating-point value representing the output duty-cycle, |
jessexm | 0:9df5e4328319 | 53 | * specified as a percentage. The value should lie between |
jessexm | 0:9df5e4328319 | 54 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
jessexm | 0:9df5e4328319 | 55 | * Values outside this range will be saturated to 0.0f or 1.0f. |
jessexm | 0:9df5e4328319 | 56 | */ |
jessexm | 0:9df5e4328319 | 57 | void write(float value) { |
jessexm | 0:9df5e4328319 | 58 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 59 | lock_deep_sleep(); |
jessexm | 0:9df5e4328319 | 60 | pwmout_write(value); |
jessexm | 0:9df5e4328319 | 61 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 62 | } |
jessexm | 0:9df5e4328319 | 63 | |
jessexm | 0:9df5e4328319 | 64 | /** Return the current output duty-cycle setting, measured as a percentage (float) |
jessexm | 0:9df5e4328319 | 65 | * |
jessexm | 0:9df5e4328319 | 66 | * @returns |
jessexm | 0:9df5e4328319 | 67 | * A floating-point value representing the current duty-cycle being output on the pin, |
jessexm | 0:9df5e4328319 | 68 | * measured as a percentage. The returned value will lie between |
jessexm | 0:9df5e4328319 | 69 | * 0.0f (representing on 0%) and 1.0f (representing on 100%). |
jessexm | 0:9df5e4328319 | 70 | * |
jessexm | 0:9df5e4328319 | 71 | * @note |
jessexm | 0:9df5e4328319 | 72 | * This value may not match exactly the value set by a previous write(). |
jessexm | 0:9df5e4328319 | 73 | */ |
jessexm | 0:9df5e4328319 | 74 | float read() { |
jessexm | 0:9df5e4328319 | 75 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 76 | float val = pwmout_read(); |
jessexm | 0:9df5e4328319 | 77 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 78 | return val; |
jessexm | 0:9df5e4328319 | 79 | } |
jessexm | 0:9df5e4328319 | 80 | |
jessexm | 0:9df5e4328319 | 81 | /** Set the PWM period, specified in seconds (float), keeping the duty cycle the same. |
jessexm | 0:9df5e4328319 | 82 | * |
jessexm | 0:9df5e4328319 | 83 | * @param seconds Change the period of a PWM signal in seconds (float) without modifying the duty cycle |
jessexm | 0:9df5e4328319 | 84 | * @note |
jessexm | 0:9df5e4328319 | 85 | * The resolution is currently in microseconds; periods smaller than this |
jessexm | 0:9df5e4328319 | 86 | * will be set to zero. |
jessexm | 0:9df5e4328319 | 87 | */ |
jessexm | 0:9df5e4328319 | 88 | void period(float seconds) { |
jessexm | 0:9df5e4328319 | 89 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 90 | pwmout_period(seconds); |
jessexm | 0:9df5e4328319 | 91 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 92 | } |
jessexm | 0:9df5e4328319 | 93 | |
jessexm | 0:9df5e4328319 | 94 | /** Set the PWM period, specified in milli-seconds (int), keeping the duty cycle the same. |
jessexm | 0:9df5e4328319 | 95 | * @param ms Change the period of a PWM signal in milli-seconds without modifying the duty cycle |
jessexm | 0:9df5e4328319 | 96 | */ |
jessexm | 0:9df5e4328319 | 97 | void period_ms(int ms) { |
jessexm | 0:9df5e4328319 | 98 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 99 | pwmout_period_ms(ms); |
jessexm | 0:9df5e4328319 | 100 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 101 | } |
jessexm | 0:9df5e4328319 | 102 | |
jessexm | 0:9df5e4328319 | 103 | /** Set the PWM period, specified in micro-seconds (int), keeping the duty cycle the same. |
jessexm | 0:9df5e4328319 | 104 | * @param us Change the period of a PWM signal in micro-seconds without modifying the duty cycle |
jessexm | 0:9df5e4328319 | 105 | */ |
jessexm | 0:9df5e4328319 | 106 | void period_us(int us) { |
jessexm | 0:9df5e4328319 | 107 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 108 | pwmout_period_us(us); |
jessexm | 0:9df5e4328319 | 109 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 110 | } |
jessexm | 0:9df5e4328319 | 111 | |
jessexm | 0:9df5e4328319 | 112 | /** Set the PWM pulsewidth, specified in seconds (float), keeping the period the same. |
jessexm | 0:9df5e4328319 | 113 | * @param seconds Change the pulse width of a PWM signal specified in seconds (float) |
jessexm | 0:9df5e4328319 | 114 | */ |
jessexm | 0:9df5e4328319 | 115 | void pulsewidth(float seconds) { |
jessexm | 0:9df5e4328319 | 116 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 117 | pwmout_pulsewidth(seconds); |
jessexm | 0:9df5e4328319 | 118 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 119 | } |
jessexm | 0:9df5e4328319 | 120 | |
jessexm | 0:9df5e4328319 | 121 | /** Set the PWM pulsewidth, specified in milli-seconds (int), keeping the period the same. |
jessexm | 0:9df5e4328319 | 122 | * @param ms Change the pulse width of a PWM signal specified in milli-seconds |
jessexm | 0:9df5e4328319 | 123 | */ |
jessexm | 0:9df5e4328319 | 124 | void pulsewidth_ms(int ms) { |
jessexm | 0:9df5e4328319 | 125 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 126 | pwmout_pulsewidth_ms(ms); |
jessexm | 0:9df5e4328319 | 127 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 128 | } |
jessexm | 0:9df5e4328319 | 129 | |
jessexm | 0:9df5e4328319 | 130 | /** Set the PWM pulsewidth, specified in micro-seconds (int), keeping the period the same. |
jessexm | 0:9df5e4328319 | 131 | * @param us Change the pulse width of a PWM signal specified in micro-seconds |
jessexm | 0:9df5e4328319 | 132 | */ |
jessexm | 0:9df5e4328319 | 133 | void pulsewidth_us(int us) { |
jessexm | 0:9df5e4328319 | 134 | core_util_critical_section_enter(); |
jessexm | 0:9df5e4328319 | 135 | pwmout_pulsewidth_us(us); |
jessexm | 0:9df5e4328319 | 136 | core_util_critical_section_exit(); |
jessexm | 0:9df5e4328319 | 137 | } |
jessexm | 0:9df5e4328319 | 138 | |
jessexm | 0:9df5e4328319 | 139 | /** A operator shorthand for write() |
jessexm | 0:9df5e4328319 | 140 | * \sa PwmOut::write() |
jessexm | 0:9df5e4328319 | 141 | */ |
jessexm | 0:9df5e4328319 | 142 | MAX32630FTHR_PwmOut& operator= (float value) { |
jessexm | 0:9df5e4328319 | 143 | // Underlying call is thread safe |
jessexm | 0:9df5e4328319 | 144 | write(value); |
jessexm | 0:9df5e4328319 | 145 | return *this; |
jessexm | 0:9df5e4328319 | 146 | } |
jessexm | 0:9df5e4328319 | 147 | |
jessexm | 0:9df5e4328319 | 148 | /** A operator shorthand for write() |
jessexm | 0:9df5e4328319 | 149 | * \sa PwmOut::write() |
jessexm | 0:9df5e4328319 | 150 | */ |
jessexm | 0:9df5e4328319 | 151 | MAX32630FTHR_PwmOut& operator= (PwmOut& rhs) { |
jessexm | 0:9df5e4328319 | 152 | // Underlying call is thread safe |
jessexm | 0:9df5e4328319 | 153 | write(rhs.read()); |
jessexm | 0:9df5e4328319 | 154 | return *this; |
jessexm | 0:9df5e4328319 | 155 | } |
jessexm | 0:9df5e4328319 | 156 | |
jessexm | 0:9df5e4328319 | 157 | /** An operator shorthand for read() |
jessexm | 0:9df5e4328319 | 158 | * \sa PwmOut::read() |
jessexm | 0:9df5e4328319 | 159 | */ |
jessexm | 0:9df5e4328319 | 160 | operator float() { |
jessexm | 0:9df5e4328319 | 161 | // Underlying call is thread safe |
jessexm | 0:9df5e4328319 | 162 | return read(); |
jessexm | 0:9df5e4328319 | 163 | } |
jessexm | 0:9df5e4328319 | 164 | |
jessexm | 0:9df5e4328319 | 165 | protected: |
jessexm | 0:9df5e4328319 | 166 | /** Lock deep sleep only if it is not yet locked */ |
jessexm | 0:9df5e4328319 | 167 | void lock_deep_sleep() { |
jessexm | 0:9df5e4328319 | 168 | if (_deep_sleep_locked == false) { |
jessexm | 0:9df5e4328319 | 169 | sleep_manager_lock_deep_sleep(); |
jessexm | 0:9df5e4328319 | 170 | _deep_sleep_locked = true; |
jessexm | 0:9df5e4328319 | 171 | } |
jessexm | 0:9df5e4328319 | 172 | } |
jessexm | 0:9df5e4328319 | 173 | |
jessexm | 0:9df5e4328319 | 174 | /** Unlock deep sleep in case it is locked */ |
jessexm | 0:9df5e4328319 | 175 | void unlock_deep_sleep() { |
jessexm | 0:9df5e4328319 | 176 | if (_deep_sleep_locked == true) { |
jessexm | 0:9df5e4328319 | 177 | sleep_manager_unlock_deep_sleep(); |
jessexm | 0:9df5e4328319 | 178 | _deep_sleep_locked = false; |
jessexm | 0:9df5e4328319 | 179 | } |
jessexm | 0:9df5e4328319 | 180 | } |
jessexm | 0:9df5e4328319 | 181 | |
jessexm | 0:9df5e4328319 | 182 | bool _deep_sleep_locked; |
jessexm | 0:9df5e4328319 | 183 | |
jessexm | 0:9df5e4328319 | 184 | private: |
jessexm | 0:9df5e4328319 | 185 | /** Initialize the pwm out peripheral and configure the pin |
jessexm | 0:9df5e4328319 | 186 | * |
jessexm | 0:9df5e4328319 | 187 | * @param obj The pwmout object to initialize |
jessexm | 0:9df5e4328319 | 188 | * @param pin The pwmout pin to initialize |
jessexm | 0:9df5e4328319 | 189 | */ |
jessexm | 0:9df5e4328319 | 190 | void pwmout_init(PinName pin); |
jessexm | 0:9df5e4328319 | 191 | |
jessexm | 0:9df5e4328319 | 192 | /** Deinitialize the pwmout object |
jessexm | 0:9df5e4328319 | 193 | * |
jessexm | 0:9df5e4328319 | 194 | * @param obj The pwmout object |
jessexm | 0:9df5e4328319 | 195 | */ |
jessexm | 0:9df5e4328319 | 196 | void pwmout_free(); |
jessexm | 0:9df5e4328319 | 197 | |
jessexm | 0:9df5e4328319 | 198 | /** Set the output duty-cycle in range <0.0f, 1.0f> |
jessexm | 0:9df5e4328319 | 199 | * |
jessexm | 0:9df5e4328319 | 200 | * Value 0.0f represents 0 percentage, 1.0f represents 100 percent. |
jessexm | 0:9df5e4328319 | 201 | * @param obj The pwmout object |
jessexm | 0:9df5e4328319 | 202 | * @param percent The floating-point percentage number |
jessexm | 0:9df5e4328319 | 203 | */ |
jessexm | 0:9df5e4328319 | 204 | void pwmout_write(float percent); |
jessexm | 0:9df5e4328319 | 205 | |
jessexm | 0:9df5e4328319 | 206 | /** Read the current float-point output duty-cycle |
jessexm | 0:9df5e4328319 | 207 | * |
jessexm | 0:9df5e4328319 | 208 | * @param obj The pwmout object |
jessexm | 0:9df5e4328319 | 209 | * @return A floating-point output duty-cycle |
jessexm | 0:9df5e4328319 | 210 | */ |
jessexm | 0:9df5e4328319 | 211 | float pwmout_read(); |
jessexm | 0:9df5e4328319 | 212 | |
jessexm | 0:9df5e4328319 | 213 | /** Set the PWM period specified in seconds, keeping the duty cycle the same |
jessexm | 0:9df5e4328319 | 214 | * |
jessexm | 0:9df5e4328319 | 215 | * Periods smaller than microseconds (the lowest resolution) are set to zero. |
jessexm | 0:9df5e4328319 | 216 | * @param obj The pwmout object |
jessexm | 0:9df5e4328319 | 217 | * @param seconds The floating-point seconds period |
jessexm | 0:9df5e4328319 | 218 | */ |
jessexm | 0:9df5e4328319 | 219 | void pwmout_period(float seconds); |
jessexm | 0:9df5e4328319 | 220 | |
jessexm | 0:9df5e4328319 | 221 | /** Set the PWM period specified in miliseconds, keeping the duty cycle the same |
jessexm | 0:9df5e4328319 | 222 | * |
jessexm | 0:9df5e4328319 | 223 | * @param obj The pwmout object |
jessexm | 0:9df5e4328319 | 224 | * @param ms The milisecond period |
jessexm | 0:9df5e4328319 | 225 | */ |
jessexm | 0:9df5e4328319 | 226 | void pwmout_period_ms(int ms); |
jessexm | 0:9df5e4328319 | 227 | |
jessexm | 0:9df5e4328319 | 228 | /** Set the PWM period specified in microseconds, keeping the duty cycle the same |
jessexm | 0:9df5e4328319 | 229 | * |
jessexm | 0:9df5e4328319 | 230 | * @param obj The pwmout object |
jessexm | 0:9df5e4328319 | 231 | * @param us The microsecond period |
jessexm | 0:9df5e4328319 | 232 | */ |
jessexm | 0:9df5e4328319 | 233 | void pwmout_period_us(int us); |
jessexm | 0:9df5e4328319 | 234 | |
jessexm | 0:9df5e4328319 | 235 | /** Set the PWM pulsewidth specified in seconds, keeping the period the same. |
jessexm | 0:9df5e4328319 | 236 | * |
jessexm | 0:9df5e4328319 | 237 | * @param obj The pwmout object |
jessexm | 0:9df5e4328319 | 238 | * @param seconds The floating-point pulsewidth in seconds |
jessexm | 0:9df5e4328319 | 239 | */ |
jessexm | 0:9df5e4328319 | 240 | void pwmout_pulsewidth(float seconds); |
jessexm | 0:9df5e4328319 | 241 | |
jessexm | 0:9df5e4328319 | 242 | /** Set the PWM pulsewidth specified in miliseconds, keeping the period the same. |
jessexm | 0:9df5e4328319 | 243 | * |
jessexm | 0:9df5e4328319 | 244 | * @param obj The pwmout object |
jessexm | 0:9df5e4328319 | 245 | * @param ms The floating-point pulsewidth in miliseconds |
jessexm | 0:9df5e4328319 | 246 | */ |
jessexm | 0:9df5e4328319 | 247 | void pwmout_pulsewidth_ms(int ms); |
jessexm | 0:9df5e4328319 | 248 | |
jessexm | 0:9df5e4328319 | 249 | /** Set the PWM pulsewidth specified in microseconds, keeping the period the same. |
jessexm | 0:9df5e4328319 | 250 | * |
jessexm | 0:9df5e4328319 | 251 | * @param obj The pwmout object |
jessexm | 0:9df5e4328319 | 252 | * @param us The floating-point pulsewidth in microseconds |
jessexm | 0:9df5e4328319 | 253 | */ |
jessexm | 0:9df5e4328319 | 254 | void pwmout_pulsewidth_us(int us); |
jessexm | 0:9df5e4328319 | 255 | |
jessexm | 0:9df5e4328319 | 256 | |
jessexm | 0:9df5e4328319 | 257 | PinName pin; |
jessexm | 0:9df5e4328319 | 258 | mxc_tmr_regs_t *tmr; |
jessexm | 0:9df5e4328319 | 259 | int tmr_idx; |
jessexm | 0:9df5e4328319 | 260 | int pwm_period; |
jessexm | 0:9df5e4328319 | 261 | int pulse_width; |
jessexm | 0:9df5e4328319 | 262 | int last_pulse_width; |
jessexm | 0:9df5e4328319 | 263 | void pwmout_update(void); |
jessexm | 0:9df5e4328319 | 264 | }; |
jessexm | 0:9df5e4328319 | 265 | |
jessexm | 0:9df5e4328319 | 266 | #endif |