mbed_pinmap_common.c 2.8 KB

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