Пример #1
0
// Lua: r = ow.reset( id )
static int ow_reset( lua_State *L )
{
  unsigned id = luaL_checkinteger( L, 1 );
  MOD_CHECK_ID( ow, id );
  lua_pushinteger( L, onewire_reset(id) );
  return 1;
}
Пример #2
0
// Read a DS18xx sensor
void
read_DS18xxx(unsigned char register_id)
{
  unsigned char i, pinmask = register_pinmask_map[register_id];

  // Reset and read the temperature
  if (onewire_reset(pinmask))
    {
      send_onewire_rom_commands(register_id);
      onewire_write_byte(CMD_READ_SCRATCHPAD, pinmask);

      for (i = 0; i < 9; i++)
        {
          ow_buf[i] = onewire_read_byte(pinmask);
        }

      if (ow_buf[8] == calculate_onewire_crc(ow_buf, 8) && ow_buf[7] == 0x10)
        {
          temperatures[register_id] = ow_buf[0] | (ow_buf[1] << 8);

          // If result needs scaling up then scale it up
          if (register_identification[register_id][SCALE_POSITION] == SCALE_TEMP)
            scale_DS18B20_result(register_id);
        }
      else
        {
          temperatures[register_id] = (signed int) ONEWIRE_TEMP_FAIL;
        }
    }
}
Пример #3
0
/**
 * 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;
}
Пример #4
0
void immob_check_state(struct ecudata_t* d)
{
 uint8_t i = 0, crc = 0;
 uint8_t key[8];
 if (!(d->param.bt_flags & _BV(BTF_USE_IMM)))
  return; //immibilizer was not activated

 onewire_save_io_registers();

 if (!onewire_reset())
  goto lock_system;    //not device present, lock the system!

 //Read 64-bit key
 onewire_write_byte(OWCMD_READ_ROM);
 for(; i < 8; ++i) key[i] = onewire_read_byte();

 //validate CRC8, all bytes except CRC8 byte
 for(i = 0; i < 7; ++i) crc = update_crc8(key[i], crc);

 if (crc != key[7])
  goto lock_system;    //crc doesn't match, lock the system!

 //validate read key, skip family code and CRC8 bytes
 if (!validate_key(d, key+1, IBTN_KEY_SIZE))
  goto lock_system;    //read and stored keys don't match, lock the system!

 onewire_restore_io_registers();
 return; //ok, system is unlocked

lock_system:
 onewire_restore_io_registers();
 d->sys_locked = 1;    //set locking flag
}
Пример #5
0
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;
  }
Пример #6
0
void ds1820_convert(int fd, uint64_t devcode)
{
    if(onewire_reset(fd))
    {
        onewire_address_command(fd, devcode);
        onewire_write_byte(fd, DS1820_CONVERT_TEMP); // convert
    }
}
Пример #7
0
static void
owtemp_update(void *arg)
{
	struct owtemp_softc *sc = arg;
	u_int8_t data[9];

	onewire_lock(sc->sc_onewire);
	if (onewire_reset(sc->sc_onewire) != 0)
		goto done;
	onewire_matchrom(sc->sc_onewire, sc->sc_rom);

	/*
	 * Start temperature conversion. The conversion takes up to 750ms.
	 * After sending the command, the data line must be held high for
	 * at least 750ms to provide power during the conversion process.
	 * As such, no other activity may take place on the 1-Wire bus for
	 * at least this period.
	 */
	onewire_write_byte(sc->sc_onewire, DS_CMD_CONVERT);
	tsleep(sc, PRIBIO, "owtemp", hz);

	if (onewire_reset(sc->sc_onewire) != 0)
		goto done;
	onewire_matchrom(sc->sc_onewire, sc->sc_rom);

	/*
	 * The result of the temperature measurement is placed in the
	 * first two bytes of the scratchpad.
	 */
	onewire_write_byte(sc->sc_onewire, DS_CMD_READ_SCRATCHPAD);
	onewire_read_block(sc->sc_onewire, data, 9);
#if 0
	if (onewire_crc(data, 8) == data[8]) {
		sc->sc_sensor.value = 273150000 +
		    (int)((u_int16_t)data[1] << 8 | data[0]) * 500000;
	}
#endif

	sc->sc_sensor.value_cur = sc->sc_owtemp_decode(data);
	sc->sc_sensor.state = ENVSYS_SVALID;

