t
Fork of mbed-dev by
targets/TARGET_STM/TARGET_STM32L0/pinmap.c@178:c26431f84b0d, 2017-11-09 (annotated)
- Committer:
- amithy
- Date:
- Thu Nov 09 22:14:37 2017 +0000
- Revision:
- 178:c26431f84b0d
- Parent:
- 149:156823d33999
test export
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 149:156823d33999 | 1 | /* mbed Microcontroller Library |
<> | 149:156823d33999 | 2 | ******************************************************************************* |
<> | 149:156823d33999 | 3 | * Copyright (c) 2015, STMicroelectronics |
<> | 149:156823d33999 | 4 | * All rights reserved. |
<> | 149:156823d33999 | 5 | * |
<> | 149:156823d33999 | 6 | * Redistribution and use in source and binary forms, with or without |
<> | 149:156823d33999 | 7 | * modification, are permitted provided that the following conditions are met: |
<> | 149:156823d33999 | 8 | * |
<> | 149:156823d33999 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
<> | 149:156823d33999 | 10 | * this list of conditions and the following disclaimer. |
<> | 149:156823d33999 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
<> | 149:156823d33999 | 12 | * this list of conditions and the following disclaimer in the documentation |
<> | 149:156823d33999 | 13 | * and/or other materials provided with the distribution. |
<> | 149:156823d33999 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
<> | 149:156823d33999 | 15 | * may be used to endorse or promote products derived from this software |
<> | 149:156823d33999 | 16 | * without specific prior written permission. |
<> | 149:156823d33999 | 17 | * |
<> | 149:156823d33999 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
<> | 149:156823d33999 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
<> | 149:156823d33999 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
<> | 149:156823d33999 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
<> | 149:156823d33999 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
<> | 149:156823d33999 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
<> | 149:156823d33999 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
<> | 149:156823d33999 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
<> | 149:156823d33999 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
<> | 149:156823d33999 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
<> | 149:156823d33999 | 28 | ******************************************************************************* |
<> | 149:156823d33999 | 29 | */ |
<> | 149:156823d33999 | 30 | #include "mbed_assert.h" |
<> | 149:156823d33999 | 31 | #include "pinmap.h" |
<> | 149:156823d33999 | 32 | #include "PortNames.h" |
<> | 149:156823d33999 | 33 | #include "mbed_error.h" |
<> | 149:156823d33999 | 34 | |
<> | 149:156823d33999 | 35 | // GPIO mode look-up table |
<> | 149:156823d33999 | 36 | // Warning: order must be the same as the one defined in PinNames.h !!! |
<> | 149:156823d33999 | 37 | static const uint32_t gpio_mode[13] = { |
<> | 149:156823d33999 | 38 | 0x00000000, // 0 = GPIO_MODE_INPUT |
<> | 149:156823d33999 | 39 | 0x00000001, // 1 = GPIO_MODE_OUTPUT_PP |
<> | 149:156823d33999 | 40 | 0x00000011, // 2 = GPIO_MODE_OUTPUT_OD |
<> | 149:156823d33999 | 41 | 0x00000002, // 3 = GPIO_MODE_AF_PP |
<> | 149:156823d33999 | 42 | 0x00000012, // 4 = GPIO_MODE_AF_OD |
<> | 149:156823d33999 | 43 | 0x00000003, // 5 = GPIO_MODE_ANALOG |
<> | 149:156823d33999 | 44 | 0x10110000, // 6 = GPIO_MODE_IT_RISING |
<> | 149:156823d33999 | 45 | 0x10210000, // 7 = GPIO_MODE_IT_FALLING |
<> | 149:156823d33999 | 46 | 0x10310000, // 8 = GPIO_MODE_IT_RISING_FALLING |
<> | 149:156823d33999 | 47 | 0x10120000, // 9 = GPIO_MODE_EVT_RISING |
<> | 149:156823d33999 | 48 | 0x10220000, // 10 = GPIO_MODE_EVT_FALLING |
<> | 149:156823d33999 | 49 | 0x10320000, // 11 = GPIO_MODE_EVT_RISING_FALLING |
<> | 149:156823d33999 | 50 | 0x10000000 // 12 = Reset IT and EVT (not in STM32Cube HAL) |
<> | 149:156823d33999 | 51 | }; |
<> | 149:156823d33999 | 52 | |
<> | 149:156823d33999 | 53 | // Enable GPIO clock and return GPIO base address |
<> | 149:156823d33999 | 54 | uint32_t Set_GPIO_Clock(uint32_t port_idx) |
<> | 149:156823d33999 | 55 | { |
<> | 149:156823d33999 | 56 | uint32_t gpio_add = 0; |
<> | 149:156823d33999 | 57 | switch (port_idx) { |
<> | 149:156823d33999 | 58 | case PortA: |
<> | 149:156823d33999 | 59 | gpio_add = GPIOA_BASE; |
<> | 149:156823d33999 | 60 | __GPIOA_CLK_ENABLE(); |
<> | 149:156823d33999 | 61 | break; |
<> | 149:156823d33999 | 62 | case PortB: |
<> | 149:156823d33999 | 63 | gpio_add = GPIOB_BASE; |
<> | 149:156823d33999 | 64 | __GPIOB_CLK_ENABLE(); |
<> | 149:156823d33999 | 65 | break; |
<> | 149:156823d33999 | 66 | #if defined(GPIOC_BASE) |
<> | 149:156823d33999 | 67 | case PortC: |
<> | 149:156823d33999 | 68 | gpio_add = GPIOC_BASE; |
<> | 149:156823d33999 | 69 | __GPIOC_CLK_ENABLE(); |
<> | 149:156823d33999 | 70 | break; |
<> | 149:156823d33999 | 71 | #endif |
<> | 149:156823d33999 | 72 | #if defined(GPIOD_BASE) |
<> | 149:156823d33999 | 73 | case PortD: |
<> | 149:156823d33999 | 74 | gpio_add = GPIOD_BASE; |
<> | 149:156823d33999 | 75 | __GPIOD_CLK_ENABLE(); |
<> | 149:156823d33999 | 76 | break; |
<> | 149:156823d33999 | 77 | #endif |
<> | 149:156823d33999 | 78 | #if defined(GPIOH_BASE) |
<> | 149:156823d33999 | 79 | case PortH: |
<> | 149:156823d33999 | 80 | gpio_add = GPIOH_BASE; |
<> | 149:156823d33999 | 81 | __GPIOH_CLK_ENABLE(); |
<> | 149:156823d33999 | 82 | break; |
<> | 149:156823d33999 | 83 | #endif |
<> | 149:156823d33999 | 84 | default: |
<> | 149:156823d33999 | 85 | error("Pinmap error: wrong port number."); |
<> | 149:156823d33999 | 86 | break; |
<> | 149:156823d33999 | 87 | } |
<> | 149:156823d33999 | 88 | return gpio_add; |
<> | 149:156823d33999 | 89 | } |
<> | 149:156823d33999 | 90 | |
<> | 149:156823d33999 | 91 | /** |
<> | 149:156823d33999 | 92 | * Configure pin (mode, speed, output type and pull-up/pull-down) |
<> | 149:156823d33999 | 93 | */ |
<> | 149:156823d33999 | 94 | void pin_function(PinName pin, int data) |
<> | 149:156823d33999 | 95 | { |
<> | 149:156823d33999 | 96 | MBED_ASSERT(pin != (PinName)NC); |
<> | 149:156823d33999 | 97 | // Get the pin informations |
<> | 149:156823d33999 | 98 | uint32_t mode = STM_PIN_MODE(data); |
<> | 149:156823d33999 | 99 | uint32_t pupd = STM_PIN_PUPD(data); |
<> | 149:156823d33999 | 100 | uint32_t afnum = STM_PIN_AFNUM(data); |
<> | 149:156823d33999 | 101 | |
<> | 149:156823d33999 | 102 | uint32_t port_index = STM_PORT(pin); |
<> | 149:156823d33999 | 103 | uint32_t pin_index = STM_PIN(pin); |
<> | 149:156823d33999 | 104 | |
<> | 149:156823d33999 | 105 | // Enable GPIO clock |
<> | 149:156823d33999 | 106 | uint32_t gpio_add = Set_GPIO_Clock(port_index); |
<> | 149:156823d33999 | 107 | GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; |
<> | 149:156823d33999 | 108 | |
<> | 149:156823d33999 | 109 | // Configure GPIO |
<> | 149:156823d33999 | 110 | GPIO_InitTypeDef GPIO_InitStructure; |
<> | 149:156823d33999 | 111 | GPIO_InitStructure.Pin = (uint32_t)(1 << pin_index); |
<> | 149:156823d33999 | 112 | GPIO_InitStructure.Mode = gpio_mode[mode]; |
<> | 149:156823d33999 | 113 | GPIO_InitStructure.Pull = pupd; |
<> | 149:156823d33999 | 114 | GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; |
<> | 149:156823d33999 | 115 | GPIO_InitStructure.Alternate = afnum; |
<> | 149:156823d33999 | 116 | HAL_GPIO_Init(gpio, &GPIO_InitStructure); |
<> | 149:156823d33999 | 117 | |
<> | 149:156823d33999 | 118 | // [TODO] Disconnect JTAG-DP + SW-DP signals. |
<> | 149:156823d33999 | 119 | // Warning: Need to reconnect under reset |
<> | 149:156823d33999 | 120 | //if ((pin == PA_13) || (pin == PA_14)) { |
<> | 149:156823d33999 | 121 | // |
<> | 149:156823d33999 | 122 | //} |
<> | 149:156823d33999 | 123 | //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { |
<> | 149:156823d33999 | 124 | // |
<> | 149:156823d33999 | 125 | //} |
<> | 149:156823d33999 | 126 | } |
<> | 149:156823d33999 | 127 | |
<> | 149:156823d33999 | 128 | /** |
<> | 149:156823d33999 | 129 | * Configure pin pull-up/pull-down |
<> | 149:156823d33999 | 130 | */ |
<> | 149:156823d33999 | 131 | void pin_mode(PinName pin, PinMode mode) |
<> | 149:156823d33999 | 132 | { |
<> | 149:156823d33999 | 133 | MBED_ASSERT(pin != (PinName)NC); |
<> | 149:156823d33999 | 134 | uint32_t port_index = STM_PORT(pin); |
<> | 149:156823d33999 | 135 | uint32_t pin_index = STM_PIN(pin); |
<> | 149:156823d33999 | 136 | |
<> | 149:156823d33999 | 137 | // Enable GPIO clock |
<> | 149:156823d33999 | 138 | uint32_t gpio_add = Set_GPIO_Clock(port_index); |
<> | 149:156823d33999 | 139 | GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; |
<> | 149:156823d33999 | 140 | |
<> | 149:156823d33999 | 141 | // Configure pull-up/pull-down resistors |
<> | 149:156823d33999 | 142 | uint32_t pupd = (uint32_t)mode; |
<> | 149:156823d33999 | 143 | if (pupd > 2) |
<> | 149:156823d33999 | 144 | { |
<> | 149:156823d33999 | 145 | pupd = 0; // Open-drain = No pull-up/No pull-down |
<> | 149:156823d33999 | 146 | } |
<> | 149:156823d33999 | 147 | gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPD0 << (pin_index * 2))); |
<> | 149:156823d33999 | 148 | gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2)); |
<> | 149:156823d33999 | 149 | } |
<> | 149:156823d33999 | 150 | |
<> | 149:156823d33999 | 151 | /* Internal function for setting the gpiomode/function |
<> | 149:156823d33999 | 152 | * without changing Pull mode |
<> | 149:156823d33999 | 153 | */ |
<> | 149:156823d33999 | 154 | void pin_function_gpiomode(PinName pin, uint32_t gpiomode) { |
<> | 149:156823d33999 | 155 | |
<> | 149:156823d33999 | 156 | /* Read current pull state from HW to avoid over-write*/ |
<> | 149:156823d33999 | 157 | uint32_t port_index = STM_PORT(pin); |
<> | 149:156823d33999 | 158 | uint32_t pin_index = STM_PIN(pin); |
<> | 149:156823d33999 | 159 | GPIO_TypeDef *gpio = (GPIO_TypeDef *) Set_GPIO_Clock(port_index); |
<> | 149:156823d33999 | 160 | uint32_t temp = gpio->PUPDR; |
<> | 149:156823d33999 | 161 | uint32_t pull = (temp >> (pin_index * 2U)) & GPIO_PUPDR_PUPD0; |
<> | 149:156823d33999 | 162 | |
<> | 149:156823d33999 | 163 | /* Then re-use global function for updating the mode part*/ |
<> | 149:156823d33999 | 164 | pin_function(pin, STM_PIN_DATA(gpiomode, pull, 0)); |
<> | 149:156823d33999 | 165 | } |