Watchdog Timer

Fork of mbed-src by mbed official

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