PigweedLogger.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include "FreeRTOS.h"
  18. #include "pw_sys_io_ameba/init.h"
  19. #include "semphr.h"
  20. #include <lib/support/logging/CHIPLogging.h>
  21. #include <pw_hdlc/encoder.h>
  22. #include <pw_stream/sys_io_stream.h>
  23. #include "pw_span/span.h"
  24. #include <assert.h>
  25. namespace PigweedLogger {
  26. namespace {
  27. constexpr uint8_t kLogHdlcAddress = 1; // Send log messages to HDLC address 1 (other than RPC communication)
  28. constexpr size_t kWriteBufferSize = 128; // Buffer for constructing HDLC frames
  29. SemaphoreHandle_t ameba_log_mutex;
  30. pw::stream::SysIoWriter sWriter;
  31. size_t sWriteBufferPos;
  32. char sWriteBuffer[kWriteBufferSize];
  33. bool uartInitialised;
  34. void send()
  35. {
  36. pw::hdlc::WriteUIFrame(kLogHdlcAddress, pw::as_bytes(pw::span(sWriteBuffer, sWriteBufferPos)), sWriter);
  37. sWriteBufferPos = 0;
  38. }
  39. } // namespace
  40. void init()
  41. {
  42. ameba_log_mutex = xSemaphoreCreateMutex();
  43. assert(ameba_log_mutex != NULL);
  44. pw_sys_io_Init();
  45. uartInitialised = true;
  46. }
  47. int putString(const char * buffer, size_t size)
  48. {
  49. xSemaphoreTake(ameba_log_mutex, portMAX_DELAY);
  50. assert(sWriteBufferPos < kWriteBufferSize);
  51. for (size_t i = 0; i < size; ++i)
  52. {
  53. if (buffer[i] == '\r')
  54. continue;
  55. if (buffer[i] == '\n')
  56. {
  57. send();
  58. continue;
  59. }
  60. sWriteBuffer[sWriteBufferPos++] = buffer[i];
  61. if (sWriteBufferPos == kWriteBufferSize)
  62. send();
  63. }
  64. xSemaphoreGive(ameba_log_mutex);
  65. return size;
  66. }
  67. SemaphoreHandle_t * getSemaphore()
  68. {
  69. return &ameba_log_mutex;
  70. }
  71. } // namespace PigweedLogger