drv_gpio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-16 bigmagic first version
  9. */
  10. #include "drv_gpio.h"
  11. #ifdef BSP_USING_PIN
  12. uint32_t raspi_get_pin_state(uint32_t fselnum)
  13. {
  14. uint32_t gpfsel = 0;
  15. switch (fselnum)
  16. {
  17. case 0:
  18. gpfsel = GPIO_REG_GPFSEL0(GPIO_BASE);
  19. break;
  20. case 1:
  21. gpfsel = GPIO_REG_GPFSEL1(GPIO_BASE);
  22. break;
  23. case 2:
  24. gpfsel = GPIO_REG_GPFSEL2(GPIO_BASE);
  25. break;
  26. case 3:
  27. gpfsel = GPIO_REG_GPFSEL3(GPIO_BASE);
  28. break;
  29. case 4:
  30. gpfsel = GPIO_REG_GPFSEL4(GPIO_BASE);
  31. break;
  32. case 5:
  33. gpfsel = GPIO_REG_GPFSEL5(GPIO_BASE);
  34. break;
  35. default:
  36. break;
  37. }
  38. return gpfsel;
  39. }
  40. void raspi_set_pin_state(uint32_t fselnum, uint32_t gpfsel)
  41. {
  42. switch (fselnum)
  43. {
  44. case 0:
  45. GPIO_REG_GPFSEL0(GPIO_BASE) = gpfsel;
  46. break;
  47. case 1:
  48. GPIO_REG_GPFSEL1(GPIO_BASE) = gpfsel;
  49. break;
  50. case 2:
  51. GPIO_REG_GPFSEL2(GPIO_BASE) = gpfsel;
  52. break;
  53. case 3:
  54. GPIO_REG_GPFSEL3(GPIO_BASE) = gpfsel;
  55. break;
  56. case 4:
  57. GPIO_REG_GPFSEL4(GPIO_BASE) = gpfsel;
  58. break;
  59. case 5:
  60. GPIO_REG_GPFSEL5(GPIO_BASE) = gpfsel;
  61. break;
  62. default:
  63. break;
  64. }
  65. }
  66. static void raspi_pin_mode(struct rt_device *dev, rt_base_t pin, rt_base_t mode)
  67. {
  68. uint32_t fselnum = pin / 10;
  69. uint32_t fselrest = pin % 10;
  70. uint32_t gpfsel = 0;
  71. gpfsel &= ~((uint32_t)(0x07 << (fselrest * 3)));
  72. gpfsel |= (uint32_t)(mode << (fselrest * 3));
  73. switch (fselnum)
  74. {
  75. case 0:
  76. GPIO_REG_GPFSEL0(GPIO_BASE) = gpfsel;
  77. break;
  78. case 1:
  79. GPIO_REG_GPFSEL1(GPIO_BASE) = gpfsel;
  80. break;
  81. case 2:
  82. GPIO_REG_GPFSEL2(GPIO_BASE) = gpfsel;
  83. break;
  84. case 3:
  85. GPIO_REG_GPFSEL3(GPIO_BASE) = gpfsel;
  86. break;
  87. case 4:
  88. GPIO_REG_GPFSEL4(GPIO_BASE) = gpfsel;
  89. break;
  90. case 5:
  91. GPIO_REG_GPFSEL5(GPIO_BASE) = gpfsel;
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. void prev_raspi_pin_mode(GPIO_PIN pin, GPIO_FUNC mode)
  98. {
  99. uint32_t fselnum = pin / 10;
  100. uint32_t fselrest = pin % 10;
  101. uint32_t gpfsel = 0;
  102. gpfsel = raspi_get_pin_state(fselnum);
  103. gpfsel &= ~((uint32_t)(0x07 << (fselrest * 3)));
  104. gpfsel |= (uint32_t)(mode << (fselrest * 3));
  105. raspi_set_pin_state(fselnum, gpfsel);
  106. }
  107. static void raspi_pin_write(struct rt_device *dev, rt_base_t pin, rt_base_t value)
  108. {
  109. uint32_t num = pin / 32;
  110. if(num == 0)
  111. {
  112. if(value == 0)
  113. {
  114. GPIO_REG_GPSET0(GPIO_BASE) = 1 << (pin % 32);
  115. }
  116. else
  117. {
  118. GPIO_REG_GPCLR0(GPIO_BASE) = 1 << (pin % 32);
  119. }
  120. }
  121. else
  122. {
  123. if(value == 0)
  124. {
  125. GPIO_REG_GPSET1(GPIO_BASE) = 1 << (pin % 32);
  126. }
  127. else
  128. {
  129. GPIO_REG_GPCLR1(GPIO_BASE) = 1 << (pin % 32);
  130. }
  131. }
  132. }
  133. static int raspi_pin_read(struct rt_device *device, rt_base_t pin)
  134. {
  135. return 0;
  136. }
  137. static rt_err_t raspi_pin_attach_irq(struct rt_device *device, rt_int32_t pin, rt_uint32_t mode, void (*hdr)(void *args), void *args)
  138. {
  139. return RT_EOK;
  140. }
  141. static rt_err_t raspi_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
  142. {
  143. return RT_EOK;
  144. }
  145. rt_err_t raspi_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  146. {
  147. return RT_EOK;
  148. }
  149. static const struct rt_pin_ops ops =
  150. {
  151. raspi_pin_mode,
  152. raspi_pin_write,
  153. raspi_pin_read,
  154. raspi_pin_attach_irq,
  155. raspi_pin_detach_irq,
  156. raspi_pin_irq_enable,
  157. RT_NULL,
  158. };
  159. #endif
  160. int rt_hw_gpio_init(void)
  161. {
  162. #ifdef BSP_USING_PIN
  163. rt_device_pin_register("gpio", &ops, RT_NULL);
  164. #endif
  165. return 0;
  166. }
  167. INIT_DEVICE_EXPORT(rt_hw_gpio_init);