pinmap.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* mbed Microcontroller Library
  2. *******************************************************************************
  3. * Copyright (c) 2017, STMicroelectronics
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  24. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *******************************************************************************
  29. */
  30. #include "mbed_assert.h"
  31. #include "pinmap.h"
  32. #include "PortNames.h"
  33. #include "mbed_error.h"
  34. #include "pin_device.h"
  35. extern GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx);
  36. const uint32_t ll_pin_defines[16] = {
  37. LL_GPIO_PIN_0,
  38. LL_GPIO_PIN_1,
  39. LL_GPIO_PIN_2,
  40. LL_GPIO_PIN_3,
  41. LL_GPIO_PIN_4,
  42. LL_GPIO_PIN_5,
  43. LL_GPIO_PIN_6,
  44. LL_GPIO_PIN_7,
  45. LL_GPIO_PIN_8,
  46. LL_GPIO_PIN_9,
  47. LL_GPIO_PIN_10,
  48. LL_GPIO_PIN_11,
  49. LL_GPIO_PIN_12,
  50. LL_GPIO_PIN_13,
  51. LL_GPIO_PIN_14,
  52. LL_GPIO_PIN_15
  53. };
  54. /**
  55. * Configure pin (mode, speed, output type and pull-up/pull-down)
  56. */
  57. void pin_function(PinName pin, int data)
  58. {
  59. MBED_ASSERT(pin != (PinName)NC);
  60. // Get the pin informations
  61. uint32_t mode = STM_PIN_FUNCTION(data);
  62. uint32_t afnum = STM_PIN_AFNUM(data);
  63. uint32_t port = STM_PORT(pin);
  64. uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
  65. uint32_t ll_mode = 0;
  66. // Enable GPIO clock
  67. GPIO_TypeDef *gpio = Set_GPIO_Clock(port);
  68. /* Set default speed to high.
  69. * For most families there are dedicated registers so it is
  70. * not so important, register can be set at any time.
  71. * But for families like F1, speed only applies to output.
  72. */
  73. #if defined (TARGET_STM32F1)
  74. if (mode == STM_PIN_OUTPUT) {
  75. #endif
  76. LL_GPIO_SetPinSpeed(gpio, ll_pin, LL_GPIO_SPEED_FREQ_HIGH);
  77. #if defined (TARGET_STM32F1)
  78. }
  79. #endif
  80. switch (mode) {
  81. case STM_PIN_INPUT:
  82. ll_mode = LL_GPIO_MODE_INPUT;
  83. break;
  84. case STM_PIN_OUTPUT:
  85. ll_mode = LL_GPIO_MODE_OUTPUT;
  86. break;
  87. case STM_PIN_ALTERNATE:
  88. ll_mode = LL_GPIO_MODE_ALTERNATE;
  89. // In case of ALT function, also set he afnum
  90. stm_pin_SetAFPin(gpio, pin, afnum);
  91. break;
  92. case STM_PIN_ANALOG:
  93. ll_mode = LL_GPIO_MODE_ANALOG;
  94. break;
  95. default:
  96. MBED_ASSERT(0);
  97. break;
  98. }
  99. LL_GPIO_SetPinMode(gpio, ll_pin, ll_mode);
  100. #if defined(GPIO_ASCR_ASC0)
  101. /* For families where Analog Control ASC0 register is present */
  102. if (STM_PIN_ANALOG_CONTROL(data)) {
  103. LL_GPIO_EnablePinAnalogControl(gpio, ll_pin);
  104. } else {
  105. LL_GPIO_DisablePinAnalogControl(gpio, ll_pin);
  106. }
  107. #endif
  108. /* For now by default use Speed HIGH for output or alt modes */
  109. if ((mode == STM_PIN_OUTPUT) || (mode == STM_PIN_ALTERNATE)) {
  110. if (STM_PIN_OD(data)) {
  111. LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
  112. } else {
  113. LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
  114. }
  115. }
  116. stm_pin_PullConfig(gpio, ll_pin, STM_PIN_PUPD(data));
  117. stm_pin_DisconnectDebug(pin);
  118. }
  119. /**
  120. * Configure pin pull-up/pull-down
  121. */
  122. void pin_mode(PinName pin, PinMode mode)
  123. {
  124. MBED_ASSERT(pin != (PinName)NC);
  125. uint32_t port_index = STM_PORT(pin);
  126. uint32_t ll_pin = ll_pin_defines[STM_PIN(pin)];
  127. // Enable GPIO clock
  128. GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
  129. uint32_t function = LL_GPIO_GetPinMode(gpio, ll_pin);
  130. if ((function == LL_GPIO_MODE_OUTPUT) || (function == LL_GPIO_MODE_ALTERNATE)) {
  131. if ((mode == OpenDrainNoPull) || (mode == OpenDrainPullUp) || (mode == OpenDrainPullDown)) {
  132. LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_OPENDRAIN);
  133. } else {
  134. LL_GPIO_SetPinOutputType(gpio, ll_pin, LL_GPIO_OUTPUT_PUSHPULL);
  135. }
  136. }
  137. if ((mode == OpenDrainPullUp) || (mode == PullUp)) {
  138. stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLUP);
  139. } else if ((mode == OpenDrainPullDown) || (mode == PullDown)) {
  140. stm_pin_PullConfig(gpio, ll_pin, GPIO_PULLDOWN);
  141. } else {
  142. stm_pin_PullConfig(gpio, ll_pin, GPIO_NOPULL);
  143. }
  144. }