twif.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #ifndef _DEV_TWIF_H_
  2. #define _DEV_TWIF_H_
  3. /*
  4. * Copyright (C) 2001-2005 by egnite Software GmbH. 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. * $Log$
  36. * Revision 1.3 2008/08/11 06:59:59 haraldkipp
  37. * BSD types replaced by stdint types (feature request #1282721).
  38. *
  39. * Revision 1.2 2005/10/24 10:56:30 haraldkipp
  40. * Added const modifier to transmit data pointer in TwMasterTransact().
  41. *
  42. * Revision 1.1.1.1 2003/05/09 14:41:09 haraldkipp
  43. * Initial using 3.2.1
  44. *
  45. * Revision 1.2 2003/03/31 14:53:23 harald
  46. * Prepare release 3.1
  47. *
  48. */
  49. #include <sys/types.h>
  50. #include <cfg/arch.h>
  51. #include <cfg/twi.h>
  52. #include <dev/irqreg.h>
  53. #include <stdint.h>
  54. /*!
  55. * \brief TWI ioctl() control codes
  56. */
  57. #define TWI_SETSPEED 0x0401 /*!< \brief Set transfer speed. */
  58. #define TWI_GETSPEED 0x0402 /*!< \brief Query transfer speed. */
  59. #define TWI_SETSLAVEADDRESS 0x0403 /*!< \brief Set local slave address. */
  60. #define TWI_GETSLAVEADDRESS 0x0404 /*!< \brief Query local slave address. */
  61. #define TWI_SETSTATUS 0x0409 /*!< \brief Set status. */
  62. #define TWI_GETSTATUS 0x040a /*!< \brief Query status. */
  63. /*!
  64. * \brief TWI error codes
  65. */
  66. #define TWERR_OK 0 /*!< \brief No error occured. */
  67. #define TWERR_TIMEOUT -1 /*!< \brief Interface timeout. */
  68. #define TWERR_BUS -2 /*!< \brief Bus error. */
  69. #define TWERR_IF_LOCKED -3 /*!< \brief Interface locked. */
  70. #define TWERR_SLA_NACK -4 /*!< \brief No slave response. */
  71. #define TWERR_DATA_NACK -5 /*!< \brief Data not acknowledged. */
  72. #define TWERR_OVRE -6 /*!< \brief Overrun Error. */
  73. #define TWERR_ARBLOST -7 /*!< \brief Arbitration lost. */
  74. #define TWERR_SPCTOUT -8 /*!< \brief Start/Stop Condition timeout */
  75. #define TWERR_BUSY -9 /*!< \brief Bus is busy timeout */
  76. #define TWSLA_MIN 17 /*!< \brief Lowest slave address.
  77. * Addresses below are reserved
  78. * for special purposes.
  79. */
  80. #define TWSLA_MAX 79 /*!< \brief Highest slave address.
  81. * Addresses above are reserved
  82. * for special purposes.
  83. */
  84. #define TWSLA_BCAST 0 /*!< \brief Broadcast slave address. */
  85. #define TWSLA_HOST 16 /*!< \brief Host slave address. */
  86. #define TWSLA_DEFAULT 193 /*!< \brief Default slave address. */
  87. typedef struct _NUTTWIBUS NUTTWIBUS;
  88. /* Include architecture specific TWI implementation */
  89. #if defined(__AVR__)
  90. #include "dev/twibus_avr.h"
  91. #elif defined(__arm__) && defined(__CORTEX__)
  92. #if defined(MCU_STM32)
  93. #include <arch/cm3/stm/stm32_twi.h>
  94. #endif
  95. #elif defined(__arm__) && !defined(__CORTEX__)
  96. #if defined(MCU_AT91R40008)
  97. #include "dev/twibus_bbif.h"
  98. #else
  99. #include "dev/twibus_at91.h"
  100. #endif
  101. #elif defined(__m68k__)
  102. #include <arch/m68k/twi.h>
  103. #endif
  104. /*!
  105. * \brief TWI/I2C bus structure.
  106. */
  107. struct _NUTTWIBUS {
  108. /*! \brief Bus base address.
  109. */
  110. uptr_t bus_base;
  111. /*! \brief Bus data and event interrupt handler.
  112. */
  113. IRQ_HANDLER *bus_sig_ev;
  114. /*! \brief Bus error interrupt handler.
  115. * If not supported by your device, leave it empty.
  116. */
  117. IRQ_HANDLER *bus_sig_er;
  118. /*! \brief Bus lock queue.
  119. */
  120. HANDLE bus_mutex;
  121. /*! \brief Interface Control Block.
  122. */
  123. NUTTWIICB *bus_icb;
  124. /*! \brief DMA channel for TX direction.
  125. */
  126. uint_fast8_t bus_dma_tx;
  127. /*! \brief DMA channel for RX direction.
  128. */
  129. uint_fast8_t bus_dma_rx;
  130. /*! \brief Initialize bus controller.
  131. *
  132. * This routine is called during device registration.
  133. */
  134. int (*bus_initbus) (void);
  135. /*! \brief Recover bus controller.
  136. *
  137. * This routine is called for recovering where a slave hangs with SCL low.
  138. */
  139. int (*bus_recover) (void);
  140. };
  141. extern int NutTwiMasterTranceive( NUTTWIBUS *bus,
  142. uint8_t sla,
  143. const void *txdata, uint16_t txlen,
  144. void *rxdata, uint16_t rxsiz,
  145. uint32_t tmo );
  146. extern int NutTwiMasterRegRead( NUTTWIBUS *bus,
  147. uint8_t sla,
  148. uint32_t iadr, uint8_t iadrlen,
  149. void *rxdata, uint16_t rxsiz,
  150. uint32_t tmo );
  151. extern int NutTwiMasterRegWrite( NUTTWIBUS *bus,
  152. uint8_t sla,
  153. uint32_t iadr, uint8_t iadrlen,
  154. const void *txdata, uint16_t txsiz,
  155. uint32_t tmo );
  156. extern int NutTwiMasterError(NUTTWIBUS *bus);
  157. extern int NutTwiSlaveListen(NUTTWIBUS *bus, uint8_t *sla, void *rxdata, uint16_t rxsiz, uint32_t tmo);
  158. extern int NutTwiSlaveRespond(NUTTWIBUS *bus, void *txdata, uint16_t txlen, uint32_t tmo);
  159. extern int NutTwiSlaveError(NUTTWIBUS *bus);
  160. extern uint16_t NutTwiIndexes( NUTTWIBUS *bus, uint8_t idx );
  161. extern int NutTwiIOCtl( NUTTWIBUS *bus, int req, void *conf );
  162. extern int NutRegisterTwiBus( NUTTWIBUS *bus, uint8_t sla );
  163. extern int NutDestroyTwiBus( NUTTWIBUS *bus);
  164. /*
  165. * Nut/OS Adaption to old TWI implementation
  166. */
  167. #if defined(DEF_TWIBUS)
  168. #define TwInit(slv) NutRegisterTwiBus(&DEF_TWIBUS, slv)
  169. #define TwIOCtl(req, conf) NutTwiIOCtl(&DEF_TWIBUS, req, conf)
  170. #define TwMasterTransact( sla, txd, txl, rxd, rxs, tmo) NutTwiMasterTranceive(&DEF_TWIBUS, sla, txd, txl, rxd, rxs, tmo)
  171. #define TwMasterRegRead( sla, iadr, ial, rxd, rxs, tmo) NutTwiMasterRegRead(&DEF_TWIBUS, sla, iadr, ial, rxd, rxs, tmo)
  172. #define TwMasterRegWrite( sla, iadr, ial, txd, txs, tmo) NutTwiMasterRegWrite(&DEF_TWIBUS, sla, iadr, ial, txd, txs, tmo)
  173. #define TwMasterError(void) NutTwiMasterError(&DEF_TWIBUS)
  174. #define TwMasterIndexes( idx) NutTwiIndexes(&DEF_TWIBUS, idx)
  175. #define TwSlaveListen(sla, rxdata, rxsiz, tmo) NutTwiSlaveListen(&DEF_TWIBUS, rxdata, rxsiz, tmo)
  176. #define TwSlaveRespond(txdata, txlen, tmo) NutTwiSlaveRespond(&DEF_TWIBUS, txlen, tmo)
  177. #define TwSlaveError(void) NutTwiSlaveError(&DEF_TWIBUS)
  178. #else
  179. #define TwInit(slv) -1
  180. #define TwIOCtl(req, conf)
  181. #define TwMasterTransact( sla, txd, txl, rxd, rxs, tmo) -1
  182. static inline int TwMasterRegRead(uint8_t sla, uint32_t iadr, uint8_t ial, const void* rxd, uint16_t rxs, uint32_t tmo)
  183. {
  184. (void)sla;
  185. (void)iadr;
  186. (void)ial;
  187. (void)rxd;
  188. (void)rxs;
  189. (void)tmo;
  190. return -1;
  191. }
  192. #define TwMasterRegWrite( sla, iadr, ial, txd, txs, tmo) -1
  193. #define TwMasterError(void)
  194. #define TwMasterIndexes( idx)
  195. #define TwSlaveListen(sla, rxdata, rxsiz, tmo)
  196. #define TwSlaveRespond(txdata, txlen, tmo)
  197. #define TwSlaveError(void)
  198. #endif
  199. #endif