drv_gpio.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. static void raspi_pin_mode(struct rt_device *dev, rt_base_t pin, rt_base_t mode)
  13. {
  14. uint32_t fselnum = pin / 10;
  15. uint32_t fselrest = pin % 10;
  16. uint32_t gpfsel = 0;
  17. gpfsel &= ~((uint32_t)(0x07 << (fselrest * 3)));
  18. gpfsel |= (uint32_t)(mode << (fselrest * 3));
  19. switch (fselnum)
  20. {
  21. case 0:
  22. GPIO_REG_GPFSEL0(GPIO_BASE) = gpfsel;
  23. break;
  24. case 1:
  25. GPIO_REG_GPFSEL1(GPIO_BASE) = gpfsel;
  26. break;
  27. case 2:
  28. GPIO_REG_GPFSEL2(GPIO_BASE) = gpfsel;
  29. break;
  30. case 3:
  31. GPIO_REG_GPFSEL3(GPIO_BASE) = gpfsel;
  32. break;
  33. case 4:
  34. GPIO_REG_GPFSEL4(GPIO_BASE) = gpfsel;
  35. break;
  36. case 5:
  37. GPIO_REG_GPFSEL5(GPIO_BASE) = gpfsel;
  38. break;
  39. default:
  40. break;
  41. }
  42. }
  43. static void raspi_pin_write(struct rt_device *dev, rt_base_t pin, rt_base_t value)
  44. {
  45. uint32_t num = pin / 32;
  46. if(num == 0)
  47. {
  48. if(value == 0)
  49. {
  50. GPIO_REG_GPSET0(GPIO_BASE) = 1 << (pin % 32);
  51. }
  52. else
  53. {
  54. GPIO_REG_GPCLR0(GPIO_BASE) = 1 << (pin % 32);
  55. }
  56. }
  57. else
  58. {
  59. if(value == 0)
  60. {
  61. GPIO_REG_GPSET1(GPIO_BASE) = 1 << (pin % 32);
  62. }
  63. else
  64. {
  65. GPIO_REG_GPCLR1(GPIO_BASE) = 1 << (pin % 32);
  66. }
  67. }
  68. }
  69. static int raspi_pin_read(struct rt_device *device, rt_base_t pin)
  70. {
  71. return 0;
  72. }
  73. 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)
  74. {
  75. return RT_EOK;
  76. }
  77. static rt_err_t raspi_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
  78. {
  79. return RT_EOK;
  80. }
  81. rt_err_t raspi_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  82. {
  83. return RT_EOK;
  84. }
  85. static const struct rt_pin_ops ops =
  86. {
  87. raspi_pin_mode,
  88. raspi_pin_write,
  89. raspi_pin_read,
  90. raspi_pin_attach_irq,
  91. raspi_pin_detach_irq,
  92. raspi_pin_irq_enable,
  93. RT_NULL,
  94. };
  95. #endif
  96. int rt_hw_gpio_init(void)
  97. {
  98. #ifdef BSP_USING_PIN
  99. rt_device_pin_register("gpio", &ops, RT_NULL);
  100. #endif
  101. return 0;
  102. }
  103. INIT_DEVICE_EXPORT(rt_hw_gpio_init);