~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-noctigon-dm11-boost.c

  • Committer: Selene ToyKeeper
  • Date: 2023-11-04 15:09:10 UTC
  • mfrom: (483.1.175 anduril2)
  • Revision ID: bzr@toykeeper.net-20231104150910-ddd3afw4nhfvof2l
merged anduril2 branch -> fsm, with *years* of changes
(this also means this code is now Anduril 2 instead of Anduril 1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Noctigon DM11 (boost driver) PWM helper functions
 
2
// Copyright (C) 2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
#include "chan-rgbaux.c"
 
7
 
 
8
 
 
9
void set_level_zero();
 
10
 
 
11
void set_level_main(uint8_t level);
 
12
bool gradual_tick_main(uint8_t gt);
 
13
 
 
14
 
 
15
Channel channels[] = {
 
16
    { // channel 1 only
 
17
        .set_level    = set_level_main,
 
18
        .gradual_tick = gradual_tick_main
 
19
    },
 
20
    RGB_AUX_CHANNELS
 
21
};
 
22
 
 
23
 
 
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;
 
28
 
 
29
    // turn off all LEDs
 
30
    ch1_dsm_lvl = 0;
 
31
    CH1_PWM = 0;
 
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
 
35
}
 
36
 
 
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);
 
40
 
 
41
    // set delta-sigma soft levels
 
42
    ch1_dsm_lvl = ch1;
 
43
 
 
44
    // set hardware PWM levels and init dsm loop
 
45
    CH1_PWM = ch1_pwm = ch1 >> 7;
 
46
 
 
47
    // enable timer overflow interrupt so DSM can work
 
48
    DSM_INTCTRL |= DSM_OVF_bm;
 
49
 
 
50
    // force reset phase when turning on from zero
 
51
    // (because otherwise the initial response is inconsistent)
 
52
    if (! actual_level) PWM_CNT = 0;
 
53
 
 
54
    CH1_ENABLE_PORT  |= (1 << CH1_ENABLE_PIN );  // enable opamp
 
55
    CH1_ENABLE_PORT2 |= (1 << CH1_ENABLE_PIN2);  // enable PMIC
 
56
}
 
57
 
 
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)
 
61
ISR(DSM_vect) {
 
62
    // set new hardware values first,
 
63
    // for best timing (reduce effect of interrupt jitter)
 
64
    CH1_PWM = ch1_pwm;
 
65
 
 
66
    // calculate next values, now that timing matters less
 
67
 
 
68
    // accumulate error
 
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);
 
72
    // clear carry bit
 
73
    ch1_dsm &= 0x7f;
 
74
}
 
75
 
 
76
 
 
77
bool gradual_tick_main(uint8_t gt) {
 
78
    PWM_DATATYPE ch1 = PWM_GET(pwm1_levels, gt);
 
79
 
 
80
    // adjust multiple times based on current brightness
 
81
    // (so it adjusts faster/coarser when bright, slower/finer when dim)
 
82
 
 
83
    // higher shift = slower/finer adjustments
 
84
    const uint8_t shift = 9;  // ((255 << 7) >> 9) = 63 max
 
85
    uint8_t steps;
 
86
 
 
87
    steps = ch1_dsm_lvl >> shift;
 
88
    for (uint8_t i=0; i<=steps; i++)
 
89
        GRADUAL_ADJUST_SIMPLE(ch1, ch1_dsm_lvl);
 
90
 
 
91
    if ((ch1 == ch1_dsm_lvl)
 
92
       ) {
 
93
        return true;  // done
 
94
    }
 
95
    return false;  // not done yet
 
96
}
 
97