pin.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, int 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. #ifdef RT_USING_DEVICE_OPS
  63. const static struct rt_device_ops pin_ops =
  64. {
  65. RT_NULL,
  66. RT_NULL,
  67. RT_NULL,
  68. _pin_read,
  69. _pin_write,
  70. _pin_control
  71. };
  72. #endif
  73. int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
  74. {
  75. _hw_pin.parent.type = RT_Device_Class_Miscellaneous;
  76. _hw_pin.parent.rx_indicate = RT_NULL;
  77. _hw_pin.parent.tx_complete = RT_NULL;
  78. #ifdef RT_USING_DEVICE_OPS
  79. _hw_pin.parent.ops = &pin_ops;
  80. #else
  81. _hw_pin.parent.init = RT_NULL;
  82. _hw_pin.parent.open = RT_NULL;
  83. _hw_pin.parent.close = RT_NULL;
  84. _hw_pin.parent.read = _pin_read;
  85. _hw_pin.parent.write = _pin_write;
  86. _hw_pin.parent.control = _pin_control;
  87. #endif
  88. _hw_pin.ops = ops;
  89. _hw_pin.parent.user_data = user_data;
  90. /* register a character device */
  91. rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);
  92. return 0;
  93. }
  94. rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
  95. void (*hdr)(void *args), void *args)
  96. {
  97. RT_ASSERT(_hw_pin.ops != RT_NULL);
  98. if(_hw_pin.ops->pin_attach_irq)
  99. {
  100. return _hw_pin.ops->pin_attach_irq(&_hw_pin.parent, pin, mode, hdr, args);
  101. }
  102. return RT_ENOSYS;
  103. }
  104. rt_err_t rt_pin_detach_irq(rt_int32_t pin)
  105. {
  106. RT_ASSERT(_hw_pin.ops != RT_NULL);
  107. if(_hw_pin.ops->pin_detach_irq)
  108. {
  109. return _hw_pin.ops->pin_detach_irq(&_hw_pin.parent, pin);
  110. }
  111. return RT_ENOSYS;
  112. }
  113. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
  114. {
  115. RT_ASSERT(_hw_pin.ops != RT_NULL);
  116. if(_hw_pin.ops->pin_irq_enable)
  117. {
  118. return _hw_pin.ops->pin_irq_enable(&_hw_pin.parent, pin, enabled);
  119. }
  120. return RT_ENOSYS;
  121. }
  122. /* RT-Thread Hardware PIN APIs */
  123. void rt_pin_mode(rt_base_t pin, rt_base_t mode)
  124. {
  125. RT_ASSERT(_hw_pin.ops != RT_NULL);
  126. _hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
  127. }
  128. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_mode, pinMode, set hardware pin mode);
  129. void rt_pin_write(rt_base_t pin, rt_base_t value)
  130. {
  131. RT_ASSERT(_hw_pin.ops != RT_NULL);
  132. _hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
  133. }
  134. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin);
  135. int rt_pin_read(rt_base_t pin)
  136. {
  137. RT_ASSERT(_hw_pin.ops != RT_NULL);
  138. return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
  139. }
  140. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin);