mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Fri Jul 31 14:00:09 2015 +0100
Revision:
599:efb83a170500
Parent:
507:d4fc7603a669
Child:
603:3c75ef011213
Synchronized with git revision 5b1dc60c03fa1b3a5d082ac137d15d6584f7fe1a

Full URL: https://github.com/mbedmicro/mbed/commit/5b1dc60c03fa1b3a5d082ac137d15d6584f7fe1a/

MAXWSNENV, MAX32600MBED - Fixing pwm array search.

Who changed what in which revision?

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