Skip to content

Commit

Permalink
Merge pull request #1071 from UltimateHackingKeyboard/reduce-audible-…
Browse files Browse the repository at this point in the history
…noise

Reduce audible noise, and expose related settings for experimentation…
  • Loading branch information
mondalaci authored Jan 1, 2025
2 parents 560418b + d5bd220 commit 61a8ab3
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
44 changes: 41 additions & 3 deletions device/src/keyboard/leds.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,64 @@ 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) {
k_mutex_lock(&SpiMutex, K_FOREVER);

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);
writeSpi(0x02);
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);
Expand Down
1 change: 1 addition & 0 deletions device/src/keyboard/leds.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
14 changes: 14 additions & 0 deletions right/src/usb_commands/usb_command_set_variable.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "config_manager.h"
#include "ledmap.h"

#if defined(__ZEPHYR__) && DEVICE_IS_KEYBOARD
#include "keyboard/leds.h"
#endif

void UsbCommand_SetVariable(const uint8_t *GenericHidOutBuffer, uint8_t *GenericHidInBuffer)
{
usb_variable_id_t variableId = GetUsbRxBufferUint8(1);
Expand All @@ -34,5 +38,15 @@ void UsbCommand_SetVariable(const uint8_t *GenericHidOutBuffer, uint8_t *Generic
break;
case UsbVariable_StatusBuffer:
break;
case UsbVariable_LedAudioRegisters:
#if defined(__ZEPHYR__) && DEVICE_IS_KEYBOARD
uint8_t phaseDelay = GetUsbRxBufferUint8(2);
uint8_t spreadSpectrum = GetUsbRxBufferUint8(3);
uint8_t pwmFrequency = GetUsbRxBufferUint8(4);
UpdateLedAudioRegisters(phaseDelay, spreadSpectrum, pwmFrequency);
#endif
break;
default:
break;
}
}
1 change: 1 addition & 0 deletions right/src/usb_protocol_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
UsbVariable_DebounceTimeRelease,
UsbVariable_UsbReportSemaphore,
UsbVariable_StatusBuffer,
UsbVariable_LedAudioRegisters,
} usb_variable_id_t;

typedef enum {
Expand Down

0 comments on commit 61a8ab3

Please sign in to comment.