test

Committer:
elijahsj
Date:
Mon Nov 09 00:33:19 2020 -0500
Revision:
2:4364577b5ad8
Parent:
1:8a094db1347f
copied mbed library

Who changed what in which revision?

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