test

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Tue Nov 08 17:45:16 2016 +0000
Revision:
150:02e0a0aed4ec
This updates the lib to the mbed lib v129

Who changed what in which revision?

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