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