2020-05-05 07:33:16 +01:00
|
|
|
/*
|
2021-10-04 20:59:10 +01:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-05-05 07:33:16 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <exosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::log {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-12-08 03:25:06 +00:00
|
|
|
constexpr inline uart::Port DefaultLogPort = uart::Port_ReservedDebug;
|
2020-12-11 11:18:21 +00:00
|
|
|
constexpr inline u32 DefaultLogFlags = static_cast<u32>(uart::Flag_None);
|
2020-12-08 03:25:06 +00:00
|
|
|
constexpr inline int DefaultBaudRate = 115200;
|
|
|
|
constinit uart::Port g_log_port = DefaultLogPort;
|
2020-05-05 07:33:16 +01:00
|
|
|
constinit bool g_initialized_uart = false;
|
|
|
|
|
2020-12-08 03:25:06 +00:00
|
|
|
ALWAYS_INLINE void SetupUartClock(uart::Port port) {
|
|
|
|
/* The debug port must always be set up, for compatibility with official hos. */
|
|
|
|
pinmux::SetupUartA();
|
|
|
|
clkrst::EnableUartAClock();
|
|
|
|
|
|
|
|
/* If logging to a joy-con port, configure appropriately. */
|
|
|
|
if (port == uart::Port_LeftJoyCon) {
|
2020-05-13 18:56:07 +01:00
|
|
|
/* Logging to left joy-con (e.g. with Joyless). */
|
2020-07-02 22:06:42 +01:00
|
|
|
static_assert(uart::Port_LeftJoyCon == uart::Port_C);
|
2020-05-13 18:56:07 +01:00
|
|
|
pinmux::SetupUartC();
|
|
|
|
clkrst::EnableUartCClock();
|
2020-12-08 03:25:06 +00:00
|
|
|
} else if (port == uart::Port_RightJoyCon) {
|
2020-07-02 22:06:42 +01:00
|
|
|
/* Logging to right joy-con (e.g. with Joyless). */
|
|
|
|
static_assert(uart::Port_RightJoyCon == uart::Port_B);
|
|
|
|
pinmux::SetupUartB();
|
|
|
|
clkrst::EnableUartBClock();
|
2020-05-13 18:56:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 07:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Initialize() {
|
2020-12-11 11:18:21 +00:00
|
|
|
return Initialize(DefaultLogPort, DefaultBaudRate, DefaultLogFlags);
|
2020-12-08 03:25:06 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 11:18:21 +00:00
|
|
|
void Initialize(uart::Port port, u32 baud_rate, u32 flags) {
|
2020-05-13 18:56:07 +01:00
|
|
|
/* Initialize pinmux and clock for the target uart port. */
|
2020-12-08 03:25:06 +00:00
|
|
|
SetupUartClock(port);
|
2020-05-13 18:56:07 +01:00
|
|
|
|
2020-05-05 07:33:16 +01:00
|
|
|
/* Initialize the target uart port. */
|
2020-12-11 11:18:21 +00:00
|
|
|
uart::Initialize(port, baud_rate, flags);
|
2020-05-05 07:33:16 +01:00
|
|
|
|
|
|
|
/* Note that we've initialized. */
|
2020-12-08 03:25:06 +00:00
|
|
|
g_log_port = port;
|
2020-05-05 07:33:16 +01:00
|
|
|
g_initialized_uart = true;
|
|
|
|
}
|
|
|
|
|
2020-05-13 18:56:07 +01:00
|
|
|
void Finalize() {
|
|
|
|
g_initialized_uart = false;
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:17:38 +00:00
|
|
|
NOINLINE void VPrintf(const char *fmt, ::std::va_list vl) {
|
|
|
|
/* TODO: What's a good size for the log buffer? Nintendo uses 0x100, but this seems big. */
|
|
|
|
char log_buf[0x80];
|
|
|
|
const auto len = util::TVSNPrintf(log_buf, sizeof(log_buf), fmt, vl);
|
|
|
|
|
|
|
|
if (g_initialized_uart) {
|
2020-12-08 03:25:06 +00:00
|
|
|
uart::SendText(g_log_port, log_buf, len);
|
2020-11-14 20:17:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NOINLINE void Printf(const char *fmt, ...) {
|
|
|
|
::std::va_list vl;
|
|
|
|
va_start(vl, fmt);
|
|
|
|
VPrintf(fmt, vl);
|
|
|
|
va_end(vl);
|
2020-06-08 11:53:40 +01:00
|
|
|
}
|
|
|
|
|
2020-11-14 20:17:38 +00:00
|
|
|
NOINLINE void Dump(const void *src, size_t size) {
|
|
|
|
const u8 *src_u8 = static_cast<const u8 *>(src);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
if ((i % 0x20) == 0x00) {
|
|
|
|
Printf("%03zx| ", i);
|
|
|
|
}
|
|
|
|
Printf("%02x ", src_u8[i]);
|
|
|
|
if ((i % 0x20) == 0x1F) {
|
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((size % 0x20) != 0) {
|
|
|
|
Printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-13 18:56:07 +01:00
|
|
|
void SendText(const void *text, size_t size) {
|
2020-11-14 20:17:38 +00:00
|
|
|
if (g_initialized_uart) {
|
2020-12-08 03:25:06 +00:00
|
|
|
uart::SendText(g_log_port, text, size);
|
2020-05-13 18:56:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flush() {
|
2020-11-14 20:17:38 +00:00
|
|
|
if (g_initialized_uart) {
|
2020-12-08 03:25:06 +00:00
|
|
|
uart::WaitFlush(g_log_port);
|
2020-05-13 18:56:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 20:17:38 +00:00
|
|
|
}
|