1 /* mbed Microcontroller Library
2 * Copyright (c) 2006-2013 ARM Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "mbed_assert.h"
18 #include "mbed_error.h"
21 * Set the pin into input, output, alternate function or analog mode
23 void pin_function(PinName pin, int data) {
24 MBED_ASSERT(pin != (PinName)NC);
26 int mode = STM_PIN_MODE(data);
27 int func = STM_PIN_FUNC(data);
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)));
35 int offset = pin_index << 1;
36 gpio->MODER &= ~(0x3 << offset);
37 gpio->MODER |= mode << offset;
39 // Set high-speed mode
40 gpio->OSPEEDR &= ~(0x3 << offset);
41 gpio->OSPEEDR |= (0x2 << offset);
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;
51 gpio->AFR[1] &= ~(0xF << offset);
52 gpio->AFR[1] |= func << offset;
56 void pin_mode(PinName pin, PinMode mode) {
57 MBED_ASSERT(pin != (PinName)NC);
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;
64 GPIO_TypeDef * gpio = ((GPIO_TypeDef *) (GPIOA_BASE + (port_index << 10)));
65 if (mode == OpenDrain) {
66 gpio->OTYPER |= 1 << pin_index;
69 gpio->OTYPER &= ~(1 << pin_index);
70 gpio->PUPDR &= ~(0x3 << offset);
71 gpio->PUPDR |= mode << offset;