diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index 701d7ded90..0838840777 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -45,6 +45,31 @@ else ifeq ($(strip $(DEBUG_MATRIX_SCAN_RATE_ENABLE)), api) OPT_DEFS += -DDEBUG_MATRIX_SCAN_RATE endif +ifeq ($(strip $(CONSOLE_ENABLE)), yes) + SENDCHAR_DRIVER ?= console +else + SENDCHAR_DRIVER ?= none +endif +VALID_SENDCHAR_DRIVER_TYPES := console uart custom +ifneq ($(strip $(SENDCHAR_DRIVER)),none) + ifeq ($(filter $(SENDCHAR_DRIVER),$(VALID_SENDCHAR_DRIVER_TYPES)),) + $(call CATASTROPHIC_ERROR,Invalid SENDCHAR_DRIVER,SENDCHAR_DRIVER="$(SENDCHAR_DRIVER)" is not a valid logging driver type) + else + OPT_DEFS += -DSENDCHAR_DRIVER_$(strip $(shell echo $(SENDCHAR_DRIVER) | tr '[:lower:]' '[:upper:]')) + + ifneq ($(strip $(SENDCHAR_DRIVER)), custom) + QUANTUM_SRC += $(QUANTUM_DIR)/logging/sendchar_$(strip $(shell echo $(SENDCHAR_DRIVER) | tr '[:upper:]' '[:lower:]')).c + endif + + ifeq ($(strip $(SENDCHAR_DRIVER)), uart) + UART_DRIVER_REQUIRED = yes + endif + endif +else + OPT_DEFS += -DNO_PRINT + OPT_DEFS += -DNO_DEBUG +endif + AUDIO_ENABLE ?= no ifeq ($(strip $(AUDIO_ENABLE)), yes) ifeq ($(PLATFORM),CHIBIOS) diff --git a/keyboards/handwired/onekey/keymaps/uart_log/halconf.h b/keyboards/handwired/onekey/keymaps/uart_log/halconf.h new file mode 100644 index 0000000000..392d1cca75 --- /dev/null +++ b/keyboards/handwired/onekey/keymaps/uart_log/halconf.h @@ -0,0 +1,7 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#define HAL_USE_SERIAL TRUE + +#include_next diff --git a/keyboards/handwired/onekey/keymaps/uart_log/keymap.c b/keyboards/handwired/onekey/keymaps/uart_log/keymap.c new file mode 100644 index 0000000000..d1f788e50b --- /dev/null +++ b/keyboards/handwired/onekey/keymaps/uart_log/keymap.c @@ -0,0 +1,30 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later +#include QMK_KEYBOARD_H + +enum custom_keycodes { + KC_HELLO = SAFE_RANGE, +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + LAYOUT_ortho_1x1(KC_HELLO) +}; + +bool process_record_user(uint16_t keycode, keyrecord_t *record) { + switch (keycode) { + case KC_HELLO: + if (record->event.pressed) { + println("Hello world!"); + } + return false; + } + return true; +} + +void keyboard_post_init_user(void) { + // Customise these values to desired behaviour + debug_enable=true; + debug_matrix=true; + //debug_keyboard=true; + //debug_mouse=true; +} diff --git a/keyboards/handwired/onekey/keymaps/uart_log/mcuconf.h b/keyboards/handwired/onekey/keymaps/uart_log/mcuconf.h new file mode 100644 index 0000000000..cf377e05dc --- /dev/null +++ b/keyboards/handwired/onekey/keymaps/uart_log/mcuconf.h @@ -0,0 +1,8 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later +#pragma once + +#include_next + +#undef STM32_SERIAL_USE_USART1 +#define STM32_SERIAL_USE_USART1 TRUE diff --git a/keyboards/handwired/onekey/keymaps/uart_log/rules.mk b/keyboards/handwired/onekey/keymaps/uart_log/rules.mk new file mode 100644 index 0000000000..8d0f744107 --- /dev/null +++ b/keyboards/handwired/onekey/keymaps/uart_log/rules.mk @@ -0,0 +1,3 @@ +DEBUG_MATRIX_SCAN_RATE_ENABLE = api + +SENDCHAR_DRIVER = uart diff --git a/keyboards/ibm/model_m/mschwingen/mschwingen.c b/keyboards/ibm/model_m/mschwingen/mschwingen.c index 34a878c769..c2fe6bc1eb 100644 --- a/keyboards/ibm/model_m/mschwingen/mschwingen.c +++ b/keyboards/ibm/model_m/mschwingen/mschwingen.c @@ -16,7 +16,6 @@ */ #include #include "mschwingen.h" -#include "uart.h" #include "print.h" #include "sendchar.h" #include "sleep_led.h" @@ -25,15 +24,6 @@ #include "ws2812.h" #endif -#ifdef UART_DEBUG -# undef sendchar -static int8_t capture_sendchar(uint8_t c) { - // sendchar(c); - uart_write(c); - return 0; -} -#endif - static uint16_t blink_cycle_timer; static bool blink_state = false; static uint8_t isRecording = 0; @@ -88,11 +78,6 @@ void keyboard_pre_init_kb(void) { gpio_set_pin_output(MODELM_STATUS_LED); gpio_write_pin_high(MODELM_STATUS_LED); _delay_ms(50); -#ifdef UART_DEBUG - uart_init(115200); - print_set_sendchar(capture_sendchar); - uprintf("\r\nHello world!\r\n"); -#endif gpio_set_pin_output(SR_LOAD_PIN); gpio_set_pin_output(SR_CLK_PIN); diff --git a/keyboards/ibm/model_m/mschwingen/post_rules.mk b/keyboards/ibm/model_m/mschwingen/post_rules.mk index 025068e057..0f1908e58d 100644 --- a/keyboards/ibm/model_m/mschwingen/post_rules.mk +++ b/keyboards/ibm/model_m/mschwingen/post_rules.mk @@ -1,5 +1,5 @@ UART_DEBUG ?= no ifeq ($(strip $(UART_DEBUG)), yes) - OPT_DEFS += -DUART_DEBUG + SENDCHAR_DRIVER = uart endif diff --git a/keyboards/ibm/model_m/mschwingen/rules.mk b/keyboards/ibm/model_m/mschwingen/rules.mk index 65761bcf9a..ad5ce0921c 100644 --- a/keyboards/ibm/model_m/mschwingen/rules.mk +++ b/keyboards/ibm/model_m/mschwingen/rules.mk @@ -1,7 +1,7 @@ CUSTOM_MATRIX = lite SRC += matrix.c -UART_DRIVER_REQUIRED = yes + SPI_DRIVER_REQUIRED = yes OPT_DEFS += -DSLEEP_LED_ENABLE diff --git a/quantum/keyboard.c b/quantum/keyboard.c index d7836cf36e..56c79298cf 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -192,7 +192,7 @@ void set_activity_timestamps(uint32_t matrix_timestamp, uint32_t encoder_timesta last_input_modification_time = MAX(matrix_timestamp, MAX(encoder_timestamp, pointing_device_timestamp)); } -// Only enable this if console is enabled to print to +// Only enable this if logging is enabled to print to #if defined(DEBUG_MATRIX_SCAN_RATE) static uint32_t matrix_timer = 0; static uint32_t matrix_scan_count = 0; @@ -203,9 +203,7 @@ void matrix_scan_perf_task(void) { uint32_t timer_now = timer_read32(); if (TIMER_DIFF_32(timer_now, matrix_timer) >= 1000) { -# if defined(CONSOLE_ENABLE) dprintf("matrix scan frequency: %lu\n", matrix_scan_count); -# endif last_matrix_scan_count = matrix_scan_count; matrix_timer = timer_now; matrix_scan_count = 0; diff --git a/quantum/logging/sendchar_console.c b/quantum/logging/sendchar_console.c new file mode 100644 index 0000000000..7048760865 --- /dev/null +++ b/quantum/logging/sendchar_console.c @@ -0,0 +1,8 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later +#include "sendchar.h" +int8_t console_write(uint8_t c); + +int8_t sendchar(uint8_t c) { + return console_write(c); +} diff --git a/quantum/logging/sendchar_uart.c b/quantum/logging/sendchar_uart.c new file mode 100644 index 0000000000..ce3b6fbed2 --- /dev/null +++ b/quantum/logging/sendchar_uart.c @@ -0,0 +1,19 @@ +// Copyright 2023 QMK +// SPDX-License-Identifier: GPL-2.0-or-later +#include "sendchar.h" +#include "uart.h" + +#ifndef SENDCHAR_BAUD_RATE +# define SENDCHAR_BAUD_RATE 9600 +#endif + +int8_t sendchar(uint8_t c) { + static bool s_init = false; + if (!s_init) { + uart_init(SENDCHAR_BAUD_RATE); + s_init = true; + } + + uart_write(c); + return 0; +} diff --git a/tmk_core/protocol.mk b/tmk_core/protocol.mk index 8f01976548..2227e42dcd 100644 --- a/tmk_core/protocol.mk +++ b/tmk_core/protocol.mk @@ -39,10 +39,6 @@ endif ifeq ($(strip $(CONSOLE_ENABLE)), yes) OPT_DEFS += -DCONSOLE_ENABLE -else - # TODO: decouple this so other print backends can exist - OPT_DEFS += -DNO_PRINT - OPT_DEFS += -DNO_DEBUG endif ifeq ($(strip $(NKRO_ENABLE)), yes) diff --git a/tmk_core/protocol/chibios/chibios.c b/tmk_core/protocol/chibios/chibios.c index cf948154f9..f4f3aa43a7 100644 --- a/tmk_core/protocol/chibios/chibios.c +++ b/tmk_core/protocol/chibios/chibios.c @@ -30,7 +30,6 @@ #include "usb_device_state.h" #include "mousekey.h" #include "led.h" -#include "sendchar.h" #include "debug.h" #include "print.h" diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 5a5354416f..1e03de6225 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -504,7 +504,7 @@ void send_digitizer(report_digitizer_t *report) { #ifdef CONSOLE_ENABLE -int8_t sendchar(uint8_t c) { +int8_t console_write(uint8_t c) { return (int8_t)send_report_buffered(USB_ENDPOINT_IN_CONSOLE, &c, sizeof(uint8_t)); } diff --git a/tmk_core/protocol/chibios/usb_main.h b/tmk_core/protocol/chibios/usb_main.h index 5ba6fb1961..9a94bc9270 100644 --- a/tmk_core/protocol/chibios/usb_main.h +++ b/tmk_core/protocol/chibios/usb_main.h @@ -53,7 +53,7 @@ void usb_event_queue_task(void); #ifdef CONSOLE_ENABLE /* Putchar over the USB console */ -int8_t sendchar(uint8_t c); +int8_t console_write(uint8_t c); #endif /* CONSOLE_ENABLE */ diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c index 81da035f0c..4a9ea427d8 100644 --- a/tmk_core/protocol/lufa/lufa.c +++ b/tmk_core/protocol/lufa/lufa.c @@ -42,7 +42,6 @@ #include "keyboard.h" #include "action.h" #include "led.h" -#include "sendchar.h" #include "debug.h" #ifdef SLEEP_LED_ENABLE # include "sleep_led.h" @@ -204,7 +203,7 @@ static void console_flush_task(void) { while (Endpoint_IsReadWriteAllowed()) Endpoint_Write_8(0); - // flush sendchar packet + // flush console packet if (Endpoint_IsINReady()) { Endpoint_ClearIN(); } @@ -582,7 +581,7 @@ void send_digitizer(report_digitizer_t *report) { } /******************************************************************************* - * sendchar + * CONSOLE ******************************************************************************/ #ifdef CONSOLE_ENABLE # define SEND_TIMEOUT 5 @@ -590,13 +589,13 @@ void send_digitizer(report_digitizer_t *report) { * * FIXME: Needs doc */ -int8_t sendchar(uint8_t c) { +int8_t console_write(uint8_t c) { // Do not wait if the previous write has timed_out. - // Because sendchar() is called so many times, waiting each call causes big lag. + // Because console_write() is called so many times, waiting each call causes big lag. // The `timed_out` state is an approximation of the ideal `is_listener_disconnected?` state. static bool timed_out = false; - // prevents console_flush_task() from running during sendchar() runs. + // prevents console_flush_task() from running during console_write() runs. // or char will be lost. These two function is mutually exclusive. CONSOLE_FLUSH_SET(false); diff --git a/tmk_core/protocol/vusb/protocol.c b/tmk_core/protocol/vusb/protocol.c index 41ccf451fd..d4a81c044a 100644 --- a/tmk_core/protocol/vusb/protocol.c +++ b/tmk_core/protocol/vusb/protocol.c @@ -25,7 +25,6 @@ #include "debug.h" #include "suspend.h" #include "wait.h" -#include "sendchar.h" #ifdef SLEEP_LED_ENABLE # include "sleep_led.h" diff --git a/tmk_core/protocol/vusb/vusb.c b/tmk_core/protocol/vusb/vusb.c index fdbfcc17dc..968e0f37e3 100644 --- a/tmk_core/protocol/vusb/vusb.c +++ b/tmk_core/protocol/vusb/vusb.c @@ -179,7 +179,7 @@ void raw_hid_task(void) { # define CONSOLE_BUFFER_SIZE 32 # define CONSOLE_EPSIZE 8 -int8_t sendchar(uint8_t c) { +int8_t console_write(uint8_t c) { rbuf_enqueue(c); return 0; }