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.

57 line
2.1KB

  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #include <Arduino.h>
  21. #if defined(UDR0) || defined(DOXYGEN)
  22. #include "MinimumSerial.h"
  23. const uint16_t MIN_2X_BAUD = F_CPU/(4*(2*0XFFF + 1)) + 1;
  24. //------------------------------------------------------------------------------
  25. void MinimumSerial::begin(uint32_t baud) {
  26. uint16_t baud_setting;
  27. // don't worry, the compiler will squeeze out F_CPU != 16000000UL
  28. if ((F_CPU != 16000000UL || baud != 57600) && baud > MIN_2X_BAUD) {
  29. // Double the USART Transmission Speed
  30. UCSR0A = 1 << U2X0;
  31. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  32. } else {
  33. // hardcoded exception for compatibility with the bootloader shipped
  34. // with the Duemilanove and previous boards and the firmware on the 8U2
  35. // on the Uno and Mega 2560.
  36. UCSR0A = 0;
  37. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  38. }
  39. // assign the baud_setting
  40. UBRR0H = baud_setting >> 8;
  41. UBRR0L = baud_setting;
  42. // enable transmit and receive
  43. UCSR0B |= (1 << TXEN0) | (1 << RXEN0);
  44. }
  45. //------------------------------------------------------------------------------
  46. int MinimumSerial::read() {
  47. if (UCSR0A & (1 << RXC0)) return UDR0;
  48. return -1;
  49. }
  50. //------------------------------------------------------------------------------
  51. size_t MinimumSerial::write(uint8_t b) {
  52. while (((1 << UDRIE0) & UCSR0B) || !(UCSR0A & (1 << UDRE0))) {}
  53. UDR0 = b;
  54. return 1;
  55. }
  56. #endif // defined(UDR0) || defined(DOXYGEN)