drv_gpio.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-11-24 勤为本 first version
  9. * 2018-05-11 zhuangwei add gpio interrupt ops
  10. */
  11. #include <rtthread.h>
  12. #include <drivers/pin.h>
  13. #include "ls1c_gpio.h"
  14. #include "ls1c.h"
  15. #include <rthw.h>
  16. #ifdef RT_USING_PIN
  17. void ls1c_pin_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode)
  18. {
  19. unsigned int gpio = pin;
  20. if (PIN_MODE_OUTPUT == mode)
  21. {
  22. gpio_init(gpio, gpio_mode_output);
  23. }
  24. else
  25. {
  26. gpio_init(gpio, gpio_mode_input);
  27. }
  28. return ;
  29. }
  30. void ls1c_pin_write(struct rt_device *device, rt_base_t pin, rt_base_t value)
  31. {
  32. unsigned int gpio = pin;
  33. if (PIN_LOW == value)
  34. {
  35. gpio_set(gpio, gpio_level_low);
  36. }
  37. else
  38. {
  39. gpio_set(gpio, gpio_level_high);
  40. }
  41. return ;
  42. }
  43. int ls1c_pin_read(struct rt_device *device, rt_base_t pin)
  44. {
  45. unsigned int gpio = pin;
  46. int value = PIN_LOW;
  47. if (0 == gpio_get(gpio))
  48. {
  49. value = PIN_LOW;
  50. }
  51. else
  52. {
  53. value = PIN_HIGH;
  54. }
  55. return value;
  56. }
  57. rt_err_t ls1c_pin_attach_irq(struct rt_device *device, rt_int32_t pin,
  58. rt_uint32_t mode, void (*hdr)(void *args), void *args)
  59. {
  60. unsigned int gpio = pin;
  61. char irq_name[10];
  62. gpio_set_irq_type(gpio, mode);
  63. rt_sprintf(irq_name, "PIN_%d", gpio);
  64. rt_hw_interrupt_install(LS1C_GPIO_TO_IRQ(gpio), (rt_isr_handler_t)hdr, args, irq_name);
  65. return RT_EOK;
  66. }
  67. rt_err_t ls1c_pin_detach_irq(struct rt_device *device, rt_int32_t pin)
  68. {
  69. return RT_EOK;
  70. }
  71. rt_err_t ls1c_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  72. {
  73. unsigned int gpio = pin;
  74. if (enabled)
  75. rt_hw_interrupt_umask(LS1C_GPIO_TO_IRQ(gpio));
  76. else
  77. rt_hw_interrupt_mask(LS1C_GPIO_TO_IRQ(gpio));
  78. return RT_EOK;
  79. }
  80. const static struct rt_pin_ops _ls1c_pin_ops =
  81. {
  82. ls1c_pin_mode,
  83. ls1c_pin_write,
  84. ls1c_pin_read,
  85. ls1c_pin_attach_irq,
  86. ls1c_pin_detach_irq,
  87. ls1c_pin_irq_enable
  88. };
  89. int hw_pin_init(void)
  90. {
  91. int ret = RT_EOK;
  92. ret = rt_device_pin_register("pin", &_ls1c_pin_ops, RT_NULL);
  93. return ret;
  94. }
  95. INIT_BOARD_EXPORT(hw_pin_init);
  96. #endif /*RT_USING_PIN */