mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
common/pinmap_common.c@0:9b334a45a8ff, 2015-10-01 (annotated)
- Committer:
- bogdanm
- Date:
- Thu Oct 01 15:25:22 2015 +0300
- Revision:
- 0:9b334a45a8ff
Initial commit on mbed-dev
Replaces mbed-src (now inactive)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 0:9b334a45a8ff | 1 | /* mbed Microcontroller Library |
bogdanm | 0:9b334a45a8ff | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 0:9b334a45a8ff | 3 | * |
bogdanm | 0:9b334a45a8ff | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 0:9b334a45a8ff | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 0:9b334a45a8ff | 6 | * You may obtain a copy of the License at |
bogdanm | 0:9b334a45a8ff | 7 | * |
bogdanm | 0:9b334a45a8ff | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 0:9b334a45a8ff | 9 | * |
bogdanm | 0:9b334a45a8ff | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 0:9b334a45a8ff | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 0:9b334a45a8ff | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 0:9b334a45a8ff | 13 | * See the License for the specific language governing permissions and |
bogdanm | 0:9b334a45a8ff | 14 | * limitations under the License. |
bogdanm | 0:9b334a45a8ff | 15 | */ |
bogdanm | 0:9b334a45a8ff | 16 | #include "pinmap.h" |
bogdanm | 0:9b334a45a8ff | 17 | #include "mbed_error.h" |
bogdanm | 0:9b334a45a8ff | 18 | |
bogdanm | 0:9b334a45a8ff | 19 | void pinmap_pinout(PinName pin, const PinMap *map) { |
bogdanm | 0:9b334a45a8ff | 20 | if (pin == NC) |
bogdanm | 0:9b334a45a8ff | 21 | return; |
bogdanm | 0:9b334a45a8ff | 22 | |
bogdanm | 0:9b334a45a8ff | 23 | while (map->pin != NC) { |
bogdanm | 0:9b334a45a8ff | 24 | if (map->pin == pin) { |
bogdanm | 0:9b334a45a8ff | 25 | pin_function(pin, map->function); |
bogdanm | 0:9b334a45a8ff | 26 | |
bogdanm | 0:9b334a45a8ff | 27 | pin_mode(pin, PullNone); |
bogdanm | 0:9b334a45a8ff | 28 | return; |
bogdanm | 0:9b334a45a8ff | 29 | } |
bogdanm | 0:9b334a45a8ff | 30 | map++; |
bogdanm | 0:9b334a45a8ff | 31 | } |
bogdanm | 0:9b334a45a8ff | 32 | error("could not pinout"); |
bogdanm | 0:9b334a45a8ff | 33 | } |
bogdanm | 0:9b334a45a8ff | 34 | |
bogdanm | 0:9b334a45a8ff | 35 | uint32_t pinmap_merge(uint32_t a, uint32_t b) { |
bogdanm | 0:9b334a45a8ff | 36 | // both are the same (inc both NC) |
bogdanm | 0:9b334a45a8ff | 37 | if (a == b) |
bogdanm | 0:9b334a45a8ff | 38 | return a; |
bogdanm | 0:9b334a45a8ff | 39 | |
bogdanm | 0:9b334a45a8ff | 40 | // one (or both) is not connected |
bogdanm | 0:9b334a45a8ff | 41 | if (a == (uint32_t)NC) |
bogdanm | 0:9b334a45a8ff | 42 | return b; |
bogdanm | 0:9b334a45a8ff | 43 | if (b == (uint32_t)NC) |
bogdanm | 0:9b334a45a8ff | 44 | return a; |
bogdanm | 0:9b334a45a8ff | 45 | |
bogdanm | 0:9b334a45a8ff | 46 | // mis-match error case |
bogdanm | 0:9b334a45a8ff | 47 | error("pinmap mis-match"); |
bogdanm | 0:9b334a45a8ff | 48 | return (uint32_t)NC; |
bogdanm | 0:9b334a45a8ff | 49 | } |
bogdanm | 0:9b334a45a8ff | 50 | |
bogdanm | 0:9b334a45a8ff | 51 | uint32_t pinmap_find_peripheral(PinName pin, const PinMap* map) { |
bogdanm | 0:9b334a45a8ff | 52 | while (map->pin != NC) { |
bogdanm | 0:9b334a45a8ff | 53 | if (map->pin == pin) |
bogdanm | 0:9b334a45a8ff | 54 | return map->peripheral; |
bogdanm | 0:9b334a45a8ff | 55 | map++; |
bogdanm | 0:9b334a45a8ff | 56 | } |
bogdanm | 0:9b334a45a8ff | 57 | return (uint32_t)NC; |
bogdanm | 0:9b334a45a8ff | 58 | } |
bogdanm | 0:9b334a45a8ff | 59 | |
bogdanm | 0:9b334a45a8ff | 60 | uint32_t pinmap_peripheral(PinName pin, const PinMap* map) { |
bogdanm | 0:9b334a45a8ff | 61 | uint32_t peripheral = (uint32_t)NC; |
bogdanm | 0:9b334a45a8ff | 62 | |
bogdanm | 0:9b334a45a8ff | 63 | if (pin == (PinName)NC) |
bogdanm | 0:9b334a45a8ff | 64 | return (uint32_t)NC; |
bogdanm | 0:9b334a45a8ff | 65 | peripheral = pinmap_find_peripheral(pin, map); |
bogdanm | 0:9b334a45a8ff | 66 | if ((uint32_t)NC == peripheral) // no mapping available |
bogdanm | 0:9b334a45a8ff | 67 | error("pinmap not found for peripheral"); |
bogdanm | 0:9b334a45a8ff | 68 | return peripheral; |
bogdanm | 0:9b334a45a8ff | 69 | } |
bogdanm | 0:9b334a45a8ff | 70 | |
bogdanm | 0:9b334a45a8ff | 71 | uint32_t pinmap_find_function(PinName pin, const PinMap* map) { |
bogdanm | 0:9b334a45a8ff | 72 | while (map->pin != NC) { |
bogdanm | 0:9b334a45a8ff | 73 | if (map->pin == pin) |
bogdanm | 0:9b334a45a8ff | 74 | return map->function; |
bogdanm | 0:9b334a45a8ff | 75 | map++; |
bogdanm | 0:9b334a45a8ff | 76 | } |
bogdanm | 0:9b334a45a8ff | 77 | return (uint32_t)NC; |
bogdanm | 0:9b334a45a8ff | 78 | } |
bogdanm | 0:9b334a45a8ff | 79 | |
bogdanm | 0:9b334a45a8ff | 80 | uint32_t pinmap_function(PinName pin, const PinMap* map) { |
bogdanm | 0:9b334a45a8ff | 81 | uint32_t function = (uint32_t)NC; |
bogdanm | 0:9b334a45a8ff | 82 | |
bogdanm | 0:9b334a45a8ff | 83 | if (pin == (PinName)NC) |
bogdanm | 0:9b334a45a8ff | 84 | return (uint32_t)NC; |
bogdanm | 0:9b334a45a8ff | 85 | function = pinmap_find_function(pin, map); |
bogdanm | 0:9b334a45a8ff | 86 | if ((uint32_t)NC == function) // no mapping available |
bogdanm | 0:9b334a45a8ff | 87 | error("pinmap not found for function"); |
bogdanm | 0:9b334a45a8ff | 88 | return function; |
bogdanm | 0:9b334a45a8ff | 89 | } |