comparison testavr.c @ 13:2c03ec600dbc OWIRE_1_3

Reduce object code size as much as possible.
author darius
date Wed, 01 Sep 2004 17:46:15 +0930
parents ccc39c9f320b
children 0940abdf2b9d
comparison
equal deleted inserted replaced
12:4b141cc7cbd4 13:2c03ec600dbc
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE. 28 * SUCH DAMAGE.
29 */ 29 */
30 30
31 #include <stdio.h>
32 #include <avr/io.h> 31 #include <avr/io.h>
33 #include <avr/interrupt.h> 32 #include <avr/interrupt.h>
34 #include <avr/signal.h> 33 #include <avr/signal.h>
35 #include <avr/pgmspace.h> 34 #include <avr/pgmspace.h>
36 #include <string.h> 35 #include <string.h>
37 #include <ctype.h> 36 #include <ctype.h>
37 #include <stdlib.h>
38 38
39 #include "1wire.h" 39 #include "1wire.h"
40 40
41 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1) 41 #define UART_BAUD_SELECT(baudRate,xtalCpu) ((xtalCpu)/((baudRate)*16l)-1)
42 #define UART_BAUD_RATE 19200 42 #define UART_BAUD_RATE 19200
43 43
44 void uart_putsP(const char *addr); 44 void uart_putsP(const char *addr);
45 void uart_puts(const char *addr); 45 void uart_puts(const char *addr);
46 int uart_putc(char c); 46 int uart_putc(char c);
47 int uart_getc(void); 47 int uart_getc(void);
48 int beginswith_P(const char *s1, const char *s2); 48 void uart_puts_dec(uint8_t a, uint8_t l);
49 49 void uart_puts_hex(uint8_t a);
50 uint8_t PROGMEM ledvals[] = {0x01, 0x03, 0x07, 0x0e, 0x1c, 0x38, 0x70, 0xe0, 0xc0, 0x80};
51 static uint8_t dir = 0;
52 static volatile uint8_t leds = 0;
53 static uint8_t ledpos = 0;
54
55 INTERRUPT(SIG_OVERFLOW0) {
56 if (!leds)
57 return;
58
59 /* Going up */
60 if (dir == 0) {
61 if (ledpos == 9) {
62 dir = 1;
63 TCNT0 = 0;
64 goto doleds;
65 }
66
67 ledpos++;
68 } else {
69 if (ledpos == 0) {
70 dir = 0;
71 TCNT0 = 0;
72 goto doleds;
73 }
74
75 ledpos--;
76 }
77
78 doleds:
79 TCNT0 = 0;
80
81 PORTA = pgm_read_byte(&ledvals[ledpos]);
82 }
83
84 void
85 usleep(uint16_t usec) {
86 /* 4Mhz = 250ns per clock cycle */
87 usec /= 2;
88 if (usec < 1)
89 return;
90
91 while (usec--)
92 asm volatile (
93 ""
94 ::);
95 }
96 50
97 int 51 int
98 main(void) { 52 main(void) {
99 uint8_t ROM[8]; 53 uint8_t ROM[8];
100 char cmdbuf[40]; 54 char cmdbuf[40];
101 int i; 55 int8_t i, arg;
102 56
103 cli(); 57 cli();
104 outp(0xff, DDRA);
105
106 /* Timer Clock divisor - CK/1024 */
107 outp(BV(CS00) | BV(CS02), TCCR0);
108 58
109 outp(0xfc, DDRC); 59 outp(0xfc, DDRC);
110 outp(0x00, PORTC); 60 outp(0x00, PORTC);
111 61
112 /* Init UART */ 62 /* Init UART */
113 outp(UART_BAUD_SELECT(UART_BAUD_RATE,XTAL_CPU), UBRR); 63 outp(UART_BAUD_SELECT(UART_BAUD_RATE,XTAL_CPU), UBRR);
114 64
115 /* Enable receiver and transmitter. Turn on transmit interrupts */ 65 /* Enable receiver and transmitter. Turn on transmit interrupts */
116 outp(BV(RXEN) | BV(TXEN), UCR); 66 outp(BV(RXEN) | BV(TXEN), UCR);
117 67
118 #if 0
119 int was;
120 uint32_t count;
121
122 was = inp(PINC) & 0x02;
123
124 count = 0;
125 while (1) {
126 if (was) {
127 if (!(inp(PINC) & 0x02)) {
128 sprintf_P(cmdbuf, PSTR("high -> low, i = %d\n\r"), count);
129 uart_puts(cmdbuf);
130 count = 0;
131 was = 0x00;
132 }
133 } else {
134 if ((inp(PINC) & 0x02)) {
135 sprintf_P(cmdbuf, PSTR("low -> high, i = %d\n\r"), count);
136 uart_puts(cmdbuf);
137 count = 0;
138 was = 0x02;
139 }
140 }
141
142 count++;
143 }
144 #endif
145
146 uart_putsP(PSTR("\n\r\n\r===============\n\r" 68 uart_putsP(PSTR("\n\r\n\r===============\n\r"
147 "Inited!\n\r\n\r")); 69 "Inited!\n\r\n\r"));
148 70
149 while (1) { 71 while (1) {
150 uart_putsP(PSTR("> ")); 72 uart_putsP(PSTR("> "));
168 continue; 90 continue;
169 91
170 uart_putc(cmdbuf[i]); 92 uart_putc(cmdbuf[i]);
171 i++; 93 i++;
172 if (i == sizeof(cmdbuf)) { 94 if (i == sizeof(cmdbuf)) {
173 uart_putsP(PSTR("\n\rLine too long\n\r")); 95 uart_putsP(PSTR("\n\rLine too long"));
174 i = 0; 96 i = 0;
175 continue; 97 break;
176 } 98 }
177 } 99 }
178 cmdbuf[i + 1] = '\0'; 100 cmdbuf[i + 1] = '\0';
179 uart_putsP(PSTR("\n\r")); 101 uart_putsP(PSTR("\n\r"));
180 if (i == 0) 102 if (i == 0)
186 "re Read a bit\n\r" 108 "re Read a bit\n\r"
187 "rb Read a byte\n\r" 109 "rb Read a byte\n\r"
188 "wr bit Write a bit\n\r" 110 "wr bit Write a bit\n\r"
189 "wb byte Write a byte (hex)\n\r" 111 "wb byte Write a byte (hex)\n\r"
190 "wc cmd [ROMID] Write command\n\r" 112 "wc cmd [ROMID] Write command\n\r"
191 "te ROMID Read the temperature from a DS1820\n\r" 113 "te ROMID Read the temperature from a DS1820\n\r"));
192 "le Turn pretty LED cycling on\n\r"));
193 114
194 continue; 115 continue;
195 } 116 }
196 117
197 if (i < 2) 118 if (i < 2)
208 if (OWReadBit()) 129 if (OWReadBit())
209 uart_putsP(PSTR("Read a 1\n\r")); 130 uart_putsP(PSTR("Read a 1\n\r"));
210 else 131 else
211 uart_putsP(PSTR("Read a 0\n\r")); 132 uart_putsP(PSTR("Read a 0\n\r"));
212 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') { 133 } else if (cmdbuf[0] == 'r' && cmdbuf[1] == 'b') {
213 sprintf_P(cmdbuf, PSTR("Read a 0x%02x\n\r"), OWReadByte()); 134 uart_putsP(PSTR("Read a 0x"));
214 uart_puts(cmdbuf); 135 uart_puts_hex(OWReadByte());
136 uart_putsP(PSTR("\n\r"));
215 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'r') { 137 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'r') {
216 int arg; 138 arg = strtol(cmdbuf + 3, (char **)NULL, 10);
217
218 if (sscanf_P(cmdbuf + 3, PSTR("%d"), &arg) != 1) {
219 uart_putsP(PSTR("Unable to parse wr command\n\r"));
220 continue;
221 }
222
223 OWWriteBit(arg); 139 OWWriteBit(arg);
224 sprintf_P(cmdbuf, PSTR("Wrote a %d\n\r"), arg); 140 uart_putsP(PSTR("Wrote a "));
225 uart_puts(cmdbuf); 141 if (arg)
142 uart_putsP(PSTR("1\n\r"));
143 else
144 uart_putsP(PSTR("0\n\r"));
226 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'b') { 145 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'b') {
227 int arg; 146 arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16);
228
229 if (sscanf_P(cmdbuf + 3, PSTR("%2x"), &arg) != 1) {
230 uart_putsP(PSTR("Unable to parse wb command\n\r"));
231 continue;
232 }
233
234 OWWriteByte(arg); 147 OWWriteByte(arg);
235 sprintf_P(cmdbuf, PSTR("Wrote a 0x%02x\n\r"), arg);
236 uart_puts(cmdbuf);
237 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') { 148 } else if (cmdbuf[0] == 'w' && cmdbuf[1] == 'c') {
238 int arg; 149 i = strlen(cmdbuf);
239 150 if (i < 5) {
240 switch (sscanf_P(cmdbuf + 3, PSTR("%x %x:%x:%x:%x:%x:%x:%x:%x:%x"), &arg, 151 uart_putsP(PSTR("No arguments\n\r"));
241 &ROM[0], &ROM[1], &ROM[2], &ROM[3], 152 continue;
242 &ROM[4], &ROM[5], &ROM[6], &ROM[7])) { 153 }
243 break; 154
244 155 arg = (int)strtol(cmdbuf + 3, (char **)NULL, 16);
245 case 1: 156 if (arg == 0) {
157 uart_putsP(PSTR("Unparseable command\n\r"));
158 continue;
159 }
160
161 if (i == 5) {
246 OWSendCmd(NULL, arg); 162 OWSendCmd(NULL, arg);
247 sprintf_P(cmdbuf, PSTR("Sent 0x%02x to all ROMS\n\r"), arg); 163 continue;
248 uart_puts(cmdbuf); 164 }
249 break; 165
250 166 if (i < 29) {
251 case 9: 167 uart_putsP(PSTR("Can't parse ROM ID\n\r"));
252 OWSendCmd(ROM, arg); 168 continue;
253 sprintf_P(cmdbuf, PSTR("Sent 0x%02x to ROM %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"), 169 }
254 arg, ROM[0], ROM[1], ROM[2], ROM[3], 170 for (i = 0; i < 8; i++)
255 ROM[4], ROM[5], ROM[6], ROM[7]); 171 ROM[i] = (int)strtol(cmdbuf + 6 + (3 * i), (char **)NULL, 16);
256 uart_puts(cmdbuf); 172
257 173 OWSendCmd(ROM, arg);
258 break;
259
260 case 0:
261 default:
262 uart_putsP(PSTR("Unable to parse wc command\n\r"));
263 break;
264 }
265
266 OWWriteByte(arg);
267 sprintf_P(cmdbuf, PSTR("Wrote a 0x%02x\n\r"), arg);
268 uart_puts(cmdbuf);
269 } else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') { 174 } else if (cmdbuf[0] == 't' && cmdbuf[1] == 'e') {
270 uint8_t crc; 175 uint8_t crc, buf[9];;
271 uint16_t temp; 176 int8_t temp;
272 uint8_t buf[9]; 177 uint16_t tfrac;
273 178
274 if (sscanf_P(cmdbuf + 3, PSTR("%x:%x:%x:%x:%x:%x:%x:%x:%x"), 179 if (strlen(cmdbuf) < 26) {
275 &ROM[0], &ROM[1], &ROM[2], &ROM[3],
276 &ROM[4], &ROM[5], &ROM[6], &ROM[7]) != 8) {
277 uart_putsP(PSTR("Unable to parse ROM ID\n\r")); 180 uart_putsP(PSTR("Unable to parse ROM ID\n\r"));
278 continue; 181 continue;
279 } 182 }
280 183
184 for (i = 0; i < 8; i++)
185 ROM[i] = (int)strtol(cmdbuf + 3 * (i + 1), (char **)NULL, 16);
186
281 if (ROM[0] != OW_FAMILY_TEMP) { 187 if (ROM[0] != OW_FAMILY_TEMP) {
282 uart_putsP(PSTR("ROM specified isn't a temperature sensor\n\r")); 188 uart_putsP(PSTR("ROM specified isn't a temperature sensor\n\r"));
283 continue; 189 continue;
284 } 190 }
285 191
286 OWSendCmd(ROM, OW_CONVERTT_CMD); 192 OWSendCmd(ROM, OW_CONVERTT_CMD);
287 #if OW_DEBUG
288 uart_putsP(PSTR("Command sent, waiting\n\r"));
289 #endif
290 i = 0; 193 i = 0;
291 while (OWReadBit() == 0) { 194 while (OWReadBit() == 0) {
292 i++; 195 i++;
293 } 196 }
294 #if OW_DEBUG
295 sprintf_P(cmdbuf, PSTR("Temp comversion took %d cycles\n\r"), i);
296 uart_puts(cmdbuf);
297 #endif
298 OWSendCmd(ROM, OW_RD_SCR_CMD); 197 OWSendCmd(ROM, OW_RD_SCR_CMD);
299 crc = 0; 198 crc = 0;
300 for (i = 0; i < 9; i++) { 199 for (i = 0; i < 9; i++) {
301 buf[i] = OWReadByte(); 200 buf[i] = OWReadByte();
302 if (i < 8) 201 if (i < 8)
303 OWCRC(buf[i], &crc); 202 OWCRC(buf[i], &crc);
304 } 203 }
305 204
205 if (crc != buf[8]) {
206 uart_putsP(PSTR("CRC mismatch\n\r"));
207 continue;
208 }
209
210 #if 0
211 uart_putsP(PSTR("temperature "));
212 uart_puts_dec(temp >> 4, 0);
213 uart_putsP(PSTR("."));
214 uart_puts_dec((temp << 12) / 6553, 0);
215 uart_putsP(PSTR("\n\r"));
216 #else
217 /* 0 Temperature LSB
218 * 1 Temperature MSB
219 * 2 Th
220 * 3 Tl
221 * 4 Reserved
222 * 5 Reserved
223 * 6 Count Remain
224 * 7 Count per C
225 * 8 CRC
226 */
227 #if 0
228 for (i = 0; i < 9; i++) {
229 uart_puts_dec(buf[i], 0);
230 uart_putsP(PSTR("\n\r"));
231 }
232 #endif
306 temp = buf[0]; 233 temp = buf[0];
307 temp |= (uint16_t)buf[1] << 8; 234 if (buf[0] & 0x01)
308 temp <<= 3; 235 temp -= 256;
309 236 temp >>= 1;
310 if (crc != buf[8]) { 237
311 sprintf_P(cmdbuf, PSTR("CRC mismatch got %d vs calcd %d\n\r"), buf[8], crc); 238 tfrac = buf[7] - buf[6];
312 uart_puts(cmdbuf); 239 tfrac *= (uint16_t)100;
313 continue; 240 tfrac /= buf[7];
314 } 241 tfrac += 75;
315 242 if (tfrac < 100) {
316 sprintf_P(cmdbuf, PSTR("temperature %d.%01d\n\r"), 243 temp--;
317 temp >> 4, (temp << 12) / 6553); 244 } else {
318 uart_puts(cmdbuf); 245 tfrac -= 100;
246 }
247
248 if (temp < 0){
249 uart_putc('-');
250 uart_puts_dec(-temp, 0);
251 } else
252 uart_puts_dec(temp, 0);
253 uart_putsP(PSTR("."));
254 uart_puts_dec(tfrac, 1);
255 uart_putsP(PSTR("\n\r"));
256
257 #endif
319 } else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') { 258 } else if (cmdbuf[0] == 's' && cmdbuf[1] == 'r') {
320 memset(ROM, 0, 8); 259 memset(ROM, 0, 8);
321 260
322 i = OWFirst(ROM, 1, 0); 261 i = OWFirst(ROM, 1, 0);
323 do { 262 do {
343 break; 282 break;
344 } 283 }
345 284
346 if (i != OW_FOUND) 285 if (i != OW_FOUND)
347 break; 286 break;
348 287
349 sprintf_P(cmdbuf, PSTR("%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n\r"), 288 for (i = 0; i < 7; i++) {
350 ROM[0], 289 uart_puts_hex(ROM[i]);
351 ROM[1], 290 uart_putc(':');
352 ROM[2], 291 }
353 ROM[3], 292 uart_puts_hex(ROM[7]);
354 ROM[4], 293 uart_putsP(PSTR("\n\r"));
355 ROM[5],
356 ROM[6],
357 ROM[7]);
358 uart_puts(cmdbuf);
359 294
360 i = OWNext(ROM, 1, 0); 295 i = OWNext(ROM, 1, 0);
361 } while (1); 296 } while (1);
362 } else if (cmdbuf[0] == 'l' && cmdbuf[1] == 'e') {
363 if (leds == 0) {
364 leds = 1;
365 ledpos = 0;
366 outp(0, TCNT0);
367 sbi(TIMSK, TOIE0);
368 sei();
369 uart_putsP(PSTR("Starting\n\r"));
370 } else {
371 leds = 0;
372 ledpos = 0;
373 PORTA = 0x00;
374 cbi(TIMSK, TOIE0);
375 cli();
376 uart_putsP(PSTR("Stopping\n\r"));
377 }
378 } else { 297 } else {
379 badcmd: 298 badcmd:
380 uart_putsP(PSTR("Unknown command, ? for a list\n\r")); 299 uart_putsP(PSTR("Unknown command, ? for a list\n\r"));
381 } 300 }
382 301
405 uart_puts(const char *addr) { 324 uart_puts(const char *addr) {
406 while (*addr) 325 while (*addr)
407 uart_putc(*addr++); 326 uart_putc(*addr++);
408 } 327 }
409 328
329 void
330 uart_puts_dec(uint8_t a, uint8_t l) {
331 char s[4];
332
333 if (l && a < 10)
334 uart_putsP(PSTR("0"));
335 uart_puts(utoa(a, s, 10));
336 }
337
338 void
339 uart_puts_hex(uint8_t a) {
340 char s[3];
341
342 if (a < 0x10)
343 uart_putc('0');
344
345 uart_puts(utoa(a, s, 16));
346 }
347
410 int 348 int
411 uart_getc(void) { 349 uart_getc(void) {
412 while (!(inp(USR) & 0x80)) 350 while (!(inp(USR) & 0x80))
413 ; 351 ;
414 352