gpio_api.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* mbed Microcontroller Library
  2. *******************************************************************************
  3. * Copyright (c) 2015, 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 "gpio_api.h"
  32. #include "pinmap.h"
  33. #include "mbed_error.h"
  34. #include "pin_device.h"
  35. extern const uint32_t ll_pin_defines[16];
  36. // Enable GPIO clock and return GPIO base address
  37. GPIO_TypeDef *Set_GPIO_Clock(uint32_t port_idx)
  38. {
  39. uint32_t gpio_add = 0;
  40. switch (port_idx) {
  41. case PortA:
  42. gpio_add = GPIOA_BASE;
  43. __HAL_RCC_GPIOA_CLK_ENABLE();
  44. break;
  45. case PortB:
  46. gpio_add = GPIOB_BASE;
  47. __HAL_RCC_GPIOB_CLK_ENABLE();
  48. break;
  49. #if defined(GPIOC_BASE)
  50. case PortC:
  51. gpio_add = GPIOC_BASE;
  52. __HAL_RCC_GPIOC_CLK_ENABLE();
  53. break;
  54. #endif
  55. #if defined GPIOD_BASE
  56. case PortD:
  57. gpio_add = GPIOD_BASE;
  58. __HAL_RCC_GPIOD_CLK_ENABLE();
  59. break;
  60. #endif
  61. #if defined GPIOE_BASE
  62. case PortE:
  63. gpio_add = GPIOE_BASE;
  64. __HAL_RCC_GPIOE_CLK_ENABLE();
  65. break;
  66. #endif
  67. #if defined GPIOF_BASE
  68. case PortF:
  69. gpio_add = GPIOF_BASE;
  70. __HAL_RCC_GPIOF_CLK_ENABLE();
  71. break;
  72. #endif
  73. #if defined GPIOG_BASE
  74. case PortG:
  75. #if defined TARGET_STM32L4
  76. __HAL_RCC_PWR_CLK_ENABLE();
  77. HAL_PWREx_EnableVddIO2();
  78. #endif
  79. gpio_add = GPIOG_BASE;
  80. __HAL_RCC_GPIOG_CLK_ENABLE();
  81. break;
  82. #endif
  83. #if defined GPIOH_BASE
  84. case PortH:
  85. gpio_add = GPIOH_BASE;
  86. __HAL_RCC_GPIOH_CLK_ENABLE();
  87. break;
  88. #endif
  89. #if defined GPIOI_BASE
  90. case PortI:
  91. gpio_add = GPIOI_BASE;
  92. __HAL_RCC_GPIOI_CLK_ENABLE();
  93. break;
  94. #endif
  95. #if defined GPIOJ_BASE
  96. case PortJ:
  97. gpio_add = GPIOJ_BASE;
  98. __HAL_RCC_GPIOJ_CLK_ENABLE();
  99. break;
  100. #endif
  101. #if defined GPIOK_BASE
  102. case PortK:
  103. gpio_add = GPIOK_BASE;
  104. __HAL_RCC_GPIOK_CLK_ENABLE();
  105. break;
  106. #endif
  107. default:
  108. error("Pinmap error: wrong port number.");
  109. break;
  110. }
  111. return (GPIO_TypeDef *) gpio_add;
  112. }
  113. uint32_t gpio_set(PinName pin)
  114. {
  115. MBED_ASSERT(pin != (PinName)NC);
  116. pin_function(pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
  117. return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask
  118. }
  119. void gpio_init(gpio_t *obj, PinName pin)
  120. {
  121. obj->pin = pin;
  122. if (pin == (PinName)NC) {
  123. return;
  124. }
  125. uint32_t port_index = STM_PORT(pin);
  126. // Enable GPIO clock
  127. GPIO_TypeDef *gpio = Set_GPIO_Clock(port_index);
  128. // Fill GPIO object structure for future use
  129. obj->mask = gpio_set(pin);
  130. obj->gpio = gpio;
  131. obj->ll_pin = ll_pin_defines[STM_PIN(obj->pin)];
  132. obj->reg_in = &gpio->IDR;
  133. obj->reg_set = &gpio->BSRR;
  134. #ifdef GPIO_IP_WITHOUT_BRR
  135. obj->reg_clr = &gpio->BSRR;
  136. #else
  137. obj->reg_clr = &gpio->BRR;
  138. #endif
  139. }
  140. void gpio_mode(gpio_t *obj, PinMode mode)
  141. {
  142. pin_mode(obj->pin, mode);
  143. }
  144. inline void gpio_dir(gpio_t *obj, PinDirection direction)
  145. {
  146. if (direction == PIN_INPUT) {
  147. LL_GPIO_SetPinMode(obj->gpio, obj->ll_pin, LL_GPIO_MODE_INPUT);
  148. } else {
  149. LL_GPIO_SetPinMode(obj->gpio, obj->ll_pin, LL_GPIO_MODE_OUTPUT);
  150. }
  151. }