uart.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // TODO: Teensy support(ATMega32u4/AT90USB128)
  2. // Fixed for Arduino Duemilanove ATmega168p by Jun Wako
  3. /* UART Example for Teensy USB Development Board
  4. * http://www.pjrc.com/teensy/
  5. * Copyright (c) 2009 PJRC.COM, LLC
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. // Version 1.0: Initial Release
  26. // Version 1.1: Add support for Teensy 2.0, minor optimizations
  27. #include <avr/io.h>
  28. #include <avr/interrupt.h>
  29. #include "uart.h"
  30. // These buffers may be any size from 2 to 256 bytes.
  31. #define RX_BUFFER_SIZE 64
  32. #define TX_BUFFER_SIZE 40
  33. static volatile uint8_t tx_buffer[TX_BUFFER_SIZE];
  34. static volatile uint8_t tx_buffer_head;
  35. static volatile uint8_t tx_buffer_tail;
  36. static volatile uint8_t rx_buffer[RX_BUFFER_SIZE];
  37. static volatile uint8_t rx_buffer_head;
  38. static volatile uint8_t rx_buffer_tail;
  39. // Initialize the UART
  40. void uart_init(uint32_t baud) {
  41. cli();
  42. UBRR0 = (F_CPU / 4 / baud - 1) / 2;
  43. UCSR0A = (1 << U2X0);
  44. UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
  45. UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
  46. tx_buffer_head = tx_buffer_tail = 0;
  47. rx_buffer_head = rx_buffer_tail = 0;
  48. sei();
  49. }
  50. // Transmit a byte
  51. void uart_putchar(uint8_t c) {
  52. uint8_t i;
  53. i = tx_buffer_head + 1;
  54. if (i >= TX_BUFFER_SIZE) i = 0;
  55. while (tx_buffer_tail == i)
  56. ; // wait until space in buffer
  57. // cli();
  58. tx_buffer[i] = c;
  59. tx_buffer_head = i;
  60. UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0) | (1 << UDRIE0);
  61. // sei();
  62. }
  63. // Receive a byte
  64. uint8_t uart_getchar(void) {
  65. uint8_t c, i;
  66. while (rx_buffer_head == rx_buffer_tail)
  67. ; // wait for character
  68. i = rx_buffer_tail + 1;
  69. if (i >= RX_BUFFER_SIZE) i = 0;
  70. c = rx_buffer[i];
  71. rx_buffer_tail = i;
  72. return c;
  73. }
  74. // Return the number of bytes waiting in the receive buffer.
  75. // Call this before uart_getchar() to check if it will need
  76. // to wait for a byte to arrive.
  77. uint8_t uart_available(void) {
  78. uint8_t head, tail;
  79. head = rx_buffer_head;
  80. tail = rx_buffer_tail;
  81. if (head >= tail) return head - tail;
  82. return RX_BUFFER_SIZE + head - tail;
  83. }
  84. // Transmit Interrupt
  85. ISR(USART_UDRE_vect) {
  86. uint8_t i;
  87. if (tx_buffer_head == tx_buffer_tail) {
  88. // buffer is empty, disable transmit interrupt
  89. UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
  90. } else {
  91. i = tx_buffer_tail + 1;
  92. if (i >= TX_BUFFER_SIZE) i = 0;
  93. UDR0 = tx_buffer[i];
  94. tx_buffer_tail = i;
  95. }
  96. }
  97. // Receive Interrupt
  98. ISR(USART_RX_vect) {
  99. uint8_t c, i;
  100. c = UDR0;
  101. i = rx_buffer_head + 1;
  102. if (i >= RX_BUFFER_SIZE) i = 0;
  103. if (i != rx_buffer_tail) {
  104. rx_buffer[i] = c;
  105. rx_buffer_head = i;
  106. }
  107. }