spi_dev.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * File : spi_dev.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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. */
  23. #include <rtthread.h>
  24. #include <drivers/spi.h>
  25. /* SPI bus device interface, compatible with RT-Thread 0.3.x/1.0.x */
  26. static rt_size_t _spi_bus_device_read(rt_device_t dev,
  27. rt_off_t pos,
  28. void *buffer,
  29. rt_size_t size)
  30. {
  31. struct rt_spi_bus *bus;
  32. bus = (struct rt_spi_bus *)dev;
  33. RT_ASSERT(bus != RT_NULL);
  34. RT_ASSERT(bus->owner != RT_NULL);
  35. return rt_spi_transfer(bus->owner, RT_NULL, buffer, size);
  36. }
  37. static rt_size_t _spi_bus_device_write(rt_device_t dev,
  38. rt_off_t pos,
  39. const void *buffer,
  40. rt_size_t size)
  41. {
  42. struct rt_spi_bus *bus;
  43. bus = (struct rt_spi_bus *)dev;
  44. RT_ASSERT(bus != RT_NULL);
  45. RT_ASSERT(bus->owner != RT_NULL);
  46. return rt_spi_transfer(bus->owner, buffer, RT_NULL, size);
  47. }
  48. static rt_err_t _spi_bus_device_control(rt_device_t dev,
  49. int cmd,
  50. void *args)
  51. {
  52. /* TODO: add control command handle */
  53. switch (cmd)
  54. {
  55. case 0: /* set device */
  56. break;
  57. case 1:
  58. break;
  59. }
  60. return RT_EOK;
  61. }
  62. #ifdef RT_USING_DEVICE_OPS
  63. const static struct rt_device_ops spi_bus_ops =
  64. {
  65. RT_NULL,
  66. RT_NULL,
  67. RT_NULL,
  68. _spi_bus_device_read,
  69. _spi_bus_device_write,
  70. _spi_bus_device_control
  71. };
  72. #endif
  73. rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name)
  74. {
  75. struct rt_device *device;
  76. RT_ASSERT(bus != RT_NULL);
  77. device = &bus->parent;
  78. /* set device type */
  79. device->type = RT_Device_Class_SPIBUS;
  80. /* initialize device interface */
  81. #ifdef RT_USING_DEVICE_OPS
  82. device->ops = &spi_bus_ops;
  83. #else
  84. device->init = RT_NULL;
  85. device->open = RT_NULL;
  86. device->close = RT_NULL;
  87. device->read = _spi_bus_device_read;
  88. device->write = _spi_bus_device_write;
  89. device->control = _spi_bus_device_control;
  90. #endif
  91. /* register to device manager */
  92. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  93. }
  94. /* SPI Dev device interface, compatible with RT-Thread 0.3.x/1.0.x */
  95. static rt_size_t _spidev_device_read(rt_device_t dev,
  96. rt_off_t pos,
  97. void *buffer,
  98. rt_size_t size)
  99. {
  100. struct rt_spi_device *device;
  101. device = (struct rt_spi_device *)dev;
  102. RT_ASSERT(device != RT_NULL);
  103. RT_ASSERT(device->bus != RT_NULL);
  104. return rt_spi_transfer(device, RT_NULL, buffer, size);
  105. }
  106. static rt_size_t _spidev_device_write(rt_device_t dev,
  107. rt_off_t pos,
  108. const void *buffer,
  109. rt_size_t size)
  110. {
  111. struct rt_spi_device *device;
  112. device = (struct rt_spi_device *)dev;
  113. RT_ASSERT(device != RT_NULL);
  114. RT_ASSERT(device->bus != RT_NULL);
  115. return rt_spi_transfer(device, buffer, RT_NULL, size);
  116. }
  117. static rt_err_t _spidev_device_control(rt_device_t dev,
  118. int cmd,
  119. void *args)
  120. {
  121. switch (cmd)
  122. {
  123. case 0: /* set device */
  124. break;
  125. case 1:
  126. break;
  127. }
  128. return RT_EOK;
  129. }
  130. #ifdef RT_USING_DEVICE_OPS
  131. const static struct rt_device_ops spi_device_ops =
  132. {
  133. RT_NULL,
  134. RT_NULL,
  135. RT_NULL,
  136. _spidev_device_read,
  137. _spidev_device_write,
  138. _spidev_device_control
  139. };
  140. #endif
  141. rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name)
  142. {
  143. struct rt_device *device;
  144. RT_ASSERT(dev != RT_NULL);
  145. device = &(dev->parent);
  146. /* set device type */
  147. device->type = RT_Device_Class_SPIDevice;
  148. #ifdef RT_USING_DEVICE_OPS
  149. device->ops = &spi_device_ops;
  150. #else
  151. device->init = RT_NULL;
  152. device->open = RT_NULL;
  153. device->close = RT_NULL;
  154. device->read = _spidev_device_read;
  155. device->write = _spidev_device_write;
  156. device->control = _spidev_device_control;
  157. #endif
  158. /* register to device manager */
  159. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  160. }