done:
	onewire_unlock(sc->sc_onewire);
}
Пример #8
0
__interrupt void Timer0_A0(void) {
	switch (__even_in_range(TA0IV, 6)) {
	case TA0IV_NONE: {                         		// TA0CCR0
		P1OUT &= ~(CONTROL); // relais on bis
		if (i++ == 0) { // start reading
			onewire_reset(&ow);
			onewire_write_byte(&ow, 0xcc); // skip ROM command
			onewire_write_byte(&ow, 0x44); // convert T command
			onewire_line_high(&ow);
		}
		if (i >= 14) { // read values 16
			onewire_reset(&ow);
			onewire_write_byte(&ow, 0xcc); // skip ROM command
			onewire_write_byte(&ow, 0xbe); // read scratchpad command
			for (i = 0; i < 9; i++)
				scratchpad[i] = onewire_read_byte(&ow);
			temp = ((scratchpad[1] << 8) + scratchpad[0]) * 0.0625;
			// temp control
			if (temp < 32 & TA0CCR1 < 48000) { // increase pwm
			//TA0CCR1 = TA0CCR1 +1000;
			}
			if (temp > 32 & TA0CCR1 > 1000) { // decrease pwm
			//TA0CCR1 = TA0CCR1 -1000;
			}
			i = 0;
		}
		break;
	}
	case TA0IV_TACCR1: {		                        // TA0CCR1
		P1OUT |= CONTROL; // relais off
		break;
	}
	case TA0IV_TACCR2:                                 // TA0CCR2
	{
		break;
	}
	case 6:
		break;                          // TA0CCR3
	default:
		break;
	}
}
Пример #9
0
U8 start_temp_reading(){
	if(!onewire_reset()){
		set_display_buf("e00");
		return 0;
	}
	ow_data_array[0] = OW_CONVERT_T;
	ow_data_array[1] = OW_SKIP_ROM;
	ow_process_resdata = wait_reading;
	onewire_send_bytes(2);
	return 1;
}
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);
	
}
Пример #11
0
uint8_t onewire_read_temp (struct onewire_temp_sensor *sensor)
//Temperatur eines DS18x20 auslesen und umrechnen
{
    uint8_t spad[9];
    uint8_t i;
    uint8_t crc = 0;
    float temp_f;

    //einzelnen Sensor ansprechen
    onewire_reset();
    if (onewire_presence_pulse == 0) {
        return 0;
        }
    onewire_send_byte(match_rom);

    for (i=0; i<8; i++) {
        onewire_send_byte(sensor->rom[i]);
    }

    //Scratchpad auslesen
    onewire_send_byte(read_spad);
    for (i=0; i<9; i++) {
        spad[i] = onewire_read_byte();
    }

    //CRC Check
    for (i=0; i<9; i++) {
        crc = _crc_ibutton_update(crc, spad[i]);
    }

    //Temperatur berechnen
    if (crc == 0) {
        //0,5°C Bit löschen
        spad[temperature_lsb] &= ~(1<<0);

        //Temperatur umrechnen
        if (spad[temperature_msb] == 0) {
            temp_f = spad[temperature_lsb]/2.0;
        }
        else {
            i = ~(spad[temperature_lsb]);
            temp_f = i+1;
            temp_f = (temp_f*(-1))/2.0;
        }

        //Nachkommaberechnung
        temp_f = temp_f-0.25+(16.0-spad[count_remain])/16.0;

        //Umrechnung, um Kommazahl als Int zu speichern und abspeichern
        sensor->temperature = temp_f*100;
        return 1;
    }
    return 0;
}
Пример #12
0
void onewire_read_rom(uint8_t *rom) {
//Lesen der 64-bit Adresse
    onewire_reset();
    onewire_send_byte(read_rom);

    /*Schleifenzähler läuft rückwärts, weil der Slave mit dem letzten Byte
    (Family-Code) anfängt und so der ROM-Code richtig herum in "adresse" steht*/
    for (uint8_t i=8; i>0; i--) {
        *rom = onewire_read_byte();
        rom++;
    }
}
void onewire_id_read(void) 
{ 
  int i;

  onewire_reset();
  onewire_write(0x33);
  
  for(i = 0; i < 8; i++)
    {
      onewire_id[i] = onewire_read();
    }
}
Пример #14
0
void search(onewire_t *ow, uint8_t *id, int depth, int reset)
{
  int i, b1, b2;
 
  if (depth == 64)
  {
    // we have all 64 bit in this search branch
    printf("found: ");
    for (i = 0; i < 8; i++) printf("%02x", id[i]);
    printf("\n");
    return;
  }
 
  if (reset)
  {
    if (onewire_reset(ow) != 0) { printf("reset failed\n"); return; }
    onewire_write_byte(ow, 0xF0); // search ROM command
 
    // send currently recognized bits
    for (i = 0; i < depth; i++)
    {
      b1 = onewire_read_bit(ow);
      b2 = onewire_read_bit(ow);
      onewire_write_bit(ow, id[i / 8] & (1 << (i % 8)));
    }
  }
 
  // check another bit
  b1 = onewire_read_bit(ow);
  b2 = onewire_read_bit(ow);
  if (b1 && b2) return; // no response to search
  if (!b1 && !b2) // two devices with different bits on this position
  {
    // check devices with this bit = 0
    onewire_write_bit(ow, 0);
    id[depth / 8] &= ~(1 << (depth % 8));
    search(ow, id, depth + 1, 0);
    // check devices with this bit = 1
    id[depth / 8] |= 1 << (depth % 8);
    search(ow, id, depth + 1, 1); // different branch, reset must be issued
  } else if (b1) {
    // devices have 1 on this position
    onewire_write_bit(ow, 1);
    id[depth / 8] |= 1 << (depth % 8);
    search(ow, id, depth + 1, 0);
  } else if (b2) {
    // devices have 0 on this position
    onewire_write_bit(ow, 0);
    id[depth / 8] &= ~(1 << (depth % 8));
    search(ow, id, depth + 1, 0);
  }
}
Пример #15
0
// Return if conversion command is sent succesfully
// It takes a reference to a specific device and issues the convert command specificly for it
bool
issue_convert_for_device(unsigned char register_id)
{
  unsigned char pinmask = register_pinmask_map[register_id];

  if (onewire_reset(pinmask))
    {
      send_onewire_rom_commands(register_id);
      onewire_write_byte(CMD_CONVERT_T, pinmask);
      return TRUE;
    }
  return FALSE;
}
Пример #16
0
// Return if conversion command is sent succesfully
// It takes a reference to a specific device but issues convert on the entire bus
bool
issue_convert_on_bus(unsigned char register_id)
{
  unsigned char pinmask = register_pinmask_map[register_id];

  if (onewire_reset(pinmask))
    {
      onewire_write_byte(CMD_SKIP_ROM, pinmask);
      onewire_write_byte(CMD_CONVERT_T, pinmask);
      return TRUE;
    }
  return FALSE;
}
Пример #17
0
void onewire_send_command(uint8_t *rom, uint8_t command) {
//1Wire-Slave über die 64-bit Adresse ansprechen
    unsigned char i;

    onewire_reset();

    for (i=0; i<8; i++) {
        onewire_send_byte(*rom);
        rom++;
    }

    onewire_send_byte(command);
}
Пример #18
0
void read_next_sensor(){
	U8 i;
	U8 *rom = (saved_data->ROMs[starting_val]).ROM_bytes;
	if(!onewire_reset()){
		ow_process_resdata = NULL;
		return;
	}
	ow_data_array[0] = OW_READ_SCRATCHPAD;
	ow_process_resdata = send_read_seq;
	for(i = 0; i < 8; i++){
		ow_data_array[i+1] = rom[i];
	}
	ow_data_array[9] = OW_MATCH_ROM;
	onewire_send_bytes(10);
}
Пример #19
0
bool
set_temp_resolution(unsigned char register_id, unsigned char resolution)
{
  unsigned char pinmask = register_pinmask_map[register_id];

  if (onewire_reset(pinmask))
    {
      onewire_write_byte(CMD_SKIP_ROM, pinmask);
      onewire_write_byte(CMD_WRITE_SCRATCHPAD, pinmask);
      onewire_write_byte(0, pinmask);
      onewire_write_byte(0, pinmask);
      onewire_write_byte(resolution, pinmask);
      return TRUE;
    }
  else
    {
      return FALSE;
    }
}
Пример #20
0
uint8_t inline onewire_select_device_and_issue_command(const uint8_t cmd, const uint8_t family_code)
{
    uint8_t dev_present = onewire_reset();
    if(dev_present == 0)
        {
            return EXIT_FAILURE;
        }

    onewire_writebyte(CMD_ONEWIRE_READ_ROM);

    uint8_t i;
    for(i = 0; i < 8; i++)
        onewire_readbyte();


    onewire_writebyte(cmd);

    return EXIT_SUCCESS;
}
Пример #21
0
float ds1820_read_temperature(int fd, uint64_t devcode)
{
    int i;
    unsigned char data[9];

    if(onewire_reset(fd))
    {
        onewire_address_command(fd, devcode);
        onewire_write_byte(fd, DS1820_READ_SCRATCHPAD); // read scratch

        for(i = 0; i < 9; i++) {
            data[i] = onewire_read_byte(fd);
        }
        int ds1820_temperature = data[1] & 0x0f;
        ds1820_temperature <<= 8;
        ds1820_temperature |= data[0];
        return ((float)ds1820_temperature) * 0.0625f;
    }
    return 211;
}
Пример #22
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;
}
Пример #23
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;
}
Пример #24
0
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;
}
Пример #25
0
/*
 * To send a PING:
 * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,07,01,01,36,05,37,cf}
 *
 * To identify Register 4 (expected response as per the above identification data):
 * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,08,01,01,36,02,04,2c,d8}
 *
 * To identify Register 1 (expected response as per the above identification data):
 * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,08,01,01,36,02,01,99,32}
 *
 * To Read Register 1
 * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,08,01,01,36,01,01,8f,66}
 *
 * To Write Register 3 (PWM), with low pin 2-2 sec
 * {ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,ff,0b,01,01,35,00,03,14,14,00,04,d8}
 *
 */
