Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 *******************************************************************************
lypinator 0:bb348c97df44 3 * Copyright (c) 2017, STMicroelectronics
lypinator 0:bb348c97df44 4 * All rights reserved.
lypinator 0:bb348c97df44 5 *
lypinator 0:bb348c97df44 6 * Redistribution and use in source and binary forms, with or without
lypinator 0:bb348c97df44 7 * modification, are permitted provided that the following conditions are met:
lypinator 0:bb348c97df44 8 *
lypinator 0:bb348c97df44 9 * 1. Redistributions of source code must retain the above copyright notice,
lypinator 0:bb348c97df44 10 * this list of conditions and the following disclaimer.
lypinator 0:bb348c97df44 11 * 2. Redistributions in binary form must reproduce the above copyright notice,
lypinator 0:bb348c97df44 12 * this list of conditions and the following disclaimer in the documentation
lypinator 0:bb348c97df44 13 * and/or other materials provided with the distribution.
lypinator 0:bb348c97df44 14 * 3. Neither the name of STMicroelectronics nor the names of its contributors
lypinator 0:bb348c97df44 15 * may be used to endorse or promote products derived from this software
lypinator 0:bb348c97df44 16 * without specific prior written permission.
lypinator 0:bb348c97df44 17 *
lypinator 0:bb348c97df44 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
lypinator 0:bb348c97df44 19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
lypinator 0:bb348c97df44 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
lypinator 0:bb348c97df44 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
lypinator 0:bb348c97df44 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
lypinator 0:bb348c97df44 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
lypinator 0:bb348c97df44 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
lypinator 0:bb348c97df44 25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
lypinator 0:bb348c97df44 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
lypinator 0:bb348c97df44 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
lypinator 0:bb348c97df44 28 *******************************************************************************
lypinator 0:bb348c97df44 29 */
lypinator 0:bb348c97df44 30 #include "mbed_assert.h"
lypinator 0:bb348c97df44 31 #include "pinmap.h"
lypinator 0:bb348c97df44 32 #include "PortNames.h"
lypinator 0:bb348c97df44 33 #include "mbed_error.h"
lypinator 0:bb348c97df44 34 #include "pin_device.h"
lypinator 0:bb348c97df44 35
lypinator 0:bb348c97df44 36 extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
lypinator 0:bb348c97df44 37
lypinator 0:bb348c97df44 38 const uint32_t ll_pin_defines[16] = {
lypinator 0:bb348c97df44 39 LL_GPIO_PIN_0,
lypinator 0:bb348c97df44 40 LL_GPIO_PIN_1,
lypinator 0:bb348c97df44 41 LL_GPIO_PIN_2,
lypinator 0:bb348c97df44 42 LL_GPIO_PIN_3,
lypinator 0:bb348c97df44 43 LL_GPIO_PIN_4,
lypinator 0:bb348c97df44 44 LL_GPIO_PIN_5,
lypinator 0:bb348c97df44 45 LL_GPIO_PIN_6,
lypinator 0:bb348c97df44 46 LL_GPIO_PIN_7,
lypinator 0:bb348c97df44 47 LL_GPIO_PIN_8,
lypinator 0:bb348c97df44 48 LL_GPIO_PIN_9,
lypinator 0:bb348c97df44 49 LL_GPIO_PIN_10,
lypinator 0:bb348c97df44 50 LL_GPIO_PIN_11,
lypinator 0:bb348c97df44 51 LL_GPIO_PIN_12,
lypinator 0:bb348c97df44 52 LL_GPIO_PIN_13,
lypinator 0:bb348c97df44 53 LL_GPIO_PIN_14,
lypinator 0:bb348c97df44 54 LL_GPIO_PIN_15
lypinator 0:bb348c97df44 55 };
lypinator 0:bb348c97df44 56
lypinator 0:bb348c97df44 57 /**
lypinator 0:bb348c97df44 58 * Configure pin (mode, speed, output type and pull-up/pull-down)
lypinator 0:bb348c97df44 59 */
lypinator 0:bb348c97df44 60 void pin_function(PinName pin, int data)
lypinator 0:bb348c97df44 61 {
lypinator 0:bb348c97df44 62 MBED_ASSERT(pin != (PinName)NC);
lypinator 0:bb348c97df44 63
lypinator 0:bb348c97df44 64 // Get the pin informations
lypinator 0:bb348c97df44 65 uint32_t mode = STM_PIN_FUNCTION(data);
lypinator 0:bb348c97df44 66 uint32_t afnum = STM_PIN_AFNUM(data);
lypinator 0:bb348c97df44 67 uint32_t port = STM_PORT(pin);
lypinator 0:bb348c97df44 68 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
lypinator 0:bb348c97df44 69 uint32_t ll_mode = 0;
lypinator 0:bb348c97df44 70
lypinator 0:bb348c97df44 71 // Enable GPIO clock
lypinator 0:bb348c97df44 72 GPIO_TypeDef *gpio = Set_GPIO_Clock(port);
lypinator 0:bb348c97df44 73
lypinator 0:bb348c97df44 74 /* Set default speed to high.
lypinator 0:bb348c97df44 75 * For most families there are dedicated registers so it is
lypinator 0:bb348c97df44 76 * not so important, register can be set at any time.
lypinator 0:bb348c97df44 77 * But for families like F1, speed only applies to output.
lypinator 0:bb348c97df44 78 */
lypinator 0:bb348c97df44 79 #if defined (TARGET_STM32F1)
lypinator 0:bb348c97df44 80 if (mode == STM_PIN_OUTPUT) {
lypinator 0:bb348c97df44 81 #endif
lypinator 0:bb348c97df44 82 LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
lypinator 0:bb348c97df44 83 #if defined (TARGET_STM32F1)
lypinator 0:bb348c97df44 84 }
lypinator 0:bb348c97df44 85 #endif
lypinator 0:bb348c97df44 86
lypinator 0:bb348c97df44 87 switch (mode) {
lypinator 0:bb348c97df44 88 case STM_PIN_INPUT:
lypinator 0:bb348c97df44 89 ll_mode = LL_GPIO_MODE_INPUT;
lypinator 0:bb348c97df44 90 break;
lypinator 0:bb348c97df44 91 case STM_PIN_OUTPUT:
lypinator 0:bb348c97df44 92 ll_mode = LL_GPIO_MODE_OUTPUT;
lypinator 0:bb348c97df44 93 break;
lypinator 0:bb348c97df44 94 case STM_PIN_ALTERNATE:
lypinator 0:bb348c97df44 95 ll_mode = LL_GPIO_MODE_ALTERNATE;
lypinator 0:bb348c97df44 96 // In case of ALT function, also set he afnum
lypinator 0:bb348c97df44 97 stm_pin_SetAFPin(gpio, pin, afnum);
lypinator 0:bb348c97df44 98 break;
lypinator 0:bb348c97df44 99 case STM_PIN_ANALOG:
lypinator 0:bb348c97df44 100 ll_mode = LL_GPIO_MODE_ANALOG;
lypinator 0:bb348c97df44 101 break;
lypinator 0:bb348c97df44 102 default:
lypinator 0:bb348c97df44 103 MBED_ASSERT(0);
lypinator 0:bb348c97df44 104 break;
lypinator 0:bb348c97df44 105 }
lypinator 0:bb348c97df44 106 LL_GPIO_SetPinMode(gpio, ll_pin, ll_mode);
lypinator 0:bb348c97df44 107
lypinator 0:bb348c97df44 108 #if defined(GPIO_ASCR_ASC0)
lypinator 0:bb348c97df44 109 /* For families where Analog Control ASC0 register is present */
lypinator 0:bb348c97df44 110 if (STM_PIN_ANALOG_CONTROL(data)) {
lypinator 0:bb348c97df44 111 LL_GPIO_EnablePinAnalogControl(gpio, ll_pin);
lypinator 0:bb348c97df44 112 } else {
lypinator 0:bb348c97df44 113 LL_GPIO_DisablePinAnalogControl(gpio, ll_pin);
lypinator 0:bb348c97df44 114 }
lypinator 0:bb348c97df44 115 #endif
lypinator 0:bb348c97df44 116
lypinator 0:bb348c97df44 117 /* For now by default use Speed HIGH for output or alt modes */
lypinator 0:bb348c97df44 118 if ((mode == STM_PIN_OUTPUT) || (mode == STM_PIN_ALTERNATE)) {
lypinator 0:bb348c97df44 119 if (STM_PIN_OD(data)) {
lypinator 0:bb348c97df44 120 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
lypinator 0:bb348c97df44 121 } else {
lypinator 0:bb348c97df44 122 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
lypinator 0:bb348c97df44 123 }
lypinator 0:bb348c97df44 124 }
lypinator 0:bb348c97df44 125
lypinator 0:bb348c97df44 126 stm_pin_PullConfig(gpio, ll_pin, STM_PIN_PUPD(data));
lypinator 0:bb348c97df44 127
lypinator 0:bb348c97df44 128 stm_pin_DisconnectDebug(pin);
lypinator 0:bb348c97df44 129 }
lypinator 0:bb348c97df44 130
lypinator 0:bb348c97df44 131 /**
lypinator 0:bb348c97df44 132 * Configure pin pull-up/pull-down
lypinator 0:bb348c97df44 133 */
lypinator 0:bb348c97df44 134 void pin_mode(PinName pin, PinMode mode)
lypinator 0:bb348c97df44 135 {
lypinator 0:bb348c97df44 136 MBED_ASSERT(pin != (PinName)NC);
lypinator 0:bb348c97df44 137
lypinator 0:bb348c97df44 138 uint32_t port_index = STM_PORT(pin);
lypinator 0:bb348c97df44 139 uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
lypinator 0:bb348c97df44 140 // Enable GPIO clock
lypinator 0:bb348c97df44 141 GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
lypinator 0:bb348c97df44 142 uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
lypinator 0:bb348c97df44 143
lypinator 0:bb348c97df44 144 if ((function == LL_GPIO_MODE_OUTPUT) || (function == LL_GPIO_MODE_ALTERNATE)) {
lypinator 0:bb348c97df44 145 if ((mode == OpenDrainNoPull) || (mode == OpenDrainPullUp) || (mode == OpenDrainPullDown)) {
lypinator 0:bb348c97df44 146 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
lypinator 0:bb348c97df44 147 } else {
lypinator 0:bb348c97df44 148 LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
lypinator 0:bb348c97df44 149 }
lypinator 0:bb348c97df44 150 }
lypinator 0:bb348c97df44 151
lypinator 0:bb348c97df44 152 if ((mode == OpenDrainPullUp) || (mode == PullUp)) {
lypinator 0:bb348c97df44 153 stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLUP);
lypinator 0:bb348c97df44 154 } else if ((mode == OpenDrainPullDown) || (mode == PullDown)) {
lypinator 0:bb348c97df44 155 stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLDOWN);
lypinator 0:bb348c97df44 156 } else {
lypinator 0:bb348c97df44 157 stm_pin_PullConfig(gpio, ll_pin, GPIO_NOPULL);
lypinator 0:bb348c97df44 158 }
lypinator 0:bb348c97df44 159 }