MinimumSerial.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Copyright (c) 2011-2022 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR 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
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include "common/SysCall.h"
  26. #if defined(UDR0) || defined(DOXYGEN)
  27. #include "MinimumSerial.h"
  28. const uint16_t MIN_2X_BAUD = F_CPU/(4*(2*0XFFF + 1)) + 1;
  29. //------------------------------------------------------------------------------
  30. int MinimumSerial::available() {
  31. return UCSR0A & (1 << RXC0) ? 1 : 0;
  32. }
  33. //------------------------------------------------------------------------------
  34. void MinimumSerial::begin(uint32_t baud) {
  35. uint16_t baud_setting;
  36. // don't worry, the compiler will squeeze out F_CPU != 16000000UL
  37. if ((F_CPU != 16000000UL || baud != 57600) && baud > MIN_2X_BAUD) {
  38. // Double the USART Transmission Speed
  39. UCSR0A = 1 << U2X0;
  40. baud_setting = (F_CPU / 4 / baud - 1) / 2;
  41. } else {
  42. // hardcoded exception for compatibility with the bootloader shipped
  43. // with the Duemilanove and previous boards and the firmware on the 8U2
  44. // on the Uno and Mega 2560.
  45. UCSR0A = 0;
  46. baud_setting = (F_CPU / 8 / baud - 1) / 2;
  47. }
  48. // assign the baud_setting
  49. UBRR0H = baud_setting >> 8;
  50. UBRR0L = baud_setting;
  51. // enable transmit and receive
  52. UCSR0B |= (1 << TXEN0) | (1 << RXEN0);
  53. }
  54. //------------------------------------------------------------------------------
  55. void MinimumSerial::flush() {
  56. while (((1 << UDRIE0) & UCSR0B) || !(UCSR0A & (1 << UDRE0))) {}
  57. }
  58. //------------------------------------------------------------------------------
  59. int MinimumSerial::read() {
  60. if (UCSR0A & (1 << RXC0)) {
  61. return UDR0;
  62. }
  63. return -1;
  64. }
  65. //------------------------------------------------------------------------------
  66. size_t MinimumSerial::write(uint8_t b) {
  67. while (((1 << UDRIE0) & UCSR0B) || !(UCSR0A & (1 << UDRE0))) {}
  68. UDR0 = b;
  69. return 1;
  70. }
  71. #endif // defined(UDR0) || defined(DOXYGEN)