gpio.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * File : gpio.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-01-13 weety first version
  23. */
  24. #include <rtthread.h>
  25. #include "gpio.h"
  26. #define GPIO0_BASE (DAVINCI_GPIO_BASE + 0x10)
  27. #define GPIO1_BASE (DAVINCI_GPIO_BASE + 0x38)
  28. #define GPIO2_BASE (DAVINCI_GPIO_BASE + 0x60)
  29. #define GPIO3_BASE (DAVINCI_GPIO_BASE + 0x88)
  30. static unsigned int dm365_gpio_base = (unsigned int)GPIO0_BASE;
  31. #define GPIO_OE (dm365_gpio_base + 0x00)
  32. #define GPIO_DATAIN (dm365_gpio_base + 0x10)
  33. #define GPIO_DATAOUT (dm365_gpio_base + 0x04)
  34. #define GPIO_CLROUT (dm365_gpio_base + 0x0C)
  35. #define GPIO_SETOUT (dm365_gpio_base + 0x08)
  36. #define gpio_dirin(n) *(volatile unsigned int *)((GPIO_OE)) |= 1<<(n)
  37. #define gpio_dirout(n) *(volatile unsigned int *)((GPIO_OE)) &= ~(1u<<(n))
  38. #define gpio_set(n) *(volatile unsigned int *)((GPIO_SETOUT)) = 1<<(n)
  39. #define gpio_clr(n) *(volatile unsigned int *)((GPIO_CLROUT)) = 1<<(n)
  40. #define gpio_get(n) ( ( *(volatile unsigned int *)((GPIO_DATAIN)) & (1<<(n)) ) ? 1 : 0 )
  41. #define GPIO_GRP_MASK (5)
  42. static int gpio_to_base(unsigned int gpio)
  43. {
  44. unsigned int grp_idx;
  45. int ret;
  46. grp_idx = gpio >> GPIO_GRP_MASK;
  47. switch (grp_idx) {
  48. case 0:
  49. dm365_gpio_base = (unsigned int)GPIO0_BASE;
  50. ret = 0;
  51. break;
  52. case 1:
  53. dm365_gpio_base = (unsigned int)GPIO1_BASE;
  54. ret = 0;
  55. break;
  56. case 2:
  57. dm365_gpio_base = (unsigned int)GPIO2_BASE;
  58. ret = 0;
  59. break;
  60. case 3:
  61. dm365_gpio_base = (unsigned int)GPIO3_BASE;
  62. ret = 0;
  63. break;
  64. default:
  65. ret =-RT_EIO;
  66. break;
  67. }
  68. return ret;
  69. }
  70. int gpio_direction_input(unsigned int gpio)
  71. {
  72. unsigned int offset;
  73. int ret=0;
  74. rt_ubase_t temp = rt_hw_interrupt_disable();
  75. ret = gpio_to_base(gpio);
  76. if (ret < 0) {
  77. goto gpio_free;
  78. }
  79. offset = gpio & ((1 << GPIO_GRP_MASK) -1);
  80. gpio_dirin(offset);
  81. gpio_free:
  82. rt_hw_interrupt_enable(temp);
  83. return ret;
  84. }
  85. int gpio_direction_output(unsigned int gpio, int value)
  86. {
  87. unsigned int offset;
  88. int ret=0;
  89. rt_ubase_t temp = rt_hw_interrupt_disable();
  90. ret = gpio_to_base(gpio);
  91. if (ret < 0) {
  92. goto gpio_free;
  93. }
  94. offset = gpio & ((1 << GPIO_GRP_MASK) -1);
  95. if (value) {
  96. gpio_set(offset);
  97. }
  98. else {
  99. gpio_clr(offset);
  100. }
  101. gpio_dirout(offset);
  102. gpio_free:
  103. rt_hw_interrupt_enable(temp);
  104. return ret;
  105. }
  106. int gpio_set_value(unsigned int gpio, int value)
  107. {
  108. unsigned int offset;
  109. int ret=0;
  110. rt_ubase_t temp = rt_hw_interrupt_disable();
  111. ret = gpio_to_base(gpio);
  112. if (ret < 0) {
  113. goto gpio_free;
  114. }
  115. offset = gpio & ((1 << GPIO_GRP_MASK) -1);
  116. if (value) {
  117. gpio_set(offset);
  118. }
  119. else {
  120. gpio_clr(offset);
  121. }
  122. gpio_free:
  123. rt_hw_interrupt_enable(temp);
  124. return ret;
  125. }
  126. int gpio_get_value(unsigned int gpio)
  127. {
  128. unsigned int offset;
  129. int ret=0;
  130. rt_ubase_t temp = rt_hw_interrupt_disable();
  131. ret = gpio_to_base(gpio);
  132. if (ret < 0) {
  133. goto gpio_free;
  134. }
  135. offset = gpio & ((1 << GPIO_GRP_MASK) -1);
  136. ret = gpio_get(offset);
  137. gpio_free:
  138. rt_hw_interrupt_enable(temp);
  139. return ret;
  140. }