/** * Set the onewire LED GPIO controller outputs * * @param mask Mask of outputs to enable * * @return EC_SUCCESS, or non-zero if error. */ static int led_set_mask(int mask) { int rv; /* Reset the 1-wire bus */ rv = onewire_reset(); if (rv) return rv; /* Skip ROM, since only one device */ onewire_write(0xcc); /* Write and turn on the LEDs */ onewire_write(0x5a); onewire_write(mask); onewire_write(~mask); /* Repeat inverted */ rv = onewire_read(); /* Confirmation byte */ if (rv != 0xaa) return EC_ERROR_UNKNOWN; /* The next byte is a read-back of the chip status. Since we're only * using lines as outputs, we can ignore it. */ return EC_SUCCESS; }
float ds1820_read() { uint8_t busy = 1; onewire_reset(); onewire_write(0xCC); onewire_write(0x44); delay(750); while(busy == 0) { busy = onewire_read(); printf("busy: %d\n", busy); } onewire_reset(); onewire_write(0xCC); onewire_write(0xBE); uint8_t lsb, msb, th, tl, reserved1, reserved2, count_remain, count_per_c, crc; float real_temp = 0.0f; signed char temp_read = 0; lsb = onewire_read(); msb = onewire_read(); th = onewire_read(); tl = onewire_read(); reserved1 = onewire_read(); reserved2 = onewire_read(); count_remain = onewire_read(); count_per_c = onewire_read(); crc = onewire_read(); uint8_t data[] = {lsb, msb, th, tl, reserved1, reserved2, count_remain, count_per_c}; onewire_reset(); if(crc_read(data) == crc) { temp_read = (signed char)(lsb>>1); if(msb == 255) temp_read = temp_read | 0x80; real_temp = (float)temp_read + 0.85f - (float)count_remain/(float)count_per_c; real_temp = (int)(real_temp * 10) / 10.0f; }
uint16_t onewire_temp_read(void) { uint8_t temp1,temp2; int16_t raw; onewire_reset(); onewire_write(0xcc); onewire_write(0x44); onewire_reset(); onewire_write(0xcc); onewire_write(0xbe); temp1 = onewire_read(); temp2 = onewire_read(); raw = (temp2 << 8) | temp1; return ((6 * raw) + raw / 4); }
void onewire_id_read(void) { int i; onewire_reset(); onewire_write(0x33); for(i = 0; i < 8; i++) { onewire_id[i] = onewire_read(); } }
// Lua: ow.write( id, v, power) static int ow_write( lua_State *L ) { int power = 0; unsigned id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( ow, id ); int v = (int)luaL_checkinteger( L, 2 ); if( v > 255 ) return luaL_error( L, "wrong arg range" ); if(lua_isnumber(L, 3)) power = lua_tointeger(L, 3); if(power!=0) power = 1; onewire_write((uint8_t)id, (uint8_t)v, (uint8_t)power); return 0; }
// return the temperature in C multiplied by 16 int read_DS1820() { uint8 i; uint8 present = 0; uint8 dataread[12]; int temp_read; // we might do a ds.depower() here, but the reset will take care of it. present = onewire_reset(); onewire_select(DS1820_addr); onewire_write(0xBE,0); // Read Scratchpad for ( i = 0; i < 9; i++) { // we need 9 bytes dataread[i] = onewire_read(); } temp_read = ((dataread[1] << 8) | dataread[0]); if (!is_DS18B20) { // temp_read is currently in half degrees. temp_read *= 8.0; // temp_read += - ( 8 * (dataread[7]- dataread[6]) )/dataread[7]; } return temp_read; }
int ICACHE_FLASH_ATTR ds18b20_read(ds18b20_data *read) { byte i; byte present = 0; byte type_s; byte data[12]; byte addr[8]; byte crc; float celsius; //float fahrenheit; DS18B20_DBG("Look for OneWire Devices on PIN =%d", gpioPin); if (!onewire_search(gpioPin, addr)) { DS18B20_DBG("No more addresses."); onewire_reset_search(gpioPin); delay_ms(250); return; } DS18B20_DBG("ADDRESS = %02x %02x %02x %02x %02x %02x %02x %02x", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5], addr[6], addr[7]); // the first ROM byte indicates which chip switch (addr[0]) { case 0x10: DS18B20_DBG(" Chip = DS18S20"); // or old DS1820 type_s = 1; break; case 0x28: DS18B20_DBG(" Chip = DS18B20"); type_s = 0; break; case 0x22: DS18B20_DBG(" Chip = DS1822"); type_s = 0; break; default: DS18B20_DBG("Device is not a DS18x20 family device."); return; } onewire_reset(gpioPin); onewire_select(gpioPin, addr); onewire_write(gpioPin, 0x44, 1); // start conversion, use ds.write(0x44,1) with parasite power on at the end delay_ms(1000); present = onewire_reset(gpioPin); onewire_select(gpioPin, addr); onewire_write(gpioPin, 0xBE, 1); // Read Scratchpad //DS18B20_DBG(" Present = %d", present); for ( i = 0; i < 9; i++) { // we need 9 bytes data[i] = onewire_read(gpioPin); DS18B20_DBG(" DATA: %02x ", data[i]); } crc = onewire_crc8(data, 8); DS18B20_DBG(" CRC=%d ", crc); if (crc != data[8]) { DS18B20_DBG("CRC is not valid!"); return; } // Convert the data to actual temperature // because the result is a 16 bit signed integer, it should // be stored to an "int16_t" type, which is always 16 bits // even when compiled on a 32 bit processor. int16_t raw = (data[1] << 8) | data[0]; if (type_s) { raw = raw << 3; // 9 bit resolution default if (data[7] == 0x10) { // "count remain" gives full 12 bit resolution raw = (raw & 0xFFF0) + 12 - data[6]; } } else { byte cfg = (data[4] & 0x60); // at lower res, the low bits are undefined, so let's zero them if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms //// default is 12 bit resolution, 750 ms conversion time } celsius = (float) raw / 16.0; //fahrenheit = celsius * 1.8 + 32.0; read->temp = celsius; DS18B20_DBG("Temperature = "); char *temp_string = (char*) os_zalloc(64); c_sprintf(temp_string, "%f", read->temp); DS18B20_DBG(" %s Celsius", temp_string); os_free(temp_string); //DS18B20_DBG(" %2.2f Fahrenheit", fahrenheit); return 1; }
// // Do a ROM skip // void ICACHE_FLASH_ATTR onewire_skip() { onewire_write(ONEWIRE_SKIP_ROM, 0); // Skip ROM }
// // Do a ROM select // void ICACHE_FLASH_ATTR onewire_select(const uint8 *rom) { onewire_write(ONEWIRE_MATCH_ROM, 0); // Choose ROM for (uint8 i = 0; i < 8; i++) { onewire_write(rom[i], 0); } }
/* pass array of 8 bytes in */ uint32 ICACHE_FLASH_ATTR onewire_search(struct onewire_search_state *state) { // If last search returned the last device (no conflicts). if (state->lastDeviceFlag) { init_search_state(state); return FALSE; } // 1-Wire reset if (!onewire_reset()) { // Reset the search and return fault code. init_search_state(state); return ONEWIRE_SEARCH_NO_DEVICES; } // issue the search command onewire_write(ONEWIRE_SEARCH_ROM, 0); uint8 search_direction; int32 last_zero = -1; // Loop through all 8 bytes = 64 bits int32 id_bit_index; for (id_bit_index = 0; id_bit_index < 8 * ROM_BYTES; id_bit_index++) { const uint32 rom_byte_number = id_bit_index / BITS_PER_BYTE; const uint32 rom_byte_mask = 1 << (id_bit_index % BITS_PER_BYTE); // Read a bit and its complement const uint32 id_bit = onewire_read_bit(); const uint32 cmp_id_bit = onewire_read_bit(); // Line high for both reads means there are no slaves on the 1wire bus. if (id_bit == 1 && cmp_id_bit == 1) { // Reset the search and return fault code. init_search_state(state); return ONEWIRE_SEARCH_NO_DEVICES; } // No conflict for current bit: all devices coupled have 0 or 1 if (id_bit != cmp_id_bit) { // Obviously, we continue the search using the same bit as all the devices have. search_direction = id_bit; } else { // if this discrepancy is before the Last Discrepancy // on a previous next then pick the same as last time if (id_bit_index < state->lastDiscrepancy) { search_direction = ((state->address[rom_byte_number] & rom_byte_mask) > 0); } else { // if equal to last pick 1, if not then pick 0 search_direction = (id_bit_index == state->lastDiscrepancy); } // if 0 was picked then record its position in LastZero if (search_direction == 0) { last_zero = id_bit_index; } } // set or clear the bit in the ROM byte rom_byte_number with mask rom_byte_mask if (search_direction == 1) { state->address[rom_byte_number] |= rom_byte_mask; } else { state->address[rom_byte_number] &= ~rom_byte_mask; } // For the current bit position, write the bit we choose to use in the current search. // Any devices that don't match are disabled. onewire_write_bit(search_direction); } state->lastDiscrepancy = last_zero; // check for last device if (state->lastDiscrepancy == -1) { state->lastDeviceFlag = TRUE; } if (crc8(state->address, 7) != state->address[7]) { // Reset the search and return fault code. init_search_state(state); return ONEWIRE_SEARCH_CRC_INVALID; } return ONEWIRE_SEARCH_FOUND; }
void start_DS1820() { onewire_reset(); onewire_select(DS1820_addr); onewire_write(0x44,0); // start conversion, with parasite power off at the end }