twbbi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2005-2007 by egnite Software 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. /*
  36. * $Id: twbbi.c 2935 2010-04-01 12:14:17Z haraldkipp $
  37. */
  38. #include "utils.h"
  39. #include "dialog.h"
  40. #include "twbbi.h"
  41. #define PIO_BASE 0xFFFF0000 /*!< \brief PIO base address. */
  42. #define PIO_PER (PIO_BASE + 0x00) /*!< \brief PIO enable register. */
  43. #define PIO_OER (PIO_BASE + 0x10) /*!< \brief Output enable register. */
  44. #define PIO_ODR (PIO_BASE + 0x14) /*!< \brief Output disable register. */
  45. #define PIO_CODR (PIO_BASE + 0x34) /*!< \brief Clear output data register. */
  46. #define PIO_PDSR (PIO_BASE + 0x3C) /*!< \brief Pin data status register. */
  47. #ifndef TWI_SDA_BIT
  48. #define TWI_SDA_BIT 16
  49. #endif
  50. #ifndef TWI_SDA_PE_REG
  51. #define TWI_SDA_PE_REG PIO_PER
  52. #endif
  53. #ifndef TWI_SDA_OE_REG
  54. #define TWI_SDA_OE_REG PIO_OER
  55. #endif
  56. #ifndef TWI_SDA_OD_REG
  57. #define TWI_SDA_OD_REG PIO_ODR
  58. #endif
  59. #ifndef TWI_SDA_COD_REG
  60. #define TWI_SDA_COD_REG PIO_CODR
  61. #endif
  62. #ifndef TWI_SDA_PDS_REG
  63. #define TWI_SDA_PDS_REG PIO_PDSR
  64. #endif
  65. #ifndef TWI_SCL_BIT
  66. #define TWI_SCL_BIT 17
  67. #endif
  68. #ifndef TWI_SCL_PE_REG
  69. #define TWI_SCL_PE_REG PIO_PER
  70. #endif
  71. #ifndef TWI_SCL_OE_REG
  72. #define TWI_SCL_OE_REG PIO_OER
  73. #endif
  74. #ifndef TWI_SCL_OD_REG
  75. #define TWI_SCL_OD_REG PIO_ODR
  76. #endif
  77. #ifndef TWI_SCL_COD_REG
  78. #define TWI_SCL_COD_REG PIO_CODR
  79. #endif
  80. #define outr(_reg, _val) (*((volatile unsigned int *)(_reg)) = (_val))
  81. #define inr(_reg) (*((volatile unsigned int *)(_reg)))
  82. #define _BV(bit) (1 << bit)
  83. #ifndef TWI_DELAY
  84. #define TWI_DELAY 8
  85. #endif
  86. #define SDA_LOW() outr(TWI_SDA_OE_REG, _BV(TWI_SDA_BIT))
  87. #define SDA_HIGH() outr(TWI_SDA_OD_REG, _BV(TWI_SDA_BIT))
  88. #define SDA_STAT() (inr(TWI_SDA_PDS_REG) & _BV(TWI_SDA_BIT))
  89. #define SCL_LOW() outr(TWI_SCL_OE_REG, _BV(TWI_SCL_BIT))
  90. #define SCL_HIGH() outr(TWI_SCL_OD_REG, _BV(TWI_SCL_BIT))
  91. static void TwDelay(int nops)
  92. {
  93. while (nops--) {
  94. _NOP();
  95. }
  96. }
  97. static void TwStart(void)
  98. {
  99. SDA_HIGH();
  100. TwDelay(TWI_DELAY);
  101. SCL_HIGH();
  102. TwDelay(TWI_DELAY);
  103. SDA_LOW();
  104. TwDelay(TWI_DELAY);
  105. SCL_LOW();
  106. TwDelay(TWI_DELAY);
  107. }
  108. static void TwStop(void)
  109. {
  110. SDA_LOW();
  111. TwDelay(TWI_DELAY);
  112. SCL_HIGH();
  113. TwDelay(2 * TWI_DELAY);
  114. SDA_HIGH();
  115. TwDelay(8 * TWI_DELAY);
  116. }
  117. static void TwAck(void)
  118. {
  119. SDA_LOW();
  120. SCL_HIGH();
  121. TwDelay(2 * TWI_DELAY);
  122. SCL_LOW();
  123. SDA_HIGH();
  124. }
  125. static int TwPut(unsigned char octet)
  126. {
  127. int i;
  128. for (i = 0x80; i; i >>= 1) {
  129. /* Set the data bit. */
  130. if (octet & i) {
  131. SDA_HIGH();
  132. } else {
  133. SDA_LOW();
  134. }
  135. /* Wait for data to stabelize. */
  136. TwDelay(TWI_DELAY);
  137. /* Toggle the clock. */
  138. SCL_HIGH();
  139. TwDelay(2 * TWI_DELAY);
  140. SCL_LOW();
  141. }
  142. /* Set data line high to receive the ACK bit. */
  143. SDA_HIGH();
  144. /* ACK should appear shortly after the clock's rising edge. */
  145. SCL_HIGH();
  146. TwDelay(2 * TWI_DELAY);
  147. if (SDA_STAT()) {
  148. i = -1;
  149. } else {
  150. i = 0;
  151. }
  152. SCL_LOW();
  153. return i;
  154. }
  155. static unsigned char TwGet(void)
  156. {
  157. unsigned char rc = 0;
  158. int i;
  159. /* SDA is input. */
  160. SDA_HIGH();
  161. for (i = 0x80; i; i >>= 1) {
  162. TwDelay(TWI_DELAY);
  163. /* Data should appear shortly after the clock's rising edge. */
  164. SCL_HIGH();
  165. TwDelay(2 * TWI_DELAY);
  166. /* SDA read. */
  167. if (SDA_STAT()) {
  168. rc |= i;
  169. }
  170. SCL_LOW();
  171. }
  172. return rc;
  173. }
  174. int TwMasterTransact(unsigned char sla, void *txdata, unsigned short txlen, void *rxdata, unsigned short rxsiz, unsigned long tmo)
  175. {
  176. int rc = 0;
  177. unsigned char *cp;
  178. if (txlen) {
  179. TwStart();
  180. /* Send SLA+W and check for ACK. */
  181. if ((rc = TwPut(sla << 1)) == 0) {
  182. for (cp = (unsigned char *) txdata; txlen--; cp++) {
  183. if ((rc = TwPut(*cp)) != 0) {
  184. break;
  185. }
  186. }
  187. }
  188. }
  189. if (rc == 0 && rxsiz) {
  190. TwStart();
  191. /* Send SLA+R and check for ACK. */
  192. if ((rc = TwPut((sla << 1) | 1)) == 0) {
  193. for (cp = rxdata;; cp++) {
  194. *cp = TwGet();
  195. if (++rc >= rxsiz) {
  196. break;
  197. }
  198. TwAck();
  199. }
  200. }
  201. }
  202. TwStop();
  203. return rc;
  204. }
  205. void TwInit(void)
  206. {
  207. SDA_HIGH();
  208. SCL_HIGH();
  209. outr(TWI_SDA_COD_REG, _BV(TWI_SDA_BIT));
  210. outr(TWI_SCL_COD_REG, _BV(TWI_SCL_BIT));
  211. outr(TWI_SDA_PE_REG, _BV(TWI_SDA_BIT));
  212. outr(TWI_SCL_PE_REG, _BV(TWI_SCL_BIT));
  213. }