~toykeeper/flashlight-firmware/fsm

« back to all changes in this revision

Viewing changes to ToyKeeper/hwdef-thefreeman-lin16dac.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
// thefreeman linear t1616 DAC driver helper functions
 
2
// Copyright (C) 2023 Selene ToyKeeper
 
3
// SPDX-License-Identifier: GPL-3.0-or-later
 
4
#pragma once
 
5
 
 
6
#include "chan-aux.c"
 
7
 
 
8
void set_level_zero();
 
9
 
 
10
void set_level_main(uint8_t level);
 
11
bool gradual_tick_main(uint8_t gt);
 
12
 
 
13
 
 
14
Channel channels[] = {
 
15
    { // main LEDs
 
16
        .set_level    = set_level_main,
 
17
        .gradual_tick = gradual_tick_main
 
18
    },
 
19
    AUX_CHANNELS
 
20
};
 
21
 
 
22
 
 
23
void set_level_zero() {
 
24
    DAC_LVL  = 0;  // DAC off
 
25
    DAC_VREF = V055;  // low Vref
 
26
    HDR_ENABLE_PORT &= ~(1 << HDR_ENABLE_PIN);  // HDR off
 
27
 
 
28
    // prevent post-off flash
 
29
    //IN_NFET_ENABLE_PORT |= (1 << IN_NFET_ENABLE_PIN);
 
30
    //delay_4ms(IN_NFET_DELAY_TIME/4);
 
31
    //IN_NFET_ENABLE_PORT &= ~(1 << IN_NFET_ENABLE_PIN);
 
32
 
 
33
    // turn off opamp last
 
34
    OPAMP_ENABLE_PORT &= ~(1 << OPAMP_ENABLE_PIN);  // Opamp off
 
35
}
 
36
 
 
37
// single set of LEDs with 1 regulated power channel
 
38
// and low/high HDR plus low/high Vref as different "gears"
 
39
void set_level_main(uint8_t level) {
 
40
    #if 0  // unsure if this helps anything
 
41
    uint8_t noflash = 0;
 
42
 
 
43
    // when turning on from off, try to prevent a flash
 
44
    if ((! actual_level) && (level < HDR_ENABLE_LEVEL_MIN)) {
 
45
        noflash = 1;
 
46
    }
 
47
    #endif
 
48
 
 
49
    // Opamp on first, to give it a few extra microseconds to spin up
 
50
    OPAMP_ENABLE_PORT |= (1 << OPAMP_ENABLE_PIN);
 
51
 
 
52
    // pre-load ramp data so it can be assigned faster later
 
53
    PWM_DATATYPE dac_lvl  = PWM_GET(pwm1_levels, level);
 
54
    PWM_DATATYPE dac_vref = PWM_GET(pwm_tops, level);
 
55
 
 
56
    // enable HDR on top half of ramp
 
57
    if (level >= (HDR_ENABLE_LEVEL_MIN-1))
 
58
        HDR_ENABLE_PORT |= (1 << HDR_ENABLE_PIN);
 
59
    else
 
60
        HDR_ENABLE_PORT &= ~(1 << HDR_ENABLE_PIN);
 
61
 
 
62
    #if 0
 
63
    if (noflash) {
 
64
        // wait for flash prevention to finish
 
65
        delay_4ms(OPAMP_ON_DELAY/4);
 
66
    }
 
67
    #endif
 
68
 
 
69
    // set these in successive clock cycles to avoid getting out of sync
 
70
    // (minimizes ramp bumps when changing gears)
 
71
    DAC_LVL  = dac_lvl;
 
72
    DAC_VREF = dac_vref;
 
73
}
 
74
 
 
75
bool gradual_tick_main(uint8_t gt) {
 
76
    // if HDR and Vref "engine gear" is the same, do a small adjustment...
 
77
    // otherwise, simply jump to the next ramp level
 
78
    //   and let set_level() handle any gear changes
 
79
 
 
80
    PWM_DATATYPE dac_next  = PWM_GET(pwm1_levels, gt);
 
81
    PWM_DATATYPE vref_next = PWM_GET(pwm_tops, gt);
 
82
 
 
83
    // different gear = full adjustment
 
84
    if (vref_next != DAC_VREF) return true;  // let parent set_level() for us
 
85
 
 
86
    // same gear = small adjustment
 
87
    GRADUAL_ADJUST_SIMPLE(dac_next, DAC_LVL);
 
88
    if (dac_next == DAC_LVL) return true;  // done
 
89
 
 
90
    return false;  // not done yet
 
91
}
 
92