fixed drive strength

Dependents:   capstone_i2c

Fork of mbed-dev by mbed official

Committer:
cpadua
Date:
Tue Apr 11 20:39:24 2017 +0000
Revision:
163:1d4c9d0af1e9
Parent:
149:156823d33999
fixed i2c-api.c

Who changed what in which revision?

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