spi_dev.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name)
  63. {
  64. struct rt_device *device;
  65. RT_ASSERT(bus != RT_NULL);
  66. device = &bus->parent;
  67. /* set device type */
  68. device->type = RT_Device_Class_SPIBUS;
  69. /* initialize device interface */
  70. device->init = RT_NULL;
  71. device->open = RT_NULL;
  72. device->close = RT_NULL;
  73. device->read = _spi_bus_device_read;
  74. device->write = _spi_bus_device_write;
  75. device->control = _spi_bus_device_control;
  76. /* register to device manager */
  77. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  78. }
  79. /* SPI Dev device interface, compatible with RT-Thread 0.3.x/1.0.x */
  80. static rt_size_t _spidev_device_read(rt_device_t dev,
  81. rt_off_t pos,
  82. void *buffer,
  83. rt_size_t size)
  84. {
  85. struct rt_spi_device *device;
  86. device = (struct rt_spi_device *)dev;
  87. RT_ASSERT(device != RT_NULL);
  88. RT_ASSERT(device->bus != RT_NULL);
  89. return rt_spi_transfer(device, RT_NULL, buffer, size);
  90. }
  91. static rt_size_t _spidev_device_write(rt_device_t dev,
  92. rt_off_t pos,
  93. const void *buffer,
  94. rt_size_t size)
  95. {
  96. struct rt_spi_device *device;
  97. device = (struct rt_spi_device *)dev;
  98. RT_ASSERT(device != RT_NULL);
  99. RT_ASSERT(device->bus != RT_NULL);
  100. return rt_spi_transfer(device, buffer, RT_NULL, size);
  101. }
  102. static rt_err_t _spidev_device_control(rt_device_t dev,
  103. int cmd,
  104. void *args)
  105. {
  106. switch (cmd)
  107. {
  108. case 0: /* set device */
  109. break;
  110. case 1:
  111. break;
  112. }
  113. return RT_EOK;
  114. }
  115. rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name)
  116. {
  117. struct rt_device *device;
  118. RT_ASSERT(dev != RT_NULL);
  119. device = &(dev->parent);
  120. /* set device type */
  121. device->type = RT_Device_Class_SPIDevice;
  122. device->init = RT_NULL;
  123. device->open = RT_NULL;
  124. device->close = RT_NULL;
  125. device->read = _spidev_device_read;
  126. device->write = _spidev_device_write;
  127. device->control = _spidev_device_control;
  128. /* register to device manager */
  129. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR);
  130. }