RawSerial.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. #include "drivers/RawSerial.h"
  17. #include "platform/mbed_wait_api.h"
  18. #include <stdio.h>
  19. #include <cstdarg>
  20. #if DEVICE_SERIAL
  21. #define STRING_STACK_LIMIT 120
  22. namespace mbed {
  23. RawSerial::RawSerial(PinName tx, PinName rx, int baud) : SerialBase(tx, rx, baud)
  24. {
  25. // No lock needed in the constructor
  26. }
  27. int RawSerial::getc()
  28. {
  29. lock();
  30. int ret = _base_getc();
  31. unlock();
  32. return ret;
  33. }
  34. int RawSerial::putc(int c)
  35. {
  36. lock();
  37. int ret = _base_putc(c);
  38. unlock();
  39. return ret;
  40. }
  41. int RawSerial::puts(const char *str)
  42. {
  43. lock();
  44. while (*str) {
  45. putc(*str ++);
  46. }
  47. unlock();
  48. return 0;
  49. }
  50. // Experimental support for printf in RawSerial. No Stream inheritance
  51. // means we can't call printf() directly, so we use sprintf() instead.
  52. // We only call malloc() for the sprintf() buffer if the buffer
  53. // length is above a certain threshold, otherwise we use just the stack.
  54. int RawSerial::printf(const char *format, ...)
  55. {
  56. lock();
  57. std::va_list arg;
  58. va_start(arg, format);
  59. // ARMCC microlib does not properly handle a size of 0.
  60. // As a workaround supply a dummy buffer with a size of 1.
  61. char dummy_buf[1];
  62. int len = vsnprintf(dummy_buf, sizeof(dummy_buf), format, arg);
  63. if (len < STRING_STACK_LIMIT) {
  64. char temp[STRING_STACK_LIMIT];
  65. vsprintf(temp, format, arg);
  66. puts(temp);
  67. } else {
  68. char *temp = new char[len + 1];
  69. vsprintf(temp, format, arg);
  70. puts(temp);
  71. delete[] temp;
  72. }
  73. va_end(arg);
  74. unlock();
  75. return len;
  76. }
  77. /** Acquire exclusive access to this serial port
  78. */
  79. void RawSerial::lock()
  80. {
  81. // No lock used - external synchronization required
  82. }
  83. /** Release exclusive access to this serial port
  84. */
  85. void RawSerial::unlock()
  86. {
  87. // No lock used - external synchronization required
  88. }
  89. } // namespace mbed
  90. #endif