diff options
| author | Stefan Kerkmann <karlk90@pm.me> | 2022-10-04 23:10:24 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-04 23:10:24 +0200 |
| commit | 996a9006d3f101dc5c48b58339e2e7323040fd59 (patch) | |
| tree | 56bb76db42e23e3a1bc343ee7d35ee307e08a0a0 /quantum/backlight | |
| parent | b6ea8837e4c81f364d30bdc74bf2cdccb2a1a185 (diff) | |
[Core] PWM Backlight for RP2040 (#17706)
Diffstat (limited to 'quantum/backlight')
| -rw-r--r-- | quantum/backlight/backlight_chibios.c | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/quantum/backlight/backlight_chibios.c b/quantum/backlight/backlight_chibios.c index e8f9e70f78..30e95bd5c8 100644 --- a/quantum/backlight/backlight_chibios.c +++ b/quantum/backlight/backlight_chibios.c | |||
| @@ -40,16 +40,22 @@ | |||
| 40 | # endif | 40 | # endif |
| 41 | #endif | 41 | #endif |
| 42 | 42 | ||
| 43 | static PWMConfig pwmCFG = {0xFFFF, /* PWM clock frequency */ | 43 | #ifndef BACKLIGHT_PWM_COUNTER_FREQUENCY |
| 44 | 256, /* PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */ | 44 | # define BACKLIGHT_PWM_COUNTER_FREQUENCY 0xFFFF |
| 45 | NULL, /* Breathing Callback */ | 45 | #endif |
| 46 | { /* Default all channels to disabled - Channels will be configured durring init */ | 46 | |
| 47 | {PWM_OUTPUT_DISABLED, NULL}, | 47 | #ifndef BACKLIGHT_PWM_PERIOD |
| 48 | {PWM_OUTPUT_DISABLED, NULL}, | 48 | # define BACKLIGHT_PWM_PERIOD 256 |
| 49 | {PWM_OUTPUT_DISABLED, NULL}, | 49 | #endif |
| 50 | {PWM_OUTPUT_DISABLED, NULL}}, | 50 | |
| 51 | 0, /* HW dependent part.*/ | 51 | static PWMConfig pwmCFG = { |
| 52 | 0}; | 52 | .frequency = BACKLIGHT_PWM_COUNTER_FREQUENCY, /* PWM clock frequency */ |
| 53 | .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 */ | ||
| 54 | }; | ||
| 55 | |||
| 56 | #ifdef BACKLIGHT_BREATHING | ||
| 57 | static virtual_timer_t breathing_vt; | ||
| 58 | #endif | ||
| 53 | 59 | ||
| 54 | // See http://jared.geek.nz/2013/feb/linear-led-pwm | 60 | // See http://jared.geek.nz/2013/feb/linear-led-pwm |
| 55 | static uint16_t cie_lightness(uint16_t v) { | 61 | static uint16_t cie_lightness(uint16_t v) { |
| @@ -60,10 +66,11 @@ static uint16_t cie_lightness(uint16_t v) { | |||
| 60 | // to get a useful result with integer division, we shift left in the expression above | 66 | // to get a useful result with integer division, we shift left in the expression above |
| 61 | // and revert what we've done again after squaring. | 67 | // and revert what we've done again after squaring. |
| 62 | y = y * y * y >> 8; | 68 | y = y * y * y >> 8; |
| 63 | if (y > 0xFFFFUL) // prevent overflow | 69 | if (y > 0xFFFFUL) { // prevent overflow |
| 64 | return 0xFFFFU; | 70 | return 0xFFFFU; |
| 65 | else | 71 | } else { |
| 66 | return (uint16_t)y; | 72 | return (uint16_t)y; |
| 73 | } | ||
| 67 | } | 74 | } |
| 68 | } | 75 | } |
| 69 | 76 | ||
| @@ -85,6 +92,7 @@ void backlight_init_ports(void) { | |||
| 85 | backlight_set(get_backlight_level()); | 92 | backlight_set(get_backlight_level()); |
| 86 | 93 | ||
| 87 | #ifdef BACKLIGHT_BREATHING | 94 | #ifdef BACKLIGHT_BREATHING |
| 95 | chVTObjectInit(&breathing_vt); | ||
| 88 | if (is_backlight_breathing()) { | 96 | if (is_backlight_breathing()) { |
| 89 | breathing_enable(); | 97 | breathing_enable(); |
| 90 | } | 98 | } |
| @@ -92,7 +100,9 @@ void backlight_init_ports(void) { | |||
| 92 | } | 100 | } |
| 93 | 101 | ||
| 94 | void backlight_set(uint8_t level) { | 102 | void backlight_set(uint8_t level) { |
| 95 | if (level > BACKLIGHT_LEVELS) level = BACKLIGHT_LEVELS; | 103 | if (level > BACKLIGHT_LEVELS) { |
| 104 | level = BACKLIGHT_LEVELS; | ||
| 105 | } | ||
| 96 | 106 | ||
| 97 | if (level == 0) { | 107 | if (level == 0) { |
| 98 | // Turn backlight off | 108 | // Turn backlight off |
| @@ -115,20 +125,19 @@ void backlight_task(void) {} | |||
| 115 | */ | 125 | */ |
| 116 | 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}; | 126 | 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}; |
| 117 | 127 | ||
| 118 | void breathing_callback(PWMDriver *pwmp); | 128 | static void breathing_callback(virtual_timer_t *vtp, void *p); |
| 119 | 129 | ||
| 120 | bool is_breathing(void) { | 130 | bool is_breathing(void) { |
| 121 | return pwmCFG.callback != NULL; | 131 | return chVTIsArmed(&breathing_vt); |
| 122 | } | 132 | } |
| 123 | 133 | ||
| 124 | void breathing_enable(void) { | 134 | void breathing_enable(void) { |
| 125 | pwmCFG.callback = breathing_callback; | 135 | /* Update frequency is 256Hz -> 3906us intervals */ |
| 126 | pwmEnablePeriodicNotification(&BACKLIGHT_PWM_DRIVER); | 136 | chVTSetContinuous(&breathing_vt, TIME_US2I(3906), breathing_callback, NULL); |
| 127 | } | 137 | } |
| 128 | 138 | ||
| 129 | void breathing_disable(void) { | 139 | void breathing_disable(void) { |
| 130 | pwmCFG.callback = NULL; | 140 | chVTReset(&breathing_vt); |
| 131 | pwmDisablePeriodicNotification(&BACKLIGHT_PWM_DRIVER); | ||
| 132 | 141 | ||
| 133 | // Restore backlight level | 142 | // Restore backlight level |
| 134 | backlight_set(get_backlight_level()); | 143 | backlight_set(get_backlight_level()); |
| @@ -139,7 +148,7 @@ static inline uint16_t scale_backlight(uint16_t v) { | |||
| 139 | return v / BACKLIGHT_LEVELS * get_backlight_level(); | 148 | return v / BACKLIGHT_LEVELS * get_backlight_level(); |
| 140 | } | 149 | } |
| 141 | 150 | ||
| 142 | void breathing_callback(PWMDriver *pwmp) { | 151 | static void breathing_callback(virtual_timer_t *vtp, void *p) { |
| 143 | uint8_t breathing_period = get_breathing_period(); | 152 | uint8_t breathing_period = get_breathing_period(); |
| 144 | uint16_t interval = (uint16_t)breathing_period * 256 / BREATHING_STEPS; | 153 | uint16_t interval = (uint16_t)breathing_period * 256 / BREATHING_STEPS; |
| 145 | 154 | ||
| @@ -150,7 +159,7 @@ void breathing_callback(PWMDriver *pwmp) { | |||
| 150 | uint32_t duty = cie_lightness(rescale_limit_val(scale_backlight(breathing_table[index] * 256))); | 159 | uint32_t duty = cie_lightness(rescale_limit_val(scale_backlight(breathing_table[index] * 256))); |
| 151 | 160 | ||
| 152 | chSysLockFromISR(); | 161 | chSysLockFromISR(); |
| 153 | pwmEnableChannelI(pwmp, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty)); | 162 | pwmEnableChannelI(&BACKLIGHT_PWM_DRIVER, BACKLIGHT_PWM_CHANNEL - 1, PWM_FRACTION_TO_WIDTH(&BACKLIGHT_PWM_DRIVER, 0xFFFF, duty)); |
| 154 | chSysUnlockFromISR(); | 163 | chSysUnlockFromISR(); |
| 155 | } | 164 | } |
| 156 | 165 | ||
