5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
jacobjohnson
Date:
Mon Feb 27 17:45:05 2017 +0000
Revision:
1:f30bdcd2b33b
Parent:
0:098463de4c5d
changed the inputscale from 1 to 7 in analogin_api.c.  This will need to be changed later, and accessed from the main level, but for now this allows the  adc to read a value from 0 to 3.7V, instead of just up to 1V.;

Who changed what in which revision?

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