#include #include "os_type.h" #include "osapi.h" #include "user_interface.h" #include "gpio.h" #include "onewire.h" sint8 ICACHE_FLASH_ATTR init(oneWire_t *owDev) { uint32_t time; // reset the ow line, check for presence? time = system_get_time() + 500; gpio_output_set(0, BIT4, BIT4, 0); // pull GPIO4 low while(system_get_time() < time); // delay 500uS gpio_output_set(0, 0, 0, BIT4); // let go of GPIO4 time = system_get_time() + 60; while(system_get_time() < time); uint8_t presence = (uint8_t)(gpio_input_get() >> 4) & 0x1; if(presence) { return 1; } else { return -1; } } void ICACHE_FLASH_ATTR transact(oneWire_t *owDev, ds18b20_cmds cmd) { uint32_t time; uint8_t sendBit; uint8_t inBit = 0x00; uint8_t inByte = 0x00; if((cmd == SKIP_ROM) | (cmd == CONVERT_T)) { for(uint8_t i = 7; i >= 0; i--) { sendBit = (cmd >> i) & 0x1; if(sendBit == 1) { time = system_get_time() + 10; gpio_output_set(0, BIT4, BIT4, 0); // pull low while(system_get_time() < time); gpio_output_set(0, 0, 0, BIT4); // let go } else { time = system_get_time() + 100; gpio_output_set(0, BIT4, BIT4, 0); //pull low while(system_get_time() < time); gpio_output_set(0, 0, 0, BIT4); } } } else if(cmd == SCRATCH_READ) { for(uint8_t i = 7; i >= 0; i--) { sendBit = (cmd >> i) & 0x1; if(sendBit == 1) { time = system_get_time() + 10; gpio_output_set(0, BIT4, BIT4, 0); // pull low while(system_get_time() < time); gpio_output_set(0, 0, 0, BIT4); // let go } else { time = system_get_time() + 100; gpio_output_set(0, BIT4, BIT4, 0); //pull low while(system_get_time() < time); gpio_output_set(0, 0, 0, BIT4); } } // now read the scratchpad for(uint8_t i = 0; i < 2; i++) { for(uint8_t j = 0; j < 8; j++) { time = system_get_time() + 8; gpio_output_set(0, BIT4, BIT4, 0); while(system_get_time() < time); gpio_output_set(0, 0, 0, BIT4); inBit = (uint8_t)(gpio_input_get() >> 4) & 0x01; inByte |= inBit << j; } owDev->scratchpad[i] = inByte; } } }