~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to hello_world/STAR_smaller.c

  • Committer: Selene Scriven
  • Date: 2015-03-17 08:56:50 UTC
  • mto: This revision was merged to the branch mainline in revision 124.
  • Revision ID: ubuntu@toykeeper.net-20150317085650-s89wr9h28n2co7z1
Added TheStar firmwares from _the_

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This is a copy of JonnyC/STAR_on-time_memory/STAR_1.1.c , modified only
 
3
 * slightly to shave off a few bytes.  It has moon mode hard-coded as
 
4
 * "enabled" and turbo step-down disabled.  This is because the original
 
5
 * firmware was compiling to a file bigger than 1024 bytes, and I wanted to
 
6
 * have something I could flash as a test.  This provided my first working
 
7
 * self-flashed firmware, and is intended to help others to test their
 
8
 * toolchain and general flashing setup.  Treat it like "hello world".
 
9
 *
 
10
 * Original author: JonnyC
 
11
 * Size reduced by: ToyKeeper / Selene Scriven
 
12
 *
 
13
 * NANJG 105C Diagram
 
14
 *           ---
 
15
 *         -|   |- VCC
 
16
 *  Star 4 -|   |- Voltage ADC
 
17
 *  Star 3 -|   |- PWM
 
18
 *     GND -|   |- Star 2
 
19
 *           ---
 
20
 *
 
21
 * CPU speed is 4.8Mhz without the 8x divider when low fuse is 0x75
 
22
 *
 
23
 * define F_CPU 4800000  CPU: 4.8MHz  PWM: 9.4kHz       ####### use low fuse: 0x75  #######
 
24
 *                             /8     PWM: 1.176kHz     ####### use low fuse: 0x65  #######
 
25
 * define F_CPU 9600000  CPU: 9.6MHz  PWM: 19kHz        ####### use low fuse: 0x7a  #######
 
26
 *                             /8     PWM: 2.4kHz       ####### use low fuse: 0x6a  #######
 
27
 * 
 
28
 * Above PWM speeds are for phase-correct PWM.  This program uses Fast-PWM, which when the CPU is 4.8MHz will be 18.75 kHz
 
29
 *
 
30
 * FUSES
 
31
 *              I use these fuse settings
 
32
 *              Low:  0x75
 
33
 *              High: 0xff
 
34
 *
 
35
 * STARS
 
36
 *              Star 2 = Moon if connected
 
37
 *              Star 3 = H-L if connected, L-H if not
 
38
 *              Star 4 = Memory if not connected
 
39
 *
 
40
 * VOLTAGE
 
41
 *              Resistor values for voltage divider (reference BLF-VLD README for more info)
 
42
 *              Reference voltage can be anywhere from 1.0 to 1.2, so this cannot be all that accurate
 
43
 *
 
44
 *           VCC
 
45
 *            |
 
46
 *           Vd (~.25 v drop from protection diode)
 
47
 *            |
 
48
 *          1912 (R1 19,100 ohms)
 
49
 *            |
 
50
 *            |---- PB2 from MCU
 
51
 *            |
 
52
 *          4701 (R2 4,700 ohms)
 
53
 *            |
 
54
 *           GND
 
55
 *
 
56
 *              ADC = ((V_bat - V_diode) * R2   * 255) / ((R1    + R2  ) * V_ref)
 
57
 *              125 = ((3.0   - .25    ) * 4700 * 255) / ((19100 + 4700) * 1.1  )
 
58
 *              121 = ((2.9   - .25    ) * 4700 * 255) / ((19100 + 4700) * 1.1  )
 
59
 *
 
60
 *              Well 125 and 121 were too close, so it shut off right after lowering to low mode, so I went with
 
61
 *              130 and 120
 
62
 *
 
63
 *              To find out what value to use, plug in the target voltage (V) to this equation
 
64
 *                      value = (V * 4700 * 255) / (23800 * 1.1)
 
65
 *      
 
66
 */
 
67
#define F_CPU 4800000UL
 
68
 
 
69
/*
 
70
 * =========================================================================
 
71
 * Settings to modify per driver
 
72
 */
 
73
 
 
74
#define VOLTAGE_MON                     // Comment out to disable
 
75
 
 
76
#define MODE_MOON                       8       // Can comment out to remove mode, but should be set through soldering stars
 
77
#define MODE_LOW                        14  // Can comment out to remove mode
 
78
#define MODE_MED                        39      // Can comment out to remove mode
 
79
#define MODE_HIGH_W_TURBO       110     // MODE_HIGH value when turbo is enabled
 
