From 366be0f7e9b4f408b7494fcb68142ac70d909170 Mon Sep 17 00:00:00 2001
From: Joel Challis <git@zvecr.com>
Date: Fri, 16 Jul 2021 17:08:34 +0100
Subject: [PATCH] Migrate arm_atsam print logic to use common framework
 (#13554)

---
 tmk_core/common/arm_atsam/_print.h           | 34 ---------
 tmk_core/common/arm_atsam/printf.c           | 72 --------------------
 tmk_core/common/arm_atsam/printf.h           |  7 --
 tmk_core/common/arm_atsam/printf.mk          |  1 -
 tmk_core/protocol/arm_atsam/main_arm_atsam.c | 54 +++++++++++++++
 5 files changed, 54 insertions(+), 114 deletions(-)
 delete mode 100644 tmk_core/common/arm_atsam/_print.h
 delete mode 100644 tmk_core/common/arm_atsam/printf.c
 delete mode 100644 tmk_core/common/arm_atsam/printf.h
 delete mode 100644 tmk_core/common/arm_atsam/printf.mk

diff --git a/tmk_core/common/arm_atsam/_print.h b/tmk_core/common/arm_atsam/_print.h
deleted file mode 100644
index 04320ee38b..0000000000
--- a/tmk_core/common/arm_atsam/_print.h
+++ /dev/null
@@ -1,34 +0,0 @@
-/* Copyright 2012 Jun Wako <wakojun@gmail.com> */
-/* Very basic print functions, intended to be used with usb_debug_only.c
- * http://www.pjrc.com/teensy/
- * Copyright (c) 2008 PJRC.COM, LLC
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-#pragma once
-
-#include "arm_atsam/printf.h"
-
-// Create user & normal print defines
-#define xprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__)
-#define print(s) __xprintf(s)
-#define println(s) __xprintf(s "\r\n")
-#define uprint(s) __xprintf(s)
-#define uprintln(s) __xprintf(s "\r\n")
-#define uprintf(fmt, ...) __xprintf(fmt, ##__VA_ARGS__)
diff --git a/tmk_core/common/arm_atsam/printf.c b/tmk_core/common/arm_atsam/printf.c
deleted file mode 100644
index 2cb59706a8..0000000000
--- a/tmk_core/common/arm_atsam/printf.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
-Copyright 2018 Massdrop Inc.
-
-This program is free software: you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation, either version 2 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that 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 "printf.h"
-#include "sendchar.h"
-
-#ifdef CONSOLE_ENABLE
-
-#    include "samd51j18a.h"
-#    include "arm_atsam_protocol.h"
-#    include <string.h>
-#    include <stdarg.h>
-
-void console_printf(char *fmt, ...) {
-    while (udi_hid_con_b_report_trans_ongoing) {
-    }  // Wait for any previous transfers to complete
-
-    static char console_printbuf[CONSOLE_PRINTBUF_SIZE];  // Print and send buffer
-    va_list     va;
-    int         result;
-
-    va_start(va, fmt);
-    result = vsnprintf(console_printbuf, CONSOLE_PRINTBUF_SIZE, fmt, va);
-    va_end(va);
-
-    uint32_t irqflags;
-    char *   pconbuf  = console_printbuf;  // Pointer to start send from
-    int      send_out = CONSOLE_EPSIZE;    // Bytes to send per transfer
-
-    while (result > 0) {  // While not error and bytes remain
-        while (udi_hid_con_b_report_trans_ongoing) {
-        }  // Wait for any previous transfers to complete
-
-        irqflags = __get_PRIMASK();
-        __disable_irq();
-        __DMB();
-
-        if (result < CONSOLE_EPSIZE) {                      // If remaining bytes are less than console epsize
-            memset(udi_hid_con_report, 0, CONSOLE_EPSIZE);  // Clear the buffer
-            send_out = result;                              // Send remaining size
-        }
-
-        memcpy(udi_hid_con_report, pconbuf, send_out);  // Copy data into the send buffer
-
-        udi_hid_con_b_report_valid = 1;  // Set report valid
-        udi_hid_con_send_report();       // Send report
-
-        __DMB();
-        __set_PRIMASK(irqflags);
-
-        result -= send_out;   // Decrement result by bytes sent
-        pconbuf += send_out;  // Increment buffer point by bytes sent
-    }
-}
-
-#endif  // CONSOLE_ENABLE
-
-void print_set_sendchar(sendchar_func_t send) {}
\ No newline at end of file
diff --git a/tmk_core/common/arm_atsam/printf.h b/tmk_core/common/arm_atsam/printf.h
deleted file mode 100644
index 95557f5b01..0000000000
--- a/tmk_core/common/arm_atsam/printf.h
+++ /dev/null
@@ -1,7 +0,0 @@
-#pragma once
-
-#define CONSOLE_PRINTBUF_SIZE 512
-
-void console_printf(char *fmt, ...);
-
-#define __xprintf console_printf
diff --git a/tmk_core/common/arm_atsam/printf.mk b/tmk_core/common/arm_atsam/printf.mk
deleted file mode 100644
index f70e02731f..0000000000
--- a/tmk_core/common/arm_atsam/printf.mk
+++ /dev/null
@@ -1 +0,0 @@
-TMK_COMMON_SRC += $(PLATFORM_COMMON_DIR)/printf.c
diff --git a/tmk_core/protocol/arm_atsam/main_arm_atsam.c b/tmk_core/protocol/arm_atsam/main_arm_atsam.c
index ce0f54593c..abea121344 100644
--- a/tmk_core/protocol/arm_atsam/main_arm_atsam.c
+++ b/tmk_core/protocol/arm_atsam/main_arm_atsam.c
@@ -140,6 +140,57 @@ void send_consumer(uint16_t data) {
 #endif  // EXTRAKEY_ENABLE
 }
 
+#ifdef CONSOLE_ENABLE
+#    define CONSOLE_PRINTBUF_SIZE 512
+static char     console_printbuf[CONSOLE_PRINTBUF_SIZE];
+static uint16_t console_printbuf_len = 0;
+
+int8_t sendchar(uint8_t c) {
+    if (console_printbuf_len >= CONSOLE_PRINTBUF_SIZE) return -1;
+
+    console_printbuf[console_printbuf_len++] = c;
+    return 0;
+}
+
+void main_subtask_console_flush(void) {
+    while (udi_hid_con_b_report_trans_ongoing) {
+    }  // Wait for any previous transfers to complete
+
+    uint16_t result = console_printbuf_len;
+    uint32_t irqflags;
+    char *   pconbuf  = console_printbuf;  // Pointer to start send from
+    int      send_out = CONSOLE_EPSIZE;    // Bytes to send per transfer
+
+    while (result > 0) {  // While not error and bytes remain
+        while (udi_hid_con_b_report_trans_ongoing) {
+        }  // Wait for any previous transfers to complete
+
+        irqflags = __get_PRIMASK();
+        __disable_irq();
+        __DMB();
+
+        if (result < CONSOLE_EPSIZE) {                      // If remaining bytes are less than console epsize
+            memset(udi_hid_con_report, 0, CONSOLE_EPSIZE);  // Clear the buffer
+            send_out = result;                              // Send remaining size
+        }
+
+        memcpy(udi_hid_con_report, pconbuf, send_out);  // Copy data into the send buffer
+
+        udi_hid_con_b_report_valid = 1;  // Set report valid
+        udi_hid_con_send_report();       // Send report
+
+        __DMB();
+        __set_PRIMASK(irqflags);
+
+        result -= send_out;   // Decrement result by bytes sent
+        pconbuf += send_out;  // Increment buffer point by bytes sent
+    }
+
+    console_printbuf_len = 0;
+}
+
+#endif  // CONSOLE_ENABLE
+
 void main_subtask_usb_state(void) {
     static uint64_t fsmstate_on_delay = 0;                          // Delay timer to be sure USB is actually operating before bringing up hardware
     uint8_t         fsmstate_now      = USB->DEVICE.FSMSTATUS.reg;  // Current state from hardware register
@@ -214,6 +265,9 @@ void main_subtasks(void) {
     main_subtask_usb_state();
     main_subtask_power_check();
     main_subtask_usb_extra_device();
+#ifdef CONSOLE_ENABLE
+    main_subtask_console_flush();
+#endif
 #ifdef RAW_ENABLE
     main_subtask_raw();
 #endif