pin.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * File : pin.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, 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. * 2015-01-20 Bernard the first version
  23. */
  24. #include <drivers/pin.h>
  25. #ifdef RT_USING_FINSH
  26. #include <finsh.h>
  27. #endif
  28. static struct rt_device_pin _hw_pin;
  29. static rt_size_t _pin_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  30. {
  31. struct rt_device_pin_status *status;
  32. struct rt_device_pin *pin = (struct rt_device_pin *)dev;
  33. /* check parameters */
  34. RT_ASSERT(pin != RT_NULL);
  35. status = (struct rt_device_pin_status *) buffer;
  36. if (status == RT_NULL || size != sizeof(*status)) return 0;
  37. status->status = pin->ops->pin_read(dev, status->pin);
  38. return size;
  39. }
  40. static rt_size_t _pin_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  41. {
  42. struct rt_device_pin_status *status;
  43. struct rt_device_pin *pin = (struct rt_device_pin *)dev;
  44. /* check parameters */
  45. RT_ASSERT(pin != RT_NULL);
  46. status = (struct rt_device_pin_status *) buffer;
  47. if (status == RT_NULL || size != sizeof(*status)) return 0;
  48. pin->ops->pin_write(dev, (rt_base_t)status->pin, (rt_base_t)status->status);
  49. return size;
  50. }
  51. static rt_err_t _pin_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  52. {
  53. struct rt_device_pin_mode *mode;
  54. struct rt_device_pin *pin = (struct rt_device_pin *)dev;
  55. /* check parameters */
  56. RT_ASSERT(pin != RT_NULL);
  57. mode = (struct rt_device_pin_mode *) args;
  58. if (mode == RT_NULL) return -RT_ERROR;
  59. pin->ops->pin_mode(dev, (rt_base_t)mode->pin, (rt_base_t)mode->mode);
  60. return 0;
  61. }
  62. int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
  63. {
  64. _hw_pin.parent.type = RT_Device_Class_Miscellaneous;
  65. _hw_pin.parent.rx_indicate = RT_NULL;
  66. _hw_pin.parent.tx_complete = RT_NULL;
  67. _hw_pin.parent.init = RT_NULL;
  68. _hw_pin.parent.open = RT_NULL;
  69. _hw_pin.parent.close = RT_NULL;
  70. _hw_pin.parent.read = _pin_read;
  71. _hw_pin.parent.write = _pin_write;
  72. _hw_pin.parent.control = _pin_control;
  73. _hw_pin.ops = ops;
  74. _hw_pin.parent.user_data = user_data;
  75. /* register a character device */
  76. rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);
  77. return 0;
  78. }
  79. rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
  80. void (*hdr)(void *args), void *args)
  81. {
  82. RT_ASSERT(_hw_pin.ops != RT_NULL);
  83. if(_hw_pin.ops->pin_attach_irq)
  84. {
  85. return _hw_pin.ops->pin_attach_irq(&_hw_pin.parent, pin, mode, hdr, args);
  86. }
  87. return RT_ENOSYS;
  88. }
  89. rt_err_t rt_pin_dettach_irq(rt_int32_t pin)
  90. {
  91. RT_ASSERT(_hw_pin.ops != RT_NULL);
  92. if(_hw_pin.ops->pin_dettach_irq)
  93. {
  94. return _hw_pin.ops->pin_dettach_irq(&_hw_pin.parent, pin);
  95. }
  96. return RT_ENOSYS;
  97. }
  98. rt_err_t pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
  99. {
  100. RT_ASSERT(_hw_pin.ops != RT_NULL);
  101. if(_hw_pin.ops->pin_irq_enable)
  102. {
  103. return _hw_pin.ops->pin_irq_enable(&_hw_pin.parent, pin, enabled);
  104. }
  105. return RT_ENOSYS;
  106. }
  107. /* RT-Thread Hardware PIN APIs */
  108. void rt_pin_mode(rt_base_t pin, rt_base_t mode)
  109. {
  110. RT_ASSERT(_hw_pin.ops != RT_NULL);
  111. _hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
  112. }
  113. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_mode, pinMode, set hardware pin mode);
  114. void rt_pin_write(rt_base_t pin, rt_base_t value)
  115. {
  116. RT_ASSERT(_hw_pin.ops != RT_NULL);
  117. _hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
  118. }
  119. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin);
  120. int rt_pin_read(rt_base_t pin)
  121. {
  122. RT_ASSERT(_hw_pin.ops != RT_NULL);
  123. return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
  124. }
  125. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin);