osprintf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief
  6. *
  7. * Copyright (c) 2015 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * 4. This software may only be redistributed and used in connection with an
  27. * Atmel microcontroller product.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  32. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  33. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * \asf_license_stop
  42. *
  43. */
  44. #include "asf.h"
  45. #include <stdio.h>
  46. #include <stdarg.h>
  47. #include "FreeRTOS.h"
  48. #include "osprintf.h"
  49. xSemaphoreHandle trace_lock;
  50. void osprintf_init(void *usart, const void *opt)
  51. {
  52. trace_lock = xSemaphoreCreateMutex();
  53. stdio_serial_init(usart, opt);
  54. }
  55. void osprintf(const char *fmt, ...)
  56. {
  57. va_list args;
  58. va_start(args, fmt);
  59. xSemaphoreTake(trace_lock, portMAX_DELAY);
  60. vprintf(fmt, args);
  61. xSemaphoreGive(trace_lock);
  62. va_end(args);
  63. }
  64. void osprint(const char *s)
  65. {
  66. xSemaphoreTake(trace_lock, portMAX_DELAY);
  67. puts(s);
  68. xSemaphoreGive(trace_lock);
  69. }
  70. void osprint_hex_array(uint8_t *p, int sz)
  71. {
  72. xSemaphoreTake(trace_lock, portMAX_DELAY);
  73. while (sz) {
  74. if (sz < 8) {
  75. while (sz--) {
  76. printf("%02x ", *p);
  77. p++;
  78. }
  79. break;
  80. }
  81. printf("%02x %02x %02x %02x %02x %02x %02x %02x\n",
  82. p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
  83. p += 8;
  84. sz -= 8;
  85. }
  86. printf("\n");
  87. xSemaphoreGive(trace_lock);
  88. }
  89. void osprint_sem_take(void)
  90. {
  91. xSemaphoreTake(trace_lock, portMAX_DELAY);
  92. }
  93. void osprint_sem_give(void)
  94. {
  95. xSemaphoreGive(trace_lock);
  96. }