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