void
operate_device(void)
{

  // Create messaging variables
  unsigned char response_opcode = RESPONSE_UNDEFINED, p, pinmask;

  // The main loop of the device
  while (TRUE)
    {
      // Operate main device functions
      operate_onewire_temp_measurement();

      // Take care of messaging
      if (get_device_message() && !process_generic_messages())
        {
          switch (message_buffer.content[OPCODE])
            {
          case SET_REGISTER:
            response_opcode = COMMAND_FAIL;
            break;
          case READ_REGISTER:
            // p holds register to read
            p = message_buffer.content[PARAMETER_START];
            // Branch based on register number
            if (p == 1)
              {
                message_buffer.content[PARAMETER_START] = temperatures[p - 1]
                    & 0x00ff;
                message_buffer.content[PARAMETER_START + 1] = (temperatures[p
                    - 1] >> 8) & 0x00ff;
                message_buffer.index = PARAMETER_START + 1;

                response_opcode = COMMAND_SUCCESS;
              }
            else if (p == 2)
              {
                pinmask = register_pinmask_map[0];
                onewire_reset(pinmask);
                onewire_write_byte(CMD_READ_ROM, pinmask);

                p = 0;
                do
                  {
                    message_buffer.content[PARAMETER_START + p] =
                        onewire_read_byte(pinmask);
                  }
                while (++p < 8);
                message_buffer.index = PARAMETER_START + 7;

                if (message_buffer.content[PARAMETER_START + p]
                    == calculate_onewire_crc(
                        message_buffer.content + PARAMETER_START, 8))
                  response_opcode = COMMAND_SUCCESS;
                else
                  response_opcode = COMMAND_FAIL;
              }
            else
              {
                response_opcode = COMMAND_FAIL;
              }
            break;
          default:
            response_opcode = COMMAND_FAIL;
            break;
            }
          send_response(response_opcode);
        }
Пример #26
0
int main() {
	U32 T_LED = 0L;  // time of last digit update
	U32 Tow = 0L;    // 1-wire time
	U32 T_btns = 0L; // buttons timer
	U8 old_btns_state = 0;
	U8 show_temp = 0; // =0 to show number; = 1 to show temp

	// Configure clocking
	CLK_CKDIVR = 0; // F_HSI = 16MHz, f_CPU = 16MHz
	// Configure pins
	CFG_GCR |= 1; // disable SWIM
	LED_init();
	CCR |= 0x28; // make shure that we are on level 3 - disabled SW priority
	// Configure Timer1
	// prescaler = f_{in}/f_{tim1} - 1
	// set Timer1 to 1MHz: 16/1 - 1 = 15
	TIM1_PSCRH = 0;
	TIM1_PSCRL = 15; // LSB should be written last as it updates prescaler
	// auto-reload each 1ms: TIM_ARR = 1000 = 0x03E8
	TIM1_ARRH = 0x03;
	TIM1_ARRL = 0xE8;
	// interrupts: update
	TIM1_IER = TIM_IER_UIE;
	// auto-reload + interrupt on overflow + enable
	TIM1_CR1 = TIM_CR1_APRE | TIM_CR1_URS | TIM_CR1_CEN;

	onewire_setup();
	eeprom_default_setup();

	// enable all interrupts
	enableInterrupts();
	set_display_buf("---");
	if(get_starting_val()){
		matchROM = 1; // if ROMs not empty, start scanning
		starting_val = 0; // reset starting value
	}
	start_temp_reading();
	// Loop
	do {
		if(((U16)(Global_time - T_time) > temper_delay) || (T_time > Global_time)){
			T_time = Global_time;
			if(show_temp){ // show temperature
				temper_delay = TEMPER_delay;
				show_temp = 0;
				if(!temp_ready || temp_readed == ERR_TEMP_VAL){
					set_display_buf("eab");
				}else{
					temp_ready = 0;
					if(temp_readed > -100 && temp_readed < 1000){
						display_int((int)temp_readed, 0);
						display_DP_at_pos(1);
					}else{ // display only integer part
						display_int((int)temp_readed/10, 0);
					}
				}
				if(matchROM) ++starting_val;
			}else{ // show number
				show_temp = 1;
				temper_delay = NUMBER_delay;
				if(matchROM){
					if(get_starting_val()){
						display_int(starting_val+1,0);
						read_next_sensor();
					}else{
						starting_val = 0;
						if(start_temp_reading()){
							if(!get_starting_val()){
								matchROM = 0;
								set_display_buf("eee");
							}else{
								display_int(starting_val+1,0);
								read_next_sensor();
							}
						}
					}
				}else{
					start_temp_reading();
				}
			}
		}
		if((U8)(Global_time - T_LED) > LED_delay){
			T_LED = Global_time;
			show_next_digit();
		}
		if((U8)(Global_time - T_btns) > BTNS_delay){
			T_btns = Global_time;
			if(!block_keys && old_btns_state != buttons_state){
				U8 pressed = old_btns_state & ~buttons_state; // pressed buttons are ones
				if(pressed){ // some buttons were pressed
					if(pressed & STORE_BTN){
						if(onewire_reset()){
							delete_notexistant = 0;
							block_keys = 1;
							onewire_send_byte(OW_READ_ROM);
							while(OW_BUSY);
							ow_process_resdata = store_last_ROM;
							onewire_receive_bytes(8);
						}
					}else if(pressed & DELETE_BTN){
						delete_notexistant = 1;
					}else if(pressed & DELALL_BTN){
						U8 i;
						for(i = 0; i < MAX_SENSORS; i++){
							erase_saved_ROM(i);
							set_display_buf("---");
							matchROM = 0;
						}
					}
					//display_int(pressed, 0);
				}else{ // some buttons released
					//display_int(~old_btns_state & buttons_state, 0); // released are ones
				}
			}
			old_btns_state = buttons_state;
		}
		if(Global_time != Tow){ // process every byte not frequently than once per 1ms
			Tow = Global_time;
			process_onewire();
		}
		if(waitforread){
			if(onewire_reset()){
				ow_process_resdata = send_read_seq;
				waitforread = 0;
				if(!matchROM){
					ow_data_array[0] = OW_READ_SCRATCHPAD;
					ow_data_array[1] = OW_SKIP_ROM;
					onewire_send_bytes(2);
				}else{
					read_next_sensor();
				}
			}
		}
	} while(1);
}
Пример #27
0
int main()
{
  onewire_t ow;
  int i;
  uint8_t scratchpad[9];
  uint8_t id[8];
 
  WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer
  BCSCTL1 = CALBC1_8MHZ;
  DCOCTL = CALDCO_8MHZ;
  uart_setup();
 
  ow.port_out = &P1OUT;
  ow.port_in = &P1IN;
  ow.port_ren = &P1REN;
  ow.port_dir = &P1DIR;
  ow.pin = BIT7;

  printf("start\n");
  search(&ow, id, 0, 1);
  printf("done\n");
 
  onewire_reset(&ow);
  onewire_write_byte(&ow, 0xcc); // skip ROM command
  onewire_write_byte(&ow, 0x44); // convert T command
  onewire_line_high(&ow);
//  DELAY_MS(850); // at least 750 ms for the default 12-bit resolution
// while __delay_cycles(>100000) give the __delay_cycles max value error:
for (i = 0; i < 65; i++) __delay_cycles(100000);

  onewire_reset(&ow);
  onewire_write_byte(&ow, 0xcc); // skip ROM command
  onewire_write_byte(&ow, 0xbe); // read scratchpad command
  for (i = 0; i < 9; i++) scratchpad[i] = onewire_read_byte(&ow);
  for (i = 0; i < 9; i++) printf("%02x", scratchpad[i]);
  //meas = scratchpad[0];  // LSB
  //meas |= ( (uint16_t)scratchpad[1] ) << 8; // MSB
  int j=0;
        uint16_t temp = 0;
        for(j = 16; j > 0; i--){
                temp >>= 1;
                if (onewire_read_bit(&ow)) {
                        temp |= 0x8000;
                }
	}
  if(temp<0x8000){
        return(temp*0.0625);
    }
    else
    {
        temp=(~temp)+1;
        return(temp*0.0625);
    }

  printf("%02x\n", temp);
  printf("%02x\n", scratchpad[2]);
  printf("%02x°C\n");
 
  _BIS_SR(LPM0_bits + GIE);
  return 0;
}
Пример #28
0
void
operate_device(void)
{

  // Create messaging variables
  unsigned char response_opcode = RESPONSE_UNDEFINED, p, pinmask;

  bool got_message;

  // The main loop of the device
  while (TRUE)
    {
      // Operate main device functions
      operate_onewire_temp_measurement();

      got_message = get_device_message();
#if 0
      /*
      *    Watch communication activity on bus and reset the device outputs
      *    if no communication is seen whithin timeout
      */
      if (!got_message)
        {
          if (timeout_occured(BUS_COMMUNICATION_WATCHDOG_TIMER, BUS_COMMUNICATION_TIMEOUT_MS))
          {
            // Re-initialize the device - shut every output down
            device_specific_init();
            init_device_comm(HOST_ID, COMM_SPEED_9600_H);

            // Reset the timer
            reset_timeout(BUS_COMMUNICATION_WATCHDOG_TIMER);
          }
        } else {
          reset_timeout(BUS_COMMUNICATION_WATCHDOG_TIMER);
        }
#endif

      // Take care of messaging
      if (got_message && !process_generic_messages())
        {
          // Set response opcode to undefined to ensure issue identification
          response_opcode = RESPONSE_UNDEFINED;

          switch (message_buffer.content[OPCODE])
            {
          case SET_REGISTER:
            // p holds register to write
            p = message_buffer.content[PARAMETER_START];

            // Preset response opcode to success
            response_opcode = COMMAND_SUCCESS;

            if(p>0 && p<=3)
              {
/*          Address 1:  External temp sensor
*           Address 2:  Living temp sensor
*           Address 3:  Upstairs temp sensor
*/
                // Read-only temp registers - command fails
                response_opcode = COMMAND_FAIL;
              } else if( p<=5 ) {
/*          Address 4:  Living floor valve
*           Address 5:  Upstairs floor valve
*/
              pinmask = register_pinmask_map[p-1];

              if (onewire_reset(pinmask))
                {
                  // If the value read and the value got on the bus do not equal then toggle the value of the DS2405 switch
                  if((message_buffer.content[PARAMETER_START + 1] > 0) != ReadDS2405(register_rom_map[p-1], pinmask))
                    {
                      if(onewire_reset(pinmask))
                        {
                          send_onewire_rom_commands(p-1);
                        } else {
                          response_opcode = COMMAND_FAIL;
                        }
                    }
                } else {
                  response_opcode = COMMAND_FAIL;
                }
              } else {
/*            Any other write addresses fail
 */
                response_opcode = COMMAND_FAIL;
              }
            message_buffer.index = PARAMETER_START-1;
            break;
          case READ_REGISTER:
            // p holds register to write
            p = message_buffer.content[PARAMETER_START];

            // Preset response opcode to success
            response_opcode = COMMAND_SUCCESS;

            if(p>0 && p<=3)
              {
/*          Address 1:  External temp sensor
*           Address 2:  Living temp sensor
*           Address 3:  Upstairs temp sensor
*/
                message_buffer.content[PARAMETER_START] = temperatures[p - 1] & 0x00ff;
                message_buffer.content[PARAMETER_START + 1] = (temperatures[p- 1] >> 8) & 0x00ff;
                message_buffer.index = PARAMETER_START + 1;
              } else if( p<=5 ) {
/*          Address 4:  Living floor valve
*           Address 5:  Upstairs floor valve
*/
              pinmask = register_pinmask_map[p-1];
              if (onewire_reset(pinmask))
                {
                  message_buffer.content[PARAMETER_START] = ReadDS2405(register_rom_map[p-1], pinmask);
                  message_buffer.index = PARAMETER_START;
                } else {
                  response_opcode = COMMAND_FAIL;
                  message_buffer.index = PARAMETER_START-1;
                }
              } else if (p == 6){
/*           Test address to read a single onewire device's ROM code
*/
                  pinmask = register_pinmask_map[0];

                  onewire_reset(pinmask);
                  onewire_write_byte(CMD_READ_ROM, pinmask);

                  for (p = 0; p < 8; p++)
                    message_buffer.content[PARAMETER_START+p] =  onewire_read_byte(pinmask);

                  message_buffer.index = PARAMETER_START+7;
              } else {
/*            Any other read addresses fail
*/
                response_opcode = COMMAND_FAIL;
                message_buffer.index = PARAMETER_START-1;
              }
            break;
          default:
            response_opcode = COMMAND_FAIL;
            break;
            }
Пример #29
0
void
owctr_update_counter(void *arg, int bank)
{
	struct owctr_softc *sc = arg;
	u_int32_t counter;
	u_int16_t crc;
	u_int8_t *buf;

	rw_enter_write(&sc->sc_lock);
	onewire_lock(sc->sc_onewire, 0);
	if (onewire_reset(sc->sc_onewire) != 0)
		goto done;

	buf = malloc(DS2423_COUNTER_BUFSZ, M_DEVBUF, M_NOWAIT);
	if (buf == NULL) {
		printf("%s: malloc() failed\n", sc->sc_dev.dv_xname);
		goto done;
	}

	onewire_matchrom(sc->sc_onewire, sc->sc_rom);
	buf[0] = DSCTR_CMD_READ_MEMCOUNTER;
	buf[1] = bank;
	buf[2] = bank >> 8;
	onewire_write_byte(sc->sc_onewire, buf[0]);
	onewire_write_byte(sc->sc_onewire, buf[1]);
	onewire_write_byte(sc->sc_onewire, buf[2]);
	onewire_read_block(sc->sc_onewire, &buf[3], DS2423_COUNTER_BUFSZ-3);

	crc = onewire_crc16(buf, DS2423_COUNTER_BUFSZ-2);
	crc ^= buf[DS2423_COUNTER_BUF_CRC]
		| (buf[DS2423_COUNTER_BUF_CRC+1] << 8);
	if ( crc != 0xffff) {
		printf("%s: invalid CRC\n", sc->sc_dev.dv_xname);
		if (bank == DS2423_COUNTER_BANK_A) {
			sc->sc_counterA.value = 0;
			sc->sc_counterA.status = SENSOR_S_UNKNOWN;
			sc->sc_counterA.flags |= SENSOR_FUNKNOWN;
		} else {
			sc->sc_counterB.value = 0;
			sc->sc_counterB.status = SENSOR_S_UNKNOWN;
			sc->sc_counterB.flags |= SENSOR_FUNKNOWN;
		}
	} else {
		counter = buf[DS2423_COUNTER_BUF_COUNTER]
			| (buf[DS2423_COUNTER_BUF_COUNTER+1] << 8)
			| (buf[DS2423_COUNTER_BUF_COUNTER+2] << 16)
			| (buf[DS2423_COUNTER_BUF_COUNTER+3] << 24);
		if (bank == DS2423_COUNTER_BANK_A) {
			sc->sc_counterA.value = counter;
			sc->sc_counterA.status = SENSOR_S_UNSPEC;
			sc->sc_counterA.flags &= ~SENSOR_FUNKNOWN;
		} else {
			sc->sc_counterB.value = counter;
			sc->sc_counterB.status = SENSOR_S_UNSPEC;
			sc->sc_counterB.flags &= ~SENSOR_FUNKNOWN;
		}
	}

	onewire_reset(sc->sc_onewire);
	free(buf, M_DEVBUF);

done:
	onewire_unlock(sc->sc_onewire);
	rw_exit_write(&sc->sc_lock);
}
Пример #30
0
void onewire_start_temp(void) {
// Temperaturmessung an allen DS18x20 starten
    onewire_reset();
    onewire_send_byte(skip_rom);
    onewire_send_byte(convert_t);
}