-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
362 lines (339 loc) · 8.83 KB
/
main.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
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "uart.h"
#include "onewire.h"
#include "ds18x20.h"
#define BAUD 9600
#define CMD_ON 'O'
#define CMD_OUTPUT 'o'
#define CMD_INPUT 'i'
#define CMD_OFF 'F'
#define CMD_SCAN 'S'
#define CMD_READ_ALL 'R'
#define CMD_CHECK 'C'
#define CMD_OUTPUT_STATE 'A'
#define EEPROM_PORTA_STATE (uint8_t*)0x10
#define EEPROM_PORTB_STATE (uint8_t*)0x11
#define EEPROM_PORTC_STATE (uint8_t*)0x12
#define EEPROM_PORTD_STATE (uint8_t*)0x13
#define EEPROM_DDRA_STATE (uint8_t*)0x20
#define EEPROM_DDRB_STATE (uint8_t*)0x21
#define EEPROM_DDRC_STATE (uint8_t*)0x22
#define EEPROM_DDRD_STATE (uint8_t*)0x23
#define MAXSENSORS 7
char buffer[UART_TX_BUFFER_SIZE];
uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE];
uint8_t nSensors;
volatile uint8_t last_command;
//Scan bus for sensors
static uint8_t
search_sensors (void)
{
uint8_t i;
uint8_t id[OW_ROMCODE_SIZE];
uint8_t diff, nSensors;
ow_reset ();
nSensors = 0;
diff = OW_SEARCH_FIRST;
while (diff != OW_LAST_DEVICE && nSensors < MAXSENSORS) {
DS18X20_find_sensor (&diff, &id[0]);
if (diff == OW_PRESENCE_ERR) {
break;
}
if (diff == OW_DATA_ERR) {
break;
}
for (i = 0; i < OW_ROMCODE_SIZE; i++)
gSensorIDs[nSensors][i] = id[i];
nSensors++;
}
return nSensors;
}
static void
uart_put_temp_maxres (int32_t tval)
{
char s[10];
DS18X20_format_from_maxres (tval, s, 10);
uart_puts (s);
}
static void
do_scan_ds1820 (void)
{
uart_puts_P ("+INFO Scanning...\r\n");
nSensors = search_sensors ();
sprintf (buffer, "+OK Found %d Sensors\r\n", nSensors);
uart_puts (buffer);
}
void
do_read_ds1820 (void)
{
if (!(nSensors > 0) || nSensors > 8) {
do_scan_ds1820 ();
}
if (DS18X20_start_meas (DS18X20_POWER_PARASITE, NULL) == DS18X20_OK) {
_delay_ms (DS18B20_TCONV_12BIT);
int32_t temp_eminus4;
uint8_t i;
for (i = 0; i < nSensors; i++) {
sprintf (buffer, "+TEMP %d %x%x%x%x%x%x%x%x ", i,
gSensorIDs[i][0], gSensorIDs[i][1], gSensorIDs[i][2],
gSensorIDs[i][3], gSensorIDs[i][4], gSensorIDs[i][5],
gSensorIDs[i][6], gSensorIDs[i][7]);
uart_puts (buffer);
if (DS18X20_read_maxres (&gSensorIDs[i][0], &temp_eminus4)
== DS18X20_OK) {
uart_put_temp_maxres (temp_eminus4);
uart_puts_P ("\r\n");
}
else {
uart_puts_P ("-ERR CRC Error (lost connection?)\r\n");
}
}
}
else {
uart_puts_P ("-ERR Start meas. failed (short circuit?)\r\n");
}
}
void
handle_command (char *command)
{
if (command[0] == CMD_ON) {
if (command[1] == 'A') {
#ifdef PORTA
PORTA |= _BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_PORTA_STATE, PORTA);
#else
uart_puts_P ("-ERR Wrong PORT Selection\r\n");
#endif
}
else if (command[1] == 'B') {
PORTB |= _BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_PORTB_STATE, PORTB);
}
else if (command[1] == 'C') {
PORTC |= _BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_PORTC_STATE, PORTC);
}
else if (command[1] == 'D') {
PORTD |= _BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_PORTD_STATE, PORTD);
}
else {
uart_puts_P ("-ERR Wrong PORT Selection\r\n");
return;
}
uart_puts_P ("+OK Turning off output ");
uart_putc (command[2]);
uart_puts_P (" On Port ");
uart_putc (command[1]);
uart_puts_P ("\r\n");
}
else if (command[0] == CMD_OFF) {
if (command[1] == 'A') {
#ifdef PORTA
PORTA &= ~_BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_PORTA_STATE, PORTA);
#else
uart_puts_P ("-ERR Wrong PORT Selection\r\n");
#endif
}
else if (command[1] == 'B') {
PORTB &= ~_BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_PORTB_STATE, PORTB);
}
else if (command[1] == 'C') {
PORTC &= ~_BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_PORTC_STATE, PORTC);
}
else if (command[1] == 'D') {
PORTD &= ~_BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_PORTD_STATE, PORTD);
}
else {
uart_puts_P ("-ERR Wrong PORT Selection\r\n");
return;
}
uart_puts_P ("+OK Turning off output ");
uart_putc (command[2]);
uart_puts_P (" On Port ");
uart_putc (command[1]);
uart_puts_P ("\r\n");
}
else if (command[0] == CMD_OUTPUT) {
if (command[1] == 'A') {
#ifdef PORTA
DDRA |= _BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_DDRA_STATE, DDRA);
#else
uart_puts_P ("-ERR Wrong PORT Selection\r\n");
#endif
}
else if (command[1] == 'B') {
DDRB |= _BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_DDRB_STATE, DDRB);
}
else if (command[1] == 'C') {
DDRC |= _BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_DDRC_STATE, DDRC);
}
else if (command[1] == 'D') {
DDRD |= _BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_DDRD_STATE, DDRD);
}
else {
uart_puts_P ("-ERR Wrong PORT Selection\r\n");
return;
}
uart_puts_P ("+OK Set Channel ");
uart_putc (command[2]);
uart_puts_P (" On Port ");
uart_putc (command[1]);
uart_puts_P (" To Output\r\n");
}
else if (command[0] == CMD_INPUT) {
if (command[1] == 'A') {
#ifdef PORTA
DDRA &= ~_BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_DDRA_STATE, DDRA);
#else
uart_puts_P ("-ERR Wrong PORT Selection\r\n");
#endif
}
else if (command[1] == 'B') {
DDRB &= ~_BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_DDRB_STATE, DDRB);
}
else if (command[1] == 'C') {
DDRC &= ~_BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_DDRC_STATE, DDRC);
}
else if (command[1] == 'D') {
DDRD &= ~_BV (command[2] - 0x30);
eeprom_write_byte (EEPROM_DDRD_STATE, DDRD);
}
else {
uart_puts_P ("-ERR Wrong PORT Selection\r\n");
return;
}
uart_puts_P ("+OK Set Channel ");
uart_putc (command[2]);
uart_puts_P (" On Port ");
uart_putc (command[1]);
uart_puts_P (" To Input\r\n");
}
else if (command[0] == CMD_SCAN) {
do_scan_ds1820 ();
}
else if (command[0] == CMD_READ_ALL) {
do_read_ds1820 ();
}
else if (command[0] == CMD_CHECK) {
uart_puts ("+OK Running\r\n");
}
else if (command[0] == CMD_OUTPUT_STATE) {
if (command[1] == 'A') {
#ifdef PORTA
sprintf (buffer, "+OK DDRA=%x PORTA=%x\r\n", DDRA, PORTA);
#else
uart_puts_P ("-ERR Unknown Command: ");
#endif
}
else if (command[1] == 'B') {
sprintf (buffer, "+OK DDRB=%x PORTB=%x\r\n", DDRB, PORTB);
uart_puts (buffer);
}
else if (command[1] == 'C') {
sprintf (buffer, "+OK DDRC=%x PORTC=%x\r\n", DDRC, PORTC);
uart_puts (buffer);
}
else if (command[1] == 'D') {
sprintf (buffer, "+OK DDRD=%x PORTD=%x\r\n", DDRD, PORTD);
uart_puts (buffer);
}
else {
uart_puts_P ("-ERR Wrong PORT Selection (OUTPUT_STATE)\r\n");
return;
}
}
else {
uart_puts_P ("-ERR Unknown Command: ");
uart_puts (command);
uart_puts_P ("\r\n");
}
}
void
setup_timer (void)
{
DDRC |= (1 << 5); // Set LED as output
TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
TIMSK1 |= (1 << OCIE1A); // Enable CTC interrupt
OCR1A = 46875; // Set CTC compare value to 1Hz at 12MHz AVR clock, with a prescaler of 256
TCCR1B |= ((1 << CS12) | (0 << CS11) | (0 << CS10)); // Start timer at Fcpu/256
}
ISR (TIMER1_COMPA_vect)
{
PORTC ^= (1 << 5); // Toggle the LED
if (last_command > 100) {
uart_puts_P ("+INFO Keep-Alive\r\n");
}
}
void load_eeprom(void) {
#ifdef PORTA
DDRA = eeprom_read_byte(EEPROM_DDRA_STATE);
PORTA = eeprom_read_byte(EEPROM_PORTA_STATE);
#endif
DDRB = eeprom_read_byte(EEPROM_DDRA_STATE);
PORTB = eeprom_read_byte(EEPROM_PORTB_STATE);
DDRC = eeprom_read_byte(EEPROM_DDRA_STATE);
PORTC = eeprom_read_byte(EEPROM_PORTC_STATE);
DDRD = eeprom_read_byte(EEPROM_DDRA_STATE);
PORTD = eeprom_read_byte(EEPROM_PORTD_STATE);
}
int
main (void)
{
unsigned int c;
uint8_t counter = 0;
char buf[8];
load_eeprom();
setup_timer ();
uart_init (UART_BAUD_SELECT (9600, F_CPU));
ow_set_bus (&PIND, &PORTD, &DDRD, PD6);
sei ();
uart_puts_P ("+INFO Booted!\r\n");
for (;;) { // main loop
c = uart_getc ();
if (c & UART_NO_DATA) {
//
}
else {
if (c & UART_FRAME_ERROR) {
uart_puts_P ("Frame Error: ");
}
if (c & UART_OVERRUN_ERROR) {
uart_puts_P ("Overrun Error: ");
}
if (c & UART_BUFFER_OVERFLOW) {
uart_puts_P ("Overflow Error: ");
}
if ((unsigned char) c == '\r' || (unsigned char) c == '\n') {
buf[counter] = '\0';
counter = 0;
handle_command (buf);
}
else if ((unsigned char) c > 0) {
buf[counter++] = (unsigned char) c;
if (counter == 8) {
memset (buf, 8, '\0');
counter = 0;
}
}
}
}
}