pin.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * 2015-01-20 Bernard the first version
  9. * 2021-02-06 Meco Man fix RT_ENOSYS code in negative
  10. */
  11. #include <drivers/pin.h>
  12. #ifdef RT_USING_FINSH
  13. #include <finsh.h>
  14. #endif
  15. static struct rt_device_pin _hw_pin;
  16. static rt_size_t _pin_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  17. {
  18. struct rt_device_pin_status *status;
  19. struct rt_device_pin *pin = (struct rt_device_pin *)dev;
  20. /* check parameters */
  21. RT_ASSERT(pin != RT_NULL);
  22. status = (struct rt_device_pin_status *) buffer;
  23. if (status == RT_NULL || size != sizeof(*status)) return 0;
  24. status->status = pin->ops->pin_read(dev, status->pin);
  25. return size;
  26. }
  27. static rt_size_t _pin_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  28. {
  29. struct rt_device_pin_status *status;
  30. struct rt_device_pin *pin = (struct rt_device_pin *)dev;
  31. /* check parameters */
  32. RT_ASSERT(pin != RT_NULL);
  33. status = (struct rt_device_pin_status *) buffer;
  34. if (status == RT_NULL || size != sizeof(*status)) return 0;
  35. pin->ops->pin_write(dev, (rt_base_t)status->pin, (rt_base_t)status->status);
  36. return size;
  37. }
  38. static rt_err_t _pin_control(rt_device_t dev, int cmd, void *args)
  39. {
  40. struct rt_device_pin_mode *mode;
  41. struct rt_device_pin *pin = (struct rt_device_pin *)dev;
  42. /* check parameters */
  43. RT_ASSERT(pin != RT_NULL);
  44. mode = (struct rt_device_pin_mode *) args;
  45. if (mode == RT_NULL) return -RT_ERROR;
  46. pin->ops->pin_mode(dev, (rt_base_t)mode->pin, (rt_base_t)mode->mode);
  47. return 0;
  48. }
  49. #ifdef RT_USING_DEVICE_OPS
  50. const static struct rt_device_ops pin_ops =
  51. {
  52. RT_NULL,
  53. RT_NULL,
  54. RT_NULL,
  55. _pin_read,
  56. _pin_write,
  57. _pin_control
  58. };
  59. #endif
  60. int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data)
  61. {
  62. _hw_pin.parent.type = RT_Device_Class_Miscellaneous;
  63. _hw_pin.parent.rx_indicate = RT_NULL;
  64. _hw_pin.parent.tx_complete = RT_NULL;
  65. #ifdef RT_USING_DEVICE_OPS
  66. _hw_pin.parent.ops = &pin_ops;
  67. #else
  68. _hw_pin.parent.init = RT_NULL;
  69. _hw_pin.parent.open = RT_NULL;
  70. _hw_pin.parent.close = RT_NULL;
  71. _hw_pin.parent.read = _pin_read;
  72. _hw_pin.parent.write = _pin_write;
  73. _hw_pin.parent.control = _pin_control;
  74. #endif
  75. _hw_pin.ops = ops;
  76. _hw_pin.parent.user_data = user_data;
  77. /* register a character device */
  78. rt_device_register(&_hw_pin.parent, name, RT_DEVICE_FLAG_RDWR);
  79. return 0;
  80. }
  81. rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
  82. void (*hdr)(void *args), void *args)
  83. {
  84. RT_ASSERT(_hw_pin.ops != RT_NULL);
  85. if(_hw_pin.ops->pin_attach_irq)
  86. {
  87. return _hw_pin.ops->pin_attach_irq(&_hw_pin.parent, pin, mode, hdr, args);
  88. }
  89. return -RT_ENOSYS;
  90. }
  91. rt_err_t rt_pin_detach_irq(rt_int32_t pin)
  92. {
  93. RT_ASSERT(_hw_pin.ops != RT_NULL);
  94. if(_hw_pin.ops->pin_detach_irq)
  95. {
  96. return _hw_pin.ops->pin_detach_irq(&_hw_pin.parent, pin);
  97. }
  98. return -RT_ENOSYS;
  99. }
  100. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled)
  101. {
  102. RT_ASSERT(_hw_pin.ops != RT_NULL);
  103. if(_hw_pin.ops->pin_irq_enable)
  104. {
  105. return _hw_pin.ops->pin_irq_enable(&_hw_pin.parent, pin, enabled);
  106. }
  107. return -RT_ENOSYS;
  108. }
  109. /* RT-Thread Hardware PIN APIs */
  110. void rt_pin_mode(rt_base_t pin, rt_base_t mode)
  111. {
  112. RT_ASSERT(_hw_pin.ops != RT_NULL);
  113. _hw_pin.ops->pin_mode(&_hw_pin.parent, pin, mode);
  114. }
  115. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_mode, pinMode, set hardware pin mode);
  116. void rt_pin_write(rt_base_t pin, rt_base_t value)
  117. {
  118. RT_ASSERT(_hw_pin.ops != RT_NULL);
  119. _hw_pin.ops->pin_write(&_hw_pin.parent, pin, value);
  120. }
  121. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_write, pinWrite, write value to hardware pin);
  122. int rt_pin_read(rt_base_t pin)
  123. {
  124. RT_ASSERT(_hw_pin.ops != RT_NULL);
  125. return _hw_pin.ops->pin_read(&_hw_pin.parent, pin);
  126. }
  127. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_read, pinRead, read status from hardware pin);
  128. rt_base_t rt_pin_get(const char *name)
  129. {
  130. RT_ASSERT(_hw_pin.ops != RT_NULL);
  131. RT_ASSERT(name[0] == 'P');
  132. if(_hw_pin.ops->pin_get == RT_NULL)
  133. {
  134. return -RT_ENOSYS;
  135. }
  136. return _hw_pin.ops->pin_get(name);
  137. }
  138. FINSH_FUNCTION_EXPORT_ALIAS(rt_pin_get, pinGet, get pin number from hardware pin);