-
Notifications
You must be signed in to change notification settings - Fork 1
/
heating_ctr.c
304 lines (225 loc) · 5.05 KB
/
heating_ctr.c
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
#define F_CPU 1000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <stdint.h>
static volatile uint32_t cnt =0;
static volatile uint32_t mark_cnt =0;
static volatile uint8_t events = 0;
#define EVT_TIMER 1
#define EVT_CNTR 2
#define EVT_MARK 4
#define EE_MEM ((const uint8_t *)0)
#define EE_SIZE 128
/*
f_T1ovf = 1MHz/(65536*1024) = 0.014... Hz
-> ~53.64 times/hr
=> eeprom written ~every 66 secs of operational time
eeprom life = 100k writes, w/ wear levelling x25 = 2.5Mwrites
* 66 secs = 165Msecs lifetime = ~5 years
* EE_WDIV = ~20 years lifetime for eeprom
(written every 66*4 = 264 secs = 4:24 min:sec)
*/
#define EE_WRDIV 4
static const uint8_t *ee_ptr = EE_MEM;
ISR(INT0_vect) {
if(events & EVT_MARK)
return;
mark_cnt = cnt;
events |= EVT_MARK;
}
ISR(TIMER0_OVF_vect) {
cnt++;
events |= EVT_CNTR;
}
ISR(TIMER1_OVF_vect) {
events |= EVT_TIMER;
}
static volatile uint16_t tof = 0;
ISR(TIMER1_CAPT_vect) {
tof = ICR1;
TIMSK &= ~(1<<ICIE1);
}
static uint8_t nibble2hex(uint8_t n) {
return (n + ((n < 10) ? ('0') : ('a' - 10)));
}
static void u32tostr(uint8_t *buf, uint32_t n) {
uint8_t *ptr = buf+8;
do {
--ptr;
*ptr = nibble2hex(n&0xf);
n>>=4;
}while(buf != ptr);
}
static void cksum(uint8_t *buf) {
uint8_t sum=0;
for(;*buf;buf++)
sum += *buf;
*buf++ = nibble2hex(sum>>4);
*buf = nibble2hex(sum&0xf);
}
static void uart_send(char *buf) {
for(;*buf;buf++) {
loop_until_bit_is_set( UCSRA, UDRE );
UDR = *buf;
}
}
#ifndef eeprom_update_byte
static void eeprom_update_byte( const uint8_t *p, uint8_t val) {
if(eeprom_read_byte(p) != val)
eeprom_write_byte((uint8_t *)p, val);
}
#endif
static void write_eeprom(uint32_t val) {
uint8_t i=4;
uint8_t ckval = 0x5A;
/* write from least to most significant byte.
This way, a partial written counter cannot
be any higher, than the actual value, since
the value already present in the MSBs is
less or equal to the actual counter value,
since it's the same-significance-part of
an old counter value */
ee_ptr+= 3;
do {
uint8_t dbyte = val&0xff;
ckval ^= dbyte;
eeprom_update_byte(ee_ptr--, ~dbyte);
val>>=8;
} while(--i);
/* write cksum */
ee_ptr+=4;
eeprom_update_byte(ee_ptr++, ~ckval);
// handle ptr rollover
if(ee_ptr >= (EE_MEM + (EE_SIZE/5)))
ee_ptr = EE_MEM;
}
static inline void load_eeprom(void) {
const uint8_t *ptr = EE_MEM;
uint32_t val=0;
do {
uint8_t ckval=0x5A;
uint8_t i=4;
while(1) {
uint8_t dbyte = ~(eeprom_read_byte(ptr++));
ckval ^= dbyte;
if(!(i--))
break;
val <<= 8;
val |= dbyte;
}
if((!ckval) && (val > cnt)) {
cnt = val;
ee_ptr = ptr;
}
} while(ptr < (EE_MEM + (EE_SIZE/5)));
// handle ptr rollover
if(ee_ptr >= (EE_MEM + (EE_SIZE/5)))
ee_ptr = EE_MEM;
}
static inline uint16_t ping(void) {
uint16_t res;
tof = 0xffff;
// reconfigure T1
TIMSK &= ~(1<<TOIE1);
TCCR1B = 0;
TCNT1 = 0;
// start timer & send ping
cli();
TCCR1B = (1<<ICNC1)|(1<<CS11);
DDRD |= (1<<PD6);
sei();
_delay_us(400);
DDRD &= ~(1<<PD6);
// sensor deadtime
_delay_us(500);
// enable ICP
TIFR = (1<<ICF1);
TIMSK |= (1<<ICIE1);
// wait 15ms (~2m)
_delay_ms(15);
cli();
res = tof;
sei();
// reconfigure T1
TCCR1B = 0;
TCNT1 = 0;
TIMSK &= ~(1<<ICIE1);
TIFR = (1<<TOV1);
TIMSK |= (1<<TOIE1);
TCCR1B = (5<<CS10);
return res;
}
int main(void) {
char buf[36];
uint8_t ee_timer = 0;
PORTB = (1<<PB7)|(1<<PB4);
DDRB = (1<<PB7)|(1<<PB4);
PORTD = 0;
DDRD = 0;
UBRRH=0;
UBRRL = 12; // 9,6 kBaud
UCSRA = (1<<U2X);
UCSRB = (1<<TXEN);
UCSRC = (3<<UCSZ0); // 8 bit, 1 stopbit
TCCR0A = (1<<WGM01)|(1<<WGM00);
TCCR0B = (1<<WGM02)|(7<<CS00);
OCR0A = 49;
TCCR1A = 0;
TCCR1B = (5<<CS10);
TCCR1C = 0;
TIMSK = (1<<TOIE1) | (1<<TOIE0);
MCUCR = (1<<ISC01);
GIMSK = (1<<INT0);
set_sleep_mode(SLEEP_MODE_IDLE);
buf[0]='*';
buf[1+8] = buf[1+8+1+8] = buf[1+8+1+8+1+8] = ',';
buf[1+8+1+8+1+8+2] = ',';
buf[30]='#';
buf[31]='\r';
buf[32]='\n';
buf[33]=0;
load_eeprom();
while(1) {
cli();
if(events & EVT_TIMER) {
uint32_t cnt_copy = cnt;
uint32_t mark_copy = mark_cnt;
uint8_t evts_copy = events;
uint32_t echo;
events = 0;
sei();
PORTB &= ~(1<<PB7); // LED on
echo = ping();
u32tostr((uint8_t*)buf+1, cnt_copy);
u32tostr((uint8_t*)buf+1+8+1, mark_copy);
u32tostr((uint8_t*)buf+1+8+1+8+1, echo);
// error indicator - inverted input
buf[28] = (PINB & (1<<PB0)) ? '0' : '1';
if(buf[28]&1) {
PORTB &= ~(1<<PB4); // ERROR-LED on
}
else {
PORTB |= (1<<PB4); // ERROR-LED off
}
buf[30]=0;
cksum((uint8_t *)buf);
uart_send(buf);
/* eeprom write pending? */
if(ee_timer) {
ee_timer--;
if(!ee_timer)
write_eeprom(cnt_copy);
}
else if(evts_copy & EVT_CNTR)
ee_timer = EE_WRDIV; // counter changed -> schedule eeprom write
PORTB |= (1<<PB7); // LED off
}
else
sei();
sleep_mode();
}
return 0;
}