RawSerial.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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_RAW_SERIAL_H
  17. #define MBED_RAW_SERIAL_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_SERIAL) || defined(DOXYGEN_ONLY)
  20. #include "drivers/SerialBase.h"
  21. #include "hal/serial_api.h"
  22. #include "platform/NonCopyable.h"
  23. namespace mbed {
  24. /** \addtogroup drivers */
  25. /** A serial port (UART) for communication with other serial devices
  26. * This is a variation of the Serial class that doesn't use streams,
  27. * thus making it safe to use in interrupt handlers with the RTOS.
  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: Not protected
  33. *
  34. * Example:
  35. * @code
  36. * // Send a char to the PC
  37. *
  38. * #include "mbed.h"
  39. *
  40. * RawSerial pc(USBTX, USBRX);
  41. *
  42. * int main() {
  43. * pc.putc('A');
  44. * }
  45. * @endcode
  46. * @ingroup drivers
  47. */
  48. class RawSerial: public SerialBase, private NonCopyable<RawSerial> {
  49. public:
  50. /** Create a RawSerial port, connected to the specified transmit and receive pins, with the specified baud.
  51. *
  52. * @param tx Transmit pin
  53. * @param rx Receive pin
  54. * @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
  55. *
  56. * @note
  57. * Either tx or rx may be specified as NC if unused
  58. */
  59. RawSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
  60. /** Write a char to the serial port
  61. *
  62. * @param c The char to write
  63. *
  64. * @returns The written char or -1 if an error occurred
  65. */
  66. int putc(int c);
  67. /** Read a char from the serial port
  68. *
  69. * @returns The char read from the serial port
  70. */
  71. int getc();
  72. /** Write a string to the serial port
  73. *
  74. * @param str The string to write
  75. *
  76. * @returns 0 if the write succeeds, EOF for error
  77. */
  78. int puts(const char *str);
  79. int printf(const char *format, ...);
  80. protected:
  81. /* Acquire exclusive access to this serial port
  82. */
  83. virtual void lock(void);
  84. /* Release exclusive access to this serial port
  85. */
  86. virtual void unlock(void);
  87. };
  88. } // namespace mbed
  89. #endif
  90. #endif