drv_gpio.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (c) 2006-2020, 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. void prev_raspi_pin_mode(GPIO_PIN pin, GPIO_FUNC mode)
  13. {
  14. uint32_t fselnum = pin / 10;
  15. uint32_t fselrest = pin % 10;
  16. uint32_t gpfsel = 0;
  17. switch (fselnum)
  18. {
  19. case 0:
  20. gpfsel = GPIO_REG_GPFSEL0(GPIO_BASE);
  21. break;
  22. case 1:
  23. gpfsel = GPIO_REG_GPFSEL1(GPIO_BASE);
  24. break;
  25. case 2:
  26. gpfsel = GPIO_REG_GPFSEL2(GPIO_BASE);
  27. break;
  28. case 3:
  29. gpfsel = GPIO_REG_GPFSEL3(GPIO_BASE);
  30. break;
  31. case 4:
  32. gpfsel = GPIO_REG_GPFSEL4(GPIO_BASE);
  33. break;
  34. case 5:
  35. gpfsel = GPIO_REG_GPFSEL5(GPIO_BASE);
  36. break;
  37. default:
  38. break;
  39. }
  40. gpfsel &= ~((uint32_t)(0x07 << (fselrest * 3)));
  41. gpfsel |= (uint32_t)(mode << (fselrest * 3));
  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. prev_raspi_pin_mode((GPIO_PIN)pin, (GPIO_FUNC)mode);
  69. }
  70. static void raspi_pin_write(struct rt_device *dev, rt_base_t pin, rt_base_t value)
  71. {
  72. uint32_t num = pin / 32;
  73. if(num == 0)
  74. {
  75. if(value == 0)
  76. {
  77. GPIO_REG_GPSET0(GPIO_BASE) = 1 << (pin % 32);
  78. }
  79. else
  80. {
  81. GPIO_REG_GPCLR0(GPIO_BASE) = 1 << (pin % 32);
  82. }
  83. }
  84. else
  85. {
  86. if(value == 0)
  87. {
  88. GPIO_REG_GPSET1(GPIO_BASE) = 1 << (pin % 32);
  89. }
  90. else
  91. {
  92. GPIO_REG_GPCLR1(GPIO_BASE) = 1 << (pin % 32);
  93. }
  94. }
  95. }
  96. static int raspi_pin_read(struct rt_device *device, rt_base_t pin)
  97. {
  98. return 0;
  99. }
  100. 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)
  101. {
  102. return RT_EOK;
  103. }
  104. static rt_err_t raspi_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
  105. {
  106. return RT_EOK;
  107. }
  108. rt_err_t raspi_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  109. {
  110. return RT_EOK;
  111. }
  112. static const struct rt_pin_ops ops =
  113. {
  114. raspi_pin_mode,
  115. raspi_pin_write,
  116. raspi_pin_read,
  117. raspi_pin_attach_irq,
  118. raspi_pin_detach_irq,
  119. raspi_pin_irq_enable,
  120. };
  121. #endif
  122. int rt_hw_gpio_init(void)
  123. {
  124. #ifdef BSP_USING_PIN
  125. rt_device_pin_register("gpio", &ops, RT_NULL);
  126. #endif
  127. return 0;
  128. }
  129. INIT_DEVICE_EXPORT(rt_hw_gpio_init);