80
#define MODE_HIGH                       120     // Can comment out to remove mode
 
81
//#define MODE_TURBO                    255     // Can comment out to remove mode
 
82
#define TURBO_TIMEOUT           240 // How many WTD ticks before before dropping down (.5 sec each)
 
83
 
 
84
#define WDT_TIMEOUT                     2       // Number of WTD ticks before mode is saved (.5 sec each)
 
85
 
 
86
#define ADC_LOW                         130     // When do we start ramping
 
87
#define ADC_CRIT                        120 // When do we shut the light off
 
88
 
 
89
/*
 
90
 * =========================================================================
 
91
 */
 
92
 
 
93
#ifdef MODE_TURBO
 
94
#undef  MODE_HIGH
 
95
#define MODE_HIGH       MODE_HIGH_W_TURBO
 
96
#endif
 
97
 
 
98
//#include <avr/pgmspace.h>
 
99
//#include <avr/io.h>
 
100
#include <util/delay.h>
 
101
#include <avr/interrupt.h>
 
102
#include <avr/wdt.h>
 
103
#include <avr/eeprom.h>
 
104
#include <avr/sleep.h>
 
105
//#include <avr/power.h>
 
106
 
 
107
#define STAR2_PIN   PB0
 
108
#define STAR3_PIN   PB4
 
109
#define STAR4_PIN   PB3
 
110
#define PWM_PIN     PB1
 
111
#define VOLTAGE_PIN PB2
 
112
#define ADC_CHANNEL 0x01        // MUX 01 corresponds with PB2
 
113
#define ADC_DIDR        ADC1D   // Digital input disable bit corresponding with PB2
 
114
#define ADC_PRSCL   0x06        // clk/64
 
115
 
 
116
#define PWM_LVL         OCR0B   // OCR0B is the output compare register for PB1
 
117
 
 
118
/*
 
119
 * global variables
 
120
 */
 
121
 
 
122
// Mode storage
 
123
uint8_t eepos = 0;
 
124
uint8_t eep[32];
 
125
uint8_t memory = 0;
 
126
 
 
127
// Modes (gets set when the light starts up based on stars)
 
128
static uint8_t modes[10];  // Don't need 10, but keeping it high enough to handle all
 
129
volatile uint8_t mode_idx = 0;
 
130
int     mode_dir = 0; // 1 or -1. Determined when checking stars. Do we increase or decrease the idx when moving up to a higher mode.
 
131
uint8_t mode_cnt = 0;
 
132
 
 
133
uint8_t lowbatt_cnt = 0;
 
134
 
 
135
void store_mode_idx(uint8_t lvl) {  //central method for writing (with wear leveling)
 
136
        uint8_t oldpos=eepos;
 
137
        eepos=(eepos+1)&31;  //wear leveling, use next cell
 
138
        // Write the current mode
 
139
        EEARL=eepos;  EEDR=lvl; EECR=32+4; EECR=32+4+2;  //WRITE  //32:write only (no erase)  4:enable  2:go
 
140
        while(EECR & 2); //wait for completion
 
141
        // Erase the last mode
 
142
        EEARL=oldpos;           EECR=16+4; EECR=16+4+2;  //ERASE  //16:erase only (no write)  4:enable  2:go
 
143
}
 
144
inline void read_mode_idx() {
 
145
        eeprom_read_block(&eep, 0, 32);
 
146
        while((eep[eepos] == 0xff) && (eepos < 32)) eepos++;
 
147
        if (eepos < 32) mode_idx = eep[eepos];//&0x10; What the?
 
148
        else eepos=0;
 
149
}
 
150
 
 
151
inline void next_mode() {
 
152
        if (mode_idx == 0 && mode_dir == -1) {
 
153
                // Wrap around
 
154
                mode_idx = mode_cnt - 1;
 
155
        } else {
 
156
                mode_idx += mode_dir;
 
157
                if (mode_idx > (mode_cnt - 1)) {
 
158
                        // Wrap around
 
159
                        mode_idx = 0;
 
160
                }
 
161
        }
 
162
}
 
163
 
 
164
inline void WDT_on() {
 
165
        // Setup watchdog timer to only interrupt, not reset, every 500ms.
 
166
        cli();                                                  // Disable interrupts
 
167
        wdt_reset();                                    // Reset the WDT
 
168
        WDTCR |= (1<<WDCE) | (1<<WDE);  // Start timed sequence
 
169
        WDTCR = (1<<WDTIE) | (1<<WDP2) | (1<<WDP0); // Enable interrupt every 500ms
 
170
        sei();                                                  // Enable interrupts
 
171
}
 
