1
// Noctigon DM11 (boost driver) PWM helper functions
2
// Copyright (C) 2023 Selene ToyKeeper
3
// SPDX-License-Identifier: GPL-3.0-or-later
6
#include "chan-rgbaux.c"
11
void set_level_main(uint8_t level);
12
bool gradual_tick_main(uint8_t gt);
15
Channel channels[] = {
17
.set_level = set_level_main,
18
.gradual_tick = gradual_tick_main
24
void set_level_zero() {
25
// disable timer overflow interrupt
26
// (helps improve button press handling from Off state)
27
DSM_INTCTRL &= ~DSM_OVF_bm;
32
PWM_CNT = 0; // reset phase
33
CH1_ENABLE_PORT &= ~(1 << CH1_ENABLE_PIN ); // disable opamp
34
CH1_ENABLE_PORT2 &= ~(1 << CH1_ENABLE_PIN2); // disable PMIC
37
// single set of LEDs with single power channel, boost
38
void set_level_main(uint8_t level) {
39
PWM_DATATYPE ch1 = PWM_GET(pwm1_levels, level);
41
// set delta-sigma soft levels
44
// set hardware PWM levels and init dsm loop
45
CH1_PWM = ch1_pwm = ch1 >> 7;
47
// enable timer overflow interrupt so DSM can work
48
DSM_INTCTRL |= DSM_OVF_bm;
50
// force reset phase when turning on from zero
51
// (because otherwise the initial response is inconsistent)
52
if (! actual_level) PWM_CNT = 0;
54
CH1_ENABLE_PORT |= (1 << CH1_ENABLE_PIN ); // enable opamp
55
CH1_ENABLE_PORT2 |= (1 << CH1_ENABLE_PIN2); // enable PMIC
58
// delta-sigma modulation of PWM outputs
59
// happens on each Timer overflow (every 512 cpu clock cycles)
60
// uses 8-bit pwm w/ 7-bit dsm (0b 0PPP PPPP PDDD DDDD)
62
// set new hardware values first,
63
// for best timing (reduce effect of interrupt jitter)
66
// calculate next values, now that timing matters less
69
ch1_dsm += (ch1_dsm_lvl & 0x007f);
70
// next PWM = base PWM value + carry bit
71
ch1_pwm = (ch1_dsm_lvl >> 7) + (ch1_dsm > 0x7f);
77
bool gradual_tick_main(uint8_t gt) {
78
PWM_DATATYPE ch1 = PWM_GET(pwm1_levels, gt);
80
// adjust multiple times based on current brightness
81
// (so it adjusts faster/coarser when bright, slower/finer when dim)
83
// higher shift = slower/finer adjustments
84
const uint8_t shift = 9; // ((255 << 7) >> 9) = 63 max
87
steps = ch1_dsm_lvl >> shift;
88
for (uint8_t i=0; i<=steps; i++)
89
GRADUAL_ADJUST_SIMPLE(ch1, ch1_dsm_lvl);
91
if ((ch1 == ch1_dsm_lvl)
95
return false; // not done yet