-
Notifications
You must be signed in to change notification settings - Fork 0
/
usb.c
429 lines (328 loc) · 8.88 KB
/
usb.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
USB interface for temperature probe with DS18X20 from Dallas Semiconductor
*/
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include "onewire/onewire.h"
#include "usbdrv/usbdrv.h"
/* State mashine states */
typedef enum { DETECT, CONFIG, MEASURE, ACQUIRE, IDLE } sensor_state_t;
/* Onboard LED */
#define LED_G_PIN PC0
#define LED_R_PIN PC1
#define LED_OUT PORTC
#define LED_DDR DDRC
/* One wire configuration */
#define OW_PIN PB3
#define OW_IN &PINB
#define OW_OUT &PORTB
#define OW_DDR &DDRB
/* DS18X20 configuration */
#define DS18X20_SP_SIZE 9
#define DS18X20_C_SIZE 3
#define DS18X20_CONVERT_T 0x44
#define DS18X20_READ_T 0xbe
#define DS18X20_WRITE_C 0x4e
#define DS18X20_SAVE_C 0x48
#define DS18X20_CONFIG_REG 4
#define DS18S20_ID 0x10
#define DS18B20_ID 0x28
/* USB commands */
#define USB_MEASURE 1
#define USB_READ_TEMP 2
#define USB_READ_NUMBER 3 /* Read number of sensors. */
#define USB_READ_ROM 4
#define USB_DETECT 5
#define USB_PRECISION 6
#define USB_SETMODE 7
#define USB_GETMODE 8
#define USB_POKE 9
#define USB_PEEK 10
#define USB_PRECISION_GET 61
/* Main loop delay */
#define MAIN_DELAY_MS 2
/* Repeat measurements every 10 seconds. */
#define IDLE_INTERVAL_MS 10000
/* Wait 750 ms before acquiring. */
#define ACQUIRE_INTERVAL_MS 750
#define MAXSENSORS 5
/* Storage */
uint8_t id_sensor[MAXSENSORS][OW_ROMCODE_SIZE];
uint8_t sp_sensor[MAXSENSORS][DS18X20_SP_SIZE];
/* Number of attached sensors */
uint8_t nr_sensors;
uint8_t cur_sensor;
/* Current operating mode */
uint8_t mode;
/* State mashine */
sensor_state_t state;
/* USB return message */
static uchar replyBuf[DS18X20_SP_SIZE + 1] = { 0 };
/* USB HID report descriptor */
PROGMEM const char usbHidReportDescriptor[22] = {
0x05, 0x0c,
0x0a, 0x05, 0x01,
0xa1, 0x01,
0x0a, 0x05, 0x01,
0x15, 0x00,
0x26, 0xff, 0x00,
0x75, 0x08,
0x95, 0x10,
0xb1, 0x02,
0xc0
};
/* == Procedures == */
inline void led_G_On(void) {
LED_OUT &= ~_BV(LED_G_PIN);
}
inline void led_G_Off(void) {
LED_OUT |= _BV(LED_G_PIN);
}
inline void led_R_On(void) {
LED_OUT &= ~_BV(LED_R_PIN);
}
inline void led_R_Off(void) {
LED_OUT |= _BV(LED_R_PIN);
}
inline void schedule_detection(void) {
state = DETECT;
/* Skip part A */
nr_sensors = 0;
}
inline void schedule_reconfiguration(uint8_t sensor_id) {
state = CONFIG;
/* Go to part A */
cur_sensor = sensor_id;
}
uint8_t detect_sensors(void) {
/* Detect connected sensors, save their ROM to id_sensor and return number of detected sensors. */
uint8_t i;
uint8_t id[OW_ROMCODE_SIZE];
uint8_t cur_sensor;
uint8_t diff;
cur_sensor = 0;
for ( diff = OW_SEARCH_FIRST; diff != OW_LAST_DEVICE && cur_sensor < MAXSENSORS; ) {
diff = ow_rom_search( diff, &id[0] );
if ( diff == OW_PRESENCE_ERR || diff == OW_DATA_ERR )
break;
if ( id[0] == DS18B20_ID || id[0] == DS18S20_ID ) {
/* Count only DS18X20 sensors. */
for (i = 0; i < OW_ROMCODE_SIZE; i++ )
/* Copy ROM. */
id_sensor[cur_sensor][i] = id[i];
cur_sensor++;
}
}
return cur_sensor;
}
USB_PUBLIC uchar usbFunctionSetup(uchar data[8]) {
uint8_t sensor_id;
uint8_t i;
usbRequest_t *rq = (void *)data;
usbMsgPtr = (usbMsgPtr_t)replyBuf;
if ( (rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_VENDOR ) {
/* Handle our requests. */
switch ( rq->bRequest ) {
case USB_MEASURE:
if ( state == IDLE ) {
state = MEASURE;
replyBuf[0] = 1;
}
else
replyBuf[0] = 0;
return 1;
case USB_READ_TEMP:
sensor_id = rq->wValue.bytes[0];
if ( sensor_id >= nr_sensors )
return 0;
for ( i = 0; i < DS18X20_SP_SIZE; i++ )
/* Copy scratchpad. */
replyBuf[i] = sp_sensor[sensor_id][i];
return DS18X20_SP_SIZE;
case USB_READ_NUMBER:
replyBuf[0] = nr_sensors;
return 1;
case USB_READ_ROM:
sensor_id = rq->wValue.bytes[0];
if ( sensor_id >= nr_sensors )
return 0;
for ( i = 0; i < OW_ROMCODE_SIZE; i++ )
/* Copy ROM. */
replyBuf[i] = id_sensor[sensor_id][i];
return OW_ROMCODE_SIZE;
case USB_DETECT:
schedule_detection();
replyBuf[0] = 1;
return 1;
/*
Precision is represented by 6th and 7th bit in DS18X20_CONFIG_REG byte.
*/
case USB_PRECISION_GET:
sensor_id = rq->wValue.bytes[0] & 0x1f;
if (sensor_id >= nr_sensors)
return 0;
if (sp_sensor[sensor_id][DS18X20_CONFIG_REG] & 0x1f == 0x1f) {
replyBuf[0] = (sp_sensor[sensor_id][DS18X20_CONFIG_REG] >> 5);
return 1;
}
return 0;
case USB_PRECISION:
sensor_id = rq->wValue.bytes[0] & 0x1f;
if ( sensor_id >= nr_sensors )
return 0;
sp_sensor[sensor_id][DS18X20_CONFIG_REG] = ((rq->wValue.bytes[0] >> 1) & 0x60) | 0x1f;
schedule_reconfiguration(sensor_id);
replyBuf[0] = sp_sensor[sensor_id][DS18X20_CONFIG_REG];
return 1;
case USB_SETMODE:
mode = rq->wValue.bytes[0] & 0x01;
case USB_GETMODE:
replyBuf[0] = mode;
return 1;
case USB_POKE:
sensor_id = rq->wValue.bytes[0];
if ( sensor_id >= nr_sensors )
return 0;
sp_sensor[sensor_id][2] = rq->wIndex.bytes[0];
sp_sensor[sensor_id][3] = rq->wIndex.bytes[1];
schedule_reconfiguration(sensor_id);
case USB_PEEK:
sensor_id = rq->wValue.bytes[0];
if ( sensor_id >= nr_sensors )
return 0;
replyBuf[0] = sp_sensor[sensor_id][2];
replyBuf[1] = sp_sensor[sensor_id][3];
return 2;
}
}
else if ( (rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS ) {
if ( rq->bRequest == USBRQ_HID_GET_REPORT ) {
for ( i = 0; i < 2; i++ )
replyBuf[i] = sp_sensor[0][i];
return 2;
}
}
return 0;
}
#define COUNTER_RESET \
delay_counter = 0; \
cur_sensor = 0;
int main() {
uint8_t i;
uint32_t delay_counter;
/* Enable watchdog timer with 1 second. */
wdt_enable(WDTO_1S);
/* Set LED pin as output. */
LED_DDR = _BV(LED_G_PIN) | _BV(LED_R_PIN);
led_G_Off();
led_R_Off();
/* Initialize one wire interface. */
ow_set_bus(OW_IN, OW_OUT, OW_DDR, OW_PIN);
/* Initialize the driver. */
usbInit();
usbDeviceDisconnect();
for ( i = 0; i < 250; i++ ) {
wdt_reset();
_delay_ms(2);
}
usbDeviceConnect();
/* Enable global interrupts. */
sei();
/* Starting state. */
schedule_detection();
COUNTER_RESET
mode = 1;
while (1) {
/* Reset the watchdog timer. */
wdt_reset();
/* Poll USB data. */
usbPoll();
/*
if ( usbInterruptIsReady() ) {
usbSetInterrupt((void *)&sp_sensor[0][0], 2);
}
*/
if ( cur_sensor < nr_sensors ) {
/* PART A */
switch ( state ) {
#ifdef SEPARATE_MEASURE
/* state MEASURE */
case ACQUIRE:
ow_command(DS18X20_CONVERT_T, &id_sensor[cur_sensor][0]);
break;
#endif
/* state ACQUIRE */
case IDLE:
_delay_ms(1);
delay_counter += 1;
ow_command(DS18X20_READ_T, &id_sensor[cur_sensor][0]);
for ( i = 0; i < DS18X20_SP_SIZE; i++ )
/* Copy scratchpad */
sp_sensor[cur_sensor][i] = ow_byte_rd();
break;
case CONFIG:
ow_command(DS18X20_WRITE_C, &id_sensor[cur_sensor][0]);
for ( i = 0; i < DS18X20_C_SIZE; i++ )
ow_byte_wr(sp_sensor[cur_sensor][i + 2]);
ow_command(DS18X20_SAVE_C, &id_sensor[cur_sensor][0]);
/* Skip others. */
cur_sensor = nr_sensors;
/* Set next state */
state = IDLE;
break;
default:
break;
}
cur_sensor++;
}
else {
/* PART B */
if ( state == DETECT ) {
/* Search for connected sensors. */
led_R_On();
nr_sensors = detect_sensors();
led_R_Off();
/* Set next state. */
state = mode ? IDLE : MEASURE;
/* Skip part A. */
cur_sensor = nr_sensors;
/* Do not apply delay. */
continue;
}
else if ( state == MEASURE ) {
/* Start measuring. */
led_G_On();
#ifndef SEPARATE_MEASURE
ow_command(DS18X20_CONVERT_T, 0);
#else
/* Go to part A. */
cur_sensor = 0;
#endif
/* Set next state. */
state = ACQUIRE;
delay_counter = 0;
}
else if ( state == ACQUIRE && delay_counter > ACQUIRE_INTERVAL_MS ) {
/* Start acquiring data. */
led_G_Off();
/* Set next state. */
state = IDLE;
COUNTER_RESET
}
else if ( state == IDLE && delay_counter > IDLE_INTERVAL_MS ) {
/* Restart. */
state = MEASURE;
delay_counter = 0;
}
/* In mode 1 stay in IDLE state */
if ( !mode || state != IDLE )
delay_counter += MAIN_DELAY_MS;
/* Apply delay */
_delay_ms(MAIN_DELAY_MS);
}
}
return 0;
}