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