mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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