TWI_AVR8.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2017.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2017 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. #include "../../../Common/Common.h"
  27. #if (ARCH == ARCH_AVR8) && defined(TWCR)
  28. #define __INCLUDE_FROM_TWI_C
  29. #include "../TWI.h"
  30. uint8_t TWI_StartTransmission(const uint8_t SlaveAddress,
  31. const uint8_t TimeoutMS)
  32. {
  33. for (;;)
  34. {
  35. bool BusCaptured = false;
  36. uint16_t TimeoutRemaining;
  37. TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
  38. TimeoutRemaining = (TimeoutMS * 100);
  39. while (TimeoutRemaining && !(BusCaptured))
  40. {
  41. if (TWCR & (1 << TWINT))
  42. {
  43. switch (TWSR & TW_STATUS_MASK)
  44. {
  45. case TW_START:
  46. case TW_REP_START:
  47. BusCaptured = true;
  48. break;
  49. case TW_MT_ARB_LOST:
  50. TWCR = ((1 << TWINT) | (1 << TWSTA) | (1 << TWEN));
  51. continue;
  52. default:
  53. TWCR = (1 << TWEN);
  54. return TWI_ERROR_BusFault;
  55. }
  56. }
  57. _delay_us(10);
  58. TimeoutRemaining--;
  59. }
  60. if (!(TimeoutRemaining))
  61. {
  62. TWCR = (1 << TWEN);
  63. return TWI_ERROR_BusCaptureTimeout;
  64. }
  65. TWDR = SlaveAddress;
  66. TWCR = ((1 << TWINT) | (1 << TWEN));
  67. TimeoutRemaining = (TimeoutMS * 100);
  68. while (TimeoutRemaining)
  69. {
  70. if (TWCR & (1 << TWINT))
  71. break;
  72. _delay_us(10);
  73. TimeoutRemaining--;
  74. }
  75. if (!(TimeoutRemaining))
  76. return TWI_ERROR_SlaveResponseTimeout;
  77. switch (TWSR & TW_STATUS_MASK)
  78. {
  79. case TW_MT_SLA_ACK:
  80. case TW_MR_SLA_ACK:
  81. return TWI_ERROR_NoError;
  82. default:
  83. TWCR = ((1 << TWINT) | (1 << TWSTO) | (1 << TWEN));
  84. return TWI_ERROR_SlaveNotReady;
  85. }
  86. }
  87. }
  88. bool TWI_SendByte(const uint8_t Byte)
  89. {
  90. TWDR = Byte;
  91. TWCR = ((1 << TWINT) | (1 << TWEN));
  92. while (!(TWCR & (1 << TWINT)));
  93. return ((TWSR & TW_STATUS_MASK) == TW_MT_DATA_ACK);
  94. }
  95. bool TWI_ReceiveByte(uint8_t* const Byte,
  96. const bool LastByte)
  97. {
  98. uint8_t TWCRMask;
  99. if (LastByte)
  100. TWCRMask = ((1 << TWINT) | (1 << TWEN));
  101. else
  102. TWCRMask = ((1 << TWINT) | (1 << TWEN) | (1 << TWEA));
  103. TWCR = TWCRMask;
  104. while (!(TWCR & (1 << TWINT)));
  105. *Byte = TWDR;
  106. uint8_t Status = (TWSR & TW_STATUS_MASK);
  107. return ((LastByte) ? (Status == TW_MR_DATA_NACK) : (Status == TW_MR_DATA_ACK));
  108. }
  109. uint8_t TWI_ReadPacket(const uint8_t SlaveAddress,
  110. const uint8_t TimeoutMS,
  111. const uint8_t* InternalAddress,
  112. uint8_t InternalAddressLen,
  113. uint8_t* Buffer,
  114. uint16_t Length)
  115. {
  116. uint8_t ErrorCode;
  117. if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
  118. TimeoutMS)) == TWI_ERROR_NoError)
  119. {
  120. while (InternalAddressLen--)
  121. {
  122. if (!(TWI_SendByte(*(InternalAddress++))))
  123. {
  124. ErrorCode = TWI_ERROR_SlaveNAK;
  125. break;
  126. }
  127. }
  128. if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
  129. TimeoutMS)) == TWI_ERROR_NoError)
  130. {
  131. while (Length--)
  132. {
  133. if (!(TWI_ReceiveByte(Buffer++, (Length == 0))))
  134. {
  135. ErrorCode = TWI_ERROR_SlaveNAK;
  136. break;
  137. }
  138. }
  139. TWI_StopTransmission();
  140. }
  141. }
  142. return ErrorCode;
  143. }
  144. uint8_t TWI_WritePacket(const uint8_t SlaveAddress,
  145. const uint8_t TimeoutMS,
  146. const uint8_t* InternalAddress,
  147. uint8_t InternalAddressLen,
  148. const uint8_t* Buffer,
  149. uint16_t Length)
  150. {
  151. uint8_t ErrorCode;
  152. if ((ErrorCode = TWI_StartTransmission((SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
  153. TimeoutMS)) == TWI_ERROR_NoError)
  154. {
  155. while (InternalAddressLen--)
  156. {
  157. if (!(TWI_SendByte(*(InternalAddress++))))
  158. {
  159. ErrorCode = TWI_ERROR_SlaveNAK;
  160. break;
  161. }
  162. }
  163. while (Length--)
  164. {
  165. if (!(TWI_SendByte(*(Buffer++))))
  166. {
  167. ErrorCode = TWI_ERROR_SlaveNAK;
  168. break;
  169. }
  170. }
  171. TWI_StopTransmission();
  172. }
  173. return ErrorCode;
  174. }
  175. #endif