From 7d48a9d86b809587a16da800d24cacdb7be3c532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20Monda?= Date: Wed, 1 Jan 2025 01:19:50 +0100 Subject: [PATCH 1/2] Reduce audible noise, and expose related settings for experimentation via the device protocol. --- device/src/keyboard/leds.c | 44 +++++++++++++++++-- device/src/keyboard/leds.h | 1 + .../usb_commands/usb_command_set_variable.c | 14 ++++++ right/src/usb_protocol_handler.h | 1 + 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/device/src/keyboard/leds.c b/device/src/keyboard/leds.c index 68ccb52b2..adb3eee78 100644 --- a/device/src/keyboard/leds.c +++ b/device/src/keyboard/leds.c @@ -78,6 +78,45 @@ static void recalculateScaling() { } } +void UpdateLedAudioRegisters(uint8_t phaseDelay, uint8_t spreadSpectrum, uint8_t pwmFrequency) { + k_mutex_lock(&SpiMutex, K_FOREVER); + + // Set phase delay + setLedsCs(true); + writeSpi(LedPagePrefix | 2); + writeSpi(0x02); + writeSpi(phaseDelay | 0b00110011); + setLedsCs(false); + printk("Phase delay: %d\n", phaseDelay); + + // Set spread spectrum + setLedsCs(true); + writeSpi(LedPagePrefix | 2); + writeSpi(0x25); + writeSpi(spreadSpectrum); + setLedsCs(false); + printk("Spread spectrum: %d\n", spreadSpectrum); + + // Enter test mode to set PWM frequency + setLedsCs(true); + writeSpi(LedPagePrefix | 2); + writeSpi(0x52); + writeSpi(0xe0); + writeSpi(1); + setLedsCs(false); + + // Set PWM frequency + setLedsCs(true); + writeSpi(LedPagePrefix | 2); + writeSpi(0x52); + writeSpi(0xe2); + writeSpi(pwmFrequency); + setLedsCs(false); + printk("PWM frequency: %d\n", pwmFrequency); + + k_mutex_unlock(&SpiMutex); +} + void ledUpdater() { k_sleep(K_MSEC(100)); while (true) { @@ -85,7 +124,6 @@ void ledUpdater() { setOperationMode(1); - // Set 180 degree phase delay to reduce audible noise, although it doesn't seem to make a difference setLedsCs(true); writeSpi(LedPagePrefix | 2); @@ -93,11 +131,11 @@ void ledUpdater() { writeSpi(0b10110011); setLedsCs(false); - // Enable spread spectrum with 15% range and 1980us cycle time, which substantially reduces audible noise + // Enable spread spectrum with 5% range and 1980us cycle time, which substantially reduces audible noise setLedsCs(true); writeSpi(LedPagePrefix | 2); writeSpi(0x25); - writeSpi(0x14); + writeSpi(0x10); setLedsCs(false); setLedsCs(true); diff --git a/device/src/keyboard/leds.h b/device/src/keyboard/leds.h index 147bc5a8f..0e1c68e8b 100644 --- a/device/src/keyboard/leds.h +++ b/device/src/keyboard/leds.h @@ -18,5 +18,6 @@ extern void Uhk80_UpdateLeds(); extern void InitLeds(void); + extern void UpdateLedAudioRegisters(uint8_t spreadSpectrum, uint8_t phaseDelay, uint8_t pwmFrequency); #endif // __LEDS_H__ diff --git a/right/src/usb_commands/usb_command_set_variable.c b/right/src/usb_commands/usb_command_set_variable.c index c263a113a..d3ffd1ca6 100644 --- a/right/src/usb_commands/usb_command_set_variable.c +++ b/right/src/usb_commands/usb_command_set_variable.c @@ -8,6 +8,10 @@ #include "config_manager.h" #include "ledmap.h" +#ifdef __ZEPHYR__ +#include "keyboard/leds.h" +#endif + void UsbCommand_SetVariable(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer) { usb_variable_id_t variableId = GetUsbRxBufferUint8(1); @@ -34,5 +38,15 @@ void UsbCommand_SetVariable(const uint8_t *GenericHidOutBuffer, uint8_t *Generic break; case UsbVariable_StatusBuffer: break; + case UsbVariable_LedAudioRegisters: +#ifdef __ZEPHYR__ + uint8_t phaseDelay = GetUsbRxBufferUint8(2); + uint8_t spreadSpectrum = GetUsbRxBufferUint8(3); + uint8_t pwmFrequency = GetUsbRxBufferUint8(4); + UpdateLedAudioRegisters(phaseDelay, spreadSpectrum, pwmFrequency); +#endif + break; + default: + break; } } diff --git a/right/src/usb_protocol_handler.h b/right/src/usb_protocol_handler.h index 96a30e36e..e8fc91d4c 100644 --- a/right/src/usb_protocol_handler.h +++ b/right/src/usb_protocol_handler.h @@ -75,6 +75,7 @@ UsbVariable_DebounceTimeRelease, UsbVariable_UsbReportSemaphore, UsbVariable_StatusBuffer, + UsbVariable_LedAudioRegisters, } usb_variable_id_t; typedef enum { From d5bd220e9f4de501de17736b6330ff500f275657 Mon Sep 17 00:00:00 2001 From: Karel Tucek Date: Wed, 1 Jan 2025 10:00:29 +0100 Subject: [PATCH 2/2] Fix build for dongle. --- right/src/usb_commands/usb_command_set_variable.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/right/src/usb_commands/usb_command_set_variable.c b/right/src/usb_commands/usb_command_set_variable.c index d3ffd1ca6..171730508 100644 --- a/right/src/usb_commands/usb_command_set_variable.c +++ b/right/src/usb_commands/usb_command_set_variable.c @@ -8,7 +8,7 @@ #include "config_manager.h" #include "ledmap.h" -#ifdef __ZEPHYR__ +#if defined(__ZEPHYR__) && DEVICE_IS_KEYBOARD #include "keyboard/leds.h" #endif @@ -39,7 +39,7 @@ void UsbCommand_SetVariable(const uint8_t *GenericHidOutBuffer, uint8_t *Generic case UsbVariable_StatusBuffer: break; case UsbVariable_LedAudioRegisters: -#ifdef __ZEPHYR__ +#if defined(__ZEPHYR__) && DEVICE_IS_KEYBOARD uint8_t phaseDelay = GetUsbRxBufferUint8(2); uint8_t spreadSpectrum = GetUsbRxBufferUint8(3); uint8_t pwmFrequency = GetUsbRxBufferUint8(4);