annotate testavr.c @ 17:a58b41b7d15c

Covert to a later version of avr-libc. Stuff like not using inb/outb, etc..
author darius
date Fri, 25 Nov 2005 21:45:51 +1030
parents 026dc24d85e0
children 108a703c39e6
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;
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
56 uint8_t crc, buf[9];;
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
57 int8_t temp;
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
58 uint16_t tfrac;
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
59
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
60 cli();
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
61
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
62 DDRC = 0xfc;
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
63 PORTC = 0x00;
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
64 DDRA = 0xff;
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
65
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
66 /* Init UART */
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
67 UBRR = UART_BAUD_SELECT(UART_BAUD_RATE,XTAL_CPU);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
68
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
69 /* 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
70 UCR = _BV(RXEN) | _BV(TXEN);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
71
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
72 uart_putsP(PSTR("\n\r\n\r===============\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
73 "Inited!\n\r\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
74
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
75 while (1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
76 uart_putsP(PSTR("> "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
77 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
78 while (1) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
79 cmdbuf[i] = tolower(uart_getc());
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
80 if (cmdbuf[i] == '\n' || cmdbuf[i] == '\r')
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
81 break;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
82
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
83 /* Backspace/Delete */
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
84 if (cmdbuf[i] == 0x08) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
85 if (i > 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
86 uart_putsP(PSTR("\010\040\010"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
87 i--;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
88 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
89 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
90 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
91
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
92 /* Anything unprintable just ignore it */
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
93 if (!isprint(cmdbuf[i]))
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
94 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
95
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
96 uart_putc(cmdbuf[i]);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
97 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
98 if (i == sizeof(cmdbuf)) {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
99 uart_putsP(PSTR("\n\rLine too long"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
100 i = 0;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
101 break;
4
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 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
104 cmdbuf[i + 1] = '\0';
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
105 uart_putsP(PSTR("\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
106 if (i == 0)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
107 continue;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
108
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
109 if (cmdbuf[0] == '?') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
110 uart_putsP(PSTR("rs Reset and check for presence\n\r"
7
a940431af6f5 Add usage for sr command.
darius
parents: 4
diff changeset
111 "sr Search the bus for ROMs\n\r"
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
112 "re Read a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
113 "rb Read a byte\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
114 "wr bit Write a bit\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
115 "wb byte Write a byte (hex)\n\r"
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
116 "wc cmd [ROMID] Write command\n\r"
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
117 "te ROMID Read the temperature from a DS1820\n\r"
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
118 "le byte Set the LED port\n\r"));
11
ccc39c9f320b Enable pretty LED command.
darius
parents: 10
diff changeset
119
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
120 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
121 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
122
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
123 if (i < 2)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
124 goto badcmd;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
125
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
126 if (cmdbuf[0] == 'r' && cmdbuf[1] == 's') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
127 uart_putsP(PSTR("Resetting... "));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
128
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
129 if (OWTouchReset() == 1)
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
130 uart_putsP(PSTR("No presence pulse found\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
131 else
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
132 uart_putsP(PSTR("Presence pulse found\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] == 'e') {
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
134 if (OWReadBit())
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
135 uart_putsP(PSTR("Read a 1\n\r"));
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
136 else
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
137 uart_putsP(PSTR("Read a 0\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
138 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
139 uart_putsP(PSTR("Read a 0x"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
140 uart_puts_hex(OWReadByte());
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
141 uart_putsP(PSTR("\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
142 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'r') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
143 arg = strtol(cmdbuf + 3, (char **)NULL, 10);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
144 OWWriteBit(arg);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
145 uart_putsP(PSTR("Wrote a "));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
146 if (arg)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
147 uart_putsP(PSTR("1\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
148 else
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
149 uart_putsP(PSTR("0\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
150 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'b') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
151 arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
152 OWWriteByte(arg);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
153 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
154 i = strlen(cmdbuf);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
155 if (i < 5) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
156 uart_putsP(PSTR("No arguments\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
157 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
158 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
159
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
160 arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
161 if (arg == 0) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
162 uart_putsP(PSTR("Unparseable command\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
163 continue;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
164 }
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
165
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
166 if (i == 5) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
167 OWSendCmd(NULL, arg);
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
168 continue;
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
169 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
170
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
171 if (i < 29) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
172 uart_putsP(PSTR("Can't parse ROM ID\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
173 continue;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
174 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
175 for (i = 0; i < 8; i++)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
176 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
177
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
178 OWSendCmd(ROM, arg);
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
179 } else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
180 if (strlen(cmdbuf) < 26) {
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
181 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
182 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
183 }
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
184
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
185 for (i = 0; i < 8; i++)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
186 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
187
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
188 if (ROM[0] != OW_FAMILY_TEMP) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
189 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
190 continue;
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
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
193 OWSendCmd(ROM, OW_CONVERTT_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
194 i = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
195 while (OWReadBit() == 0) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
196 i++;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
197 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
198 OWSendCmd(ROM, OW_RD_SCR_CMD);
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
199 crc = 0;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
200 for (i = 0; i < 9; i++) {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
201 buf[i] = OWReadByte();
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
202 if (i < 8)
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
203 OWCRC(buf[i], &crc);
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
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
206 if (crc != buf[8]) {
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
207 uart_putsP(PSTR("CRC mismatch\n\r"));
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
208 continue;
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
209 }
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
210
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
211 #if 0
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
212 uart_putsP(PSTR("temperature "));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
213 uart_puts_dec(temp >> 4, 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
214 uart_putsP(PSTR("."));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
215 uart_puts_dec((temp << 12) / 6553, 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
216 uart_putsP(PSTR("\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
217 #else
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
218 /* 0 Temperature LSB
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
219 * 1 Temperature MSB
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
220 * 2 Th
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
221 * 3 Tl
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
222 * 4 Reserved
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
223 * 5 Reserved
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
224 * 6 Count Remain
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
225 * 7 Count per C
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
226 * 8 CRC
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
227 */
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
228 #if 0
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
229 for (i = 0; i < 9; i++) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
230 uart_puts_dec(buf[i], 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
231 uart_putsP(PSTR("\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
232 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
233 #endif
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
234 temp = buf[0];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
235 if (buf[0] & 0x01)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
236 temp -= 256;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
237 temp >>= 1;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
238
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
239 tfrac = buf[7] - buf[6];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
240 tfrac *= (uint16_t)100;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
241 tfrac /= buf[7];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
242 tfrac += 75;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
243 if (tfrac < 100) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
244 temp--;
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
245 } else {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
246 tfrac -= 100;
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
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
249 if (temp < 0){
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
250 uart_putc('-');
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
251 uart_puts_dec(-temp, 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
252 } else
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
253 uart_puts_dec(temp, 0);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
254 uart_putsP(PSTR("."));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
255 uart_puts_dec(tfrac, 1);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
256 uart_putsP(PSTR("\n\r"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
257
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
258 #endif
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
259 } else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
260 memset(ROM, 0, 8);
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
261
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
262 i = OWFirst(ROM, 1, 0);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
263 do {
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
264 switch (i) {
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
265 case OW_BADWIRE:
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
266 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
267 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
268
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
269 case OW_NOPRESENCE:
16
026dc24d85e0 Spell presence correctly.
darius
parents: 15
diff changeset
270 uart_putsP(PSTR("No presence pulse found\n\r"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
271 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
272
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
273 case OW_BADCRC:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
274 uart_putsP(PSTR("Bad CRC\n\r"));
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
275 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
276
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
277 case OW_NOMODULES:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
278 case OW_FOUND:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
279 break;
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
280
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
281 default:
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
282 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
283 break;
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
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
286 if (i != OW_FOUND)
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
287 break;
13
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
288
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
289 for (i = 0; i < 7; i++) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
290 uart_puts_hex(ROM[i]);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
291 uart_putc(':');
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
292 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
293 uart_puts_hex(ROM[7]);
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
294 uart_putsP(PSTR("\n\r"));
10
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
295
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
296 i = OWNext(ROM, 1, 0);
eb1faf51968e - Add some useful return values to search functions.
darius
parents: 9
diff changeset
297 } while (1);
15
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
298 } else if (cmdbuf[0] == 'l' && cmdbuf[1] == 'e') {
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
299 crc = (uint8_t)strtol(cmdbuf + 3, (char **)NULL, 16);
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
300 PORTA = crc;
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
301 #if 1
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
302 uart_putsP(PSTR("LEDs set to 0x"));
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
303 uart_puts_hex(crc);
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
304 uart_putsP(PSTR("\n\r"));
0940abdf2b9d - Add LED commands.
darius
parents: 13
diff changeset
305 #endif
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
306 } else {
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
307 badcmd:
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
308 uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
0
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
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
313 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
314 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
315
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
316 int
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
317 uart_putc(char c) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
318 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
319 UDR = c;
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
320
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
321 return(0);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
322 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
323
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
324 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
325 uart_putsP(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
326 char c;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
327
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
328 while ((c = pgm_read_byte_near(addr++)))
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
329 uart_putc(c);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
330 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
331
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
332 void
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
333 uart_puts(const char *addr) {
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
334 while (*addr)
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
335 uart_putc(*addr++);
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
336 }
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
337
13
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_dec(uint8_t a, uint8_t l) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
340 char s[4];
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 (l && a < 10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
343 uart_putsP(PSTR("0"));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
344 uart_puts(utoa(a, s, 10));
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
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
347 void
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
348 uart_puts_hex(uint8_t a) {
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
349 char s[3];
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
350
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
351 if (a < 0x10)
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
352 uart_putc('0');
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
353
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
354 uart_puts(utoa(a, s, 16));
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
355 }
2c03ec600dbc Reduce object code size as much as possible.
darius
parents: 11
diff changeset
356
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
357 int
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
358 uart_getc(void) {
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
359 while (!USR & 0x80)
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
360 ;
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
361
17
a58b41b7d15c Covert to a later version of avr-libc. Stuff like not using inb/outb,
darius
parents: 16
diff changeset
362 return (UDR);
0
ffeab3c04e83 Initial revision
darius
parents:
diff changeset
363 }
4
81e2f85e02ce Implement a command driven 1 wire bus tool.
darius
parents: 0
diff changeset
364