spibus.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef _DEV_SPIBUS_H_
  2. #define _DEV_SPIBUS_H_
  3. /*
  4. * Copyright (C) 2008-2009 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file include/dev/spibus.h
  38. * \brief SPI bus declarations.
  39. *
  40. * \verbatim
  41. * $Id: spibus.h 5707 2014-05-23 13:56:17Z u_bonnes $
  42. * \endverbatim
  43. */
  44. #include <dev/irqreg.h>
  45. #include <sys/device.h>
  46. #include <sys/event.h>
  47. #define SPI_CURRENT_MODE ((uint16_t)-1)
  48. #define SPI_MODE_CPHA 0x0001 /* Data updated on leading edge. */
  49. #define SPI_MODE_CPOL 0x0002 /* Idle clock is high. */
  50. #define SPI_MODE_FAULT 0x0004 /* Enables mode fault detection. */
  51. #define SPI_MODE_HALFDUPLEX 0x0200 /* HalfDuplex Mode: Release MOSI and treat as MISO */
  52. #define SPI_MODE_LOOPBACK 0x0400 /* Loopback mode. */
  53. #define SPI_MODE_SLAVE 0x0800 /* Slave mode. */
  54. #define SPI_MODE_CSKEEP 0x1000 /* Chip select remains active after transfer. */
  55. #define SPI_MODE_CSDEC 0x2000 /* Decoded chip selects. */
  56. #define SPI_MODE_CSHIGH 0x4000 /* Chip select is high active. */
  57. #define SPI_MODE_UPDATE 0x8000 /* Mode update required. */
  58. #define SPI_MODE_0 0x0000
  59. #define SPI_MODE_1 SPI_MODE_CPHA
  60. #define SPI_MODE_2 SPI_MODE_CPOL
  61. #define SPI_MODE_3 (SPI_MODE_CPOL | SPI_MODE_CPHA)
  62. #define SPI_CURRENT_RATE ((uint32_t)-1)
  63. #define SPI_CURRENT_BITS ((uint8_t)-1)
  64. typedef struct _NUTSPIBUS NUTSPIBUS;
  65. typedef struct _NUTSPINODE NUTSPINODE;
  66. /*!
  67. * \brief SPI bus structure.
  68. */
  69. struct _NUTSPIBUS {
  70. /*! \brief Bus lock queue.
  71. */
  72. HANDLE bus_mutex;
  73. /*! \brief Bus wait ready queue.
  74. */
  75. HANDLE bus_ready;
  76. /*! \brief Bus base address.
  77. */
  78. uintptr_t bus_base;
  79. /*! \brief Bus interrupt handler.
  80. */
  81. IRQ_HANDLER *bus_sig;
  82. /*! \brief Initialize bus controller.
  83. *
  84. * This routine is called during device registration.
  85. */
  86. int (*bus_initnode) (NUTSPINODE *);
  87. /*! \brief Allocate the bus.
  88. *
  89. * Locks the bus and activates the chip select.
  90. */
  91. int (*bus_alloc) (NUTSPINODE *, uint32_t);
  92. /*! \brief Release the bus.
  93. *
  94. * Deactivates the chip select and unlocks the bus.
  95. */
  96. int (*bus_release) (NUTSPINODE *);
  97. /*! \brief Transfer data.
  98. */
  99. int (*bus_transfer) (NUTSPINODE *, const void *, void *, int);
  100. /*! \brief Wait for transfer ready.
  101. */
  102. int (*bus_wait) (NUTSPINODE *, uint32_t);
  103. /*! \brief Set node's SPI mode.
  104. */
  105. uint_fast16_t (*bus_set_mode) (NUTSPINODE *, uint_fast16_t);
  106. /*! \brief Set node's clock rate.
  107. */
  108. uint_fast32_t (*bus_set_rate) (NUTSPINODE *, uint_fast32_t);
  109. /*! \brief Set node's data bit size.
  110. */
  111. uint_fast8_t (*bus_set_bits) (NUTSPINODE *, uint_fast8_t);
  112. };
  113. /*!
  114. * \brief SPI node structure.
  115. */
  116. struct _NUTSPINODE {
  117. /*! \brief Pointer to the bus controller driver.
  118. *
  119. * Dynamically set when registering the SPI device.
  120. */
  121. NUTSPIBUS *node_bus;
  122. /*! \brief Pointer to the bus driver's device control block.
  123. *
  124. * Dynamically set during bus_initnode().
  125. */
  126. void *node_stat;
  127. /*! \brief SPI clock rate.
  128. *
  129. * Statically configured, but may change dynamically.
  130. */
  131. uint_fast32_t node_rate;
  132. /*! \brief SPI mode.
  133. *
  134. * Statically configured, but may change dynamically.
  135. */
  136. uint_fast16_t node_mode;
  137. /*! \brief SPI data bits.
  138. *
  139. * Statically configured, but may change dynamically.
  140. */
  141. uint_fast8_t node_bits;
  142. /*! \brief Chip select.
  143. *
  144. * Dynamically set when registering the SPI device.
  145. */
  146. uint_fast8_t node_cs;
  147. /*! \brief Pointer to the node driver's device control block.
  148. *
  149. * Statically configured in most cases.
  150. */
  151. void *node_dcb;
  152. };
  153. extern int NutRegisterSpiDevice(NUTDEVICE * dev, NUTSPIBUS * bus, int cs);
  154. extern uint_fast16_t NutSpiBusSetMode(NUTSPINODE * node, uint_fast16_t mode);
  155. extern uint_fast32_t NutSpiBusSetRate(NUTSPINODE * node, uint_fast32_t rate);
  156. extern uint_fast8_t NutSpiBusSetBits(NUTSPINODE * node, uint_fast8_t bits);
  157. extern uint_fast8_t NutSpiBusHalfDuplex(NUTSPINODE * node, uint_fast8_t ena);
  158. extern int NutSpiBusWait(NUTSPINODE * node, uint32_t tmo);
  159. #endif