Example #1
0
File: sb-001.c Project: shah-/sb
static unsigned w1_read_byte(int mask) {
	unsigned result = 0;
	if(w1_read(mask)) result |= 0x01;
	if(w1_read(mask)) result |= 0x02;
	if(w1_read(mask)) result |= 0x04;
	if(w1_read(mask)) result |= 0x08;
	if(w1_read(mask)) result |= 0x10;
	if(w1_read(mask)) result |= 0x20;
	if(w1_read(mask)) result |= 0x40;
	if(w1_read(mask)) result |= 0x80;
	return result;
}
Example #2
0
static int handle_1w(const char *line)
{
	if (strncmp(line, "read", 4) == 0 && line[4] != '_') {
		int n = 1;
		if (line[4] == ' ')
			n = undec(line+5);

		if (n < 1) {
			printf("\nERROR:arguments\n");
			return 0;
		}

		printf("1WIRE DATA");
		while (n--) {
			int r = w1_read(w1);
			if (r < 0) {
				printf("\nERROR:%i\n", r);
				return 0;
			}
			printf(" %02x", r);
		}
		printf("\nOK\n");
	} else
	if (strncmp(line, "write ", 6) == 0) {
		line += 5;
		int written = 0;
		while (line[0] == ' ') {
			int data = unhex8(line+1);
			if (data < 0)
				break;
			int r = w1_write(w1, data);
			if (r < 0) {
				printf("ERROR:%i\n", r);
				return 0;
			}
			line += 3;
			written++;
		}
		printf("OK:%i\n", written);
	} else
	if (strncmp(line, "read_rom", 8) == 0) {
		w1_addr_t addr;
		int r = w1_read_rom(w1, &addr);
		if (r < 0) {
			printf("ERROR:%i\n", r);
			return 0;
		}
		u8 *b = addr.bytes;
		printf("OK:%02x%02x%02x%02x%02x%02x%02x%02x\n",
				b[0], b[1], b[2], b[3],
				b[4], b[5], b[6], b[7]);
	} else
	if (strncmp(line, "match_rom ", 10) == 0) {
		w1_addr_t addr;
		w1_unpack(&addr, line+10);
		int r = w1_match_rom(w1, addr);
		if (r < 0) {
			printf("ERROR:%i\n", r);
			return 0;
		}
		printf("OK\n");
	} else
	if (strncmp(line, "skip_rom", 8) == 0) {
		int r = w1_skip_rom(w1);
		if (r < 0) {
			printf("ERROR:%i\n", r);
			return 0;
		}
		printf("OK\n");
	} else
	if (strcmp(line, "scan") == 0) {
		w1_addr_t addrs[5];
		int n = w1_scan(w1, addrs, 5);
		if (n <= 0) {
			printf("ERROR:%i\n", n);
			return 0;
		}
		printf("OK:%i 1-wire devices:\n", n);
		int i;
		for (i=0; i<n; i++) {
			u8 *b = addrs[i].bytes;
			printf("1WIRE DEV %02x%02x%02x%02x%02x%02x%02x%02x\n",
					b[0], b[1], b[2], b[3],
					b[4], b[5], b[6], b[7]);
		}
	} else
	if (strcmp(line, "scan_read") == 0) {
		w1_addr_t addrs[5];
		int n = w1_scan(w1, addrs, 5);
		if (n <= 0) {
			printf("ERROR:%i\n", n);
			return 0;
		}
		printf("OK:%i 1-wire devices:\n", n);
		int i;
		for (i=0; i<n; i++) {
			u8 *b = addrs[i].bytes;
			if (addrs[i].bytes[0] == FAMILY_DS18B20) {
				s16 temp;
				u8 tmp[9];
				w1_match_rom(w1, addrs[i]);
				w1_write(w1, 0x44); /* conversion */
				mdelay(750); /* 750ms conversion time */
				w1_match_rom(w1, addrs[i]);
				w1_write(w1, 0xbe); /* read */

				int j;
				for (j=0; j<9; j++)
					tmp[j] = w1_read(w1);

				if (crc8r(tmp, 9) != 0)
					continue;

				temp = tmp[1]<<8 | tmp[0];

				printf("1WIRE DS18B20 %02x%02x%02x%02x%02x%02x%02x%02x ",
						b[0], b[1], b[2], b[3],
						b[4], b[5], b[6], b[7]);
				if (temp < 0) {
					printf("-");
					temp = -temp;
				}
				printf("%i.%02i\n", temp>>4, 100*(temp&0xf)/16);
			} else {