mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_DISCO_F303VC/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 | 157:90e3acc479a2 | 1 | /* mbed Microcontroller Library |
mbed_official | 157:90e3acc479a2 | 2 | ******************************************************************************* |
mbed_official | 157:90e3acc479a2 | 3 | * Copyright (c) 2014, STMicroelectronics |
mbed_official | 157:90e3acc479a2 | 4 | * All rights reserved. |
mbed_official | 157:90e3acc479a2 | 5 | * |
mbed_official | 157:90e3acc479a2 | 6 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 157:90e3acc479a2 | 7 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 157:90e3acc479a2 | 8 | * |
mbed_official | 157:90e3acc479a2 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 157:90e3acc479a2 | 10 | * this list of conditions and the following disclaimer. |
mbed_official | 157:90e3acc479a2 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 157:90e3acc479a2 | 12 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 157:90e3acc479a2 | 13 | * and/or other materials provided with the distribution. |
mbed_official | 157:90e3acc479a2 | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mbed_official | 157:90e3acc479a2 | 15 | * may be used to endorse or promote products derived from this software |
mbed_official | 157:90e3acc479a2 | 16 | * without specific prior written permission. |
mbed_official | 157:90e3acc479a2 | 17 | * |
mbed_official | 157:90e3acc479a2 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mbed_official | 157:90e3acc479a2 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mbed_official | 157:90e3acc479a2 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mbed_official | 157:90e3acc479a2 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mbed_official | 157:90e3acc479a2 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 157:90e3acc479a2 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mbed_official | 157:90e3acc479a2 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 157:90e3acc479a2 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mbed_official | 157:90e3acc479a2 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mbed_official | 157:90e3acc479a2 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 157:90e3acc479a2 | 28 | ******************************************************************************* |
mbed_official | 157:90e3acc479a2 | 29 | */ |
mbed_official | 227:7bd0639b8911 | 30 | #include "mbed_assert.h" |
mbed_official | 157:90e3acc479a2 | 31 | #include "pinmap.h" |
mbed_official | 241:ffe41b0c8126 | 32 | #include "PortNames.h" |
mbed_official | 251:de9a1e4ffd79 | 33 | #include "error.h" |
mbed_official | 157:90e3acc479a2 | 34 | |
mbed_official | 157:90e3acc479a2 | 35 | // Enable GPIO clock and return GPIO base address |
mbed_official | 157:90e3acc479a2 | 36 | uint32_t Set_GPIO_Clock(uint32_t port_idx) { |
mbed_official | 157:90e3acc479a2 | 37 | uint32_t gpio_add; |
mbed_official | 157:90e3acc479a2 | 38 | switch (port_idx) { |
mbed_official | 157:90e3acc479a2 | 39 | case PortA: |
mbed_official | 157:90e3acc479a2 | 40 | gpio_add = GPIOA_BASE; |
mbed_official | 157:90e3acc479a2 | 41 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); |
mbed_official | 157:90e3acc479a2 | 42 | break; |
mbed_official | 157:90e3acc479a2 | 43 | case PortB: |
mbed_official | 157:90e3acc479a2 | 44 | gpio_add = GPIOB_BASE; |
mbed_official | 157:90e3acc479a2 | 45 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); |
mbed_official | 157:90e3acc479a2 | 46 | break; |
mbed_official | 157:90e3acc479a2 | 47 | case PortC: |
mbed_official | 157:90e3acc479a2 | 48 | gpio_add = GPIOC_BASE; |
mbed_official | 157:90e3acc479a2 | 49 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE); |
mbed_official | 157:90e3acc479a2 | 50 | break; |
mbed_official | 157:90e3acc479a2 | 51 | case PortD: |
mbed_official | 157:90e3acc479a2 | 52 | gpio_add = GPIOD_BASE; |
mbed_official | 157:90e3acc479a2 | 53 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD, ENABLE); |
mbed_official | 157:90e3acc479a2 | 54 | break; |
mbed_official | 157:90e3acc479a2 | 55 | case PortE: |
mbed_official | 157:90e3acc479a2 | 56 | gpio_add = GPIOE_BASE; |
mbed_official | 157:90e3acc479a2 | 57 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE); |
mbed_official | 157:90e3acc479a2 | 58 | break; |
mbed_official | 157:90e3acc479a2 | 59 | case PortF: |
mbed_official | 157:90e3acc479a2 | 60 | gpio_add = GPIOF_BASE; |
mbed_official | 157:90e3acc479a2 | 61 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE); |
mbed_official | 157:90e3acc479a2 | 62 | break; |
mbed_official | 157:90e3acc479a2 | 63 | default: |
mbed_official | 157:90e3acc479a2 | 64 | gpio_add = 0; |
mbed_official | 157:90e3acc479a2 | 65 | error("Port number is not correct."); |
mbed_official | 157:90e3acc479a2 | 66 | break; |
mbed_official | 157:90e3acc479a2 | 67 | } |
mbed_official | 157:90e3acc479a2 | 68 | return gpio_add; |
mbed_official | 157:90e3acc479a2 | 69 | } |
mbed_official | 157:90e3acc479a2 | 70 | |
mbed_official | 157:90e3acc479a2 | 71 | /** |
mbed_official | 157:90e3acc479a2 | 72 | * Configure pin (mode, speed, output type and pull-up/pull-down) |
mbed_official | 157:90e3acc479a2 | 73 | */ |
mbed_official | 157:90e3acc479a2 | 74 | void pin_function(PinName pin, int data) { |
mbed_official | 227:7bd0639b8911 | 75 | MBED_ASSERT(pin != (PinName)NC); |
mbed_official | 157:90e3acc479a2 | 76 | |
mbed_official | 157:90e3acc479a2 | 77 | // Get the pin informations |
mbed_official | 157:90e3acc479a2 | 78 | uint32_t mode = STM_PIN_MODE(data); |
mbed_official | 157:90e3acc479a2 | 79 | uint32_t otype = STM_PIN_OTYPE(data); |
mbed_official | 157:90e3acc479a2 | 80 | uint32_t pupd = STM_PIN_PUPD(data); |
mbed_official | 157:90e3acc479a2 | 81 | uint32_t afnum = STM_PIN_AFNUM(data); |
mbed_official | 157:90e3acc479a2 | 82 | |
mbed_official | 157:90e3acc479a2 | 83 | uint32_t port_index = STM_PORT(pin); |
mbed_official | 157:90e3acc479a2 | 84 | uint32_t pin_index = STM_PIN(pin); |
mbed_official | 157:90e3acc479a2 | 85 | |
mbed_official | 157:90e3acc479a2 | 86 | // Enable GPIO clock |
mbed_official | 157:90e3acc479a2 | 87 | uint32_t gpio_add = Set_GPIO_Clock(port_index); |
mbed_official | 157:90e3acc479a2 | 88 | GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; |
mbed_official | 157:90e3acc479a2 | 89 | |
mbed_official | 157:90e3acc479a2 | 90 | // Configure Alternate Function |
mbed_official | 157:90e3acc479a2 | 91 | // Warning: Must be done before the GPIO is initialized |
mbed_official | 157:90e3acc479a2 | 92 | if (afnum != 0xFF) { |
mbed_official | 157:90e3acc479a2 | 93 | GPIO_PinAFConfig(gpio, (uint16_t)pin_index, afnum); |
mbed_official | 157:90e3acc479a2 | 94 | } |
mbed_official | 157:90e3acc479a2 | 95 | |
mbed_official | 157:90e3acc479a2 | 96 | // Configure GPIO |
mbed_official | 157:90e3acc479a2 | 97 | GPIO_InitTypeDef GPIO_InitStructure; |
mbed_official | 157:90e3acc479a2 | 98 | GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index); |
mbed_official | 157:90e3acc479a2 | 99 | GPIO_InitStructure.GPIO_Mode = (GPIOMode_TypeDef)mode; |
mbed_official | 157:90e3acc479a2 | 100 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; |
mbed_official | 157:90e3acc479a2 | 101 | GPIO_InitStructure.GPIO_OType = (GPIOOType_TypeDef)otype; |
mbed_official | 157:90e3acc479a2 | 102 | GPIO_InitStructure.GPIO_PuPd = (GPIOPuPd_TypeDef)pupd; |
mbed_official | 157:90e3acc479a2 | 103 | GPIO_Init(gpio, &GPIO_InitStructure); |
mbed_official | 157:90e3acc479a2 | 104 | |
mbed_official | 157:90e3acc479a2 | 105 | // [TODO] Disconnect JTAG-DP + SW-DP signals. |
mbed_official | 157:90e3acc479a2 | 106 | // Warning: Need to reconnect under reset |
mbed_official | 157:90e3acc479a2 | 107 | //if ((pin == PA_13) || (pin == PA_14)) { |
mbed_official | 157:90e3acc479a2 | 108 | // |
mbed_official | 157:90e3acc479a2 | 109 | //} |
mbed_official | 157:90e3acc479a2 | 110 | //if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { |
mbed_official | 157:90e3acc479a2 | 111 | // |
mbed_official | 227:7bd0639b8911 | 112 | //} |
mbed_official | 157:90e3acc479a2 | 113 | } |
mbed_official | 157:90e3acc479a2 | 114 | |
mbed_official | 157:90e3acc479a2 | 115 | /** |
mbed_official | 157:90e3acc479a2 | 116 | * Configure pin pull-up/pull-down |
mbed_official | 157:90e3acc479a2 | 117 | */ |
mbed_official | 157:90e3acc479a2 | 118 | void pin_mode(PinName pin, PinMode mode) { |
mbed_official | 227:7bd0639b8911 | 119 | MBED_ASSERT(pin != (PinName)NC); |
mbed_official | 157:90e3acc479a2 | 120 | |
mbed_official | 157:90e3acc479a2 | 121 | uint32_t port_index = STM_PORT(pin); |
mbed_official | 157:90e3acc479a2 | 122 | uint32_t pin_index = STM_PIN(pin); |
mbed_official | 157:90e3acc479a2 | 123 | |
mbed_official | 157:90e3acc479a2 | 124 | // Enable GPIO clock |
mbed_official | 157:90e3acc479a2 | 125 | uint32_t gpio_add = Set_GPIO_Clock(port_index); |
mbed_official | 157:90e3acc479a2 | 126 | GPIO_TypeDef *gpio = (GPIO_TypeDef *)gpio_add; |
mbed_official | 157:90e3acc479a2 | 127 | |
mbed_official | 157:90e3acc479a2 | 128 | // Configure pull-up/pull-down resistors |
mbed_official | 157:90e3acc479a2 | 129 | uint32_t pupd = (uint32_t)mode; |
mbed_official | 227:7bd0639b8911 | 130 | if (pupd > 2) |
mbed_official | 227:7bd0639b8911 | 131 | pupd = 0; // Open-drain = No pull-up/No pull-down |
mbed_official | 157:90e3acc479a2 | 132 | gpio->PUPDR &= (uint32_t)(~(GPIO_PUPDR_PUPDR0 << (pin_index * 2))); |
mbed_official | 157:90e3acc479a2 | 133 | gpio->PUPDR |= (uint32_t)(pupd << (pin_index * 2)); |
mbed_official | 157:90e3acc479a2 | 134 | |
mbed_official | 157:90e3acc479a2 | 135 | } |