Serial.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_SERIAL_H
  17. #define MBED_SERIAL_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY)
  20. #include "Stream.h"
  21. #include "SerialBase.h"
  22. #include "PlatformMutex.h"
  23. #include "serial_api.h"
  24. #include "platform/NonCopyable.h"
  25. namespace mbed {
  26. /** \addtogroup drivers */
  27. /** A serial port (UART) for communication with other serial devices
  28. *
  29. * Can be used for Full Duplex communication, or Simplex by specifying
  30. * one pin as NC (Not Connected)
  31. *
  32. * @note Synchronization level: Thread safe
  33. *
  34. * Example:
  35. * @code
  36. * // Print "Hello World" to the PC
  37. *
  38. * #include "mbed.h"
  39. *
  40. * Serial pc(USBTX, USBRX);
  41. *
  42. * int main() {
  43. * pc.printf("Hello World\n");
  44. * }
  45. * @endcode
  46. * @ingroup drivers
  47. */
  48. class Serial : public SerialBase, public Stream, private NonCopyable<Serial> {
  49. public:
  50. #if DEVICE_SERIAL_ASYNCH
  51. using SerialBase::read;
  52. using SerialBase::write;
  53. #endif
  54. /** Create a Serial port, connected to the specified transmit and receive pins
  55. *
  56. * @param tx Transmit pin
  57. * @param rx Receive pin
  58. * @param name The name of the stream associated with this serial port (optional)
  59. * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
  60. *
  61. * @note
  62. * Either tx or rx may be specified as NC if unused
  63. */
  64. Serial(PinName tx, PinName rx, const char *name = NULL, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
  65. /** Create a Serial port, connected to the specified transmit and receive pins, with the specified baud
  66. *
  67. * @param tx Transmit pin
  68. * @param rx Receive pin
  69. * @param baud The baud rate of the serial port
  70. *
  71. * @note
  72. * Either tx or rx may be specified as NC if unused
  73. */
  74. Serial(PinName tx, PinName rx, int baud);
  75. /* Stream gives us a FileHandle with non-functional poll()/readable()/writable. Pass through
  76. * the calls from the SerialBase instead for backwards compatibility. This problem is
  77. * part of why Stream and Serial should be deprecated.
  78. */
  79. bool readable()
  80. {
  81. return SerialBase::readable();
  82. }
  83. bool writable()
  84. {
  85. return SerialBase::writeable();
  86. }
  87. bool writeable()
  88. {
  89. return SerialBase::writeable();
  90. }
  91. protected:
  92. virtual int _getc();
  93. virtual int _putc(int c);
  94. virtual void lock();
  95. virtual void unlock();
  96. PlatformMutex _mutex;
  97. };
  98. } // namespace mbed
  99. #endif
  100. #endif