Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-dev/targets/TARGET_Maxim/TARGET_MAX32620/pwmout_api.c@18:6a4db94011d3, 2017-05-14 (annotated)
- Committer:
- sahilmgandhi
- Date:
- Sun May 14 23:18:57 2017 +0000
- Revision:
- 18:6a4db94011d3
Publishing again
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| sahilmgandhi | 18:6a4db94011d3 | 1 | /******************************************************************************* | 
| sahilmgandhi | 18:6a4db94011d3 | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. | 
| sahilmgandhi | 18:6a4db94011d3 | 3 | * | 
| sahilmgandhi | 18:6a4db94011d3 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a | 
| sahilmgandhi | 18:6a4db94011d3 | 5 | * copy of this software and associated documentation files (the "Software"), | 
| sahilmgandhi | 18:6a4db94011d3 | 6 | * to deal in the Software without restriction, including without limitation | 
| sahilmgandhi | 18:6a4db94011d3 | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | 
| sahilmgandhi | 18:6a4db94011d3 | 8 | * and/or sell copies of the Software, and to permit persons to whom the | 
| sahilmgandhi | 18:6a4db94011d3 | 9 | * Software is furnished to do so, subject to the following conditions: | 
| sahilmgandhi | 18:6a4db94011d3 | 10 | * | 
| sahilmgandhi | 18:6a4db94011d3 | 11 | * The above copyright notice and this permission notice shall be included | 
| sahilmgandhi | 18:6a4db94011d3 | 12 | * in all copies or substantial portions of the Software. | 
| sahilmgandhi | 18:6a4db94011d3 | 13 | * | 
| sahilmgandhi | 18:6a4db94011d3 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | 
| sahilmgandhi | 18:6a4db94011d3 | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 
| sahilmgandhi | 18:6a4db94011d3 | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | 
| sahilmgandhi | 18:6a4db94011d3 | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES | 
| sahilmgandhi | 18:6a4db94011d3 | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | 
| sahilmgandhi | 18:6a4db94011d3 | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | 
| sahilmgandhi | 18:6a4db94011d3 | 20 | * OTHER DEALINGS IN THE SOFTWARE. | 
| sahilmgandhi | 18:6a4db94011d3 | 21 | * | 
| sahilmgandhi | 18:6a4db94011d3 | 22 | * Except as contained in this notice, the name of Maxim Integrated | 
| sahilmgandhi | 18:6a4db94011d3 | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated | 
| sahilmgandhi | 18:6a4db94011d3 | 24 | * Products, Inc. Branding Policy. | 
| sahilmgandhi | 18:6a4db94011d3 | 25 | * | 
| sahilmgandhi | 18:6a4db94011d3 | 26 | * The mere transfer of this software does not imply any licenses | 
| sahilmgandhi | 18:6a4db94011d3 | 27 | * of trade secrets, proprietary technology, copyrights, patents, | 
| sahilmgandhi | 18:6a4db94011d3 | 28 | * trademarks, maskwork rights, or any other form of intellectual | 
| sahilmgandhi | 18:6a4db94011d3 | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all | 
| sahilmgandhi | 18:6a4db94011d3 | 30 | * ownership rights. | 
| sahilmgandhi | 18:6a4db94011d3 | 31 | ******************************************************************************* | 
| sahilmgandhi | 18:6a4db94011d3 | 32 | */ | 
| sahilmgandhi | 18:6a4db94011d3 | 33 | |
| sahilmgandhi | 18:6a4db94011d3 | 34 | #include "mbed_assert.h" | 
| sahilmgandhi | 18:6a4db94011d3 | 35 | #include "cmsis.h" | 
| sahilmgandhi | 18:6a4db94011d3 | 36 | #include "pwmout_api.h" | 
| sahilmgandhi | 18:6a4db94011d3 | 37 | #include "pinmap.h" | 
| sahilmgandhi | 18:6a4db94011d3 | 38 | #include "clkman_regs.h" | 
| sahilmgandhi | 18:6a4db94011d3 | 39 | #include "PeripheralPins.h" | 
| sahilmgandhi | 18:6a4db94011d3 | 40 | |
| sahilmgandhi | 18:6a4db94011d3 | 41 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 42 | void pwmout_init(pwmout_t* obj, PinName pin) | 
| sahilmgandhi | 18:6a4db94011d3 | 43 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 44 | // Make sure the pin is free for GPIO use | 
| sahilmgandhi | 18:6a4db94011d3 | 45 | unsigned int port = (unsigned int)pin >> PORT_SHIFT; | 
| sahilmgandhi | 18:6a4db94011d3 | 46 | unsigned int port_pin = (unsigned int)pin & ~(0xFFFFFFFF << PORT_SHIFT); | 
| sahilmgandhi | 18:6a4db94011d3 | 47 | MBED_ASSERT(MXC_GPIO->free[port] & (0x1 << port_pin)); | 
| sahilmgandhi | 18:6a4db94011d3 | 48 | |
| sahilmgandhi | 18:6a4db94011d3 | 49 | int i = 0; | 
| sahilmgandhi | 18:6a4db94011d3 | 50 | PinMap pwm = PinMap_PWM[0]; | 
| sahilmgandhi | 18:6a4db94011d3 | 51 | |
| sahilmgandhi | 18:6a4db94011d3 | 52 | // Check if there is a pulse train already active on this port | 
| sahilmgandhi | 18:6a4db94011d3 | 53 | int pin_func = (MXC_GPIO->func_sel[port] & (0xF << (port_pin*4))) >> (port_pin*4); | 
| sahilmgandhi | 18:6a4db94011d3 | 54 | MBED_ASSERT((pin_func < 1) || (pin_func > 3)); | 
| sahilmgandhi | 18:6a4db94011d3 | 55 | |
| sahilmgandhi | 18:6a4db94011d3 | 56 | // Search through PinMap_PWM to find the pin | 
| sahilmgandhi | 18:6a4db94011d3 | 57 | while (pwm.pin != pin) { | 
| sahilmgandhi | 18:6a4db94011d3 | 58 | pwm = PinMap_PWM[++i]; | 
| sahilmgandhi | 18:6a4db94011d3 | 59 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 60 | |
| sahilmgandhi | 18:6a4db94011d3 | 61 | // Find a free PT instance on this pin | 
| sahilmgandhi | 18:6a4db94011d3 | 62 | while (pwm.pin == pin) { | 
| sahilmgandhi | 18:6a4db94011d3 | 63 | |
| sahilmgandhi | 18:6a4db94011d3 | 64 | // Check to see if this PT instance is free | 
| sahilmgandhi | 18:6a4db94011d3 | 65 | if (((mxc_pt_regs_t*)pwm.peripheral)->rate_length & MXC_F_PT_RATE_LENGTH_MODE) { | 
| sahilmgandhi | 18:6a4db94011d3 | 66 | break; | 
| sahilmgandhi | 18:6a4db94011d3 | 67 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 68 | |
| sahilmgandhi | 18:6a4db94011d3 | 69 | pwm = PinMap_PWM[++i]; | 
| sahilmgandhi | 18:6a4db94011d3 | 70 | |
| sahilmgandhi | 18:6a4db94011d3 | 71 | // Raise an assertion if we can not allocate another PT instance. | 
| sahilmgandhi | 18:6a4db94011d3 | 72 | MBED_ASSERT(pwm.pin == pin); | 
| sahilmgandhi | 18:6a4db94011d3 | 73 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 74 | |
| sahilmgandhi | 18:6a4db94011d3 | 75 | // Enable the clock | 
| sahilmgandhi | 18:6a4db94011d3 | 76 | MXC_CLKMAN->sys_clk_ctrl_7_pt = MXC_S_CLKMAN_CLK_SCALE_DIV_1; | 
| sahilmgandhi | 18:6a4db94011d3 | 77 | |
| sahilmgandhi | 18:6a4db94011d3 | 78 | // Set the obj pointer to the propper PWM instance | 
| sahilmgandhi | 18:6a4db94011d3 | 79 | obj->pwm = (mxc_pt_regs_t*)pwm.peripheral; | 
| sahilmgandhi | 18:6a4db94011d3 | 80 | |
| sahilmgandhi | 18:6a4db94011d3 | 81 | // Initialize object period and pulse width | 
| sahilmgandhi | 18:6a4db94011d3 | 82 | obj->period = -1; | 
| sahilmgandhi | 18:6a4db94011d3 | 83 | obj->pulse_width = -1; | 
| sahilmgandhi | 18:6a4db94011d3 | 84 | |
| sahilmgandhi | 18:6a4db94011d3 | 85 | // Disable the output | 
| sahilmgandhi | 18:6a4db94011d3 | 86 | obj->pwm->train = 0x0; | 
| sahilmgandhi | 18:6a4db94011d3 | 87 | obj->pwm->rate_length = 0x0; | 
| sahilmgandhi | 18:6a4db94011d3 | 88 | |
| sahilmgandhi | 18:6a4db94011d3 | 89 | // Configure the pin | 
| sahilmgandhi | 18:6a4db94011d3 | 90 | pin_mode(pin, (PinMode)PullNone); | 
| sahilmgandhi | 18:6a4db94011d3 | 91 | pin_function(pin, pwm.function); | 
| sahilmgandhi | 18:6a4db94011d3 | 92 | |
| sahilmgandhi | 18:6a4db94011d3 | 93 | // default to 20ms: standard for servos, and fine for e.g. brightness control | 
| sahilmgandhi | 18:6a4db94011d3 | 94 | pwmout_period_us(obj, 20000); | 
| sahilmgandhi | 18:6a4db94011d3 | 95 | pwmout_write (obj, 0); | 
| sahilmgandhi | 18:6a4db94011d3 | 96 | |
| sahilmgandhi | 18:6a4db94011d3 | 97 | // Set the drive mode to normal | 
| sahilmgandhi | 18:6a4db94011d3 | 98 | MXC_SET_FIELD(&MXC_GPIO->out_mode[port], (0x7 << (port_pin*4)), (MXC_V_GPIO_OUT_MODE_NORMAL << (port_pin*4))); | 
| sahilmgandhi | 18:6a4db94011d3 | 99 | |
| sahilmgandhi | 18:6a4db94011d3 | 100 | // Enable this PWM channel | 
| sahilmgandhi | 18:6a4db94011d3 | 101 | MXC_PTG->enable |= (1 << MXC_PT_GET_IDX(obj->pwm)); | 
| sahilmgandhi | 18:6a4db94011d3 | 102 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 103 | |
| sahilmgandhi | 18:6a4db94011d3 | 104 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 105 | void pwmout_free(pwmout_t* obj) | 
| sahilmgandhi | 18:6a4db94011d3 | 106 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 107 | // Set the registers to the reset value | 
| sahilmgandhi | 18:6a4db94011d3 | 108 | obj->pwm->train = 0; | 
| sahilmgandhi | 18:6a4db94011d3 | 109 | obj->pwm->rate_length = 0x08000000; | 
| sahilmgandhi | 18:6a4db94011d3 | 110 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 111 | |
| sahilmgandhi | 18:6a4db94011d3 | 112 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 113 | static void pwmout_update(pwmout_t* obj) | 
| sahilmgandhi | 18:6a4db94011d3 | 114 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 115 | // Calculate and set the divider ratio | 
| sahilmgandhi | 18:6a4db94011d3 | 116 | int div = (obj->period * (SystemCoreClock/1000000))/32; | 
| sahilmgandhi | 18:6a4db94011d3 | 117 | if (div < 2) { | 
| sahilmgandhi | 18:6a4db94011d3 | 118 | div = 2; | 
| sahilmgandhi | 18:6a4db94011d3 | 119 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 120 | MXC_SET_FIELD(&obj->pwm->rate_length, MXC_F_PT_RATE_LENGTH_RATE_CONTROL, div); | 
| sahilmgandhi | 18:6a4db94011d3 | 121 | |
| sahilmgandhi | 18:6a4db94011d3 | 122 | // Change the duty cycle to adjust the pulse width | 
| sahilmgandhi | 18:6a4db94011d3 | 123 | obj->pwm->train = (0xFFFFFFFF << (32-((32*obj->pulse_width)/obj->period))); | 
| sahilmgandhi | 18:6a4db94011d3 | 124 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 125 | |
| sahilmgandhi | 18:6a4db94011d3 | 126 | |
| sahilmgandhi | 18:6a4db94011d3 | 127 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 128 | void pwmout_write(pwmout_t* obj, float percent) | 
| sahilmgandhi | 18:6a4db94011d3 | 129 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 130 | // Saturate percent if outside of range | 
| sahilmgandhi | 18:6a4db94011d3 | 131 | if (percent < 0.0f) { | 
| sahilmgandhi | 18:6a4db94011d3 | 132 | percent = 0.0f; | 
| sahilmgandhi | 18:6a4db94011d3 | 133 | } else if(percent > 1.0f) { | 
| sahilmgandhi | 18:6a4db94011d3 | 134 | percent = 1.0f; | 
| sahilmgandhi | 18:6a4db94011d3 | 135 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 136 | |
| sahilmgandhi | 18:6a4db94011d3 | 137 | // Resize the pulse width to set the duty cycle | 
| sahilmgandhi | 18:6a4db94011d3 | 138 | pwmout_pulsewidth_us(obj, (int)(percent*obj->period)); | 
| sahilmgandhi | 18:6a4db94011d3 | 139 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 140 | |
| sahilmgandhi | 18:6a4db94011d3 | 141 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 142 | float pwmout_read(pwmout_t* obj) | 
| sahilmgandhi | 18:6a4db94011d3 | 143 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 144 | // Check for when pulsewidth or period equals 0 | 
| sahilmgandhi | 18:6a4db94011d3 | 145 | if ((obj->pulse_width == 0) || (obj->period == 0)) { | 
| sahilmgandhi | 18:6a4db94011d3 | 146 | return 0; | 
| sahilmgandhi | 18:6a4db94011d3 | 147 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 148 | |
| sahilmgandhi | 18:6a4db94011d3 | 149 | // Return the duty cycle | 
| sahilmgandhi | 18:6a4db94011d3 | 150 | return ((float)obj->pulse_width / (float)obj->period); | 
| sahilmgandhi | 18:6a4db94011d3 | 151 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 152 | |
| sahilmgandhi | 18:6a4db94011d3 | 153 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 154 | void pwmout_period(pwmout_t* obj, float seconds) | 
| sahilmgandhi | 18:6a4db94011d3 | 155 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 156 | pwmout_period_us(obj, (int)(seconds * 1000000.0f)); | 
| sahilmgandhi | 18:6a4db94011d3 | 157 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 158 | |
| sahilmgandhi | 18:6a4db94011d3 | 159 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 160 | void pwmout_period_ms(pwmout_t* obj, int ms) | 
| sahilmgandhi | 18:6a4db94011d3 | 161 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 162 | pwmout_period_us(obj, ms*1000); | 
| sahilmgandhi | 18:6a4db94011d3 | 163 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 164 | |
| sahilmgandhi | 18:6a4db94011d3 | 165 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 166 | void pwmout_period_us(pwmout_t* obj, int us) | 
| sahilmgandhi | 18:6a4db94011d3 | 167 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 168 | // Check the range of the period | 
| sahilmgandhi | 18:6a4db94011d3 | 169 | MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock/32))); | 
| sahilmgandhi | 18:6a4db94011d3 | 170 | |
| sahilmgandhi | 18:6a4db94011d3 | 171 | // Set pulse width to half the period if uninitialized | 
| sahilmgandhi | 18:6a4db94011d3 | 172 | if (obj->pulse_width == -1) { | 
| sahilmgandhi | 18:6a4db94011d3 | 173 | obj->pulse_width = us/2; | 
| sahilmgandhi | 18:6a4db94011d3 | 174 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 175 | |
| sahilmgandhi | 18:6a4db94011d3 | 176 | // Save the period | 
| sahilmgandhi | 18:6a4db94011d3 | 177 | obj->period = us; | 
| sahilmgandhi | 18:6a4db94011d3 | 178 | |
| sahilmgandhi | 18:6a4db94011d3 | 179 | // Update the registers | 
| sahilmgandhi | 18:6a4db94011d3 | 180 | pwmout_update(obj); | 
| sahilmgandhi | 18:6a4db94011d3 | 181 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 182 | |
| sahilmgandhi | 18:6a4db94011d3 | 183 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 184 | void pwmout_pulsewidth(pwmout_t* obj, float seconds) | 
| sahilmgandhi | 18:6a4db94011d3 | 185 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 186 | pwmout_pulsewidth_us(obj, (int)(seconds * 1000000.0f)); | 
| sahilmgandhi | 18:6a4db94011d3 | 187 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 188 | |
| sahilmgandhi | 18:6a4db94011d3 | 189 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 190 | void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) | 
| sahilmgandhi | 18:6a4db94011d3 | 191 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 192 | pwmout_pulsewidth_us(obj, ms*1000); | 
| sahilmgandhi | 18:6a4db94011d3 | 193 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 194 | |
| sahilmgandhi | 18:6a4db94011d3 | 195 | //****************************************************************************** | 
| sahilmgandhi | 18:6a4db94011d3 | 196 | void pwmout_pulsewidth_us(pwmout_t* obj, int us) | 
| sahilmgandhi | 18:6a4db94011d3 | 197 | { | 
| sahilmgandhi | 18:6a4db94011d3 | 198 | // Check the range of the pulsewidth | 
| sahilmgandhi | 18:6a4db94011d3 | 199 | MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock/32))); | 
| sahilmgandhi | 18:6a4db94011d3 | 200 | |
| sahilmgandhi | 18:6a4db94011d3 | 201 | // Initialize period to double the pulsewidth if uninitialized | 
| sahilmgandhi | 18:6a4db94011d3 | 202 | if (obj->period == -1) { | 
| sahilmgandhi | 18:6a4db94011d3 | 203 | obj->period = 2*us; | 
| sahilmgandhi | 18:6a4db94011d3 | 204 | } | 
| sahilmgandhi | 18:6a4db94011d3 | 205 | |
| sahilmgandhi | 18:6a4db94011d3 | 206 | // Save the pulsewidth | 
| sahilmgandhi | 18:6a4db94011d3 | 207 | obj->pulse_width = us; | 
| sahilmgandhi | 18:6a4db94011d3 | 208 | |
| sahilmgandhi | 18:6a4db94011d3 | 209 | // Update the register | 
| sahilmgandhi | 18:6a4db94011d3 | 210 | pwmout_update(obj); | 
| sahilmgandhi | 18:6a4db94011d3 | 211 | } |