SoftUART.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2010 David Prentice (david.prentice [at] farming [dot] uk)
  9. Copyright 2010 Peter Danneger
  10. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  11. Permission to use, copy, modify, distribute, and sell this
  12. software and its documentation for any purpose is hereby granted
  13. without fee, provided that the above copyright notice appear in
  14. all copies and that both that the copyright notice and this
  15. permission notice and warranty disclaimer appear in supporting
  16. documentation, and that the name of the author not be used in
  17. advertising or publicity pertaining to distribution of the
  18. software without specific, written prior permission.
  19. The author disclaims all warranties with regard to this
  20. software, including all implied warranties of merchantability
  21. and fitness. In no event shall the author be liable for any
  22. special, indirect or consequential damages or any damages
  23. whatsoever resulting from loss of use, data or profits, whether
  24. in an action of contract, negligence or other tortious action,
  25. arising out of or in connection with the use or performance of
  26. this software.
  27. */
  28. /** \file
  29. *
  30. * Software UART for both data transmission and reception. This
  31. * code continuously monitors the ring buffers set up by the main
  32. * project source file and reads/writes data as it becomes available.
  33. */
  34. #include "SoftUART.h"
  35. /** Total number of bits remaining to be sent in the current frame */
  36. static uint8_t TX_BitsRemaining;
  37. /** Temporary data variable to hold the byte being transmitted as it is shifted out */
  38. static uint8_t TX_Data;
  39. /** Total number of bits remaining to be received in the current frame */
  40. static uint8_t RX_BitsRemaining;
  41. /** Temporary data variable to hold the byte being received as it is shifted in */
  42. static uint8_t RX_Data;
  43. /** Initializes the software UART, ready for data transmission and reception into the global ring buffers. */
  44. void SoftUART_Init(void)
  45. {
  46. /* Set TX pin to output high, enable RX pull-up */
  47. STXPORT |= (1 << STX);
  48. STXDDR |= (1 << STX);
  49. SRXPORT |= (1 << SRX);
  50. /* Enable INT0 for the detection of incoming start bits that signal the start of a byte */
  51. EICRA = (1 << ISC01);
  52. EIMSK = (1 << INT0);
  53. /* Set the transmission and reception timer compare values for the default baud rate */
  54. SoftUART_SetBaud(9600);
  55. /* Setup reception timer compare ISR */
  56. TIMSK1 = (1 << OCIE1A);
  57. /* Setup transmission timer compare ISR and start the timer */
  58. TIMSK3 = (1 << OCIE3A);
  59. TCCR3B = ((1 << CS30) | (1 << WGM32));
  60. }
  61. /** ISR to detect the start of a bit being sent to the software UART. */
  62. ISR(INT0_vect, ISR_BLOCK)
  63. {
  64. /* Reset the number of reception bits remaining counter */
  65. RX_BitsRemaining = 8;
  66. /* Reset the bit reception timer to -(1/2) of the total bit time, so that the first data bit is
  67. * sampled mid way through the total bit time, making reception more robust.
  68. */
  69. TCNT1 = -(OCR1A >> 1);
  70. /* Check to see that the pin is still low (prevents glitches from starting a frame reception) */
  71. if (!(SRXPIN & (1 << SRX)))
  72. {
  73. /* Disable start bit detection ISR while the next byte is received */
  74. EIMSK = 0;
  75. /* Start the reception timer */
  76. TCCR1B = ((1 << CS10) | (1 << WGM12));
  77. }
  78. }
  79. /** ISR to manage the reception of bits to the software UART. */
  80. ISR(TIMER1_COMPA_vect, ISR_BLOCK)
  81. {
  82. /* Cache the current RX pin value for later checking */
  83. uint8_t SRX_Cached = (SRXPIN & (1 << SRX));
  84. /* Check if reception has finished */
  85. if (RX_BitsRemaining)
  86. {
  87. /* Shift the current received bit mask to the next bit position */
  88. RX_Data >>= 1;
  89. RX_BitsRemaining--;
  90. /* Store next bit into the received data variable */
  91. if (SRX_Cached)
  92. RX_Data |= (1 << 7);
  93. }
  94. else
  95. {
  96. /* Disable the reception timer as all data has now been received, re-enable start bit detection ISR */
  97. TCCR1B = 0;
  98. EIFR = (1 << INTF0);
  99. EIMSK = (1 << INT0);
  100. /* Reception complete, store the received byte if stop bit valid */
  101. if (SRX_Cached)
  102. RingBuffer_Insert(&UARTtoUSB_Buffer, RX_Data);
  103. }
  104. }
  105. /** ISR to manage the transmission of bits via the software UART. */
  106. ISR(TIMER3_COMPA_vect, ISR_BLOCK)
  107. {
  108. /* Check if transmission has finished */
  109. if (TX_BitsRemaining)
  110. {
  111. /* Set the TX line to the value of the next bit in the byte to send */
  112. if (TX_Data & (1 << 0))
  113. STXPORT &= ~(1 << STX);
  114. else
  115. STXPORT |= (1 << STX);
  116. /* Shift the transmission byte to move the next bit into position and decrement the bits remaining counter */
  117. TX_Data >>= 1;
  118. TX_BitsRemaining--;
  119. }
  120. else if (!(RX_BitsRemaining) && !(RingBuffer_IsEmpty(&USBtoUART_Buffer)))
  121. {
  122. /* Start bit - TX line low */
  123. STXPORT &= ~(1 << STX);
  124. /* Transmission complete, get the next byte to send (if available) */
  125. TX_Data = ~RingBuffer_Remove(&USBtoUART_Buffer);
  126. TX_BitsRemaining = 9;
  127. }
  128. }