172
 
 
173
inline void WDT_off()
 
174
{
 
175
        cli();                                                  // Disable interrupts
 
176
        wdt_reset();                                    // Reset the WDT
 
177
        MCUSR &= ~(1<<WDRF);                    // Clear Watchdog reset flag
 
178
        WDTCR |= (1<<WDCE) | (1<<WDE);  // Start timed sequence
 
179
        WDTCR = 0x00;                                   // Disable WDT
 
180
        sei();                                                  // Enable interrupts
 
181
}
 
182
 
 
183
inline void ADC_on() {
 
184
        ADMUX  = (1 << REFS0) | (1 << ADLAR) | ADC_CHANNEL; // 1.1v reference, left-adjust, ADC1/PB2
 
185
    DIDR0 |= (1 << ADC_DIDR);                                                   // disable digital input on ADC pin to reduce power consumption
 
186
        ADCSRA = (1 << ADEN ) | (1 << ADSC ) | ADC_PRSCL;   // enable, start, prescale
 
187
}
 
188
 
 
189
inline void ADC_off() {
 
190
        ADCSRA &= ~(1<<7); //ADC off
 
191
}
 
192
 
 
193
#ifdef VOLTAGE_MON
 
194
uint8_t low_voltage(uint8_t voltage_val) {
 
195
        // Start conversion
 
196
        ADCSRA |= (1 << ADSC);
 
197
        // Wait for completion
 
198
        while (ADCSRA & (1 << ADSC));
 
199
        // See if voltage is lower than what we were looking for
 
200
        if (ADCH < voltage_val) {
 
201
                // See if it's been low for a while
 
202
                if (++lowbatt_cnt > 8) {
 
203
                        lowbatt_cnt = 0;
 
204
                        return 1;
 
205
                }
 
206
        } else {
 
207
                lowbatt_cnt = 0;
 
208
        }
 
209
        return 0;
 
210
}
 
211
#endif
 
212
 
 
213
ISR(WDT_vect) {
 
214
        static uint8_t ticks = 0;
 
215
        if (ticks < 255) ticks++;
 
216
        
 
217
        if (ticks == WDT_TIMEOUT) {
 
218
                if (memory) {
 
219
                        store_mode_idx(mode_idx);
 
220
                } else {
 
221
                        // Reset the mode to the start for next time
 
222
                        store_mode_idx((mode_dir == 1) ? 0 : (mode_cnt - 1));
 
223
                }
 
224
#ifdef MODE_TURBO       
 
225
        //} else if (ticks == TURBO_TIMEOUT && modes[mode_idx] == MODE_TURBO) { // Doesn't make any sense why this doesn't work
 
226
        } else if (ticks == TURBO_TIMEOUT && mode_idx == (mode_cnt - 1)) {
 
227
                // Turbo mode is always at end
 
228
                PWM_LVL = modes[--mode_idx];
 
229
#endif
 
230
        }
 
231
 
 
232
}
 
233
 
 
234
int main(void)
 
