~ruther/qmk_firmware

996a9006d3f101dc5c48b58339e2e7323040fd59 — Stefan Kerkmann 2 years ago b6ea883
[Core] PWM Backlight for RP2040 (#17706)

M docs/platformdev_rp2040.md => docs/platformdev_rp2040.md +1 -0
@@ 6,6 6,7 @@ The following table shows the current driver status for peripherals on RP2040 MC
| ---------------------------------------------------------------- | ---------------------------------------------- |
| [ADC driver](adc_driver.md)                                      | Support planned (no ETA)                       |
| [Audio](audio_driver.md)                                         | Support planned (no ETA)                       |
| [Backlight](feature_backlight.md)                                | :heavy_check_mark:                             |
| [I2C driver](i2c_driver.md)                                      | :heavy_check_mark:                             |
| [SPI driver](spi_driver.md)                                      | :heavy_check_mark:                             |
| [WS2812 driver](ws2812_driver.md)                                | :heavy_check_mark: using `PIO` driver          |

M keyboards/handwired/onekey/blackpill_f401/config.h => keyboards/handwired/onekey/blackpill_f401/config.h +1 -0
@@ 20,6 20,7 @@

#define BACKLIGHT_PWM_DRIVER  PWMD5
#define BACKLIGHT_PWM_CHANNEL 1
#define BACKLIGHT_PAL_MODE 2

#define ADC_PIN A0


M keyboards/handwired/onekey/blackpill_f411/config.h => keyboards/handwired/onekey/blackpill_f411/config.h +1 -0
@@ 20,6 20,7 @@

#define BACKLIGHT_PWM_DRIVER  PWMD5
#define BACKLIGHT_PWM_CHANNEL 1
#define BACKLIGHT_PAL_MODE 2

#define ADC_PIN A0


M keyboards/handwired/onekey/blackpill_f411_tinyuf2/config.h => keyboards/handwired/onekey/blackpill_f411_tinyuf2/config.h +1 -0
@@ 20,6 20,7 @@

#define BACKLIGHT_PWM_DRIVER  PWMD5
#define BACKLIGHT_PWM_CHANNEL 1
#define BACKLIGHT_PAL_MODE 2

#define ADC_PIN A0


M keyboards/handwired/onekey/rp2040/config.h => keyboards/handwired/onekey/rp2040/config.h +3 -0
@@ 13,3 13,6 @@
#define RP2040_BOOTLOADER_DOUBLE_TAP_RESET
#define RP2040_BOOTLOADER_DOUBLE_TAP_RESET_LED GP25
#define RP2040_BOOTLOADER_DOUBLE_TAP_RESET_TIMEOUT 500U

#define BACKLIGHT_PWM_DRIVER PWMD4
#define BACKLIGHT_PWM_CHANNEL RP2040_PWM_CHANNEL_B

A keyboards/handwired/onekey/rp2040/halconf.h => keyboards/handwired/onekey/rp2040/halconf.h +9 -0
@@ 0,0 1,9 @@
// Copyright 2022 Stefan Kerkmann
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#define HAL_USE_I2C TRUE
#define HAL_USE_PWM TRUE

#include_next <halconf.h>

M keyboards/handwired/onekey/rp2040/info.json => keyboards/handwired/onekey/rp2040/info.json +4 -1
@@ 6,7 6,10 @@
        "cols": ["GP4"],
        "rows": ["GP5"]
    },
    "backlight": {
        "pin": "GP25"
    },
    "rgblight": {
        "pin": "A1"
        "pin": "GP25"
    }
}

A keyboards/handwired/onekey/rp2040/mcuconf.h => keyboards/handwired/onekey/rp2040/mcuconf.h +9 -0
@@ 0,0 1,9 @@
// Copyright 2022 Stefan Kerkmann
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include_next "mcuconf.h"

#undef RP_PWM_USE_PWM4
#define RP_PWM_USE_PWM4 TRUE

M platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/mcuconf.h => platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/mcuconf.h +13 -0
@@ 79,6 79,19 @@
#define RP_SPI_DMA_ERROR_HOOK(spip)

/*
 * PWM driver system settings.
 */
