Create UART backend for logging SP
Create UART adapter and backend for the UART driver.
Signed-off-by: Gabor Toth <gabor.toth2@arm.com>
Signed-off-by: Gabor Ambrus <gabor.ambrus@arm.com>
Change-Id: Id3adec616599959b3a5777d665fcfcadf0ccb91b
diff --git a/components/service/log/backend/uart/component.cmake b/components/service/log/backend/uart/component.cmake
new file mode 100644
index 0000000..4aa6d21
--- /dev/null
+++ b/components/service/log/backend/uart/component.cmake
@@ -0,0 +1,13 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+if (NOT DEFINED TGT)
+ message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+target_sources(${TGT} PRIVATE
+ "${CMAKE_CURRENT_LIST_DIR}/uart_backend.c"
+ )
diff --git a/components/service/log/backend/uart/uart_adapter/platform/component.cmake b/components/service/log/backend/uart/uart_adapter/platform/component.cmake
new file mode 100644
index 0000000..4225bfe
--- /dev/null
+++ b/components/service/log/backend/uart/uart_adapter/platform/component.cmake
@@ -0,0 +1,18 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+if (NOT DEFINED TGT)
+ message(FATAL_ERROR "mandatory parameter TGT is not defined.")
+endif()
+
+target_sources(${TGT} PRIVATE
+ "${CMAKE_CURRENT_LIST_DIR}/platform_uart_adapter.c"
+ )
+
+set_property(TARGET ${TGT} APPEND PROPERTY TS_PLATFORM_DRIVER_DEPENDENCIES
+ "uart"
+ )
+
diff --git a/components/service/log/backend/uart/uart_adapter/platform/platform_uart_adapter.c b/components/service/log/backend/uart/uart_adapter/platform/platform_uart_adapter.c
new file mode 100644
index 0000000..c9f1aa2
--- /dev/null
+++ b/components/service/log/backend/uart/uart_adapter/platform/platform_uart_adapter.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+#include <platform/interface/uart.h>
+#include <psa/error.h>
+#include <service/log/backend/uart/uart_adapter/uart_adapter.h>
+#include <stddef.h>
+
+/*
+ * Setup function to a platform uart driver. The actual realization of the driver
+ * will depend on the platform selected at build-time.
+ */
+static struct platform_uart_driver driver = { 0 };
+
+int uart_adapter_init(int instance)
+{
+ return platform_uart_create(&driver, instance);
+}
+
+/*
+ * Deallocate the uart interface.
+ */
+void uart_adapter_deinit(void)
+{
+ platform_uart_destroy(&driver);
+}
+
+/*
+ * Send string to uart interface for printing.
+ */
+void uart_hardware_puts(const char *msg)
+{
+ int i = 0;
+
+ while (msg[i] != '\0') {
+ driver.iface->uart_putc(driver.context, (uint8_t)msg[i]);
+ i++;
+ }
+}
+
+/*
+ * Flush the UART.
+ */
+void uart_hardware_flush(void)
+{
+ driver.iface->uart_flush(driver.context);
+}
diff --git a/components/service/log/backend/uart/uart_adapter/uart_adapter.h b/components/service/log/backend/uart/uart_adapter/uart_adapter.h
new file mode 100644
index 0000000..b67eba3
--- /dev/null
+++ b/components/service/log/backend/uart/uart_adapter/uart_adapter.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+#ifndef UART_ADAPTER_H
+#define UART_ADAPTER_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Initialise the uart adapter
+ *
+ * \param instance Deployment specific uart instance.
+ *
+ * \return 0 if successful.
+ */
+int uart_adapter_init(int instance);
+
+/**
+ * \brief Cleans-up the uart adapter.
+ */
+void uart_adapter_deinit(void);
+
+/**
+ * \brief Sends character to uart driver.
+ */
+void uart_hardware_puts(const char *msg);
+
+/**
+ * \brief Sends flush to uart driver.
+ */
+void uart_hardware_flush(void);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* UART_ADAPTER_H */
diff --git a/components/service/log/backend/uart/uart_backend.c b/components/service/log/backend/uart/uart_backend.c
new file mode 100644
index 0000000..5f13204
--- /dev/null
+++ b/components/service/log/backend/uart/uart_backend.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include <service/log/backend/log_backend.h>
+#include <service/log/backend/uart/uart_adapter/uart_adapter.h>
+#include <stddef.h>
+
+static log_status_t uart_puts(void *context, const char *str)
+{
+ (void)context;
+ uart_hardware_puts(str);
+
+ return LOG_STATUS_SUCCESS;
+}
+
+struct log_backend *uart_backend_init(int uart_instance_num)
+{
+ int status = 0;
+ static struct log_backend backend = {0};
+ static const struct log_backend_interface interface = { uart_puts };
+
+ status = uart_adapter_init(uart_instance_num);
+
+ if (status != 0)
+ return NULL;
+
+ backend.context = NULL;
+ backend.interface = &interface;
+
+ return &backend;
+}
+
+void uart_backend_deinit(void)
+{
+ uart_adapter_deinit();
+}
diff --git a/components/service/log/backend/uart/uart_backend.h b/components/service/log/backend/uart/uart_backend.h
new file mode 100644
index 0000000..72129f3
--- /dev/null
+++ b/components/service/log/backend/uart/uart_backend.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef UART_BACKEND_H
+#define UART_BACKEND_H
+
+#include <service/log/backend/log_backend.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief Initialize the UART based backend
+ *
+ * Initializes an UART backend that uses the interface provided by the
+ * UART adapter to execute UART operations.
+ *
+ * \param[in] uart_instance_num The UART hardware instance number to use
+ *
+ * \return PSA_SUCCESS if backend initialized successfully
+ */
+struct log_backend *uart_backend_init(int uart_instance_num);
+
+/**
+ * \brief Clean-up to free any resource used by the backend
+ */
+void uart_backend_deinit(void);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif /* UART_BACKEND_H */