annotate testavr.c @ 14:a9d493261c6c

- Add $Id$ - Rearrange slightly.
author darius
date Sat, 17 Sep 2005 18:02:28 +0930
parents 2c03ec600dbc
children 0940abdf2b9d
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/signal.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
34 #include <avr/pgmspace.h>
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
35 #include <string.h>
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
36 #include <ctype.h>
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
37 #include <stdlib.h>
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
38
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
39 #include "1wire.h"
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
40
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
41 #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
42 #define UART_BAUD_RATE 19200
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
43
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
44 void uart_putsP(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
45 void uart_puts(const char *addr);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
46 int uart_putc(char c);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
47 int uart_getc(void);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
48 void uart_puts_dec(uint8_t a, uint8_t l);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
49 void uart_puts_hex(uint8_t a);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
50
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
51 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
52 main(void) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
53 uint8_t ROM[8];
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
54 char cmdbuf[40];
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
55 int8_t i, arg;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
56
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
57 cli();
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
58
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
59 outp(0xfc, DDRC);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
60 outp(0x00, PORTC);
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
61
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
62 /* Init UART */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
63 outp(UART_BAUD_SELECT(UART_BAUD_RATE,XTAL_CPU), UBRR);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
64
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
65 /* Enable receiver and transmitter. Turn on transmit interrupts */
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
66 outp(BV(RXEN) | BV(TXEN), UCR);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
67
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
68 uart_putsP(PSTR("\n\r\n\r===============\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
69 "Inited!\n\r\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
70
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
71 while (1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
72 uart_putsP(PSTR("> "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
73 i = 0;
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 cmdbuf[i] = tolower(uart_getc());
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
76 if (cmdbuf[i] == '\n' || cmdbuf[i] == '\r')
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
77 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
78
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
79 /* Backspace */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
80 if (cmdbuf[i] == 010) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
81 if (i > 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
82 uart_putsP(PSTR("\010\040\010"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
83 i--;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
84 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
85 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
86 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
87
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
88 /* Anything unprintable just ignore it */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
89 if (!isprint(cmdbuf[i]))
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
90 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
91
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
92 uart_putc(cmdbuf[i]);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
93 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
94 if (i == sizeof(cmdbuf)) {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
95 uart_putsP(PSTR("\n\rLine too long"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
96 i = 0;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
97 break;
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
98 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
99 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
100 cmdbuf[i + 1] = '\0';
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
101 uart_putsP(PSTR("\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
102 if (i == 0)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
103 continue;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
104
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
105 if (cmdbuf[0] == '?') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
106 uart_putsP(PSTR("rs Reset and check for presence\n\r"
7
a940431af6f5 Add usage for sr command.
darius
parents: 4
diff changeset
107 "sr Search the bus for ROMs\n\r"
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
108 "re Read a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
109 "rb Read a byte\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
110 "wr bit Write a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
111 "wb byte Write a byte (hex)\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
112 "wc cmd [ROMID] Write command\n\r"
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
113 "te ROMID Read the temperature from a DS1820\n\r"));
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
114
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
115 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
116 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
117
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
118 if (i < 2)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
119 goto badcmd;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
120
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
121 if (cmdbuf[0] == 'r' && cmdbuf[1] == 's') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
122 uart_putsP(PSTR("Resetting... "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
123
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
124 if (OWTouchReset() == 1)
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
125 uart_putsP(PSTR("No presense pulse found\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
126 else
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
127 uart_putsP(PSTR("Presense pulse found\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
128 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'e') {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
129 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
130 uart_putsP(PSTR("Read a 1\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
131 else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
132 uart_putsP(PSTR("Read a 0\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
133 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
134 uart_putsP(PSTR("Read a 0x"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
135 uart_puts_hex(OWReadByte());
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
136 uart_putsP(PSTR("\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
137 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'r') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
138 arg = strtol(cmdbuf + 3, (char **)NULL, 10);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
139 OWWriteBit(arg);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
140 uart_putsP(PSTR("Wrote a "));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
141 if (arg)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
142 uart_putsP(PSTR("1\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
143 else
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
144 uart_putsP(PSTR("0\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
145 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'b') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
146 arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
147 OWWriteByte(arg);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
148 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
149 i = strlen(cmdbuf);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
150 if (i < 5) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
151 uart_putsP(PSTR("No arguments\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
152 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
153 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
154
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
155 arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
156 if (arg == 0) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
157 uart_putsP(PSTR("Unparseable command\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
158 continue;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
159 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
160
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
161 if (i == 5) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
162 OWSendCmd(NULL, arg);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
163 continue;
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
164 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
165
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
166 if (i < 29) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
167 uart_putsP(PSTR("Can't parse ROM ID\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
168 continue;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
169 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
170 for (i = 0; i < 8; i++)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
171 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
172
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
173 OWSendCmd(ROM, arg);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
174 } else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
175 uint8_t crc, buf[9];;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
176 int8_t temp;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
177 uint16_t tfrac;
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
178
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:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
265 uart_putsP(PSTR("Presense pulse, but no module found, bad module/cabling?\n\r"));
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:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
269 uart_putsP(PSTR("No presense pulse found\n\r"));
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);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
297 } else {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
298 badcmd:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
299 uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
300 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
301
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
302 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
303
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
304 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
305 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
306
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
307 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
308 uart_putc(char c) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
309 loop_until_bit_is_set(USR, UDRE);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
310 outp(c, UDR);
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 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
316 uart_putsP(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
317 char c;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
318
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
319 while ((c = PRG_RDB((unsigned short)addr++)))
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
320 uart_putc(c);
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_puts(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
325 while (*addr)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
326 uart_putc(*addr++);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
327 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
328
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
329 void
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
330 uart_puts_dec(uint8_t a, uint8_t l) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
331 char s[4];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
332
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
333 if (l && a < 10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
334 uart_putsP(PSTR("0"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
335 uart_puts(utoa(a, s, 10));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
336 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
337
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
338 void
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
339 uart_puts_hex(uint8_t a) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
340 char s[3];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
341
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
342 if (a < 0x10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
343 uart_putc('0');
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 uart_puts(utoa(a, s, 16));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
346 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
347
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
348 int
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
349 uart_getc(void) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
350 while (!(inp(USR) & 0x80))
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
351 ;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
352
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
353 return (inp(UDR));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
354 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
355