#define RP_PWM_USE_PWM0                     FALSE
#define RP_PWM_USE_PWM1                     FALSE
#define RP_PWM_USE_PWM2                     FALSE
#define RP_PWM_USE_PWM3                     FALSE
#define RP_PWM_USE_PWM4                     FALSE
#define RP_PWM_USE_PWM5                     FALSE
#define RP_PWM_USE_PWM6                     FALSE
#define RP_PWM_USE_PWM7                     FALSE
#define RP_PWM_IRQ_WRAP_NUMBER_PRIORITY     3

/*
 * I2C driver system settings.
 */
#define RP_I2C_USE_I2C0                     FALSE

M platforms/chibios/boards/GENERIC_RP_RP2040/configs/mcuconf.h => platforms/chibios/boards/GENERIC_RP_RP2040/configs/mcuconf.h +13 -0
@@ 79,6 79,19 @@
#define RP_SPI_DMA_ERROR_HOOK(spip)

/*
 * PWM driver system settings.
 */
#define RP_PWM_USE_PWM0                     FALSE
#define RP_PWM_USE_PWM1                     FALSE
#define RP_PWM_USE_PWM2                     FALSE
#define RP_PWM_USE_PWM3                     FALSE
#define RP_PWM_USE_PWM4                     FALSE
#define RP_PWM_USE_PWM5                     FALSE
#define RP_PWM_USE_PWM6                     FALSE
#define RP_PWM_USE_PWM7                     FALSE
#define RP_PWM_IRQ_WRAP_NUMBER_PRIORITY     3

/*
 * I2C driver system settings.
 */
#define RP_I2C_USE_I2C0                     FALSE

M platforms/chibios/boards/QMK_PM2040/configs/mcuconf.h => platforms/chibios/boards/QMK_PM2040/configs/mcuconf.h +13 -0
@@ 79,6 79,19 @@
#define RP_SPI_DMA_ERROR_HOOK(spip)

/*
 * PWM driver system settings.
 */
#define RP_PWM_USE_PWM0                     FALSE
#define RP_PWM_USE_PWM1                     FALSE
#define RP_PWM_USE_PWM2                     FALSE
#define RP_PWM_USE_PWM3                     FALSE
#define RP_PWM_USE_PWM4                     FALSE
#define RP_PWM_USE_PWM5                     FALSE
#define RP_PWM_USE_PWM6                     FALSE
#define RP_PWM_USE_PWM7                     FALSE
#define RP_PWM_IRQ_WRAP_NUMBER_PRIORITY     3

/*
 * I2C driver system settings.
 */
#define RP_I2C_USE_I2C0                     FALSE

M platforms/chibios/chibios_config.h => platforms/chibios/chibios_config.h +4 -0
@@ 28,6 28,10 @@
#    define USE_GPIOV1
#    define PAL_OUTPUT_TYPE_OPENDRAIN _Static_assert(0, "RP2040 has no Open Drain GPIO configuration, setting this is not possible");

#    define BACKLIGHT_PAL_MODE (PAL_MODE_ALTERNATE_PWM | PAL_RP_PAD_DRIVE12 | PAL_RP_GPIO_OE)
#    define BACKLIGHT_PWM_COUNTER_FREQUENCY 1000000
#    define BACKLIGHT_PWM_PERIOD BACKLIGHT_PWM_COUNTER_FREQUENCY / 2048

#    define usb_lld_endpoint_fields

#    define I2C1_SCL_PAL_MODE (PAL_MODE_ALTERNATE_I2C | PAL_RP_PAD_SLEWFAST | PAL_RP_PAD_PUE | PAL_RP_PAD_DRIVE4)

M platforms/chibios/vendors/RP/_pin_defs.h => platforms/chibios/vendors/RP/_pin_defs.h +5 -0
@@ 35,3 35,8 @@
#define GP28 28U
#define GP29 29U
#define GP30 30U

/* Aliases for GPIO PWM channels - every pin has at least one PWM channel
 * assigned */
#define RP2040_PWM_CHANNEL_A 1U
#define RP2040_PWM_CHANNEL_B 2U

M quantum/backlight/backlight_chibios.c => quantum/backlight/backlight_chibios.c +30 -21
@@ 40,16 40,22 @@
#    endif
#endif

