blob: 1a9b344ef852a1becb38553ff9402a7fdcdbcdef [file] [log] [blame]
Mark Horvathbc58a682021-11-16 13:53:49 +01001/*
2 * Copyright (c) 2018-2021 Arm Limited. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5 * this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to
7 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8 * the Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22
23#include <stdio.h>
24#include <stdarg.h>
25
26#include "FreeRTOS.h"
27#include "semphr.h"
28#include "print_log.h"
29
30SemaphoreHandle_t xUARTMutex;
31
32static SemaphoreHandle_t prvCreateUARTMutex( void )
33{
34SemaphoreHandle_t xMutexHandle;
35 xMutexHandle = xSemaphoreCreateMutex();
36 configASSERT( xMutexHandle );
37 return xMutexHandle;
38}
39
40void vUARTLockInit( void )
41{
42 xUARTMutex = prvCreateUARTMutex();
43}
44
45static BaseType_t xUARTLockAcquire()
46{
47 return xSemaphoreTake( xUARTMutex, portMAX_DELAY );
48}
49
50static BaseType_t xUARTLockRelease( void )
51{
52 return xSemaphoreGive( xUARTMutex );
53}
54
55void vLoggingPrintf(const char *format, ...)
56{
57 va_list args;
58 BaseType_t schedulerState = xTaskGetSchedulerState();
59 /* A UART lock is used here to ensure that there is
60 * at most one task accessing UART at a time.
61 */
62 if (schedulerState != taskSCHEDULER_NOT_STARTED) {
63 xUARTLockAcquire();
64 }
65 va_start( args, format );
66 vprintf( format, args );
67 va_end ( args );
68 printf("\r\n");
69 if (schedulerState != taskSCHEDULER_NOT_STARTED) {
70 xUARTLockRelease();
71 }
72}
73
74void vLoggingPrint(const char * message)
75{
76 BaseType_t schedulerState = xTaskGetSchedulerState();
77 /* A UART lock is used here to ensure that there is
78 * at most one task accessing UART at a time.
79 */
80 if (schedulerState != taskSCHEDULER_NOT_STARTED) {
81 xUARTLockAcquire();
82 }
83 puts(message);
84 printf("\r\n");
85 if (schedulerState != taskSCHEDULER_NOT_STARTED) {
86 xUARTLockRelease();
87 }
88}