2
* Generic clicky-switch-with-offtime-cap firmware.
3
* Expects a FET+1 style driver, supports two independent power channels.
4
* Similar to blf-a6.c but minus the end-user config options.
5
* (expects to be configured at compile-time, not runtime)
7
* Copyright (C) 2015 Selene Scriven
9
* This program is free software: you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation, either version 3 of the License, or
12
* (at your option) any later version.
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program. If not, see <http://www.gnu.org/licenses/>.
26
* OTC -| |- Voltage ADC
27
* Star 3 -| |- PWM (FET)
28
* GND -| |- PWM (1x7135)
32
* I use these fuse settings
33
* Low: 0x75 (4.8MHz CPU without 8x divider, 9.4kHz phase-correct PWM or 18.75kHz fast-PWM)
36
* For more details on these settings, visit http://github.com/JCapSolutions/blf-firmware/wiki/PWM-Frequency
39
* Star 2 = second PWM output channel
40
* Star 3 = mode memory if soldered, no memory by default
41
* Star 4 = Capacitor for off-time
44
* Resistor values for voltage divider (reference BLF-VLD README for more info)
45
* Reference voltage can be anywhere from 1.0 to 1.2, so this cannot be all that accurate
49
* Vd (~.25 v drop from protection diode)
51
* 1912 (R1 19,100 ohms)
55
* 4701 (R2 4,700 ohms)
59
* To find out what values to use, flash the driver with battcheck.hex
60
* and hook the light up to each voltage you need a value for. This is
61
* much more reliable than attempting to calculate the values from a
62
* theoretical formula.
64
* Same for off-time capacitor values. Measure, don't guess.
66
// Choose your MCU here, or in the build script
69
// FIXME: make 1-channel vs 2-channel power a single #define option
70
#define FET_7135_LAYOUT // specify an I/O pin layout
71
// Also, assign I/O pins in this file:
72
#include "../tk-attiny.h"
75
* =========================================================================
76
* Settings to modify per driver
79
//#define FAST 0x23 // fast PWM channel 1 only
80
//#define PHASE 0x21 // phase-correct PWM channel 1 only
81
#define FAST 0xA3 // fast PWM both channels
82
#define PHASE 0xA1 // phase-correct PWM both channels
84
#define VOLTAGE_MON // Comment out to disable LVP
86
#define OFFTIM3 // Use short/med/long off-time presses
87
// instead of just short/long
89
// output to use for blinks on battery check mode (primary PWM level, alt PWM level)
90
// Use 20,0 for a single-channel driver or 0,20 for a two-channel driver
91
#define BLINK_BRIGHTNESS 0,20
95
// PWM levels for the big circuit (FET or Nx7135)
96
#define MODESNx 0,0,0,7,56,137,255
97
// PWM levels for the small circuit (1x7135)
98
#define MODES1x 3,20,110,255,255,255,0
99
// PWM speed for each mode
100
#define MODES_PWM PHASE,FAST,FAST,FAST,FAST,FAST,PHASE
101
// Hidden modes are *before* the lowest (moon) mode, and should be specified
102
// in reverse order. So, to go backward from moon to turbo to strobe to
103
// battcheck, use BATTCHECK,STROBE,TURBO .
105
#define HIDDENMODES BIKING_STROBE,BATTCHECK,STROBE,TURBO
106
#define HIDDENMODES_PWM PHASE,PHASE,PHASE,PHASE
107
#define HIDDENMODES_ALT 0,0,0,0 // Zeroes, same length as NUM_HIDDEN
109
#define TURBO 255 // Convenience code for turbo mode
110
#define BATTCHECK 254 // Convenience code for battery check mode
111
// Uncomment to enable tactical strobe mode
112
#define STROBE 253 // Convenience code for strobe mode
113
// Uncomment to unable a 2-level stutter beacon instead of a tactical strobe
114
#define BIKING_STROBE 252 // Convenience code for biking strobe mode
115
// comment out to use minimal version instead (smaller)
116
#define FULL_BIKING_STROBE
118
#define NON_WDT_TURBO // enable turbo step-down without WDT
119
// How many timer ticks before before dropping down.
120
// Each timer tick is 500ms, so "60" would be a 30-second stepdown.
121
// Max value of 255 unless you change "ticks"
122
#define TURBO_TIMEOUT 90
124
// Calibrate voltage and OTC in this file:
125
#include "../tk-calibration.h"
128
* =========================================================================
131
// Ignore a spurious warning, we did the cast on purpose
132
#pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
134
#include <avr/pgmspace.h>
135
//#include <avr/io.h>
136
//#include <avr/interrupt.h>
137
#include <avr/eeprom.h>
138
#include <avr/sleep.h>
139
//#include <avr/power.h>
141
#define OWN_DELAY // Don't use stock delay functions.
142
#define USE_DELAY_MS // use _delay_ms()
143
#define USE_DELAY_S // Also use _delay_s(), not just _delay_ms()
144
#include "../tk-delay.h"
145
#define USE_BATTCHECK
146
//#define BATTCHECK_4bars
147
#define BATTCHECK_8bars
148
//#define BATTCHECK_VpT
149
#define BLINK_SPEED 500
150
#include "../tk-voltage.h"
156
// Config / state variables
158
uint8_t memory = 0; // mode memory, or not (set via soldered star)
159
uint8_t mode_idx = 0; // current or last-used mode number
161
// NOTE: Only '1' is known to work; -1 will probably break and is untested.
162
// In other words, short press goes to the next (higher) mode,
163
// medium press goes to the previous (lower) mode.
165
// total length of current mode group's array
166
#define mode_cnt solid_modes+NUM_HIDDEN
167
// number of regular non-hidden modes in current mode group
168
#define solid_modes NUM_MODES
169
// number of hidden modes in the current mode group
170
// (hardcoded because both groups have the same hidden modes)
171
//uint8_t hidden_modes = NUM_HIDDEN; // this is never used
174
// Modes (gets set when the light starts up based on saved config values)
175
PROGMEM const uint8_t modesNx[] = { MODESNx, HIDDENMODES };
176
PROGMEM const uint8_t modes1x[] = { MODES1x, HIDDENMODES_ALT };
177
PROGMEM const uint8_t modes_pwm[] = { MODES_PWM, HIDDENMODES_PWM };
179
void save_state() { // central method for writing (with wear leveling)
180
// a single 16-bit write uses less ROM space than two 8-bit writes
182
uint8_t oldpos=eepos;
184
eepos = (eepos+1) & (EEPSIZE-1); // wear leveling, use next cell
187
eeprom_write_byte((uint8_t *)(eepos), eep); // save current state
188
eeprom_write_byte((uint8_t *)(oldpos), 0xff); // erase old state
191
void restore_state() {
193
// find the config data
194
for(eepos=0; eepos<EEPSIZE; eepos++) {
195
eep = eeprom_read_byte((const uint8_t *)eepos);
196
if (eep != 0xff) break;
198
// unpack the config data
199
if (eepos < EEPSIZE) {
202
// unnecessary, save_state handles wrap-around
203
// (and we don't really care about it skipping cell 0 once in a while)
207
inline void next_mode() {
209
if (mode_idx >= solid_modes) {
210
// Wrap around, skipping the hidden modes
211
// (note: this also applies when going "forward" from any hidden mode)
217
inline void prev_mode() {
218
if (mode_idx == solid_modes) {
219
// If we hit the end of the hidden modes, go back to moon
221
} else if (mode_idx > 0) {
222
// Regular mode: is between 1 and TOTAL_MODES
225
// Otherwise, wrap around (this allows entering hidden modes)
226
mode_idx = mode_cnt - 1;
231
void set_output(uint8_t pwm1, uint8_t pwm2) {
232
// Need PHASE to properly turn off the light
233
if ((pwm1==0) && (pwm2==0)) {
240
void set_mode(uint8_t mode) {
241
TCCR0A = pgm_read_byte(modes_pwm + mode);
242
set_output(pgm_read_byte(modesNx + mode), pgm_read_byte(modes1x + mode));
244
// Only set output for solid modes
245
uint8_t out = pgm_read_byte(modesNx + mode);
246
if ((out < 250) || (out == 255)) {
247
set_output(pgm_read_byte(modesNx + mode), pgm_read_byte(modes1x + mode));
252
void blink(uint8_t val)
256
set_output(BLINK_BRIGHTNESS);
257
_delay_ms(BLINK_SPEED / 5);
259
_delay_ms(BLINK_SPEED * 4 / 5);
267
// Read the off-time cap *first* to get the most accurate reading
268
// Start up ADC for capacitor pin
269
DIDR0 |= (1 << CAP_DIDR); // disable digital input on ADC pin to reduce power consumption
270
ADMUX = (1 << V_REF) | (1 << ADLAR) | CAP_CHANNEL; // 1.1v reference, left-adjust, ADC3/PB3
271
ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL; // enable, start, prescale
273
// Wait for completion
274
while (ADCSRA & (1 << ADSC));
275
// Start again as datasheet says first result is unreliable
276
ADCSRA |= (1 << ADSC);
277
// Wait for completion
278
while (ADCSRA & (1 << ADSC));
279
cap_val = ADCH; // save this for later
281
// All ports default to input, but turn pull-up resistors on for the stars (not the ADC input! Made that mistake already)
282
// only one star, because one is used for PWM channel 2
283
// and the other is used for the off-time capacitor
284
PORTB = (1 << STAR3_PIN);
286
// Set PWM pin to output
287
DDRB |= (1 << PWM_PIN); // enable main channel
288
DDRB |= (1 << ALT_PWM_PIN); // enable second channel
290
// Set timer to do PWM for correct output pin and set prescaler timing
291
//TCCR0A = 0x23; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
292
//TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
294
// Set timer to do PWM for correct output pin and set prescaler timing
295
TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
297
// Read config values and saved state
301
if (cap_val > CAP_SHORT) {
302
// Indicates they did a short press, go to the next mode
303
next_mode(); // Will handle wrap arounds
305
} else if (cap_val > CAP_MED) {
306
// User did a medium press, go back one mode
307
prev_mode(); // Will handle "negative" modes and wrap-arounds
310
// Long press, keep the same mode
311
// ... or reset to the first mode
313
// Reset to the first mode
322
// Charge up the capacitor by setting CAP_PIN to output
323
DDRB |= (1 << CAP_PIN); // Output
324
PORTB |= (1 << CAP_PIN); // High
326
// Turn features on or off as needed
332
//ACSR |= (1<<7); //AC off
334
// Enable sleep mode set to Idle that will be triggered by the sleep_mode() command.
335
// Will allow us to go idle between WDT interrupts
336
//set_sleep_mode(SLEEP_MODE_IDLE); // not used due to blinky modes
343
uint8_t lowbatt_cnt = 0;
346
// Make sure voltage reading is running for later
347
ADCSRA |= (1 << ADSC);
350
output = pgm_read_byte(modesNx + mode_idx);
351
// placeholder in case strobe isn't defined, should get compiled out by -Os
354
else if (output == STROBE) {
355
// 10Hz tactical strobe
361
#endif // ifdef STROBE
363
else if (output == BIKING_STROBE) {
364
// 2-level stutter beacon for biking and such
365
#ifdef FULL_BIKING_STROBE
375
// small/minimal version
382
#endif // ifdef BIKING_STROBE
384
else if (output == BATTCHECK) {
385
// blink zero to five times to show voltage
386
// (~0%, ~25%, ~50%, ~75%, ~100%, >100%)
388
// wait between readouts
389
_delay_s(); _delay_s();
391
#endif // ifdef BATTCHECK
392
else { // Regular non-hidden solid mode
394
// This part of the code will mostly replace the WDT tick code.
396
// Do some magic here to handle turbo step-down
397
//if (ticks < 255) ticks++; // don't roll over
398
ticks ++; // actually, we don't care about roll-over prevention
399
if ((ticks > TURBO_TIMEOUT)
400
&& (output == TURBO)) {
401
mode_idx = solid_modes - 2; // step down to second-highest mode
406
// Otherwise, just sleep.
411
if (ADCSRA & (1 << ADIF)) { // if a voltage reading is ready
412
voltage = ADCH; // get_voltage();
413
// See if voltage is lower than what we were looking for
414
//if (voltage < ((mode_idx <= 1) ? ADC_CRIT : ADC_LOW)) {
415
if (voltage < ADC_LOW) {
420
// See if it's been low for a while, and maybe step down
421
if (lowbatt_cnt >= 8) {
422
// DEBUG: blink on step-down:
423
//set_output(0,0); _delay_ms(100);
424
i = mode_idx; // save space by not accessing mode_idx more than necessary
425
// properly track hidden vs normal modes
426
if (i >= solid_modes) {
427
// step down from blinky modes to medium
430
// step down from solid modes one at a time
432
} else { // Already at the lowest mode
434
// Turn off the light
436
// Power down as many components as possible
437
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
444
// Wait at least 2 seconds before lowering the level again
445
_delay_ms(250); // this will interrupt blinky modes
448
// Make sure conversion is running for next time through
449
ADCSRA |= (1 << ADSC);
452
#endif // ifdef VOLTAGE_MON
453
//sleep_mode(); // incompatible with blinky modes
456
//return 0; // Standard Return Code