mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins

Dependents:   IGLOO_board

Fork of mbed-dev by mbed official

Committer:
Anna Bridge
Date:
Fri Jun 22 16:45:37 2018 +0100
Revision:
186:707f6e361f3e
mbed-dev library. Release version 162

Who changed what in which revision?

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