Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TWI_XMEGA.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 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_XMEGA)
  28. #define __INCLUDE_FROM_TWI_C
  29. #include "../TWI.h"
  30. uint8_t TWI_StartTransmission(TWI_t* const TWI,
  31. const uint8_t SlaveAddress,
  32. const uint8_t TimeoutMS)
  33. {
  34. uint16_t TimeoutRemaining;
  35. TWI->MASTER.ADDR = SlaveAddress;
  36. TimeoutRemaining = (TimeoutMS * 100);
  37. while (TimeoutRemaining)
  38. {
  39. uint8_t status = TWI->MASTER.STATUS;
  40. if ((status & (TWI_MASTER_WIF_bm | TWI_MASTER_ARBLOST_bm)) == (TWI_MASTER_WIF_bm | TWI_MASTER_ARBLOST_bm))
  41. {
  42. TWI->MASTER.ADDR = SlaveAddress;
  43. }
  44. else if ((status & (TWI_MASTER_WIF_bm | TWI_MASTER_RXACK_bm)) == (TWI_MASTER_WIF_bm | TWI_MASTER_RXACK_bm))
  45. {
  46. TWI_StopTransmission(TWI);
  47. return TWI_ERROR_SlaveResponseTimeout;
  48. }
  49. else if (status & (TWI_MASTER_WIF_bm | TWI_MASTER_RIF_bm))
  50. {
  51. return TWI_ERROR_NoError;
  52. }
  53. _delay_us(10);
  54. TimeoutRemaining--;
  55. }
  56. if (!(TimeoutRemaining)) {
  57. if (TWI->MASTER.STATUS & TWI_MASTER_CLKHOLD_bm) {
  58. TWI_StopTransmission(TWI);
  59. }
  60. }
  61. return TWI_ERROR_BusCaptureTimeout;
  62. }
  63. bool TWI_SendByte(TWI_t* const TWI,
  64. const uint8_t Byte)
  65. {
  66. TWI->MASTER.DATA = Byte;
  67. while (!(TWI->MASTER.STATUS & TWI_MASTER_WIF_bm));
  68. return (TWI->MASTER.STATUS & TWI_MASTER_WIF_bm) && !(TWI->MASTER.STATUS & TWI_MASTER_RXACK_bm);
  69. }
  70. bool TWI_ReceiveByte(TWI_t* const TWI,
  71. uint8_t* const Byte,
  72. const bool LastByte)
  73. {
  74. if ((TWI->MASTER.STATUS & (TWI_MASTER_BUSERR_bm | TWI_MASTER_ARBLOST_bm)) == (TWI_MASTER_BUSERR_bm | TWI_MASTER_ARBLOST_bm)) {
  75. return false;
  76. }
  77. while (!(TWI->MASTER.STATUS & TWI_MASTER_RIF_bm));
  78. *Byte = TWI->MASTER.DATA;
  79. if (LastByte)
  80. TWI->MASTER.CTRLC = TWI_MASTER_ACKACT_bm | TWI_MASTER_CMD_STOP_gc;
  81. else
  82. TWI->MASTER.CTRLC = TWI_MASTER_CMD_RECVTRANS_gc;
  83. return true;
  84. }
  85. uint8_t TWI_ReadPacket(TWI_t* const TWI,
  86. const uint8_t SlaveAddress,
  87. const uint8_t TimeoutMS,
  88. const uint8_t* InternalAddress,
  89. uint8_t InternalAddressLen,
  90. uint8_t* Buffer,
  91. uint8_t Length)
  92. {
  93. uint8_t ErrorCode;
  94. if ((ErrorCode = TWI_StartTransmission(TWI, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
  95. TimeoutMS)) == TWI_ERROR_NoError)
  96. {
  97. while (InternalAddressLen--)
  98. {
  99. if (!(TWI_SendByte(TWI, *(InternalAddress++))))
  100. {
  101. ErrorCode = TWI_ERROR_SlaveNAK;
  102. break;
  103. }
  104. }
  105. if ((ErrorCode = TWI_StartTransmission(TWI, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_READ,
  106. TimeoutMS)) == TWI_ERROR_NoError)
  107. {
  108. while (Length--)
  109. {
  110. if (!(TWI_ReceiveByte(TWI, Buffer++, (Length == 0))))
  111. {
  112. ErrorCode = TWI_ERROR_SlaveNAK;
  113. break;
  114. }
  115. }
  116. }
  117. TWI_StopTransmission(TWI);
  118. }
  119. return ErrorCode;
  120. }
  121. uint8_t TWI_WritePacket(TWI_t* const TWI,
  122. const uint8_t SlaveAddress,
  123. const uint8_t TimeoutMS,
  124. const uint8_t* InternalAddress,
  125. uint8_t InternalAddressLen,
  126. const uint8_t* Buffer,
  127. uint8_t Length)
  128. {
  129. uint8_t ErrorCode;
  130. if ((ErrorCode = TWI_StartTransmission(TWI, (SlaveAddress & TWI_DEVICE_ADDRESS_MASK) | TWI_ADDRESS_WRITE,
  131. TimeoutMS)) == TWI_ERROR_NoError)
  132. {
  133. while (InternalAddressLen--)
  134. {
  135. if (!(TWI_SendByte(TWI, *(InternalAddress++))))
  136. {
  137. ErrorCode = TWI_ERROR_SlaveNAK;
  138. break;
  139. }
  140. }
  141. while (Length--)
  142. {
  143. if (!(TWI_SendByte(TWI, *(Buffer++))))
  144. {
  145. ErrorCode = TWI_ERROR_SlaveNAK;
  146. break;
  147. }
  148. }
  149. TWI_StopTransmission(TWI);
  150. }
  151. return ErrorCode;
  152. }
  153. #endif