mbed library sources

Dependents:   Freedman_v2 Nucleo_i2c_OLED_BME280_copy

Fork of mbed-src by mbed official

Committer:
bogdanm
Date:
Tue Sep 10 15:14:19 2013 +0300
Revision:
20:4263a77256ae
Child:
227:7bd0639b8911
Sync with git revision 171dda705c947bf910926a0b73d6a4797802554d

Who changed what in which revision?

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