onewire.c 3.13 KB
Newer Older
1
#include "onewire.h"
2 3 4 5 6
#include "../sockitowm/ownet.h"
#include "../sockitowm/findtype.h"

#define DEBUG_PMAC 0

7 8 9 10 11
uint8_t FamilySN[MAX_DEV1WIRE][8];
uint8_t devsnum;
uint8_t found_msk;

void own_scanbus(uint8_t portnum)
12
{
13 14 15 16 17 18 19 20
	// Find the device(s)
	found_msk = 0;
	devsnum = 0;
	devsnum += FindDevices(portnum, &FamilySN[devsnum], 0x28, MAX_DEV1WIRE - devsnum);	/* Temperature 28 sensor (SPEC) */
	if (devsnum > 0)
		found_msk |= FOUND_DS18B20;
	devsnum += FindDevices(portnum, &FamilySN[devsnum], 0x42, MAX_DEV1WIRE - devsnum);	/* Temperature 42 sensor (SCU) */
	devsnum += FindDevices(portnum, &FamilySN[devsnum], 0x43, MAX_DEV1WIRE - devsnum);	/* EEPROM */
21
#if DEBUG_PMAC
22
	mprintf("Found %d onewire devices\n", devsnum);
23
#endif
24 25
}

26
int16_t own_readtemp(uint8_t portnum, int16_t * temp, int16_t * t_frac)
27
{
28 29 30 31 32 33 34 35 36 37 38
	if (!(found_msk & FOUND_DS18B20))
		return -1;
	if (ReadTemperature28(portnum, FamilySN[0], temp)) {
		*t_frac =
		    5000 * (! !(*temp & 0x08)) + 2500 * (! !(*temp & 0x04)) +
		    1250 * (! !(*temp & 0x02)) + 625 * (! !(*temp & 0x01));
		*t_frac = *t_frac / 100 + (*t_frac % 100) / 50;
		*temp >>= 4;
		return 0;
	}
	return -1;
39 40 41
}

/* 0 = success, -1 = error */
42
int8_t get_persistent_mac(uint8_t portnum, uint8_t * mac)
43
{
44 45 46
	uint8_t read_buffer[32];
	uint8_t i;
	int8_t out;
47

48
	out = -1;
49

50 51
	if (devsnum == 0)
		return out;
52

53
	for (i = 0; i < devsnum; ++i) {
54
//#if DEBUG_PMAC
55 56 57
		mprintf("Found device: %x:%x:%x:%x:%x:%x:%x:%x\n",
		FamilySN[i][7], FamilySN[i][6], FamilySN[i][5], FamilySN[i][4],
		FamilySN[i][3], FamilySN[i][2], FamilySN[i][1], FamilySN[i][0]);
58
//#endif
59

60 61 62 63 64 65
		/* If there is a temperature sensor, use it for the low three MAC values */
		if (FamilySN[i][0] == 0x28 || FamilySN[i][0] == 0x42) {
			mac[3] = FamilySN[i][3];
			mac[4] = FamilySN[i][2];
			mac[5] = FamilySN[i][1];
			out = 0;
66
#if DEBUG_PMAC
67
			mprintf("Using temperature ID for MAC\n");
68
#endif
69 70 71 72 73 74 75 76 77 78
		}

		/* If there is an EEPROM, read page 0 for the MAC */
		if (FamilySN[i][0] == 0x43) {
			owLevel(portnum, MODE_NORMAL);
			if (ReadMem43(portnum, FamilySN[i], EEPROM_MAC_PAGE,
				      &read_buffer) == TRUE) {
				if (read_buffer[0] == 0 && read_buffer[1] == 0
				    && read_buffer[2] == 0) {
					/* Skip the EEPROM since it has not been programmed! */
79
#if DEBUG_PMAC
80 81
					mprintf("EEPROM has not been "
						"programmed with a MAC\n");
82
#endif
83 84 85
				} else {
					memcpy(mac, read_buffer, 6);
					out = 0;
86
#if DEBUG_PMAC
87 88 89 90
					mprintf("Using EEPROM page: "
						"%x:%x:%x:%x:%x:%x\n",
					     mac[0], mac[1], mac[2], mac[3],
					     mac[4], mac[5]);
91
#endif
92 93 94 95
				}
			}
		}
	}
96

97
	return out;
98 99 100
}

/* 0 = success, -1 = error */
101
int8_t set_persistent_mac(uint8_t portnum, uint8_t * mac)
102
{
103 104
	uint8_t FamilySN[1][8];
	uint8_t write_buffer[32];
105

106 107 108
	// Find the device (only the first one, we won't write MAC to all EEPROMs out there, right?)
	if (FindDevices(portnum, &FamilySN[0], 0x43, 1) == 0)
		return -1;
109

110 111
	memset(write_buffer, 0, sizeof(write_buffer));
	memcpy(write_buffer, mac, 6);
112

113
#if DEBUG_PMAC
114
	mprintf("Writing to EEPROM\n");
115
#endif
116

117 118 119 120 121
	/* Write the last EEPROM with the MAC */
	owLevel(portnum, MODE_NORMAL);
	if (Write43(portnum, FamilySN[0], EEPROM_MAC_PAGE, &write_buffer) ==
	    TRUE)
		return 0;
122

123
	return -1;
124
}