static PWMConfig pwmCFG = {0xFFFF, /* PWM clock frequency  */
                           256,    /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
                           NULL,   /* Breathing Callback */
                           {       /* Default all channels to disabled - Channels will be configured durring init */
                            {PWM_OUTPUT_DISABLED, NULL},
                            {PWM_OUTPUT_DISABLED, NULL},
                            {PWM_OUTPUT_DISABLED, NULL},
                            {PWM_OUTPUT_DISABLED, NULL}},
                           0, /* HW dependent part.*/
                           0};
#ifndef BACKLIGHT_PWM_COUNTER_FREQUENCY
#    define BACKLIGHT_PWM_COUNTER_FREQUENCY 0xFFFF
#endif

#ifndef BACKLIGHT_PWM_PERIOD
#    define BACKLIGHT_PWM_PERIOD 256
#endif

static PWMConfig pwmCFG = {
    .frequency = BACKLIGHT_PWM_COUNTER_FREQUENCY, /* PWM clock frequency  */
    .period    = BACKLIGHT_PWM_PERIOD,            /* PWM period in counter ticks. e.g. clock frequency is 10KHz, period is 256 ticks then t_period is 25.6ms */
};

#ifdef BACKLIGHT_BREATHING
static virtual_timer_t breathing_vt;
#endif

// See http://jared.geek.nz/2013/feb/linear-led-pwm
static uint16_t cie_lightness(uint16_t v) {


@@ 60,10 66,11 @@ static uint16_t cie_lightness(uint16_t v) {
        // to get a useful result with integer division, we shift left in the expression above
        // and revert what we've done again after squaring.
        y = y * y * y >> 8;
        if (y > 0xFFFFUL) // prevent overflow
        if (y > 0xFFFFUL) { // prevent overflow
            return 0xFFFFU;
        else
        } else {
            return (uint16_t)y;
        }
    }
}



@@ 85,6 92,7 @@ void backlight_init_ports(void) {
    backlight_set(get_backlight_level());

#ifdef BACKLIGHT_BREATHING
    chVTObjectInit(&breathing_vt);
    if (is_backlight_breathing()) {
        breathing_enable();
    }


@@ 92,7 100,9 @@ void backlight_init_ports(void) {
}

void backlight_set(uint8_t level) {
    if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS;
    if (level > BACKLIGHT_LEVELS) {
        level = BACKLIGHT_LEVELS;
    }

    if (level == 0) {
        // Turn backlight off


@@ 115,20 125,19 @@ void backlight_task(void) {}
 */
static const uint8_t breathing_table[BREATHING_STEPS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

void breathing_callback(PWMDriver *pwmp);
static void breathing_callback(virtual_timer_t *vtp, void *p);

bool is_breathing(void) {
    return pwmCFG.callback != NULL;
    return chVTIsArmed(&breathing_vt);
}

void breathing_enable(void) {
    pwmCFG.callback = breathing_callback;
    pwmEnablePeriodicNotification(&BACKLIGHT_PWM_DRIVER);
    /* Update frequency is 256Hz -> 3906us intervals */
    chVTSetContinuous(&breathing_vt, TIME_US2I(3906), breathing_callback, NULL);
}

void breathing_disable(void) {
    pwmCFG.callback = NULL;
    pwmDisablePeriodicNotification(&BACKLIGHT_PWM_DRIVER);
    chVTReset(&breathing_vt);

    // Restore backlight level
    backlight_set(get_backlight_level());


@@ 139,7 148,7 @@ static inline uint16_t scale_backlight(uint16_t v) {
    return v / BACKLIGHT_LEVELS * get_backlight_level();
}

void breathing_callback(PWMDriver *pwmp) {
static void breathing_callback(virtual_timer_t *vtp, void *p) {
    uint8_t  breathing_period = get_breathing_period();
    uint16_t interval         = (uint16_t)breathing_period * 256 / BREATHING_STEPS;



@@ 150,7 159,7 @@ void breathing_callback(PWMDriver *pwmp) {
    uint32_t duty                     = cie_lightness(rescale_limit_val(scale_backlight(breathing_table[index] * 256)));

    chSysLockFromISR();
    pwmEnableChannelI(pwmp, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
    pwmEnableChannelI(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty));
    chSysUnlockFromISR();
}