From ce45e9dc5873ff73268d0a66a114ae8a82d5b48c Mon Sep 17 00:00:00 2001 From: Hayley Hughes Date: Tue, 19 Dec 2023 11:08:42 +1100 Subject: [PATCH] Switch to using ChibiOS PWM driver --- keyboards/ducky/one2sf/1967st/halconf.h | 22 +++++ .../ducky/one2sf/1967st/rgb_matrix_drivers.c | 86 +++++-------------- 2 files changed, 42 insertions(+), 66 deletions(-) create mode 100644 keyboards/ducky/one2sf/1967st/halconf.h diff --git a/keyboards/ducky/one2sf/1967st/halconf.h b/keyboards/ducky/one2sf/1967st/halconf.h new file mode 100644 index 00000000000..1950c3e4c17 --- /dev/null +++ b/keyboards/ducky/one2sf/1967st/halconf.h @@ -0,0 +1,22 @@ +/* Copyright 2023 Hayley Hughes + * + * 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 . + */ + +#pragma once + +#define HAL_USE_PWM TRUE +#define HAL_USE_PAL TRUE + +#include_next diff --git a/keyboards/ducky/one2sf/1967st/rgb_matrix_drivers.c b/keyboards/ducky/one2sf/1967st/rgb_matrix_drivers.c index 980687b88b7..212e8072a51 100644 --- a/keyboards/ducky/one2sf/1967st/rgb_matrix_drivers.c +++ b/keyboards/ducky/one2sf/1967st/rgb_matrix_drivers.c @@ -76,11 +76,23 @@ void MBIA045_set_current_gain(uint8_t gain); void MBIA045_write_config_register(uint16_t regValue); void MBIA045_set_color(int index, uint8_t red, uint8_t green, uint8_t blue); void MBIA045_set_color_all(uint8_t red, uint8_t green, uint8_t blue); -void MBIA045_update_pwm_buffers(void); +void MBIA045_update_pwm_buffers(PWMDriver *pwm); void MBIA045_flush(void); void MBIA045_disable_rows(void); void MBIA045_select_row(int row); +static const PWMConfig g_pwm_cfg = { + 2000000, + 5, + NULL, + { + {PWM_OUTPUT_ACTIVE_HIGH, NULL}, // Channel 0, pin PA15 + {PWM_OUTPUT_DISABLED, MBIA045_update_pwm_buffers}, + {PWM_OUTPUT_DISABLED, NULL}, + {PWM_OUTPUT_DISABLED, NULL}, + } +}; + void MBIA045_init(void) { /* Initialise all PWM arrays to zero. * Perform one group transfer to turn LEDs off @@ -100,59 +112,12 @@ void MBIA045_init(void) { * which is used for row refresh/enable. */ - // Use the HCLK - // Note we need to 0 the bits first as the clksel register has a reset value - // of 0xFFFF_FFFF - CLK->CLKSEL1 &= ~CLK_CLKSEL1_PWM01_S_Msk; - CLK->CLKSEL1 |= 2 << CLK_CLKSEL1_PWM01_S_Pos; - - CLK->CLKSEL2 &= ~CLK_CLKSEL2_PWM01_S_E_Msk; - CLK->CLKSEL2 |= 2 << CLK_CLKSEL2_PWM01_S_E_Pos; - - // Enable the PWM peripheral clock - CLK->APBCLK |= 1 << CLK_APBCLK_PWM01_EN_Pos; - - // Set prescaler for PWM0 and PWM1 to 1 - PWMA->PPR |= 1 << PWM_PPR_CP01_Pos; - - // Enable PWM interrupt vector - // Interrupt priority value taken from chibios defaults - nvicEnableVector(NUC123_PWMA_NUMBER, 3); - - // Set clock division to 1 - PWMA->CSR |= 1 << PWM_CSR_CSR2_Pos; - - // Set pin PA12 to PWM0 - SYS->GPA_MFP |= 1 << 12; - - // Enable PWM0 output - PWMA->POE |= 1 << PWM_POE_PWM0_Pos; + palSetPadMode(GPIOA, 12, 1); - // Enable PWM0 and PWM1 auto reload - PWMA->PCR |= (1 << PWM_PCR_CH0MOD_Pos | 1 << PWM_PCR_CH1MOD_Pos); + pwmStart(&PWMD1, &g_pwm_cfg); - // Enable PWM1 reset interrupt - PWMA->PIER |= 1 << PWM_PIER_PWMIE1_Pos; - - // Set PWM0 freq to 9MHz and 50% duty cycle (it's just a nice clock) - // - // freq = HCLK/[(prescale+1)*(clock divider)*(CNR+1)] - // 72MHz/(1 + 1)*(1)*(3+1) - // - // duty = (CMR+1)/(CNR+1) - // (1+1)/(3+1) - PWMA->CNR0 = 3; - PWMA->CMR0 = 1; - - // Set PWM1 freq to 1.8kHz (duty doesn't matter) - // - // freq = HCLK/[(prescale+1)*(clock divider)*(CNR+1)] - // 72MHz/(1 + 1)*(1)*(3+1) - PWMA->CNR1 = 19999; - PWMA->CMR1 = 1; - - // Start PWM channel 0 and 1 - PWMA->PCR |= (1 << PWM_PCR_CH0EN_Pos | 1 << PWM_PCR_CH1EN_Pos); + pwmEnableChannel(&PWMD1, 0, 10); + pwmEnableChannel(&PWMD1, 1, 100000); MBIA045_disable_rows(); @@ -160,6 +125,8 @@ void MBIA045_init(void) { MBIA045_EN = PAL_LOW; MBIA045_set_current_gain(0b000011u); + + pwmEnableChannelNotification(&PWMD1, 1); } /** @@ -262,7 +229,7 @@ void MBIA045_set_color_all(uint8_t red, uint8_t green, uint8_t blue) { } } -void MBIA045_update_pwm_buffers(void) { +void MBIA045_update_pwm_buffers(PWMDriver *pwm) { /** * Pass current PWM row to MBIA045 shift registers * @@ -446,19 +413,6 @@ void MBIA045_select_row(int row) { } } -OSAL_IRQ_HANDLER(NUC123_PWMA_HANDLER) { - OSAL_IRQ_PROLOGUE(); - - /* Check for PWM1 underflow IRQ */ - if ((PWMA->PIIR >> PWM_PIIR_PWMIF1_Pos) & 1) { - /* Clear interrupt flag */ - PWMA->PIIR |= 1 << PWM_PIIR_PWMIF1_Pos; - MBIA045_update_pwm_buffers(); - } - - OSAL_IRQ_EPILOGUE(); -} - const rgb_matrix_driver_t rgb_matrix_driver = { .init = MBIA045_init, .flush = MBIA045_flush,