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.

72 line
2.4KB

  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. //------------------------------------------------------------------------------
  24. /**
  25. * Set baud rate for serial port zero and enable in non interrupt mode.
  26. * Do not call this function if you use another serial library.
  27. * \param[in] baud rate
  28. */
  29. void MinimumSerial::begin(uint32_t baud) {
  30. uint16_t baud_setting;
  31. // don't worry, the compiler will squeeze out F_CPU != 16000000UL
  32. if (F_CPU != 16000000UL || baud != 57600) {
  33. // Double the USART Transmission Speed
  34. UCSR0A = 1 << U2X0;
  35. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  36. } else {
  37. // hardcoded exception for compatibility with the bootloader shipped
  38. // with the Duemilanove and previous boards and the firmware on the 8U2
  39. // on the Uno and Mega 2560.
  40. UCSR0A = 0;
  41. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  42. }
  43. // assign the baud_setting
  44. UBRR0H = baud_setting >> 8;
  45. UBRR0L = baud_setting;
  46. // enable transmit and receive
  47. UCSR0B |= (1 << TXEN0) | (1 << RXEN0);
  48. }
  49. //------------------------------------------------------------------------------
  50. /**
  51. * Unbuffered read
  52. * \return -1 if no character is available or an available character.
  53. */
  54. int MinimumSerial::read() {
  55. if (UCSR0A & (1 << RXC0)) return UDR0;
  56. return -1;
  57. }
  58. //------------------------------------------------------------------------------
  59. /**
  60. * Unbuffered write
  61. *
  62. * \param[in] b byte to write.
  63. * \return 1
  64. */
  65. size_t MinimumSerial::write(uint8_t b) {
  66. while (((1 << UDRIE0) & UCSR0B) || !(UCSR0A & (1 << UDRE0))) {}
  67. UDR0 = b;
  68. return 1;
  69. }
  70. MinimumSerial MiniSerial;
  71. #endif // defined(UDR0) || defined(DOXYGEN)