annotate testavr.c @ 19:8dc98a293e43

Scrap our handle rolled loops and use AVR libc's macros instead.
author darius
date Mon, 12 Dec 2005 15:42:03 +1030
parents 108a703c39e6
children 2b0ed085b95b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
1 /*
9
7ed10c59ba06 Supply clock speed via the command line so 1wire-delay.h can use it.
darius
parents: 7
diff changeset
2 * Test various AVR bits and pieces
7ed10c59ba06 Supply clock speed via the command line so 1wire-delay.h can use it.
darius
parents: 7
diff changeset
3 *
7ed10c59ba06 Supply clock speed via the command line so 1wire-delay.h can use it.
darius
parents: 7
diff changeset
4 * $Id$
7ed10c59ba06 Supply clock speed via the command line so 1wire-delay.h can use it.
darius
parents: 7
diff changeset
5 *
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
6 * Copyright (c) 2004
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
7 * Daniel O'Connor <darius@dons.net.au>. All rights reserved.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
8 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
9 * Redistribution and use in source and binary forms, with or without
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
10 * modification, are permitted provided that the following conditions
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
11 * are met:
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
12 * 1. Redistributions of source code must retain the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
14 * 2. Redistributions in binary form must reproduce the above copyright
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
15 * notice, this list of conditions and the following disclaimer in the
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
16 * documentation and/or other materials provided with the distribution.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
17 *
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
18 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
21 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
28 * SUCH DAMAGE.
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
29 */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
30
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
31 #include <avr/io.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
32 #include <avr/interrupt.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
33 #include <avr/pgmspace.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
34 #include <string.h>
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
35 #include <ctype.h>
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
36 #include <stdlib.h>
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
37
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
38 #include "1wire.h"
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
39
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
40 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
41 #define UART_BAUD_RATE 19200
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
42
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
43 void uart_putsP(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
44 void uart_puts(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
45 int uart_putc(char c);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
46 int uart_getc(void);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
47 void uart_puts_dec(uint8_t a, uint8_t l);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
48 void uart_puts_hex(uint8_t a);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
49
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
50 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
51 main(void) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
52 uint8_t ROM[8];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
53 char cmdbuf[40];
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
54 int8_t i, arg;
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
55 uint8_t crc, buf[9];;
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
56 int8_t temp;
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
57 uint16_t tfrac;
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
58
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
59 cli();
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
60
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
61 DDRC = 0xfc;
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
62 PORTC = 0x00;
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
63 DDRA = 0xff;
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
64
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
65 /* Init UART */
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
66 UBRR = UART_BAUD_SELECT(UART_BAUD_RATE,XTAL_CPU);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
67
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
68 /* Enable receiver and transmitter. Turn on transmit interrupts */
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
69 UCR = _BV(RXEN) | _BV(TXEN);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
70
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
71 uart_putsP(PSTR("\n\r\n\r===============\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
72 "Inited!\n\r\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
73
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
74 while (1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
75 uart_putsP(PSTR("> "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
76 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
77 while (1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
78 cmdbuf[i] = tolower(uart_getc());
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
79 if (cmdbuf[i] == '\n' || cmdbuf[i] == '\r')
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
80 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
81
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
82 /* Backspace/Delete */
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
83 if (cmdbuf[i] == 0x08) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
84 if (i > 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
85 uart_putsP(PSTR("\010\040\010"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
86 i--;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
87 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
88 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
89 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
90
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
91 /* Anything unprintable just ignore it */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
92 if (!isprint(cmdbuf[i]))
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
93 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
94
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
95 uart_putc(cmdbuf[i]);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
96 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
97 if (i == sizeof(cmdbuf)) {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
98 uart_putsP(PSTR("\n\rLine too long"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
99 i = 0;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
100 break;
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
101 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
102 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
103 cmdbuf[i + 1] = '\0';
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
104 uart_putsP(PSTR("\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
105 if (i == 0)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
106 continue;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
107
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
108 if (cmdbuf[0] == '?') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
109 uart_putsP(PSTR("rs Reset and check for presence\n\r"
7
a940431af6f5 Add usage for sr command.
darius
parents: 4
diff changeset
110 "sr Search the bus for ROMs\n\r"
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
111 "re Read a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
112 "rb Read a byte\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
113 "wr bit Write a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
114 "wb byte Write a byte (hex)\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
115 "wc cmd [ROMID] Write command\n\r"
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
116 "te ROMID Read the temperature from a DS1820\n\r"
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
117 "le byte Set the LED port\n\r"));
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
118
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
119 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
120 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
121
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
122 if (i < 2)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
123 goto badcmd;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
124
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
125 if (cmdbuf[0] == 'r' && cmdbuf[1] == 's') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
126 uart_putsP(PSTR("Resetting... "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
127
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
128 if (OWTouchReset() == 1)
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
129 uart_putsP(PSTR("No presence pulse found\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
130 else
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
131 uart_putsP(PSTR("Presence pulse found\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
132 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'e') {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
133 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
134 uart_putsP(PSTR("Read a 1\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
135 else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
136 uart_putsP(PSTR("Read a 0\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
137 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
138 uart_putsP(PSTR("Read a 0x"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
139 uart_puts_hex(OWReadByte());
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
140 uart_putsP(PSTR("\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
141 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'r') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
142 arg = strtol(cmdbuf + 3, (char **)NULL, 10);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
143 OWWriteBit(arg);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
144 uart_putsP(PSTR("Wrote a "));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
145 if (arg)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
146 uart_putsP(PSTR("1\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
147 else
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
148 uart_putsP(PSTR("0\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
149 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'b') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
150 arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
151 OWWriteByte(arg);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
152 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
153 i = strlen(cmdbuf);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
154 if (i < 5) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
155 uart_putsP(PSTR("No arguments\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
156 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
157 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
158
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
159 arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
160 if (arg == 0) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
161 uart_putsP(PSTR("Unparseable command\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
162 continue;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
163 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
164
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
165 if (i == 5) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
166 OWSendCmd(NULL, arg);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
167 continue;
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
168 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
169
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
170 if (i < 29) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
171 uart_putsP(PSTR("Can't parse ROM ID\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
172 continue;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
173 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
174 for (i = 0; i < 8; i++)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
175 ROM[i] = (int)strtol(cmdbuf + 6 + (3 * i), (char **)NULL, 16);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
176
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
177 OWSendCmd(ROM, arg);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
178 } else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
179 if (strlen(cmdbuf) < 26) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
180 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
181 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
182 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
183
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
184 for (i = 0; i < 8; i++)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
185 ROM[i] = (int)strtol(cmdbuf + 3 * (i + 1), (char **)NULL, 16);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
186
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
187 if (ROM[0] != OW_FAMILY_TEMP) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
188 uart_putsP(PSTR("ROM specified isn't a temperature sensor\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
189 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
190 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
191
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
192 OWSendCmd(ROM, OW_CONVERTT_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
193 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
194 while (OWReadBit() == 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
195 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
196 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
197 OWSendCmd(ROM, OW_RD_SCR_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
198 crc = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
199 for (i = 0; i < 9; i++) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
200 buf[i] = OWReadByte();
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
201 if (i < 8)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
202 OWCRC(buf[i], &crc);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
203 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
204
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
205 if (crc != buf[8]) {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
206 uart_putsP(PSTR("CRC mismatch\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
207 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
208 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
209
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
210 #if 0
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
211 uart_putsP(PSTR("temperature "));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
212 uart_puts_dec(temp >> 4, 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
213 uart_putsP(PSTR("."));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
214 uart_puts_dec((temp << 12) / 6553, 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
215 uart_putsP(PSTR("\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
216 #else
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
217 /* 0 Temperature LSB
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
218 * 1 Temperature MSB
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
219 * 2 Th
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
220 * 3 Tl
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
221 * 4 Reserved
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
222 * 5 Reserved
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
223 * 6 Count Remain
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
224 * 7 Count per C
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
225 * 8 CRC
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
226 */
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
227 #if 0
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
228 for (i = 0; i < 9; i++) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
229 uart_puts_dec(buf[i], 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
230 uart_putsP(PSTR("\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
231 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
232 #endif
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
233 temp = buf[0];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
234 if (buf[0] & 0x01)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
235 temp -= 256;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
236 temp >>= 1;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
237
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
238 tfrac = buf[7] - buf[6];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
239 tfrac *= (uint16_t)100;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
240 tfrac /= buf[7];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
241 tfrac += 75;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
242 if (tfrac < 100) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
243 temp--;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
244 } else {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
245 tfrac -= 100;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
246 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
247
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
248 if (temp < 0){
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
249 uart_putc('-');
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
250 uart_puts_dec(-temp, 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
251 } else
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
252 uart_puts_dec(temp, 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
253 uart_putsP(PSTR("."));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
254 uart_puts_dec(tfrac, 1);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
255 uart_putsP(PSTR("\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
256
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
257 #endif
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
258 } else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
259 memset(ROM, 0, 8);
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
260
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
261 i = OWFirst(ROM, 1, 0);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
262 do {
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
263 switch (i) {
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
264 case OW_BADWIRE:
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
265 uart_putsP(PSTR("Presence pulse, but no module found, bad module/cabling?\n\r"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
266 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
267
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
268 case OW_NOPRESENCE:
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
269 uart_putsP(PSTR("No presence pulse found\n\r"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
270 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
271
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
272 case OW_BADCRC:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
273 uart_putsP(PSTR("Bad CRC\n\r"));
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
274 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
275
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
276 case OW_NOMODULES:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
277 case OW_FOUND:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
278 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
279
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
280 default:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
281 uart_putsP(PSTR("Unknown error from 1 wire library\n\r"));
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
282 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
283 }
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
284
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
285 if (i != OW_FOUND)
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
286 break;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
287
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
288 for (i = 0; i < 7; i++) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
289 uart_puts_hex(ROM[i]);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
290 uart_putc(':');
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
291 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
292 uart_puts_hex(ROM[7]);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
293 uart_putsP(PSTR("\n\r"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
294
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
295 i = OWNext(ROM, 1, 0);
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
296 } while (1);
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
297 } else if (cmdbuf[0] == 'l' && cmdbuf[1] == 'e') {
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
298 crc = (uint8_t)strtol(cmdbuf + 3, (char **)NULL, 16);
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
299 PORTA = crc;
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
300 #if 1
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
301 uart_putsP(PSTR("LEDs set to 0x"));
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
302 uart_puts_hex(crc);
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
303 uart_putsP(PSTR("\n\r"));
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
304 #endif
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
305 } else {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
306 badcmd:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
307 uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
308 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
309
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
310 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
311
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
312 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
313 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
314
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
315 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
316 uart_putc(char c) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
317 loop_until_bit_is_set(USR, UDRE);
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
318 UDR = c;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
319
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
320 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
321 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
322
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
323 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
324 uart_putsP(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
325 char c;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
326
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
327 while ((c = pgm_read_byte_near(addr++)))
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
328 uart_putc(c);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
329 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
330
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
331 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
332 uart_puts(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
333 while (*addr)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
334 uart_putc(*addr++);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
335 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
336
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
337 void
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
338 uart_puts_dec(uint8_t a, uint8_t l) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
339 char s[4];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
340
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
341 if (l && a < 10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
342 uart_putsP(PSTR("0"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
343 uart_puts(utoa(a, s, 10));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
344 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
345
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
346 void
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
347 uart_puts_hex(uint8_t a) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
348 char s[3];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
349
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
350 if (a < 0x10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
351 uart_putc('0');
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
352
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
353 uart_puts(utoa(a, s, 16));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
354 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
355
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
356 int
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
357 uart_getc(void) {
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
358 while (!USR & 0x80)
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
359 ;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
360
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
361 return (UDR);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
362 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
363