spibus.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2008-2009 by egnite GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file dev/spibus.c
  36. * \brief Generic SPI bus driver routines.
  37. *
  38. * \verbatim
  39. * $Id: spibus.c 5707 2014-05-23 13:56:17Z u_bonnes $
  40. * \endverbatim
  41. */
  42. #include <sys/device.h>
  43. #include <sys/nutdebug.h>
  44. #include <dev/spibus.h>
  45. /*!
  46. * \brief Register and initialize an SPI device attached to a specified bus.
  47. *
  48. * Calls the bus controller initialization and, if successful, initializes
  49. * the SPI device and adds it to the system device list.
  50. *
  51. * Applications should call this function during initialization for each
  52. * SPI device they intend to use.
  53. *
  54. * \param dev Pointer to the \ref NUTDEVICE structure, which is provided
  55. * by the device driver.
  56. * \param bus Pointer to the \ref NUTSPIBUS structure, which is provided
  57. * by the bus controller driver.
  58. * \param cs Zero based chip select number for this device. Must be set
  59. * to 0, if only one device is attached to the bus and no chip
  60. * select line is provided.
  61. *
  62. * \return 0 if the device has been registered for the first time and
  63. * the initialization was successful. The function returns -1,
  64. * if any device with the same name had been registered previously,
  65. * if the \ref NUTDEVICE structure or the chip select is invalid or
  66. * if the device or bus controller initialization failed.
  67. */
  68. int NutRegisterSpiDevice(NUTDEVICE * dev, NUTSPIBUS * bus, int cs)
  69. {
  70. int rc;
  71. NUTSPINODE *node;
  72. NUTASSERT(dev != NULL);
  73. NUTASSERT(dev->dev_icb != NULL);
  74. node = (NUTSPINODE *) dev->dev_icb;
  75. NUTASSERT(bus != NULL);
  76. NUTASSERT(bus->bus_initnode != NULL);
  77. /* Attach the bus controller driver and set the chip select number
  78. ** before calling the bus controller initialization routine. */
  79. node->node_bus = bus;
  80. node->node_cs = cs;
  81. rc = (*bus->bus_initnode) (node);
  82. /* If the bus had been successfully initialized for this device,
  83. ** then set up a mutex lock for the bus and register the device. */
  84. if (rc == 0) {
  85. NutEventPost(&bus->bus_mutex);
  86. rc = NutRegisterDevice(dev, 0, 0);
  87. }
  88. return rc;
  89. }
  90. /*!
  91. * \brief Wait until all SPI bus transfers are done.
  92. *
  93. * This dummy function is used by SPI device drivers, which do not
  94. * provide asynchronous transfers.
  95. *
  96. * \param node Specifies the SPI bus node.
  97. * \param tmo Timeout in milliseconds. To disable timeout, set this
  98. * parameter to NUT_WAIT_INFINITE.
  99. *
  100. * \return Always 0.
  101. */
  102. int NutSpiBusWait(NUTSPINODE * node, uint32_t tmo)
  103. {
  104. return 0;
  105. }
  106. /*!
  107. * \brief Set SPI mode of a specified bus device.
  108. *
  109. * The new mode will be used during the next transfer. If its value is
  110. * SPI_CURRENT_MODE, then the mode is not updated. This can be used
  111. * to query the current mode.
  112. *
  113. * Otherwise this parameter may be the or'ed combination of the
  114. * following bits:
  115. *
  116. * - SPI_MODE_CPHA: Data updated on leading edge.
  117. * - SPI_MODE_CPOL: Idle clock is high.
  118. * - SPI_MODE_FAULT: Enables mode fault detection.
  119. * - SPI_MODE_HALFDUPLEX: Enable half duplex mode.
  120. * - SPI_MODE_LOOPBACK: Loopback mode.
  121. * - SPI_MODE_SLAVE: Slave mode.
  122. * - SPI_MODE_CSKEEP: Chip select remains active after transfer.
  123. * - SPI_MODE_CSDEC: Decoded chip selects.
  124. * - SPI_MODE_CSHIGH: Chip select is high active.
  125. *
  126. * Be aware, that SPI drivers may have implemented a subset only.
  127. *
  128. * Instead of SPI_MODE_CPHA and SPI_MODE_CPOL one of the following
  129. * mode numbers may be used:
  130. *
  131. * - SPI_MODE_0: Idle clock is low and data is captured on the rising edge.
  132. * - SPI_MODE_1: Idle clock is low and data is captured on the falling edge.
  133. * - SPI_MODE_2: Idle clock is high and data is captured on the falling edge.
  134. * - SPI_MODE_3: Idle clock is high and data is captured on the rising edge.
  135. *
  136. * The return value may additionally contain the bit SPI_MODE_UPDATE,
  137. * which indicates that here had been no transfer since the last mode
  138. * update.
  139. *
  140. * \param node Specifies the SPI bus node.
  141. * \param mode New mode.
  142. *
  143. * \return Old mode.
  144. */
  145. uint_fast16_t NutSpiBusSetMode(NUTSPINODE * node, uint_fast16_t mode)
  146. {
  147. uint16_t rc = node->node_mode;
  148. if (mode != SPI_CURRENT_MODE) {
  149. node->node_mode = mode | SPI_MODE_UPDATE;
  150. }
  151. return rc;
  152. }
  153. /*!
  154. * \brief Set clock rate of a specified SPI device.
  155. *
  156. * The new clock rate will be used for the next transfer. If the given
  157. * rate is beyond the capabilities of the bus controller, it will
  158. * automatically adjusted before the transfer starts.
  159. *
  160. * \param node Specifies the SPI bus node.
  161. * \param rate New clock rate, given in bits per second. If the value is
  162. * SPI_CURRENT_RATE, then the current rate is kept.
  163. *
  164. * \return Previous rate.
  165. */
  166. uint_fast32_t NutSpiBusSetRate(NUTSPINODE * node, uint_fast32_t rate)
  167. {
  168. uint32_t rc = node->node_rate;
  169. if (rate != SPI_CURRENT_RATE) {
  170. node->node_rate = rate;
  171. node->node_mode |= SPI_MODE_UPDATE;
  172. }
  173. return rc;
  174. }
  175. /*!
  176. * \brief Set Duplex mode.
  177. *
  178. * The new duplex mode will be used for the next transfer.
  179. * When on, MOSI is tristated and data is read from MOSI.
  180. *
  181. * \param node Specifies the SPI bus node.
  182. * \param ena O to switch off, switch on else
  183. *
  184. * \return Previous half duplex state.
  185. */
  186. uint_fast8_t NutSpiBusHalfDuplex(NUTSPINODE * node, uint_fast8_t ena)
  187. {
  188. uint8_t rc = node->node_mode & SPI_MODE_HALFDUPLEX;
  189. if (ena) {
  190. node->node_mode |= SPI_MODE_HALFDUPLEX;
  191. } else {
  192. node->node_mode &= ~SPI_MODE_HALFDUPLEX;
  193. }
  194. return rc;
  195. }
  196. /*!
  197. * \brief Set clock rate of a specified SPI device.
  198. *
  199. * The new clock rate will be used for the next transfer. If the given
  200. * rate is beyond the capabilities of the bus controller, it will
  201. * automatically adjusted before the transfer starts.
  202. *
  203. * \param node Specifies the SPI bus node.
  204. * \param rate New clock rate, given in bits per second. If the value is
  205. * SPI_CURRENT_RATE, then the current rate is kept.
  206. *
  207. * \return Previous rate.
  208. */
  209. uint_fast8_t NutSpiBusSetBits(NUTSPINODE * node, uint_fast8_t bits)
  210. {
  211. uint_fast8_t rc = node->node_bits;
  212. if (bits != SPI_CURRENT_BITS) {
  213. node->node_bits = bits;
  214. node->node_mode |= SPI_MODE_UPDATE;
  215. }
  216. return rc;
  217. }