mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/hal/TARGET_STM/TARGET_STM32F4XX/pinmap.c@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #include "mbed_assert.h" |
bogdanm | 0:9b334a45a8ff | 17 | #include "pinmap.h" |
bogdanm | 0:9b334a45a8ff | 18 | #include "mbed_error.h" |
bogdanm | 0:9b334a45a8ff | 19 | |
bogdanm | 0:9b334a45a8ff | 20 | /** |
bogdanm | 0:9b334a45a8ff | 21 | * Set the pin into input, output, alternate function or analog mode |
bogdanm | 0:9b334a45a8ff | 22 | */ |
bogdanm | 0:9b334a45a8ff | 23 | void pin_function(PinName pin, int data) { |
bogdanm | 0:9b334a45a8ff | 24 | MBED_ASSERT(pin != (PinName)NC); |
bogdanm | 0:9b334a45a8ff | 25 | |
bogdanm | 0:9b334a45a8ff | 26 | int mode = STM_PIN_MODE(data); |
bogdanm | 0:9b334a45a8ff | 27 | int func = STM_PIN_FUNC(data); |
bogdanm | 0:9b334a45a8ff | 28 | |
bogdanm | 0:9b334a45a8ff | 29 | uint32_t pin_number = (uint32_t)pin; |
bogdanm | 0:9b334a45a8ff | 30 | int port_index = pin_number >> 4; |
bogdanm | 0:9b334a45a8ff | 31 | int pin_index = (pin_number & 0xF); |
bogdanm | 0:9b334a45a8ff | 32 | GPIO_TypeDef * gpio = ((GPIO_TypeDef *) (GPIOA_BASE + (port_index << 10))); |
bogdanm | 0:9b334a45a8ff | 33 | |
bogdanm | 0:9b334a45a8ff | 34 | // MODE |
bogdanm | 0:9b334a45a8ff | 35 | int offset = pin_index << 1; |
bogdanm | 0:9b334a45a8ff | 36 | gpio->MODER &= ~(0x3 << offset); |
bogdanm | 0:9b334a45a8ff | 37 | gpio->MODER |= mode << offset; |
bogdanm | 0:9b334a45a8ff | 38 | |
bogdanm | 0:9b334a45a8ff | 39 | // Set high-speed mode |
bogdanm | 0:9b334a45a8ff | 40 | gpio->OSPEEDR &= ~(0x3 << offset); |
bogdanm | 0:9b334a45a8ff | 41 | gpio->OSPEEDR |= (0x2 << offset); |
bogdanm | 0:9b334a45a8ff | 42 | |
bogdanm | 0:9b334a45a8ff | 43 | // FUNCTION |
bogdanm | 0:9b334a45a8ff | 44 | // Bottom seven pins are in AFR[0], top seven in AFR[1] |
bogdanm | 0:9b334a45a8ff | 45 | offset = (pin_index & 0x7) << 2; |
bogdanm | 0:9b334a45a8ff | 46 | if (pin_index <= 0x7) { |
bogdanm | 0:9b334a45a8ff | 47 | gpio->AFR[0] &= ~(0xF << offset); |
bogdanm | 0:9b334a45a8ff | 48 | gpio->AFR[0] |= func << offset; |
bogdanm | 0:9b334a45a8ff | 49 | } |
bogdanm | 0:9b334a45a8ff | 50 | else { |
bogdanm | 0:9b334a45a8ff | 51 | gpio->AFR[1] &= ~(0xF << offset); |
bogdanm | 0:9b334a45a8ff | 52 | gpio->AFR[1] |= func << offset; |
bogdanm | 0:9b334a45a8ff | 53 | } |
bogdanm | 0:9b334a45a8ff | 54 | } |
bogdanm | 0:9b334a45a8ff | 55 | |
bogdanm | 0:9b334a45a8ff | 56 | void pin_mode(PinName pin, PinMode mode) { |
bogdanm | 0:9b334a45a8ff | 57 | MBED_ASSERT(pin != (PinName)NC); |
bogdanm | 0:9b334a45a8ff | 58 | |
bogdanm | 0:9b334a45a8ff | 59 | uint32_t pin_number = (uint32_t)pin; |
bogdanm | 0:9b334a45a8ff | 60 | int port_index = pin_number >> 4; |
bogdanm | 0:9b334a45a8ff | 61 | int pin_index = (pin_number & 0xF); |
bogdanm | 0:9b334a45a8ff | 62 | int offset = pin_index << 1; |
bogdanm | 0:9b334a45a8ff | 63 | |
bogdanm | 0:9b334a45a8ff | 64 | GPIO_TypeDef * gpio = ((GPIO_TypeDef *) (GPIOA_BASE + (port_index << 10))); |
bogdanm | 0:9b334a45a8ff | 65 | if (mode == OpenDrain) { |
bogdanm | 0:9b334a45a8ff | 66 | gpio->OTYPER |= 1 << pin_index; |
bogdanm | 0:9b334a45a8ff | 67 | } |
bogdanm | 0:9b334a45a8ff | 68 | else { |
bogdanm | 0:9b334a45a8ff | 69 | gpio->OTYPER &= ~(1 << pin_index); |
bogdanm | 0:9b334a45a8ff | 70 | gpio->PUPDR &= ~(0x3 << offset); |
bogdanm | 0:9b334a45a8ff | 71 | gpio->PUPDR |= mode << offset; |
bogdanm | 0:9b334a45a8ff | 72 | } |
bogdanm | 0:9b334a45a8ff | 73 | } |
bogdanm | 0:9b334a45a8ff | 74 |