Modified for BG96

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Tue Mar 14 16:40:56 2017 +0000
Revision:
160:d5399cc887bb
Child:
167:e84263d55307
This updates the lib to the mbed lib v138

Who changed what in which revision?

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