Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_pinmap_common.c Source File

mbed_pinmap_common.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "hal/pinmap.h"
00018 #include "platform/mbed_error.h"
00019 
00020 void pinmap_pinout(PinName pin, const PinMap *map)
00021 {
00022     if (pin == NC) {
00023         return;
00024     }
00025 
00026     while (map->pin != NC) {
00027         if (map->pin == pin) {
00028             pin_function(pin, map->function);
00029 
00030             pin_mode(pin, PullNone);
00031             return;
00032         }
00033         map++;
00034     }
00035     MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PINMAP_INVALID), "could not pinout", pin);
00036 }
00037 
00038 uint32_t pinmap_merge(uint32_t a, uint32_t b)
00039 {
00040     // both are the same (inc both NC)
00041     if (a == b) {
00042         return a;
00043     }
00044 
00045     // one (or both) is not connected
00046     if (a == (uint32_t)NC) {
00047         return b;
00048     }
00049     if (b == (uint32_t)NC) {
00050         return a;
00051     }
00052 
00053     // mis-match error case
00054     MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PINMAP_INVALID), "pinmap mis-match", a);
00055 }
00056 
00057 uint32_t pinmap_find_peripheral(PinName pin, const PinMap *map)
00058 {
00059     while (map->pin != NC) {
00060         if (map->pin == pin) {
00061             return map->peripheral;
00062         }
00063         map++;
00064     }
00065     return (uint32_t)NC;
00066 }
00067 
00068 uint32_t pinmap_peripheral(PinName pin, const PinMap *map)
00069 {
00070     uint32_t peripheral = (uint32_t)NC;
00071 
00072     if (pin == (PinName)NC) {
00073         return (uint32_t)NC;
00074     }
00075     peripheral = pinmap_find_peripheral(pin, map);
00076     if ((uint32_t)NC == peripheral) { // no mapping available
00077         MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PINMAP_INVALID), "pinmap not found for peripheral", pin);
00078     }
00079     return peripheral;
00080 }
00081 
00082 uint32_t pinmap_find_function(PinName pin, const PinMap *map)
00083 {
00084     while (map->pin != NC) {
00085         if (map->pin == pin) {
00086             return map->function;
00087         }
00088         map++;
00089     }
00090     return (uint32_t)NC;
00091 }
00092 
00093 uint32_t pinmap_function(PinName pin, const PinMap *map)
00094 {
00095     uint32_t function = (uint32_t)NC;
00096 
00097     if (pin == (PinName)NC) {
00098         return (uint32_t)NC;
00099     }
00100     function = pinmap_find_function(pin, map);
00101     if ((uint32_t)NC == function) { // no mapping available
00102         MBED_ERROR1(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_CODE_PINMAP_INVALID), "pinmap not found for function", pin);
00103     }
00104     return function;
00105 }
00106 
00107 bool pinmap_find_peripheral_pins(const PinList *whitelist, const PinList *blacklist, int per, const PinMap *const *maps, PinName **pins, uint32_t count)
00108 {
00109     /*
00110      * This function uses recursion to find a suitable set of pins which meet the requirements.
00111      * Recursion is at max the number of pinmaps passed in - the 'count' parameter. Because of this
00112      * there is no risk of a stack overflow due to unbounded recursion.
00113      *
00114      * Below is a psuedo code example of this function's operation when finding a set of 4 pins.
00115      * The recursion depth is indicated by the number in front.
00116      *
00117      *  1. Given 4 maps and a peripheral find 4 suitable pins
00118      *      2. Given 4 maps, a peripheral and 1 pin find 3 suitable pins
00119      *          3. Given 4 maps, a peripheral and 2 pins find 2 suitable pins
00120      *              4. Given 4 maps, a peripheral and 3 pins find 1 suitable pin
00121      *              4. Return success if all pins are found, return failure if there are no suitable pins, otherwise choose the next pin and retry
00122      *          3. Return success if all pins are found, return failure if there are no suitable pins, otherwise choose the next pin and retry
00123      *      2. Return success if all pins are found, return failure if there are no suitable pins, otherwise choose the next pin and retry
00124      *  1. Return success if all pins are found, return failure if there are no suitable pins, otherwise choose the next pin and retry
00125      *
00126      */
00127 
00128     for (uint32_t i = 0; i < count; i++) {
00129         const PinMap *map = maps[i];
00130         PinName *pin = pins[i];
00131         if (*pin == NC) {
00132             for (; map->pin != NC; map++) {
00133                 if (map->peripheral != per) {
00134                     continue;
00135                 }
00136                 if (!pinmap_list_has_pin(whitelist, map->pin)) {
00137                     // Not part of this form factor
00138                     continue;
00139                 }
00140                 if (pinmap_list_has_pin(blacklist, map->pin)) {
00141                     // Restricted pin
00142                     continue;
00143                 }
00144                 bool already_in_use = false;
00145                 for (uint32_t j = 0; j < count; j++) {
00146                     if (j == i) {
00147                         // Don't compare with self
00148                         continue;
00149                     }
00150                     if (map->pin == *pins[j]) {
00151                         already_in_use = true;
00152                         break;
00153                     }
00154                 }
00155                 if (already_in_use) {
00156                     continue;
00157                 }
00158                 *pin = map->pin;
00159                 if (pinmap_find_peripheral_pins(whitelist, blacklist, per, maps, pins, count)) {
00160                     return true;
00161                 }
00162             }
00163             *pin = NC;
00164             return false;
00165         }
00166     }
00167     return true;
00168 }
00169 
00170 bool pinmap_list_has_pin(const PinList *list, PinName pin)
00171 {
00172     for (uint32_t i = 0; i < list->count; i++) {
00173         if (list->pins[i] == pin) {
00174             return true;
00175         }
00176     }
00177     return false;
00178 }
00179 
00180 bool pinmap_list_has_peripheral(const PeripheralList *list, int peripheral)
00181 {
00182     for (uint32_t i = 0; i < list->count; i++) {
00183         if (list->peripheral[i] == peripheral) {
00184             return true;
00185         }
00186     }
00187     return false;
00188 }