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