A function for finding the port and bit position of a pin

Dependents:   pin_port_test

Committer:
takuo
Date:
Mon Dec 21 07:42:10 2015 +0000
Revision:
0:5b6a266f2211
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
takuo 0:5b6a266f2211 1 /* A function for finding the port and bit position of a pin
takuo 0:5b6a266f2211 2 * Copyright 2015, Takuo Watanabe
takuo 0:5b6a266f2211 3 *
takuo 0:5b6a266f2211 4 * Licensed under the Apache License, Version 2.0 (the "License");
takuo 0:5b6a266f2211 5 * you may not use this file except in compliance with the License.
takuo 0:5b6a266f2211 6 * You may obtain a copy of the License at
takuo 0:5b6a266f2211 7 *
takuo 0:5b6a266f2211 8 * http://www.apache.org/licenses/LICENSE-2.0
takuo 0:5b6a266f2211 9 *
takuo 0:5b6a266f2211 10 * Unless required by applicable law or agreed to in writing, software
takuo 0:5b6a266f2211 11 * distributed under the License is distributed on an "AS IS" BASIS,
takuo 0:5b6a266f2211 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
takuo 0:5b6a266f2211 13 * See the License for the specific language governing permissions and
takuo 0:5b6a266f2211 14 * limitations under the License.
takuo 0:5b6a266f2211 15 */
takuo 0:5b6a266f2211 16
takuo 0:5b6a266f2211 17 #include "mbed.h"
takuo 0:5b6a266f2211 18 #include "pin_port.h"
takuo 0:5b6a266f2211 19
takuo 0:5b6a266f2211 20 int pin_port(PinName pin, PortName *port) {
takuo 0:5b6a266f2211 21 #if defined(TARGET_LPC176X)
takuo 0:5b6a266f2211 22 int p = pin - LPC_GPIO0_BASE;
takuo 0:5b6a266f2211 23 *port = (PortName)(p >> PORT_SHIFT);
takuo 0:5b6a266f2211 24 return p & ((1 << PORT_SHIFT) - 1);
takuo 0:5b6a266f2211 25 #elif defined(TARGET_LPC11UXX)
takuo 0:5b6a266f2211 26 *port = (PortName)((int)pin >> PORT_SHIFT);
takuo 0:5b6a266f2211 27 return ((1 << PORT_SHIFT) - 1) & (int)pin;
takuo 0:5b6a266f2211 28 #elif defined(TARGET_KLXX)
takuo 0:5b6a266f2211 29 *port = (PortName)((int)pin >> PORT_SHIFT);
takuo 0:5b6a266f2211 30 return (((1 << PORT_SHIFT) - 1) & (int)pin) >> 2;
takuo 0:5b6a266f2211 31 #elif defined(TARGET_KPSDK_MCUS)
takuo 0:5b6a266f2211 32 *port = (PortName)((int)pin >> GPIO_PORT_SHIFT);
takuo 0:5b6a266f2211 33 return ((1 << GPIO_PORT_SHIFT) - 1) & (int)pin;
takuo 0:5b6a266f2211 34 #elif defined(TARGET_STM32F4)
takuo 0:5b6a266f2211 35 *port = (PortName)((int)pin >> 4);
takuo 0:5b6a266f2211 36 return 0xf & (int)pin;
takuo 0:5b6a266f2211 37 #else
takuo 0:5b6a266f2211 38 #error "Unsupported target"
takuo 0:5b6a266f2211 39 #endif
takuo 0:5b6a266f2211 40 }