1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
* Copyright 2009 Ettus Research LLC
*/
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include "global.h"
#include "power.h"
#include "debug.h"
#include "error.h"
#include "ltc3675.h"
#ifdef CHARGER_TI
#include "bq24190.h"
#else
#include "ltc4155.h"
#endif // CHARGER_TI
#define AUTO_POWER_ON
#define INITIAL_DELAY 250 // ms
FUSES = { // FIXME: & FUSE_CKSEL1 for low power 128 kHz clock
.low = (FUSE_CKSEL0 & FUSE_SUT0 & FUSE_CKDIV8), // Internal 8MHz Oscillator, Slowly rising power (start-up time), Divide Clock by 8
.high = (FUSE_EESAVE & FUSE_SPIEN), // Save EEPROM between flashes // FIXME: Leave SPIEN for programming enabled?
}; // Not using watchdog as it runs during sleep and consumes power
volatile STATE _state;
/*
- Main/shared variables must be volatile
- Port pins are tri-stated on reset
* AVR_IRQ PD(5)
- Enable pull-ups on all O.D. outputs from regulator chip
* PS_POR/SRST should be driven HIGH by ATTiny?
- AVR_RESET -> RESET pin - don't configure fuse (this would disable this functionality and prohibit serial programming)
* Ship-and-store mode for charge controller?
* cli before I2C calls
* PS_TX
- en5-clk, en2-data
* Instruction following SEI is executed before interrupts
* LTC3675 real-time status doesn't contain UV/OT
* LTC3675 PGOOD -> power down (no point in checking blink state)
* On WALL, use TX, on battery use OTG switcher
* PRR - Power Reduction Register (p40)
- 100% -> 50% battery charge limit
* Check latched status for UV/OT in 3675
* If blink error reset, get latest charge status from 4155
* Fix UV status check from 3675/4155 as they currently share the same error
* Use charger termination at 8hr or Vc/x
* Check PGood on all regs after power on before setting powered=true
* Re-init 4155 on soft-power on
- Re-set 3A limit in 4155 after external power connection
- Removing power when running on battery, 4155GO 0xA0 - but WALL has been removed
- Why is charger reporting Constant Current when power is removed
* ltc3675_is_power_button_depressed: check if any reg is on, otherwise value will be invalid
* When e.g. 3.3V doesn't come up, blink code is correctly 4 but there's a very short blink before re-starting the sequence
- Vprog<Vc/x
*/
bool pmc_mask_irqs(bool mask)
{
if (_state.interrupts_enabled == false)
return false;
if (mask)
{
if (_state.interrupt_depth == 0)
cli();
++_state.interrupt_depth;
}
else
{
if (_state.interrupt_depth == 0)
return false;
--_state.interrupt_depth;
if (_state.interrupt_depth == 0)
sei();
}
return true;
}
int main(void)
{
_delay_ms(INITIAL_DELAY);
///////////////////////////////////////////////////////////////////////////
memset((void*)&_state, 0x00, sizeof(STATE));
debug_init();
debug_blink(1);
//debug_log("#"); // Will not boot if this is 21 chars long?!
debug_log("Hello world");
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // SLEEP_MODE_PWR_SAVE combination is documented as Reserved
//ltc4155_dump();
// FIXME: Init as SPI slave (FPGA is master)
// 8-bit timer for blinking errors on charge LED
TCCR0A = _BV(CTC0); // CTC mode
OCR0A = 244; // 250ms with 1024 prescale
TIMSK0 = _BV(OCIE0A); // Enable CTC on Timer 0
bool init_result = power_init();
debug_log_ex("Init", false);
_debug_log(init_result ? "+" : "-");
debug_blink(2);
//debug_blink_rev(6);
///////////////////////////////////
#ifdef AUTO_POWER_ON
power_on(); // Turn on immediately. Need to de-press power button to turn off.
debug_log("Power");
debug_blink(3);
//debug_blink_rev(10);
//debug_wait();
//ltc4155_dump();
#endif // AUTO_POWER_ON
_state.interrupts_enabled = true;
sei(); // Enable interrupts
asm("nop");
_state.wake_up = false; // This will fire the first time the regs are turned on
bool one_more = false;
while (true)
{
one_more = false;
#ifdef CHARGER_TI
if (_state.bq24190_irq)
{
bq24190_handle_irq();
_state.bq24190_irq = false;
}
#else
if ((_state.ltc4155_irq)/* || ltc4155_has_interrupt()*/) // [Don't know why PCINT ISR misses LTC4155 IRQ on power up, so double-check state of line]
{
ltc4155_handle_irq();
//ltc4155_dump();
_state.ltc4155_irq = false;
}
#endif // !CHARGER_TI
if (_state.core_power_bad) // FIXME: Check whether it's supposed to be on
{
if (power_is_subsys_on(PS_FPGA))
{
_delay_ms(1); // Seeing weird 120us drop in PGOOD during boot from flash (no apparent drop in 1.0V though)
if (tps54478_is_power_good() == false)
{
debug_log("ML:FPGA!");
//power_off();
_state.power_off = true;
/*while (_state.wake_up == false)
{
blink_error_sequence(1);
}*/
pmc_set_blink_error(BlinkError_FPGA_Power); // [If a blink error was set in power_off, this will supercede it]
}
}
_state.core_power_bad = false;
}
if ((_state.ltc3675_irq)/* || ltc3675_has_interrupt()*/) // This is fired on initial power up
{
debug_log("ML:3675+");
ltc3675_handle_irq();
if (ltc3675_is_power_good(ltc3675_get_last_status()) == false)
{
debug_log("ML:3675!");
//power_off();
_state.power_off = true;
}
_state.ltc3675_irq = false;
}
if (_state.power_off)
{
debug_log("ML:Off..");
power_off();
_state.power_off = false;
_state.wake_up = false;
}
else if (_state.wake_up)
{
_delay_ms(1); // Tapping 3.1 ohm load ing 4155 in dev setup causes transient on this line and causes power on sequence to begin again
//if (_state.powered == false) // Don't check in case button is held long enough to force LTC3675 shutdown (will not change 'powered' value)
if (ltc3675_is_waking_up())
{
debug_log("ML:On..");
power_on();
}
_state.wake_up = false;
}
// Check to see if the error state has resolved itself at the end of each sequence of the current blink error
if ((_state.blink_error != BlinkError_None) && (_state.blink_last_loop != _state.blink_loops))
{
// [Check IRQs periodically]
bool ltc3675_use_last_status = false;
/*if (ltc3675_has_interrupt())
{
//debug_set(IO_PB(6), ((_state.blink_loops % 2) == 0));
ltc3675_use_last_status = true;
ltc3675_handle_irq();
}*/
///////////////////////////
switch (_state.blink_error)
{
case BlinkError_LTC3675_UnderVoltage:
case BlinkError_LTC3675_OverTemperature:
case BlinkError_DRAM_Power:
case BlinkError_3_3V_Peripherals_Power:
case BlinkError_1_8V_Peripherals_Power:
case BlinkError_TX_Power:
if (((ltc3675_use_last_status) && (ltc3675_status_to_error(ltc3675_get_last_status()) != BlinkError_None)) ||
((ltc3675_use_last_status == false) && (ltc3675_check_status() != BlinkError_None)))
break;
debug_log("BE:3675-");
goto cancel_blink_error;
case BlinkError_FPGA_Power:
if (tps54478_is_power_good() == false)
break;
debug_log("BE:FPGA-");
goto cancel_blink_error;
default:
cancel_blink_error:
//debug_set(IO_PB(7), true);
pmc_set_blink_error(BlinkError_None);
}
////////////////////////////////////
// More periodic checks
// Need to do this has some interrupts are on PCINT, and while GIE is disabled, might change & change back
// E.g. LTC3675 IRQ due to UV, reset IRQ, re-asserts UV
#ifndef CHARGER_TI
if (ltc4155_has_interrupt())
{
debug_log("BE:4155");
_state.ltc4155_irq = true;
one_more = true;
}
#endif // !CHARGER_TI
if (ltc3675_has_interrupt())
{
debug_log("BE:3675");
_state.ltc3675_irq = true;
one_more = true;
}
if (power_is_subsys_on(PS_FPGA))
{
if (tps54478_is_power_good() == false)
{
debug_log("BE:FPGA!");
_state.core_power_bad = true;
one_more = true;
}
}
////////////////////////////////////
_state.blink_last_loop = _state.blink_loops;
}
//if (_state.timers_running == false)
if ((_state.active_timers == 0) && (one_more == false))
{
debug_log("^");
sleep_mode();
debug_log("$");
}
}
return 0;
}
uint8_t pmc_get_blink_error(void)
{
return _state.blink_error;
}
void pmc_set_blink_error(uint8_t count)
{
if ((_state.blink_error != BlinkError_None) && (count /*> _state.blink_error*/!= BlinkError_None)) // [Prioritise] Always keep first sequence running
return;
else if (_state.blink_error == count) // Don't restart if the same
return;
if (count == BlinkError_None)
{
debug_log("BLNK-");
_state.blink_stop = true;
return;
}
//char msg[25];
//sprintf(msg, "Blink code = %i\n", count);
//debug_log(msg);
debug_log_ex("BLNK ", false);
debug_log_byte(count);
_state.blink_error = count;
_state.blink_loops = 0;
_state.blink_last_loop = 0;
_state.blinker_state = 0;
_state.blink_stop = false;
charge_set_led(false);
TCNT0 = 0;
if ((TCCR0A & 0x07) == 0x00) // Might already be active with existing error
_state.active_timers++;
TCCR0A |= 0x05; // Start with 1024 prescale
}
ISR(TIMER0_COMPA_vect) // Blink the sequence, and leave one slot at the beginning and end where the LED is off so one can get a sense of how many blinks occurred
{
pmc_mask_irqs(true);
if (_state.blinker_state < (2 * _state.blink_error + 1))
charge_set_led((_state.blinker_state % 2) == 1);
_state.blinker_state++;
if (_state.blinker_state == (2 * _state.blink_error + 1 + 1))
{
_state.blinker_state = 0;
if (_state.blink_stop)
{
if ((TCCR0A & 0x07) != 0x00)
_state.active_timers--;
TCCR0A &= ~0x07;
_state.blink_error = BlinkError_None;
debug_log("BLNK.");
}
else
{
_state.blink_loops++;
}
}
pmc_mask_irqs(false);
}
|