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.

XPROGTarget.c 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /** \file
  27. *
  28. * Target-related functions for the PDI Protocol decoder.
  29. */
  30. #define INCLUDE_FROM_XPROGTARGET_C
  31. #include "XPROGTarget.h"
  32. #if defined(ENABLE_XPROG_PROTOCOL) || defined(__DOXYGEN__)
  33. /** Flag to indicate if the USART is currently in Tx or Rx mode. */
  34. bool IsSending;
  35. /** Enables the target's PDI interface, holding the target in reset until PDI mode is exited. */
  36. void XPROGTarget_EnableTargetPDI(void)
  37. {
  38. IsSending = false;
  39. /* Set Tx and XCK as outputs, Rx as input */
  40. DDRD |= (1 << 5) | (1 << 3);
  41. DDRD &= ~(1 << 2);
  42. /* Set DATA line high for at least 90ns to disable /RESET functionality */
  43. PORTD |= (1 << 3);
  44. _delay_us(100);
  45. /* Set up the synchronous USART for XMEGA communications - 8 data bits, even parity, 2 stop bits */
  46. UBRR1 = ((F_CPU / 2 / XPROG_HARDWARE_SPEED) - 1);
  47. UCSR1B = (1 << TXEN1);
  48. UCSR1C = (1 << UMSEL10) | (1 << UPM11) | (1 << USBS1) | (1 << UCSZ11) | (1 << UCSZ10) | (1 << UCPOL1);
  49. /* Send two IDLEs of 12 bits each to enable PDI interface (need at least 16 idle bits) */
  50. XPROGTarget_SendIdle();
  51. XPROGTarget_SendIdle();
  52. }
  53. /** Enables the target's TPI interface, holding the target in reset until TPI mode is exited. */
  54. void XPROGTarget_EnableTargetTPI(void)
  55. {
  56. IsSending = false;
  57. /* Set /RESET line low for at least 400ns to enable TPI functionality */
  58. AUX_LINE_DDR |= AUX_LINE_MASK;
  59. AUX_LINE_PORT &= ~AUX_LINE_MASK;
  60. _delay_us(100);
  61. /* Set Tx and XCK as outputs, Rx as input */
  62. DDRD |= (1 << 5) | (1 << 3);
  63. DDRD &= ~(1 << 2);
  64. /* Set up the synchronous USART for TPI communications - 8 data bits, even parity, 2 stop bits */
  65. UBRR1 = ((F_CPU / 2 / XPROG_HARDWARE_SPEED) - 1);
  66. UCSR1B = (1 << TXEN1);
  67. UCSR1C = (1 << UMSEL10) | (1 << UPM11) | (1 << USBS1) | (1 << UCSZ11) | (1 << UCSZ10) | (1 << UCPOL1);
  68. /* Send two IDLEs of 12 bits each to enable TPI interface (need at least 16 idle bits) */
  69. XPROGTarget_SendIdle();
  70. XPROGTarget_SendIdle();
  71. }
  72. /** Disables the target's PDI interface, exits programming mode and starts the target's application. */
  73. void XPROGTarget_DisableTargetPDI(void)
  74. {
  75. /* Switch to Rx mode to ensure that all pending transmissions are complete */
  76. if (IsSending)
  77. XPROGTarget_SetRxMode();
  78. /* Turn off receiver and transmitter of the USART, clear settings */
  79. UCSR1A = ((1 << TXC1) | (1 << RXC1));
  80. UCSR1B = 0;
  81. UCSR1C = 0;
  82. /* Tristate all pins */
  83. DDRD &= ~((1 << 5) | (1 << 3));
  84. PORTD &= ~((1 << 5) | (1 << 3) | (1 << 2));
  85. }
  86. /** Disables the target's TPI interface, exits programming mode and starts the target's application. */
  87. void XPROGTarget_DisableTargetTPI(void)
  88. {
  89. /* Switch to Rx mode to ensure that all pending transmissions are complete */
  90. if (IsSending)
  91. XPROGTarget_SetRxMode();
  92. /* Turn off receiver and transmitter of the USART, clear settings */
  93. UCSR1A |= (1 << TXC1) | (1 << RXC1);
  94. UCSR1B = 0;
  95. UCSR1C = 0;
  96. /* Set all USART lines as inputs, tristate */
  97. DDRD &= ~((1 << 5) | (1 << 3));
  98. PORTD &= ~((1 << 5) | (1 << 3) | (1 << 2));
  99. /* Tristate target /RESET line */
  100. AUX_LINE_DDR &= ~AUX_LINE_MASK;
  101. AUX_LINE_PORT &= ~AUX_LINE_MASK;
  102. }
  103. /** Sends a byte via the USART.
  104. *
  105. * \param[in] Byte Byte to send through the USART
  106. */
  107. void XPROGTarget_SendByte(const uint8_t Byte)
  108. {
  109. /* Switch to Tx mode if currently in Rx mode */
  110. if (!(IsSending))
  111. XPROGTarget_SetTxMode();
  112. /* Wait until there is space in the hardware Tx buffer before writing */
  113. while (!(UCSR1A & (1 << UDRE1)));
  114. UCSR1A |= (1 << TXC1);
  115. UDR1 = Byte;
  116. }
  117. /** Receives a byte via the hardware USART, blocking until data is received or timeout expired.
  118. *
  119. * \return Received byte from the USART
  120. */
  121. uint8_t XPROGTarget_ReceiveByte(void)
  122. {
  123. /* Switch to Rx mode if currently in Tx mode */
  124. if (IsSending)
  125. XPROGTarget_SetRxMode();
  126. /* Wait until a byte has been received before reading */
  127. while (!(UCSR1A & (1 << RXC1)) && TimeoutTicksRemaining);
  128. return UDR1;
  129. }
  130. /** Sends an IDLE via the USART to the attached target, consisting of a full frame of idle bits. */
  131. void XPROGTarget_SendIdle(void)
  132. {
  133. /* Switch to Tx mode if currently in Rx mode */
  134. if (!(IsSending))
  135. XPROGTarget_SetTxMode();
  136. /* Need to do nothing for a full frame to send an IDLE */
  137. for (uint8_t i = 0; i < BITS_IN_USART_FRAME; i++)
  138. {
  139. /* Wait for a full cycle of the clock */
  140. while (PIND & (1 << 5));
  141. while (!(PIND & (1 << 5)));
  142. while (PIND & (1 << 5));
  143. }
  144. }
  145. static void XPROGTarget_SetTxMode(void)
  146. {
  147. /* Wait for a full cycle of the clock */
  148. while (PIND & (1 << 5));
  149. while (!(PIND & (1 << 5)));
  150. while (PIND & (1 << 5));
  151. PORTD |= (1 << 3);
  152. DDRD |= (1 << 3);
  153. UCSR1B &= ~(1 << RXEN1);
  154. UCSR1B |= (1 << TXEN1);
  155. IsSending = true;
  156. }
  157. static void XPROGTarget_SetRxMode(void)
  158. {
  159. while (!(UCSR1A & (1 << TXC1)));
  160. UCSR1A |= (1 << TXC1);
  161. UCSR1B &= ~(1 << TXEN1);
  162. UCSR1B |= (1 << RXEN1);
  163. DDRD &= ~(1 << 3);
  164. PORTD &= ~(1 << 3);
  165. IsSending = false;
  166. }
  167. #endif