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.

uart.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. {
  42. cli();
  43. UBRR0 = (F_CPU / 4 / baud - 1) / 2;
  44. UCSR0A = (1<<U2X0);
  45. UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
  46. UCSR0C = (1<<UCSZ01) | (1<<UCSZ00);
  47. tx_buffer_head = tx_buffer_tail = 0;
  48. rx_buffer_head = rx_buffer_tail = 0;
  49. sei();
  50. }
  51. // Transmit a byte
  52. void uart_putchar(uint8_t c)
  53. {
  54. uint8_t i;
  55. i = tx_buffer_head + 1;
  56. if (i >= TX_BUFFER_SIZE) i = 0;
  57. while (tx_buffer_tail == i) ; // wait until space in buffer
  58. //cli();
  59. tx_buffer[i] = c;
  60. tx_buffer_head = i;
  61. UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (1<<UDRIE0);
  62. //sei();
  63. }
  64. // Receive a byte
  65. uint8_t uart_getchar(void)
  66. {
  67. uint8_t c, i;
  68. while (rx_buffer_head == rx_buffer_tail) ; // wait for character
  69. i = rx_buffer_tail + 1;
  70. if (i >= RX_BUFFER_SIZE) i = 0;
  71. c = rx_buffer[i];
  72. rx_buffer_tail = i;
  73. return c;
  74. }
  75. // Return the number of bytes waiting in the receive buffer.
  76. // Call this before uart_getchar() to check if it will need
  77. // to wait for a byte to arrive.
  78. uint8_t uart_available(void)
  79. {
  80. uint8_t head, tail;
  81. head = rx_buffer_head;
  82. tail = rx_buffer_tail;
  83. if (head >= tail) return head - tail;
  84. return RX_BUFFER_SIZE + head - tail;
  85. }
  86. // Transmit Interrupt
  87. ISR(USART_UDRE_vect)
  88. {
  89. uint8_t i;
  90. if (tx_buffer_head == tx_buffer_tail) {
  91. // buffer is empty, disable transmit interrupt
  92. UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0);
  93. } else {
  94. i = tx_buffer_tail + 1;
  95. if (i >= TX_BUFFER_SIZE) i = 0;
  96. UDR0 = tx_buffer[i];
  97. tx_buffer_tail = i;
  98. }
  99. }
  100. // Receive Interrupt
  101. ISR(USART_RX_vect)
  102. {
  103. uint8_t c, i;
  104. c = UDR0;
  105. i = rx_buffer_head + 1;
  106. if (i >= RX_BUFFER_SIZE) i = 0;
  107. if (i != rx_buffer_tail) {
  108. rx_buffer[i] = c;
  109. rx_buffer_head = i;
  110. }
  111. }