-
Notifications
You must be signed in to change notification settings - Fork 20
/
moat.c
338 lines (300 loc) · 6.49 KB
/
moat.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
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
/*
* Copyright © 2014-2015, Matthias Urlichs <matthias@urlichs.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License (included; see the file LICENSE)
* for more details.
*/
/* This code implements the main code of MoaT slaves.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "pgm.h"
#include <string.h>
#include "onewire.h"
#include "features.h"
#include "moat.h"
#include "dev_data.h"
#include "debug.h"
#include "moat_internal.h"
#include "port.h"
#include "pwm.h"
#include "count.h"
#include "console.h"
#include "timer.h"
#include "crc.h"
#define _1W_READ_GENERIC 0xF2
#define _1W_WRITE_GENERIC 0xF4
#ifdef IS_BOOTLOADER
#include "moat_dummy.c"
#endif
#ifdef IS_BOOTLOADER
ALIASDEFS(loader)
static const moat_call_t dispatch_loader __attribute__ ((progmem)) = FUNCPTRS(loader);
#endif
void end_transmission(uint16_t crc)
{
crc = ~crc;
xmit_byte(crc);
xmit_byte(crc >> 8);
{
uint16_t icrc;
recv_byte();
icrc = recv_byte_in();
recv_byte();
icrc |= recv_byte_in() << 8;
if (icrc != ~crc) {
DBG_P(" crc=");
DBG_W(crc);
DBG_P(" icrc=");
DBG_W(icrc);
DBG_C(' ');
next_idle('c');
}
// DBG_P("CRC OK ");
}
}
uint8_t moat_buf[MAXBUF];
#ifdef IS_BOOTLOADER
static const moat_call_t *dispatch;
static uint8_t tc_max;
#else
#define dispatch moat_calls
#define tc_max TC_MAX
#endif
// Inlining this code triggers a compiler bug
static void moat_read(void) __attribute__((noinline));
static void moat_read(void)
{
uint16_t crc = 0;
uint8_t dtype,chan;
uint8_t len;
uint8_t *bp=moat_buf;
const moat_call_t *mc;
read_len_fn *rlf;
read_fn *rf;
read_done_fn *rdf;
/*
Implement reading data. We read the header, write the length,
write the data, write the CRC, read the inverted CRC back, and then do
whatever necessary to effect the read (e.g. remove the read data from
a buffer, clear a flag, whatever).
*/
recv_byte();
crc = crc16(crc,_1W_READ_GENERIC);
dtype = recv_byte_in();
recv_byte();
crc = crc16(crc,dtype);
chan = recv_byte_in();
//DBG_C('0'+dtype);
#ifdef IS_BOOTLOADER
if (dtype == 0xFF) {
mc = &dispatch_loader;
} else
#endif
{
if (dtype >= tc_max)
next_idle('p');
mc = &dispatch[dtype];
}
rlf = pgm_read_ptr(&mc->read_len);
len = rlf(chan);
xmit_byte(len);
crc = crc16(crc,chan);
crc = crc16(crc,len);
rf = pgm_read_ptr(&mc->read);
rf(chan, moat_buf);
while(len--) {
xmit_byte(*bp);
crc = crc16(crc,*bp);
bp++;
}
end_transmission(crc);
rdf = pgm_read_ptr(&mc->read_done);
rdf(chan);
}
static void moat_write(void) __attribute__((noinline));
static void moat_write(void)
{
uint16_t crc = 0;
uint8_t dtype,chan;
uint8_t len;
const moat_call_t *mc;
write_check_fn *wfc;
write_fn *wf;
/*
Write data. We read the header, read the length, read the data,
write the resulting CRC, read the inverted CRC,
and then do whatever necessary to effect the write (e.g. clear a flag,
update a stored value, whatever).
*/
recv_byte();
crc = crc16(crc,_1W_WRITE_GENERIC);
dtype = recv_byte_in();
//DBG_C('W'); DBG_X(dtype);
recv_byte();
crc = crc16(crc,dtype);
chan = recv_byte_in();
recv_byte();
crc = crc16(crc,chan);
len = recv_byte_in();
recv_byte();
crc = crc16(crc,len);
crc = recv_bytes_crc(crc, moat_buf, len);
#ifdef IS_BOOTLOADER
if (dtype == 0xFF) {
mc = &dispatch_loader;
} else
#endif
{
if (dtype >= tc_max)
next_idle('W');
mc = &dispatch[dtype];
}
wfc = pgm_read_ptr(&mc->write_check);
wfc(chan,moat_buf,len);
end_transmission(crc);
wf = pgm_read_ptr(&mc->write);
wf(chan,moat_buf,len);
}
void moat_poll(void)
{
static uint8_t i = 0;
poll_fn *pf;
if (i >= tc_max) {
i = 0;
return;
// this makes sure that we do nothing if tc_max==0
}
pf = pgm_read_ptr(&dispatch[i].poll);
pf();
i++;
}
void moat_init(void)
{
uint8_t i;
#ifdef IS_BOOTLOADER
struct config_loader cl;
const moat_loader_t *loader;
#endif
const moat_call_t *mc;
init_fn *pf;
#ifdef IS_BOOTLOADER
loader = NULL;
tc_max = 0;
dispatch = NULL;
if (!cfg_read (loader, cl)) {
DBG_P("\nM:no cfg\n");
return;
}
loader = (moat_loader_t *)cl.loader;
if (! loader) {
DBG_P("\nM:no loader\n");
return;
}
if (pgm_read_byte(&loader->sig[0]) != 'M' ||
pgm_read_byte(&loader->sig[1]) != 'L') {
DBG_P("\nM:no sig ");
DBG_W((uint16_t)loader);
DBG_C('\n');
return;
}
// TODO: calculate and check CRC
tc_max = pgm_read_byte(&loader->n_types);
dispatch = pgm_read_ptr(&loader->calls);
DBG_P("\nM:has ");
DBG_X(tc_max);
DBG_C('\n');
pf = pgm_read_ptr(&loader->init);
pf();
#endif
mc = dispatch;
for(i=0;i<tc_max;i++,mc++) {
pf = pgm_read_ptr(&mc->init);
pf();
}
// Power reduction register allows us to turn off the clock to unused peripherals.
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega88__) || defined (__AVR_ATmega328__)
PRR =
(1 << PRTWI) // TWI not used at all
|(1 << PRSPI) // SPI not used at all
|(1 << PRTIM1) // Timer 1 not used at all
// Timer 2 is used for OW on Mega88
#ifndef HAVE_TIMER
|(1 << PRTIM0)
#endif
#ifndef HAVE_UART
|(1 << PRUSART0)
#endif
#if !defined(N_ADC)
|(1 << PRADC)
#endif
;
#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)\
|| defined (__AVR_ATtiny84__)
PRR =
(1 << PRTIM1) // Timer 1 not used at all
// Timer 2 is used for OW on these devices
#ifndef HAVE_UART
|(1 << PRUSI)
#endif
#if !defined(N_ADC)
|(1 << PRADC)
#endif
;
#endif
}
void do_command(uint8_t cmd)
{
if(cmd == _1W_READ_GENERIC) {
//DBG_P(":I");
moat_read();
} else if(cmd == _1W_WRITE_GENERIC) {
//DBG_P(":I");
moat_write();
} else {
DBG(0x0E);
DBG_P("?CI ");
DBG_X(cmd);
set_idle();
}
}
void update_idle(uint8_t bits)
{
if(bits < 8)
return;
moat_poll();
}
#if CONSOLE_PING
timer_t t;
#endif
void init_state(void)
{
#if CONSOLE_PING
timer_start(CONSOLE_PING,&t);
#endif
moat_init();
}
#ifdef CONDITIONAL_SEARCH
uint8_t moat_alert_present;
uint8_t condition_met(void) {
return moat_alert_present;
}
#endif
void mainloop(void) {
DBG(0x1E);
moat_poll();
#if CONSOLE_PING
if(timer_done(&t)) {
console_putc('!');
timer_start(CONSOLE_PING,&t);
}
#endif
}