mcf51cn_gpio.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 2012 by Embedded Technologies s.r.o
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. #include <dev/gpio.h>
  33. /*!
  34. * \brief Get pin configuration.
  35. *
  36. * \param bank GPIO bank/port number.
  37. * \param bit Bit number of the specified bank/port.
  38. *
  39. * \return Attribute flags of the pin.
  40. */
  41. uint32_t GpioPinConfigGet(int bank, int bit)
  42. {
  43. uint32_t rc = 0;
  44. uint8_t mask = _BV(bit);
  45. /* Verify bank */
  46. if (bank > PORTJ) {
  47. return 0;
  48. }
  49. /* Read pin role (pin mux control register) */
  50. rc = MCF_GPIO_PMC(bank) >> (2 * bit) & 0x3;
  51. /* Read pin direction */
  52. if (rc == GPIO_CFG_INPUT) {
  53. if (MCF_GPIO_DD(bank) & mask) {
  54. rc = GPIO_CFG_OUTPUT;
  55. }
  56. }
  57. /* Read pin config */
  58. if (MCF_GPIO_PE(bank) & mask) {
  59. rc |= GPIO_CFG_PULLUP;
  60. }
  61. if (MCF_GPIO_SE(bank) & mask) {
  62. rc |= GPIO_CFG_SLEW_RATE;
  63. }
  64. if (MCF_GPIO_DS(bank) & mask) {
  65. rc |= GPIO_CFG_DRIVE_STRENGTH;
  66. }
  67. if (MCF_GPIO_IFE(bank) & mask) {
  68. rc |= GPIO_CFG_INPUT_FILTER;
  69. }
  70. return rc;
  71. }
  72. /*!
  73. * \brief Set pin configuration.
  74. *
  75. * Applications may also use this function to make sure, that a specific
  76. * attribute is available for a specific pin.
  77. *
  78. * \note GPIO pins are typically initialized to a safe state after power
  79. * up. This routine is not able to determine the consequences of
  80. * changing pin configurations. In the worst case you may permanently
  81. * damage your hardware by bad pin settings.
  82. *
  83. * \param bank GPIO bank/port number.
  84. * \param bit Bit number of the specified bank/port.
  85. * \param flags Attribute flags.
  86. *
  87. * \return 0 if all attributes had been set, -1 otherwise.
  88. */
  89. int GpioPinConfigSet(int bank, int bit, uint32_t flags)
  90. {
  91. GpioPortConfigSet(bank, _BV(bit), flags);
  92. /* Check the result. */
  93. if (GpioPinConfigGet(bank, bit) != flags) {
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. /*!
  99. * \brief Set port wide pin configuration.
  100. *
  101. * \note This function does not check for undefined ports and pins or
  102. * invalid attributes. If this is required, use GpioPinConfigSet().
  103. *
  104. * \param bank GPIO bank/port number.
  105. * \param mask The given attributes are set for a specific pin, if the
  106. * corresponding bit in this mask is 1.
  107. * \param flags Attribute flags to set.
  108. *
  109. * \return Always 0.
  110. */
  111. int GpioPortConfigSet(int bank, uint32_t mask, uint32_t flags)
  112. {
  113. uint32_t role = flags & (0x03 | GPIO_CFG_OUTPUT);
  114. uint16_t role_mask;
  115. uint16_t pmc_mask;
  116. int i;
  117. /* Verify bank */
  118. if (bank > PORTJ) {
  119. return 0;
  120. }
  121. /* Configure the configurable pins only */
  122. mask &= (bank == PORTJ) ? 0x3F : 0xFF;
  123. /* Configure GPIO direction first */
  124. if (role == GPIO_CFG_OUTPUT) {
  125. MCF_GPIO_DD(bank) |= mask;
  126. } else if (role == GPIO_CFG_INPUT) {
  127. MCF_GPIO_DD(bank) &= ~mask;
  128. }
  129. /* Then configure pin role */
  130. for (pmc_mask = 0, role_mask = 0, i = 0; i < 8; i++) {
  131. if (mask & _BV(i)) {
  132. role_mask |= role << (2 * i);
  133. pmc_mask |= 0x03 << (2 * i);
  134. }
  135. }
  136. MCF_GPIO_PMC(bank) &= ~pmc_mask;
  137. MCF_GPIO_PMC(bank) |= role_mask;
  138. /* Configure pin features */
  139. if (flags & GPIO_CFG_PULLUP) {
  140. MCF_GPIO_PE(bank) |= mask;
  141. } else {
  142. MCF_GPIO_PE(bank) &= ~mask;
  143. }
  144. if (flags & GPIO_CFG_SLEW_RATE) {
  145. MCF_GPIO_SE(bank) |= mask;
  146. } else {
  147. MCF_GPIO_SE(bank) &= ~mask;
  148. }
  149. if (flags & GPIO_CFG_DRIVE_STRENGTH) {
  150. MCF_GPIO_DS(bank) |= mask;
  151. } else {
  152. MCF_GPIO_DS(bank) &= ~mask;
  153. }
  154. if (flags & GPIO_CFG_INPUT_FILTER) {
  155. MCF_GPIO_IFE(bank) |= mask;
  156. } else {
  157. MCF_GPIO_IFE(bank) &= ~mask;
  158. }
  159. return 0;
  160. }