int main()
{
    /* Disable the IRQ for the UART */
    VIC_DisableIRQ(UART0_IRQn);
    
    /* Set up the pin configuration for the UART TXD/RXD pins */
#if defined(lpc2103) || defined(lpc2102) || defined(lpc2101) 
    PINSEL_SetPinConfig(PINSEL_PinConfig_0_1_RXD0);
    PINSEL_SetPinConfig(PINSEL_PinConfig_0_0_TXD0);
#endif

    /* Enable power to the UART */
    SYSCON_EnablePeriphPowerLines(SYSCON_PeriphPowerLine_UART0);

    init_uart(UART0, 115200);
    
    /* Set up VIC slot 0 to handle the UART */
    VIC_SetSlot(0, UART0_IRQn, UART0_Handler);
    VIC_EnableSlot(0);
    
    /* Enable interrupts for the UART in the VIC */
    VIC_EnableIRQ(UART0_IRQn);

    /* Start the UART interrupts going... */
    UART_GetPendingITID(UART0);
}
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;
}