uint32_t OneWire_Init( void ) { printf("\n%s called",__FUNCTION__); printf("\nScanning 1-wire bus..."); tempidx = -1; // Assume we don't find a temperature sensor for( int i = 0; i < sizeof(tcidmapping); i++ ) { tcidmapping[i] = -1; // Assume we don't find any thermocouple interfaces } int rslt = OWFirst(); numowdevices = 0; while (rslt && numowdevices < MAX_OW_DEVICES) { memcpy(owdeviceids[numowdevices], ROM_NO, sizeof(ROM_NO)); numowdevices++; rslt = OWNext(); } if(numowdevices) { for( int iter = 0; iter < numowdevices; iter++ ) { printf("\n Found "); for( int idloop = 7; idloop >= 0; idloop-- ) { printf("%02x", owdeviceids[iter][idloop]); } uint8_t family = owdeviceids[iter][0]; if(family == OW_FAMILY_TEMP) { selectdevbyidx(iter); xferbyte(OW_WRITE_SCRATCHPAD); xferbyte(0x00); xferbyte(0x00); xferbyte(0x1f); // Reduce resolution to 0.5C to keep conversion time reasonable tempidx = iter; // Keep track of where we saw the last/only temperature sensor printf(" [Temperature sensor]"); } else if(family == OW_FAMILY_TC) { selectdevbyidx(iter); xferbyte(OW_READ_SCRATCHPAD); xferbyte(0xff); xferbyte(0xff); xferbyte(0xff); xferbyte(0xff); uint8_t tcid = xferbyte(0xff) & 0x0f; tcidmapping[tcid] = iter; // Keep track of the ID mapping printf(" [Thermocouple interface, ID %x]",tcid); } } } else { printf(" No devices found!"); } return numowdevices; }
int OneWire_PerformTemperatureConversion(void) { uint8_t scratch[9]; if(resetbus()) { xferbyte(OW_SKIP_ROM); // All devices on the bus are addressed here xferbyte(OW_CONVERT_T); setpin1(); BusyWait8(94000<<3); // For 9-bit resolution for( int i = 0; i < numowdevices; i++ ) { selectdevbyidx(i); xferbyte(OW_READ_SCRATCHPAD); for(uint32_t iter=0; iter<2; iter++) { // Only read two bytes scratch[iter] = xferbyte(0xff); //printf("%02x ",scratch[iter]); } int16_t tmp = scratch[1]<<8 | scratch[0]; devreadout[i] = tmp; } } else { BusyWait8(94000<<3); // Maintain timing if no sensors are found for the time being } return numowdevices; }
static int32_t OneWire_Work(void) { static uint8_t mystate = 0; uint8_t scratch[9]; int32_t retval = 0; if (mystate == 0) { uint32_t save = VIC_DisableIRQ(); if (resetbus()) { xferbyte(OW_SKIP_ROM); // All devices on the bus are addressed here xferbyte(OW_CONVERT_T); setpin1(); //retval = TICKS_MS(94); // For 9-bit resolution retval = TICKS_MS(100); // TC interface needs max 100ms to be ready mystate++; } VIC_RestoreIRQ( save ); } else if (mystate == 1) { for (int i = 0; i < numowdevices; i++) { uint32_t save = VIC_DisableIRQ(); selectdevbyidx(i); xferbyte(OW_READ_SCRATCHPAD); for (uint32_t iter = 0; iter < 4; iter++) { // Read four bytes scratch[iter] = xferbyte(0xff); } VIC_RestoreIRQ(save); int16_t tmp = scratch[1]<<8 | scratch[0]; devreadout[i] = tmp; tmp = scratch[3]<<8 | scratch[2]; extrareadout[i] = tmp; } mystate = 0; } else { retval = -1; } return retval; }
uint32_t OneWire_Init(void) { printf("\n%s called", __FUNCTION__); Sched_SetWorkfunc(ONEWIRE_WORK, OneWire_Work); printf("\nScanning 1-wire bus..."); tempidx = -1; // Assume we don't find a temperature sensor for (int i = 0; i < sizeof(tcidmapping); i++) { tcidmapping[i] = -1; // Assume we don't find any thermocouple interfaces } uint32_t save = VIC_DisableIRQ(); int rslt = OWFirst(); VIC_RestoreIRQ( save ); numowdevices = 0; while (rslt && numowdevices < MAX_OW_DEVICES) { memcpy(owdeviceids[numowdevices], ROM_NO, sizeof(ROM_NO)); numowdevices++; save = VIC_DisableIRQ(); rslt = OWNext(); VIC_RestoreIRQ( save ); } if (numowdevices) { for (int iter = 0; iter < numowdevices; iter++) { printf("\n Found "); for (int idloop = 7; idloop >= 0; idloop--) { printf("%02x", owdeviceids[iter][idloop]); } uint8_t family = owdeviceids[iter][0]; if (family == OW_FAMILY_TEMP1 || family == OW_FAMILY_TEMP2 || family == OW_FAMILY_TEMP3) { const char* sensorname = "UNKNOWN"; if (family == OW_FAMILY_TEMP1) { sensorname = "DS1822"; } else if (family == OW_FAMILY_TEMP2) { sensorname = "DS18B20"; } else if (family == OW_FAMILY_TEMP3) { sensorname = "DS18S20"; } save = VIC_DisableIRQ(); selectdevbyidx(iter); xferbyte(OW_WRITE_SCRATCHPAD); xferbyte(0x00); xferbyte(0x00); xferbyte(0x1f); // Reduce resolution to 0.5C to keep conversion time reasonable VIC_RestoreIRQ(save); tempidx = iter; // Keep track of where we saw the last/only temperature sensor printf(" [%s Temperature sensor]", sensorname); } else if (family == OW_FAMILY_TC) { save = VIC_DisableIRQ(); selectdevbyidx(iter); xferbyte(OW_READ_SCRATCHPAD); xferbyte(0xff); xferbyte(0xff); xferbyte(0xff); xferbyte(0xff); uint8_t tcid = xferbyte(0xff) & 0x0f; VIC_RestoreIRQ( save ); tcidmapping[tcid] = iter; // Keep track of the ID mapping printf(" [Thermocouple interface, ID %x]",tcid); } } } else { printf(" No devices found!"); } if (numowdevices) { Sched_SetState(ONEWIRE_WORK, 2, 0); // Enable OneWire task if there's at least one device } return numowdevices; }