mbed library sources. Supersedes mbed-src. removed serial

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Thu Feb 02 17:01:33 2017 +0000
Revision:
157:ff67d9f36b67
This updates the lib to the mbed lib v135

Who changed what in which revision?

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