mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_DISCO_F100RB/pinmap.c@251:de9a1e4ffd79, 2014-07-08 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Jul 08 14:15:07 2014 +0100
- Revision:
- 251:de9a1e4ffd79
- Parent:
- 250:a49055e7a707
- Child:
- 285:31249416b6f9
Synchronized with git revision b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63
Full URL: https://github.com/mbedmicro/mbed/commit/b0ac1e3485a9aa1cb98d1ca68df9d1cdbcd6ce63/
Revert "error.h -> mbed_error.h"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 126:549ba18ddd81 | 1 | /* mbed Microcontroller Library |
mbed_official | 126:549ba18ddd81 | 2 | ******************************************************************************* |
mbed_official | 126:549ba18ddd81 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 126:549ba18ddd81 | 4 | * All rights reserved. |
mbed_official | 126:549ba18ddd81 | 5 | * |
mbed_official | 126:549ba18ddd81 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 126:549ba18ddd81 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 126:549ba18ddd81 | 8 | * |
mbed_official | 126:549ba18ddd81 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 126:549ba18ddd81 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 126:549ba18ddd81 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 126:549ba18ddd81 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 126:549ba18ddd81 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 126:549ba18ddd81 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 126:549ba18ddd81 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 126:549ba18ddd81 | 16 | * without specific prior written permission. |
mbed_official | 126:549ba18ddd81 | 17 | * |
mbed_official | 126:549ba18ddd81 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 126:549ba18ddd81 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 126:549ba18ddd81 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 126:549ba18ddd81 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 126:549ba18ddd81 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 126:549ba18ddd81 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 126:549ba18ddd81 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 126:549ba18ddd81 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 126:549ba18ddd81 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 126:549ba18ddd81 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 126:549ba18ddd81 | 28 | ******************************************************************************* |
mbed_official | 126:549ba18ddd81 | 29 | */ |
mbed_official | 227:7bd0639b8911 | 30 | #include "mbed_assert.h" |
mbed_official | 142:7f2b6c44bbe3 | 31 | #include "device.h" |
mbed_official | 126:549ba18ddd81 | 32 | #include "pinmap.h" |
mbed_official | 251:de9a1e4ffd79 | 33 | #include "error.h" |
mbed_official | 126:549ba18ddd81 | 34 | |
mbed_official | 126:549ba18ddd81 | 35 | // Alternate-function mapping |
mbed_official | 126:549ba18ddd81 | 36 | static const uint32_t AF_mapping[] = { |
mbed_official | 126:549ba18ddd81 | 37 | 0, // 0 = No AF |
mbed_official | 126:549ba18ddd81 | 38 | GPIO_Remap_SPI1, // 1 |
mbed_official | 126:549ba18ddd81 | 39 | GPIO_Remap_I2C1, // 2 |
mbed_official | 126:549ba18ddd81 | 40 | GPIO_Remap_USART1, // 3 |
mbed_official | 126:549ba18ddd81 | 41 | GPIO_Remap_USART2, // 4 |
mbed_official | 126:549ba18ddd81 | 42 | GPIO_FullRemap_TIM2, // 5 |
mbed_official | 126:549ba18ddd81 | 43 | GPIO_FullRemap_TIM3, // 6 |
mbed_official | 126:549ba18ddd81 | 44 | GPIO_PartialRemap_TIM3, // 7 |
mbed_official | 126:549ba18ddd81 | 45 | GPIO_Remap_I2C1 // 8 |
mbed_official | 126:549ba18ddd81 | 46 | }; |
mbed_official | 126:549ba18ddd81 | 47 | |
mbed_official | 126:549ba18ddd81 | 48 | // Enable GPIO clock and return GPIO base address |
mbed_official | 126:549ba18ddd81 | 49 | uint32_t Set_GPIO_Clock(uint32_t port_idx) { |
mbed_official | 126:549ba18ddd81 | 50 | uint32_t gpio_add = 0; |
mbed_official | 126:549ba18ddd81 | 51 | switch (port_idx) { |
mbed_official | 126:549ba18ddd81 | 52 | case PortA: |
mbed_official | 126:549ba18ddd81 | 53 | gpio_add = GPIOA_BASE; |
mbed_official | 126:549ba18ddd81 | 54 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); |
mbed_official | 126:549ba18ddd81 | 55 | break; |
mbed_official | 126:549ba18ddd81 | 56 | case PortB: |
mbed_official | 126:549ba18ddd81 | 57 | gpio_add = GPIOB_BASE; |
mbed_official | 126:549ba18ddd81 | 58 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); |
mbed_official | 126:549ba18ddd81 | 59 | break; |
mbed_official | 126:549ba18ddd81 | 60 | case PortC: |
mbed_official | 126:549ba18ddd81 | 61 | gpio_add = GPIOC_BASE; |
mbed_official | 126:549ba18ddd81 | 62 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); |
mbed_official | 126:549ba18ddd81 | 63 | break; |
mbed_official | 126:549ba18ddd81 | 64 | case PortD: |
mbed_official | 126:549ba18ddd81 | 65 | gpio_add = GPIOD_BASE; |
mbed_official | 126:549ba18ddd81 | 66 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); |
mbed_official | 126:549ba18ddd81 | 67 | break; |
mbed_official | 126:549ba18ddd81 | 68 | default: |
mbed_official | 126:549ba18ddd81 | 69 | error("Port number is not correct."); |
mbed_official | 126:549ba18ddd81 | 70 | break; |
mbed_official | 126:549ba18ddd81 | 71 | } |
mbed_official | 126:549ba18ddd81 | 72 | return gpio_add; |
mbed_official | 126:549ba18ddd81 | 73 | } |
mbed_official | 126:549ba18ddd81 | 74 | |
mbed_official | 126:549ba18ddd81 | 75 | /** |
mbed_official | 126:549ba18ddd81 | 76 | * Configure pin (input, output, alternate function or analog) + output speed + AF |
mbed_official | 126:549ba18ddd81 | 77 | */ |
mbed_official | 126:549ba18ddd81 | 78 | void pin_function(PinName pin, int data) { |
mbed_official | 227:7bd0639b8911 | 79 | MBED_ASSERT(pin != (PinName)NC); |
mbed_official | 126:549ba18ddd81 | 80 | // Get the pin informations |
mbed_official | 126:549ba18ddd81 | 81 | uint32_t mode = STM_PIN_MODE(data); |
mbed_official | 126:549ba18ddd81 | 82 | uint32_t afnum = STM_PIN_AFNUM(data); |
mbed_official | 126:549ba18ddd81 | 83 | |
mbed_official | 126:549ba18ddd81 | 84 | uint32_t port_index = STM_PORT(pin); |
mbed_official | 126:549ba18ddd81 | 85 | uint32_t pin_index = STM_PIN(pin); |
mbed_official | 126:549ba18ddd81 | 86 | |
mbed_official | 126:549ba18ddd81 | 87 | // Enable GPIO clock |
mbed_official | 126:549ba18ddd81 | 88 | uint32_t gpio_add = Set_GPIO_Clock(port_index); |
mbed_official | 126:549ba18ddd81 | 89 | GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; |
mbed_official | 126:549ba18ddd81 | 90 | |
mbed_official | 126:549ba18ddd81 | 91 | // Enable AFIO clock |
mbed_official | 126:549ba18ddd81 | 92 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); |
mbed_official | 126:549ba18ddd81 | 93 | |
mbed_official | 126:549ba18ddd81 | 94 | // Configure Alternate Function |
mbed_official | 126:549ba18ddd81 | 95 | // Warning: Must be done before the GPIO is initialized |
mbed_official | 126:549ba18ddd81 | 96 | if (afnum > 0) { |
mbed_official | 126:549ba18ddd81 | 97 | GPIO_PinRemapConfig(AF_mapping[afnum], ENABLE); |
mbed_official | 126:549ba18ddd81 | 98 | } |
mbed_official | 126:549ba18ddd81 | 99 | |
mbed_official | 126:549ba18ddd81 | 100 | // Configure GPIO |
mbed_official | 126:549ba18ddd81 | 101 | GPIO_InitTypeDef GPIO_InitStructure; |
mbed_official | 126:549ba18ddd81 | 102 | GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index); |
mbed_official | 126:549ba18ddd81 | 103 | GPIO_InitStructure.GPIO_Mode = (GPIOMode_TypeDef)mode; |
mbed_official | 126:549ba18ddd81 | 104 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
mbed_official | 126:549ba18ddd81 | 105 | GPIO_Init(gpio, &GPIO_InitStructure); |
mbed_official | 126:549ba18ddd81 | 106 | |
mbed_official | 126:549ba18ddd81 | 107 | // Disconnect JTAG-DP + SW-DP signals. |
mbed_official | 126:549ba18ddd81 | 108 | // Warning: Need to reconnect under reset |
mbed_official | 126:549ba18ddd81 | 109 | if ((pin == PA_13) || (pin == PA_14)) { |
mbed_official | 126:549ba18ddd81 | 110 | GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); |
mbed_official | 126:549ba18ddd81 | 111 | } |
mbed_official | 126:549ba18ddd81 | 112 | if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { |
mbed_official | 126:549ba18ddd81 | 113 | GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); |
mbed_official | 227:7bd0639b8911 | 114 | } |
mbed_official | 126:549ba18ddd81 | 115 | } |
mbed_official | 126:549ba18ddd81 | 116 | |
mbed_official | 126:549ba18ddd81 | 117 | /** |
mbed_official | 126:549ba18ddd81 | 118 | * Configure pin pull-up/pull-down |
mbed_official | 126:549ba18ddd81 | 119 | */ |
mbed_official | 126:549ba18ddd81 | 120 | void pin_mode(PinName pin, PinMode mode) { |
mbed_official | 227:7bd0639b8911 | 121 | MBED_ASSERT(pin != (PinName)NC); |
mbed_official | 126:549ba18ddd81 | 122 | GPIO_InitTypeDef GPIO_InitStructure; |
mbed_official | 126:549ba18ddd81 | 123 | |
mbed_official | 126:549ba18ddd81 | 124 | uint32_t port_index = STM_PORT(pin); |
mbed_official | 126:549ba18ddd81 | 125 | uint32_t pin_index = STM_PIN(pin); |
mbed_official | 126:549ba18ddd81 | 126 | |
mbed_official | 126:549ba18ddd81 | 127 | // Enable GPIO clock |
mbed_official | 126:549ba18ddd81 | 128 | uint32_t gpio_add = Set_GPIO_Clock(port_index); |
mbed_official | 126:549ba18ddd81 | 129 | GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; |
mbed_official | 126:549ba18ddd81 | 130 | |
mbed_official | 126:549ba18ddd81 | 131 | // Configure open-drain and pull-up/down |
mbed_official | 126:549ba18ddd81 | 132 | switch (mode) { |
mbed_official | 227:7bd0639b8911 | 133 | case PullNone: |
mbed_official | 227:7bd0639b8911 | 134 | return; |
mbed_official | 227:7bd0639b8911 | 135 | case PullUp: |
mbed_official | 227:7bd0639b8911 | 136 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; |
mbed_official | 227:7bd0639b8911 | 137 | break; |
mbed_official | 227:7bd0639b8911 | 138 | case PullDown: |
mbed_official | 227:7bd0639b8911 | 139 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; |
mbed_official | 227:7bd0639b8911 | 140 | break; |
mbed_official | 227:7bd0639b8911 | 141 | case OpenDrain: |
mbed_official | 227:7bd0639b8911 | 142 | if (pin_index < 8) { |
mbed_official | 227:7bd0639b8911 | 143 | if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode |
mbed_official | 227:7bd0639b8911 | 144 | gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain |
mbed_official | 227:7bd0639b8911 | 145 | } |
mbed_official | 227:7bd0639b8911 | 146 | } else { |
mbed_official | 227:7bd0639b8911 | 147 | if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode |
mbed_official | 227:7bd0639b8911 | 148 | gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain |
mbed_official | 227:7bd0639b8911 | 149 | } |
mbed_official | 126:549ba18ddd81 | 150 | } |
mbed_official | 227:7bd0639b8911 | 151 | return; |
mbed_official | 227:7bd0639b8911 | 152 | default: |
mbed_official | 227:7bd0639b8911 | 153 | break; |
mbed_official | 126:549ba18ddd81 | 154 | } |
mbed_official | 126:549ba18ddd81 | 155 | |
mbed_official | 126:549ba18ddd81 | 156 | // Configure GPIO |
mbed_official | 126:549ba18ddd81 | 157 | GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index); |
mbed_official | 126:549ba18ddd81 | 158 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
mbed_official | 227:7bd0639b8911 | 159 | GPIO_Init(gpio, &GPIO_InitStructure); |
mbed_official | 126:549ba18ddd81 | 160 | } |