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:
Thu Aug 13 10:30:09 2015 +0100
Revision:
603:3c75ef011213
Parent:
599:efb83a170500
Synchronized with git revision 7cbf9896cc73d825b242028a5f95030a9e1016a4

Full URL: https://github.com/mbedmicro/mbed/commit/7cbf9896cc73d825b242028a5f95030a9e1016a4/

Added requirements.txt file

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 603:3c75ef011213 55 MBED_ASSERT((pin_func < 1) || (pin_func > 3));
mbed_official 603:3c75ef011213 56
mbed_official 603:3c75ef011213 57 // Search through PinMap_PWM to find the pin
mbed_official 603:3c75ef011213 58 while(pwm.pin != pin) {
mbed_official 603:3c75ef011213 59 pwm = PinMap_PWM[++i];
mbed_official 603:3c75ef011213 60 }
mbed_official 603:3c75ef011213 61
mbed_official 603:3c75ef011213 62 // Find a free PT instance on this pin
mbed_official 603:3c75ef011213 63 while(pwm.pin == pin) {
mbed_official 603:3c75ef011213 64
mbed_official 603:3c75ef011213 65 // Check to see if this PT instance is free
mbed_official 603:3c75ef011213 66 if((((mxc_pt_regs_t*)pwm.peripheral)->rate_length &
mbed_official 603:3c75ef011213 67 MXC_F_PT_RATE_LENGTH_MODE)) {
mbed_official 603:3c75ef011213 68 break;
mbed_official 507:d4fc7603a669 69 }
mbed_official 507:d4fc7603a669 70
mbed_official 603:3c75ef011213 71 pwm = PinMap_PWM[++i];
mbed_official 507:d4fc7603a669 72
mbed_official 603:3c75ef011213 73 // Raise an assertion if we can not allocate another PT instance.
mbed_official 603:3c75ef011213 74 MBED_ASSERT(pwm.pin == pin);
mbed_official 507:d4fc7603a669 75 }
mbed_official 507:d4fc7603a669 76
mbed_official 507:d4fc7603a669 77 // Enable the clock
mbed_official 507:d4fc7603a669 78 MXC_CLKMAN->clk_ctrl_2_pt = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
mbed_official 507:d4fc7603a669 79
mbed_official 507:d4fc7603a669 80 // Set the obj pointer to the propper PWM instance
mbed_official 507:d4fc7603a669 81 obj->pwm = (mxc_pt_regs_t*)pwm.peripheral;
mbed_official 507:d4fc7603a669 82
mbed_official 507:d4fc7603a669 83 // Initialize object period and pulse width
mbed_official 507:d4fc7603a669 84 obj->period = -1;
mbed_official 507:d4fc7603a669 85 obj->pulse_width = -1;
mbed_official 507:d4fc7603a669 86
mbed_official 507:d4fc7603a669 87 // Disable the output
mbed_official 507:d4fc7603a669 88 obj->pwm->train = 0x0;
mbed_official 507:d4fc7603a669 89 obj->pwm->rate_length = 0x0;
mbed_official 507:d4fc7603a669 90
mbed_official 507:d4fc7603a669 91 // Configure the pin
mbed_official 507:d4fc7603a669 92 pin_mode(pin, (PinMode)PullNone);
mbed_official 507:d4fc7603a669 93 pin_function(pin, pwm.function);
mbed_official 507:d4fc7603a669 94
mbed_official 507:d4fc7603a669 95 // default to 20ms: standard for servos, and fine for e.g. brightness control
mbed_official 507:d4fc7603a669 96 pwmout_period_us(obj, 20000);
mbed_official 507:d4fc7603a669 97 pwmout_write (obj, 0);
mbed_official 507:d4fc7603a669 98
mbed_official 603:3c75ef011213 99 // Set the drive mode to normal
mbed_official 603:3c75ef011213 100 MXC_SET_FIELD(&MXC_GPIO->out_mode[port], (0x7 << (port_pin*4)), (MXC_V_GPIO_OUT_MODE_NORMAL_DRIVE << (port_pin*4)));
mbed_official 603:3c75ef011213 101
mbed_official 507:d4fc7603a669 102 // Enable the global pwm
mbed_official 507:d4fc7603a669 103 MXC_PTG->ctrl = MXC_F_PT_CTRL_ENABLE_ALL;
mbed_official 507:d4fc7603a669 104 }
mbed_official 507:d4fc7603a669 105
mbed_official 507:d4fc7603a669 106 //******************************************************************************
mbed_official 507:d4fc7603a669 107 void pwmout_free(pwmout_t* obj)
mbed_official 507:d4fc7603a669 108 {
mbed_official 507:d4fc7603a669 109 // Set the registers to the reset value
mbed_official 507:d4fc7603a669 110 obj->pwm->train = 0;
mbed_official 507:d4fc7603a669 111 obj->pwm->rate_length = 0x08000000;
mbed_official 507:d4fc7603a669 112 }
mbed_official 507:d4fc7603a669 113
mbed_official 507:d4fc7603a669 114 //******************************************************************************
mbed_official 507:d4fc7603a669 115 static void pwmout_update(pwmout_t* obj)
mbed_official 507:d4fc7603a669 116 {
mbed_official 507:d4fc7603a669 117 // Calculate and set the divider ratio
mbed_official 507:d4fc7603a669 118 int div = (obj->period * (SystemCoreClock/1000000))/32;
mbed_official 507:d4fc7603a669 119 if (div < 2){
mbed_official 507:d4fc7603a669 120 div = 2;
mbed_official 507:d4fc7603a669 121 }
mbed_official 507:d4fc7603a669 122 MXC_SET_FIELD(&obj->pwm->rate_length, MXC_F_PT_RATE_LENGTH_RATE_CONTROL, div);
mbed_official 507:d4fc7603a669 123
mbed_official 507:d4fc7603a669 124 // Change the duty cycle to adjust the pulse width
mbed_official 507:d4fc7603a669 125 obj->pwm->train = (0xFFFFFFFF << (32-((32*obj->pulse_width)/obj->period)));
mbed_official 507:d4fc7603a669 126 }
mbed_official 507:d4fc7603a669 127
mbed_official 507:d4fc7603a669 128
mbed_official 507:d4fc7603a669 129 //******************************************************************************
mbed_official 507:d4fc7603a669 130 void pwmout_write(pwmout_t* obj, float percent)
mbed_official 507:d4fc7603a669 131 {
mbed_official 507:d4fc7603a669 132 // Saturate percent if outside of range
mbed_official 507:d4fc7603a669 133 if(percent < 0.0) {
mbed_official 507:d4fc7603a669 134 percent = 0.0;
mbed_official 507:d4fc7603a669 135 } else if(percent > 1.0) {
mbed_official 507:d4fc7603a669 136 percent = 1.0;
mbed_official 507:d4fc7603a669 137 }
mbed_official 507:d4fc7603a669 138
mbed_official 507:d4fc7603a669 139 // Resize the pulse width to set the duty cycle
mbed_official 507:d4fc7603a669 140 pwmout_pulsewidth_us(obj, (int)(percent*obj->period));
mbed_official 507:d4fc7603a669 141 }
mbed_official 507:d4fc7603a669 142
mbed_official 507:d4fc7603a669 143 //******************************************************************************
mbed_official 507:d4fc7603a669 144 float pwmout_read(pwmout_t* obj)
mbed_official 507:d4fc7603a669 145 {
mbed_official 507:d4fc7603a669 146 // Check for when pulsewidth or period equals 0
mbed_official 507:d4fc7603a669 147 if((obj->pulse_width == 0) || (obj->period == 0)){
mbed_official 507:d4fc7603a669 148 return 0;
mbed_official 507:d4fc7603a669 149 }
mbed_official 507:d4fc7603a669 150
mbed_official 507:d4fc7603a669 151 // Return the duty cycle
mbed_official 507:d4fc7603a669 152 return ((float)obj->pulse_width / (float)obj->period);
mbed_official 507:d4fc7603a669 153 }
mbed_official 507:d4fc7603a669 154
mbed_official 507:d4fc7603a669 155 //******************************************************************************
mbed_official 507:d4fc7603a669 156 void pwmout_period(pwmout_t* obj, float seconds)
mbed_official 507:d4fc7603a669 157 {
mbed_official 507:d4fc7603a669 158 pwmout_period_us(obj, (int)(seconds * 1000000.0));
mbed_official 507:d4fc7603a669 159 }
mbed_official 507:d4fc7603a669 160
mbed_official 507:d4fc7603a669 161 //******************************************************************************
mbed_official 507:d4fc7603a669 162 void pwmout_period_ms(pwmout_t* obj, int ms)
mbed_official 507:d4fc7603a669 163 {
mbed_official 507:d4fc7603a669 164 pwmout_period_us(obj, ms*1000);
mbed_official 507:d4fc7603a669 165 }
mbed_official 507:d4fc7603a669 166
mbed_official 507:d4fc7603a669 167 //******************************************************************************
mbed_official 507:d4fc7603a669 168 void pwmout_period_us(pwmout_t* obj, int us)
mbed_official 507:d4fc7603a669 169 {
mbed_official 507:d4fc7603a669 170 // Check the range of the period
mbed_official 507:d4fc7603a669 171 MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock/32)));
mbed_official 507:d4fc7603a669 172
mbed_official 507:d4fc7603a669 173 // Set pulse width to half the period if uninitialized
mbed_official 507:d4fc7603a669 174 if(obj->pulse_width == -1){
mbed_official 507:d4fc7603a669 175 obj->pulse_width = us/2;
mbed_official 507:d4fc7603a669 176 }
mbed_official 507:d4fc7603a669 177
mbed_official 507:d4fc7603a669 178 // Save the period
mbed_official 507:d4fc7603a669 179 obj->period = us;
mbed_official 507:d4fc7603a669 180
mbed_official 507:d4fc7603a669 181 // Update the registers
mbed_official 507:d4fc7603a669 182 pwmout_update(obj);
mbed_official 507:d4fc7603a669 183 }
mbed_official 507:d4fc7603a669 184
mbed_official 507:d4fc7603a669 185 //******************************************************************************
mbed_official 507:d4fc7603a669 186 void pwmout_pulsewidth(pwmout_t* obj, float seconds)
mbed_official 507:d4fc7603a669 187 {
mbed_official 507:d4fc7603a669 188 pwmout_pulsewidth_us(obj, (int)(seconds * 1000000.0));
mbed_official 507:d4fc7603a669 189 }
mbed_official 507:d4fc7603a669 190
mbed_official 507:d4fc7603a669 191 //******************************************************************************
mbed_official 507:d4fc7603a669 192 void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
mbed_official 507:d4fc7603a669 193 {
mbed_official 507:d4fc7603a669 194 pwmout_pulsewidth_us(obj, ms*1000);
mbed_official 507:d4fc7603a669 195 }
mbed_official 507:d4fc7603a669 196
mbed_official 507:d4fc7603a669 197 //******************************************************************************
mbed_official 507:d4fc7603a669 198 void pwmout_pulsewidth_us(pwmout_t* obj, int us)
mbed_official 507:d4fc7603a669 199 {
mbed_official 507:d4fc7603a669 200 // Check the range of the pulsewidth
mbed_official 507:d4fc7603a669 201 MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock/32)));
mbed_official 507:d4fc7603a669 202
mbed_official 507:d4fc7603a669 203 // Initialize period to double the pulsewidth if uninitialized
mbed_official 507:d4fc7603a669 204 if(obj->period == -1){
mbed_official 507:d4fc7603a669 205 obj->period = 2*us;
mbed_official 507:d4fc7603a669 206 }
mbed_official 507:d4fc7603a669 207
mbed_official 507:d4fc7603a669 208 // Save the pulsewidth
mbed_official 507:d4fc7603a669 209 obj->pulse_width = us;
mbed_official 507:d4fc7603a669 210
mbed_official 507:d4fc7603a669 211 // Update the register
mbed_official 507:d4fc7603a669 212 pwmout_update(obj);
mbed_official 507:d4fc7603a669 213 }