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