235
{       
 
236
        // All ports default to input, but turn pull-up resistors on for the stars (not the ADC input!  Made that mistake already)
 
237
        PORTB = (1 << STAR2_PIN) | (1 << STAR3_PIN) | (1 << STAR4_PIN);
 
238
        
 
239
        // Set PWM pin to output
 
240
        DDRB = (1 << PWM_PIN);
 
241
 
 
242
        // Set timer to do PWM for correct output pin and set prescaler timing
 
243
        TCCR0A = 0x23; // phase corrected PWM is 0x21 for PB1, fast-PWM is 0x23
 
244
        TCCR0B = 0x01; // pre-scaler for timer (1 => 1, 2 => 8, 3 => 64...)
 
245
        
 
246
        // Turn features on or off as needed
 
247
        #ifdef VOLTAGE_MON
 
248
        ADC_on();
 
249
        #else
 
250
        ADC_off();
 
251
        #endif
 
252
        ACSR   |=  (1<<7); //AC off
 
253
        
 
254
        // Load up the modes based on stars
 
255
        // Always load up the modes array in order of lowest to highest mode
 
256
        // 0 being low for soldered, 1 for pulled-up for not soldered
 
257
        // Moon
 
258
        #ifdef MODE_MOON
 
259
        /* if ((PINB & (1 << STAR2_PIN)) == 0) { */
 
260
                modes[mode_cnt++] = MODE_MOON;
 
261
        /* } */
 
262
        #endif
 
263
        #ifdef MODE_LOW
 
264
        modes[mode_cnt++] = MODE_LOW;
 
265
        #endif
 
266
        #ifdef MODE_MED
 
267
        modes[mode_cnt++] = MODE_MED;
 
268
        #endif
 
269
        #ifdef MODE_HIGH
 
270
        modes[mode_cnt++] = MODE_HIGH;
 
271
        #endif
 
272
        #ifdef MODE_TURBO
 
273
        modes[mode_cnt++] = MODE_TURBO;
 
274
        #endif
 
275
        /*
 
276
        if ((PINB & (1 << STAR3_PIN)) == 0) {
 
277
                // High to Low
 
278
                mode_dir = -1;
 
279
        } else {
 
280
        */
 
281
                mode_dir = 1;
 
282
        /*
 
283
        }
 
284
        */
 
285
        // Not soldered (1) should enable memory
 
286
        //memory = ((PINB & (1 << STAR4_PIN)) > 0) ? 1 : 0;
 
287
        memory = 0;
 
288
        
 
289
        // Enable sleep mode set to Idle that will be triggered by the sleep_mode() command.
 
290
        // Will allow us to go idle between WDT interrupts
 
291
        set_sleep_mode(SLEEP_MODE_IDLE);
 
292
        
 
293
        // Determine what mode we should fire up
 
294
        // Read the last mode that was saved
 
295
        read_mode_idx();
 
296
        if (mode_idx&0x10) {
 
297
                // Indicates we did a short press last time, go to the next mode
 
298
                // Remove short press indicator first
 
299
                mode_idx &= 0x0f;
 
300
                next_mode(); // Will handle wrap arounds
 
301
        } else {
 
302
                // Didn't have a short press, keep the same mode
 
303
        }
 
304
        // Store mode with short press indicator
 
305
        store_mode_idx(mode_idx|0x10);
 
306
        
 
307
        WDT_on();
 
308
        
 
309
        // Now just fire up the mode
 
310
        PWM_LVL = modes[mode_idx];
 
311
        
 
312
        uint8_t i = 0;
 
313
        uint8_t hold_pwm;
 
314
        while(1) {
 
315
        #ifdef VOLTAGE_MON
 
316
                if (low_voltage(ADC_LOW)) {
 
317
                        // We need to go to a lower level
 
318
                        if (mode_idx == 0 && PWM_LVL <= modes[mode_idx]) {
 
319
                                // Can't go any lower than the lowest mode
 
320
                                // Wait until we hit the critical level before flashing 10 times and turning off
 
321
                                while (!low_voltage(ADC_CRIT));
 
322
                                i = 0;
 
323
                                while (i++<10) {
 
324
                                        PWM_LVL = 0;
 
325
                                        _delay_ms(250);
 
326
                                        PWM_LVL = modes[0];
 
327
                                        _delay_ms(500);
 
328
                                }
 
329
                                // Turn off the light
 
330
                                PWM_LVL = 0;
 
331
                                // Disable WDT so it doesn't wake us up
 
332
                                WDT_off();
 
333
                                // Power down as many components as possible
 
334
                                set_sleep_mode(SLEEP_MODE_PWR_DOWN);
 
335
                                sleep_mode();
 
336
                        } else {
 
337
                                // Flash 3 times before lowering
 
338
                                hold_pwm = PWM_LVL;
 
339
                                i = 0;
 
340
                                while (i++<3) {
 
341
                                        PWM_LVL = 0;
 
342
                                        _delay_ms(250);
 
343
                                        PWM_LVL = hold_pwm;
 
344
                                        _delay_ms(500);
 
345
                                }
 
346
                                // Lower the mode by half, but don't go below lowest level
 
347
                                if ((PWM_LVL >> 1) < modes[0]) {
 
348
                                        PWM_LVL = modes[0];
 
349
                                        mode_idx = 0;
 
350
                                } else {                                        
 
351
                                        PWM_LVL = (PWM_LVL >> 1);
 
352
                                }                                       
 
353
                                // See if we should change the current mode level if we've gone under the current mode.
 
354
                                if (PWM_LVL < modes[mode_idx]) {
 
355
                                        // Lower our recorded mode
 
356
                                        mode_idx--;
 
357
                                }
 
358
                        }
 
359
                        // Wait 3 seconds before lowering the level again
 
360
                        _delay_ms(3000);
 
361
                }
 
362
        #endif
 
363
                sleep_mode();
 
364
        }
 
365
 
 
366
    return 0; // Standard Return Code
 
367
}