mbed library sources
Dependents: frdm_kl05z_gpio_test
Fork of mbed-src by
targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/pinmap.c@52:a51c77007319, 2013-12-02 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Dec 02 11:30:05 2013 +0000
- Revision:
- 52:a51c77007319
- Child:
- 58:3b55b7a41411
Synchronized with git revision 49df530ae72ce97ccc773d1f2c13b38e868e6abd
Full URL: https://github.com/mbedmicro/mbed/commit/49df530ae72ce97ccc773d1f2c13b38e868e6abd/
Add STMicroelectronics NUCLEO_F103RB target
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 52:a51c77007319 | 1 | /* mbed Microcontroller Library |
mbed_official | 52:a51c77007319 | 2 | * Copyright (c) 2006-2013 ARM Limited |
mbed_official | 52:a51c77007319 | 3 | * |
mbed_official | 52:a51c77007319 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 52:a51c77007319 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 52:a51c77007319 | 6 | * You may obtain a copy of the License at |
mbed_official | 52:a51c77007319 | 7 | * |
mbed_official | 52:a51c77007319 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 52:a51c77007319 | 9 | * |
mbed_official | 52:a51c77007319 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 52:a51c77007319 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 52:a51c77007319 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 52:a51c77007319 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 52:a51c77007319 | 14 | * limitations under the License. |
mbed_official | 52:a51c77007319 | 15 | */ |
mbed_official | 52:a51c77007319 | 16 | #include "pinmap.h" |
mbed_official | 52:a51c77007319 | 17 | #include "error.h" |
mbed_official | 52:a51c77007319 | 18 | |
mbed_official | 52:a51c77007319 | 19 | // Alternate-function mapping |
mbed_official | 52:a51c77007319 | 20 | static const uint32_t AF_mapping[] = { |
mbed_official | 52:a51c77007319 | 21 | 0, // 0 = No AF |
mbed_official | 52:a51c77007319 | 22 | GPIO_Remap_SPI1, // 1 |
mbed_official | 52:a51c77007319 | 23 | GPIO_Remap_I2C1, // 2 |
mbed_official | 52:a51c77007319 | 24 | GPIO_Remap_USART1, // 3 |
mbed_official | 52:a51c77007319 | 25 | GPIO_Remap_USART2, // 4 |
mbed_official | 52:a51c77007319 | 26 | GPIO_FullRemap_TIM2, // 5 |
mbed_official | 52:a51c77007319 | 27 | GPIO_FullRemap_TIM3, // 6 |
mbed_official | 52:a51c77007319 | 28 | GPIO_Remap_I2C1 // 7 |
mbed_official | 52:a51c77007319 | 29 | }; |
mbed_official | 52:a51c77007319 | 30 | |
mbed_official | 52:a51c77007319 | 31 | /** |
mbed_official | 52:a51c77007319 | 32 | * Set the pin function (input, output, alternate function or analog) + output speed + AF |
mbed_official | 52:a51c77007319 | 33 | */ |
mbed_official | 52:a51c77007319 | 34 | void pin_function(PinName pin, int data) { |
mbed_official | 52:a51c77007319 | 35 | if (pin == NC) return; |
mbed_official | 52:a51c77007319 | 36 | |
mbed_official | 52:a51c77007319 | 37 | // Get the pin mode and alternate-function number |
mbed_official | 52:a51c77007319 | 38 | uint32_t mode = STM_PIN_MODE(data); |
mbed_official | 52:a51c77007319 | 39 | uint32_t afnum = STM_PIN_AFNUM(data); |
mbed_official | 52:a51c77007319 | 40 | |
mbed_official | 52:a51c77007319 | 41 | // Get GPIO structure base address |
mbed_official | 52:a51c77007319 | 42 | uint32_t pin_number = (uint32_t)pin; |
mbed_official | 52:a51c77007319 | 43 | uint32_t pin_index = (pin_number & 0xF); |
mbed_official | 52:a51c77007319 | 44 | uint32_t port_index = (pin_number >> 4); |
mbed_official | 52:a51c77007319 | 45 | GPIO_TypeDef *gpio = ((GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10))); |
mbed_official | 52:a51c77007319 | 46 | |
mbed_official | 52:a51c77007319 | 47 | // Enable GPIO and AFIO clocks |
mbed_official | 52:a51c77007319 | 48 | RCC_APB2PeriphClockCmd((uint32_t)(RCC_APB2Periph_GPIOA << port_index), ENABLE); |
mbed_official | 52:a51c77007319 | 49 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); |
mbed_official | 52:a51c77007319 | 50 | |
mbed_official | 52:a51c77007319 | 51 | // Configure GPIO |
mbed_official | 52:a51c77007319 | 52 | GPIO_InitTypeDef GPIO_InitStructure; |
mbed_official | 52:a51c77007319 | 53 | GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index); |
mbed_official | 52:a51c77007319 | 54 | GPIO_InitStructure.GPIO_Mode = (GPIOMode_TypeDef)mode; |
mbed_official | 52:a51c77007319 | 55 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
mbed_official | 52:a51c77007319 | 56 | GPIO_Init(gpio, &GPIO_InitStructure); |
mbed_official | 52:a51c77007319 | 57 | |
mbed_official | 52:a51c77007319 | 58 | // Configure Alternate Function |
mbed_official | 52:a51c77007319 | 59 | if (afnum > 0) { |
mbed_official | 52:a51c77007319 | 60 | GPIO_PinRemapConfig(AF_mapping[afnum], ENABLE); |
mbed_official | 52:a51c77007319 | 61 | } |
mbed_official | 52:a51c77007319 | 62 | |
mbed_official | 52:a51c77007319 | 63 | // Disconnect JTAG-DP + SW-DP signals. |
mbed_official | 52:a51c77007319 | 64 | // Warning: Need to reconnect under reset |
mbed_official | 52:a51c77007319 | 65 | if ((pin == PA_13) || (pin == PA_14)) { |
mbed_official | 52:a51c77007319 | 66 | GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); |
mbed_official | 52:a51c77007319 | 67 | } |
mbed_official | 52:a51c77007319 | 68 | if ((pin == PA_15) || (pin == PB_3) || (pin == PB_4)) { |
mbed_official | 52:a51c77007319 | 69 | GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); |
mbed_official | 52:a51c77007319 | 70 | } |
mbed_official | 52:a51c77007319 | 71 | } |
mbed_official | 52:a51c77007319 | 72 | |
mbed_official | 52:a51c77007319 | 73 | /** |
mbed_official | 52:a51c77007319 | 74 | * Set the pin mode (open-drain/push-pull + pull-up/pull-down) |
mbed_official | 52:a51c77007319 | 75 | */ |
mbed_official | 52:a51c77007319 | 76 | void pin_mode(PinName pin, PinMode mode) { |
mbed_official | 52:a51c77007319 | 77 | if (pin == NC) return; |
mbed_official | 52:a51c77007319 | 78 | |
mbed_official | 52:a51c77007319 | 79 | GPIO_InitTypeDef GPIO_InitStructure; |
mbed_official | 52:a51c77007319 | 80 | |
mbed_official | 52:a51c77007319 | 81 | // Get GPIO structure base address |
mbed_official | 52:a51c77007319 | 82 | uint32_t pin_number = (uint32_t)pin; |
mbed_official | 52:a51c77007319 | 83 | uint32_t pin_index = (pin_number & 0xF); |
mbed_official | 52:a51c77007319 | 84 | uint32_t port_index = (pin_number >> 4); |
mbed_official | 52:a51c77007319 | 85 | GPIO_TypeDef *gpio = ((GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10))); |
mbed_official | 52:a51c77007319 | 86 | |
mbed_official | 52:a51c77007319 | 87 | // Enable GPIO clock |
mbed_official | 52:a51c77007319 | 88 | RCC_APB2PeriphClockCmd((uint32_t)(RCC_APB2Periph_GPIOA << port_index), ENABLE); |
mbed_official | 52:a51c77007319 | 89 | |
mbed_official | 52:a51c77007319 | 90 | // Configure open-drain and pull-up/down |
mbed_official | 52:a51c77007319 | 91 | switch (mode) { |
mbed_official | 52:a51c77007319 | 92 | case PullNone: |
mbed_official | 52:a51c77007319 | 93 | return; |
mbed_official | 52:a51c77007319 | 94 | case PullUp: |
mbed_official | 52:a51c77007319 | 95 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; |
mbed_official | 52:a51c77007319 | 96 | break; |
mbed_official | 52:a51c77007319 | 97 | case PullDown: |
mbed_official | 52:a51c77007319 | 98 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; |
mbed_official | 52:a51c77007319 | 99 | break; |
mbed_official | 52:a51c77007319 | 100 | case OpenDrain: |
mbed_official | 52:a51c77007319 | 101 | if (pin_index < 8) { |
mbed_official | 52:a51c77007319 | 102 | if ((gpio->CRL & (0x03 << (pin_index * 4))) > 0) { // MODE bits = Output mode |
mbed_official | 52:a51c77007319 | 103 | gpio->CRL |= (0x04 << (pin_index * 4)); // Set open-drain |
mbed_official | 52:a51c77007319 | 104 | } |
mbed_official | 52:a51c77007319 | 105 | } |
mbed_official | 52:a51c77007319 | 106 | else { |
mbed_official | 52:a51c77007319 | 107 | if ((gpio->CRH & (0x03 << ((pin_index % 8) * 4))) > 0) { // MODE bits = Output mode |
mbed_official | 52:a51c77007319 | 108 | gpio->CRH |= (0x04 << ((pin_index % 8) * 4)); // Set open-drain |
mbed_official | 52:a51c77007319 | 109 | } |
mbed_official | 52:a51c77007319 | 110 | } |
mbed_official | 52:a51c77007319 | 111 | return; |
mbed_official | 52:a51c77007319 | 112 | default: |
mbed_official | 52:a51c77007319 | 113 | break; |
mbed_official | 52:a51c77007319 | 114 | } |
mbed_official | 52:a51c77007319 | 115 | |
mbed_official | 52:a51c77007319 | 116 | // Configure GPIO |
mbed_official | 52:a51c77007319 | 117 | GPIO_InitStructure.GPIO_Pin = (uint16_t)(1 << pin_index); |
mbed_official | 52:a51c77007319 | 118 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
mbed_official | 52:a51c77007319 | 119 | GPIO_Init(gpio, &GPIO_InitStructure); |
mbed_official | 52:a51c77007319 | 120 | } |