mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Mon Dec 02 11:30:05 2013 +0000
Revision:
52:a51c77007319
Child:
70:c1fbde68b492
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?

UserRevisionLine numberNew 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 "port_api.h"
mbed_official 52:a51c77007319 17 #include "pinmap.h"
mbed_official 52:a51c77007319 18 #include "gpio_api.h"
mbed_official 52:a51c77007319 19
mbed_official 52:a51c77007319 20 #if DEVICE_PORTIN || DEVICE_PORTOUT
mbed_official 52:a51c77007319 21
mbed_official 52:a51c77007319 22 // high nibble = port number (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
mbed_official 52:a51c77007319 23 // low nibble = pin number
mbed_official 52:a51c77007319 24 PinName port_pin(PortName port, int pin_n) {
mbed_official 52:a51c77007319 25 return (PinName)(pin_n + (port << 4));
mbed_official 52:a51c77007319 26 }
mbed_official 52:a51c77007319 27
mbed_official 52:a51c77007319 28 void port_init(port_t *obj, PortName port, int mask, PinDirection dir) {
mbed_official 52:a51c77007319 29
mbed_official 52:a51c77007319 30 uint32_t port_index = (uint32_t)port; // (0=A, 1=B, 2=C, 3=D, 4=E, 5=F, ...)
mbed_official 52:a51c77007319 31
mbed_official 52:a51c77007319 32 // Enable GPIO clock
mbed_official 52:a51c77007319 33 RCC_APB2PeriphClockCmd((RCC_APB2Periph_GPIOA << port_index), ENABLE);
mbed_official 52:a51c77007319 34
mbed_official 52:a51c77007319 35 // Get GPIO structure base address
mbed_official 52:a51c77007319 36 GPIO_TypeDef *gpio = (GPIO_TypeDef *)(GPIOA_BASE + (port_index << 10));
mbed_official 52:a51c77007319 37
mbed_official 52:a51c77007319 38 // Fill PORT object structure for future use
mbed_official 52:a51c77007319 39 obj->port = port;
mbed_official 52:a51c77007319 40 obj->mask = mask;
mbed_official 52:a51c77007319 41 obj->direction = dir;
mbed_official 52:a51c77007319 42 obj->reg_in = &gpio->IDR;
mbed_official 52:a51c77007319 43 obj->reg_out = &gpio->ODR;
mbed_official 52:a51c77007319 44
mbed_official 52:a51c77007319 45 port_dir(obj, dir);
mbed_official 52:a51c77007319 46 }
mbed_official 52:a51c77007319 47
mbed_official 52:a51c77007319 48 void port_dir(port_t *obj, PinDirection dir) {
mbed_official 52:a51c77007319 49 uint32_t i;
mbed_official 52:a51c77007319 50 obj->direction = dir;
mbed_official 52:a51c77007319 51 for (i = 0; i < 16; i++) { // Process all pins
mbed_official 52:a51c77007319 52 if (obj->mask & (1 << i)) { // If the pin is used
mbed_official 52:a51c77007319 53 if (dir == PIN_OUTPUT) {
mbed_official 52:a51c77007319 54 pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_Out_PP, 0));
mbed_official 52:a51c77007319 55 }
mbed_official 52:a51c77007319 56 else { // PIN_INPUT
mbed_official 52:a51c77007319 57 pin_function(port_pin(obj->port, i), STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0));
mbed_official 52:a51c77007319 58 }
mbed_official 52:a51c77007319 59 }
mbed_official 52:a51c77007319 60 }
mbed_official 52:a51c77007319 61 }
mbed_official 52:a51c77007319 62
mbed_official 52:a51c77007319 63 void port_mode(port_t *obj, PinMode mode) {
mbed_official 52:a51c77007319 64 uint32_t i;
mbed_official 52:a51c77007319 65 for (i = 0; i < 16; i++) { // Process all pins
mbed_official 52:a51c77007319 66 if (obj->mask & (1 << i)) { // If the pin is used
mbed_official 52:a51c77007319 67 pin_mode(port_pin(obj->port, i), mode);
mbed_official 52:a51c77007319 68 }
mbed_official 52:a51c77007319 69 }
mbed_official 52:a51c77007319 70 }
mbed_official 52:a51c77007319 71
mbed_official 52:a51c77007319 72 void port_write(port_t *obj, int value) {
mbed_official 52:a51c77007319 73 *obj->reg_out = (*obj->reg_out & ~obj->mask) | (value & obj->mask);
mbed_official 52:a51c77007319 74 }
mbed_official 52:a51c77007319 75
mbed_official 52:a51c77007319 76 int port_read(port_t *obj) {
mbed_official 52:a51c77007319 77 if (obj->direction == PIN_OUTPUT) {
mbed_official 52:a51c77007319 78 return (*obj->reg_out & obj->mask);
mbed_official 52:a51c77007319 79 }
mbed_official 52:a51c77007319 80 else { // PIN_INPUT
mbed_official 52:a51c77007319 81 return (*obj->reg_in & obj->mask);
mbed_official 52:a51c77007319 82 }
mbed_official 52:a51c77007319 83 }
mbed_official 52:a51c77007319 84
mbed_official 52:a51c77007319 85 #endif