Esempio n. 1
0
//*----------------------------------------------------------------------------
//* Name     : ds_read_all_ram()
//* Brief    : Read all DS18B20 RAM register
//* Argument : DS_REG pointer  
//* Return   : Error = -1, OK = 0
//*----------------------------------------------------------------------------
static ds_read_all_ram(DS18B20_REG_T *ds) {
	int i;
	uint8 *c = NULL;
	
	if(ds == NULL) {
		return -1;	
	}	
	
	// reset DS18B20
	if(ds_reset() < 0) {
		return -1;	
	}
	
	ds_write_match_rom(ds);
	ds_write_byte(DS_CMD_READ_RAM);
	
	c = &ds->temp_lsb;
	for(i = 0; i < 9; i ++) {
		*(c ++) = ds_read_byte();	
	}
	// reset DS18B20
	if(ds_reset() < 0) {
		return -1;	
	}
	// check crc here
	if(ds->ram_crc7 !=  crc7(0, &ds->temp_lsb, 8)) {
		return -1;	
	}
	
	// calculate conver time
	return ds_calculate_conver_time(ds);
}
static dsStatus ds2431_read_memory (DALLAS_CONTEXT *dc,
                                    unsigned int address,
                                    unsigned char *data,
                                    int bufsiz)
{
  dsStatus stat;
  unsigned int i;

  if (!dc->in_mask) {
    return dsError_NoDevice;
  }
  if (address + bufsiz > 0x90) {
    return dsError_NoDevice;
  }

  stat = ds_reset(dc);
  if (stat != dsError_None) {
    return stat;
  }

  /* Only one device; skip ROM check */
  ds_write_byte(dc, SKIP_ROM);
  ds_write_byte(dc, READ_MEMORY);
  ds_write_byte(dc, address);
  ds_write_byte(dc, address >> 8);

  for (i = 0; i < bufsiz; i++) {
    data[i] = (unsigned char)ds_read_byte(dc);
  }

  return ds_reset(dc);
} /* ds2431_read_memory */
/* Read data from the ROM (which contains the S/N).
 */
static dsStatus ds_read_rom_64bit_single (DALLAS_CONTEXT *dc, int port,
                                          unsigned char *data)
{
  int x;
  dsStatus stat;

  if (!dc->in_mask) {
    return dsError_NoDevice;
  }

  stat = ds_reset(dc);
  if (stat != dsError_None) {
    return stat;
  }

  ds_write_byte(dc, READ_ROM);

  for (x = 0; x < 8; x++) {
    *(data + x) = (unsigned char)ds_read_byte(dc);
  }

  if (*data != DS2430_CODE && *data != DS2431_CODE) {
    return dsError_WrongDevice;
  }

  return calcCRC8rom(data) ? dsError_CRCError : dsError_None;
} /* ds_read_rom_64bit_single */
Esempio n. 4
0
//*----------------------------------------------------------------------------
//* Name     : ds_read_all_rom() 
//* Brief    : Read all DS18B20 ROM registers
//* Argument : DS_REG pointer    
//* Return   : Error = -1, OK = 0
//*----------------------------------------------------------------------------
static int ds_read_all_rom(DS18B20_REG_T *ds) {
	int i;
	
	if(ds == NULL) {
		return -1;	
	}
	
	// reset DS18B20
	if(ds_reset() < 0) {
		return -1;	
	}
	
	// Write commands
	ds_write_byte(DS_CMD_READ_ROM);
	
	// read 8 + 48 + 8 bits
	ds->ds_id = ds_read_byte();
	for(i = 0; i < 6; i++) {
		ds->serial_num[i] = ds_read_byte(); 
	}
	ds->rom_crc7 = ds_read_byte();
	
	// check crc7 here
	if(ds->rom_crc7 != crc7(0, (unsigned char *)ds, 7)) {
		return -1;
	}
	return 0;
}
Esempio n. 5
0
ICACHE_FLASH_ATTR
static void ds_read_temp(uint8_t *addr) {
	uint8_t data[12];

	ds_reset();
	select(addr);
	write( DS1820_READ_SCRATCHPAD, 0);

	uint8_t i;
	for (i = 0; i < 9; i++) {
		data[i] = read();
	}

	if(data[8] != crc8(data, 8)) {
		os_printf("ERROR - DS18B20 - CRC mismatch\n");
        mqttPublishError(&mqttClient, "CRC mismatch");
//		return;
	}

// float arithmetic isn't really necessary, tVal and tFract are in 1/10 �C
	uint16_t tVal, tFract;
	char tSign;

	tVal = (data[1] << 8) | data[0];
	if (tVal & 0x8000) {
		tVal = (tVal ^ 0xffff) + 1;				// 2's complement
		tSign = '-';
	} else
		tSign = ' ';

// datasize differs between DS18S20 and DS18B20 - 9bit vs 12bit
	if (addr[0] == DS18S20) {
		tFract = (tVal & 0x01) ? 50 : 0;		// 1bit Fract for DS18S20
		tVal >>= 1;
	} else {
/* Read the 64-bit (8-byte) application area of some devices.
 */
dsStatus ds_read_application_area (DALLAS_CONTEXT *dc,
                                   unsigned int address,
                                   unsigned char *data,
                                   int bufsiz)
{
  unsigned int x;
  dsStatus stat;

  if (!dc->in_mask) {
    return dsError_NoDevice;
  }

  stat = ds_reset(dc);
  if (stat != dsError_None) {
    return stat;
  }

  ds_write_byte(dc, SKIP_ROM);
  ds_write_byte(dc, READ_STATUS);
  ds_write_byte(dc, 0); /* Validation byte */

  x = ds_read_byte(dc);
  if (x != 0xFC) {
    return dsError_NotProgrammed;
  }

  stat = ds_reset(dc);
  if (stat != dsError_None) {
    return stat;
  }

  ds_write_byte(dc, SKIP_ROM);
  ds_write_byte(dc, READ_APPLICATION);

  /* LSB of address */
  ds_write_byte(dc, address & 0xFF);

  for (x = 0; x < bufsiz; x++) {
    *(data + x) = (unsigned char)ds_read_byte(dc);
  }

  return dsError_None;
} /* ds_read_application_area */
Esempio n. 7
0
//return current mahr 可以读写
float ds_get_acr(void)
{
	float acr;
	while(!ds_reset());
	ds_write_byte(CMD_ONEWIRE_SKIP_ROM);
	ds_write_byte(CMD_ONEWIRE_READ);
	ds_write_byte(CMD_ADDR_ACR);
	uint8_t msb =ds_read_byte();
	acr=(float)(msb);
	return(acr);
}
Esempio n. 8
0
// return temperature C
float ds_get_temperature(void)
{
	float temperature;
	while(!ds_reset());
	ds_write_byte(CMD_ONEWIRE_SKIP_ROM);
	ds_write_byte(CMD_ONEWIRE_READ);
	ds_write_byte(CMD_ADDR_TEMP);
	uint16_t msb =ds_read_byte();
	uint16_t lsb =ds_read_byte();
	uint16_t t = (msb<<8)|lsb;
	temperature=(float)t/32*0.125;
	return(temperature);
}
Esempio n. 9
0
//return current ma
float ds_get_current(void)
{
	float current;
	while(!ds_reset());
	ds_write_byte(CMD_ONEWIRE_SKIP_ROM);
	ds_write_byte(CMD_ONEWIRE_READ);
	ds_write_byte(CMD_ADDR_CURRENT);
	uint16_t msb =ds_read_byte();
	int16_t lsb =ds_read_byte();
	int16_t t = (msb<<8)|lsb;
	current=(float)(t*0.07813);
	return(current);
}
Esempio n. 10
0
//return volt v
float ds_get_volt(void)
{
	float volt;
	while(!ds_reset());
	ds_write_byte(CMD_ONEWIRE_SKIP_ROM);
	ds_write_byte(CMD_ONEWIRE_READ);
	ds_write_byte(CMD_ADDR_VOLT);
	uint16_t msb =ds_read_byte();
	uint16_t lsb =ds_read_byte();
	uint16_t t = (msb<<8)|lsb;
	volt=(float)(t>>5)*0.00488;
	return(volt);
}
Esempio n. 11
0
//*----------------------------------------------------------------------------
//* Name     : ds_read_temp()
//* Brief    : Read tempreture from DS18B20, should be called after ds_star-
//*			   t_con(), time para is in ds->con_time
//* Argument : DS_REG pointer    
//* Return   : Error = -1, OK = 0
//*----------------------------------------------------------------------------
int DS_read_temp(DS18B20_REG_T * ds) {
	
	if(ds == NULL) {
		return -1;	
	}	
	// reset DS18B20
	if(ds_reset() < 0)
		return -1;
	// we read all ram and check crc
	if(ds_read_all_ram(ds) != 0)
		return -1;
	return ((ds->temp_msb << 8) | ds->temp_lsb);
}
Esempio n. 12
0
//*----------------------------------------------------------------------------
//* Name     : ds_start_con()
//* Brief    : Write command "Start convert tempreture"
//* Argument : DS_REG pointer    
//* Return   : Error = -1, OK = 0
//*----------------------------------------------------------------------------
int DS_start_con(DS18B20_REG_T * ds) {
	if(ds == NULL) {
		return -1;	
	}
	// reset first
	if(ds_reset() < 0) {
		return -1;	
	}
	// write match rom command
	ds_write_match_rom(ds);
	// write convert command
	ds_write_byte(DS_CMD_START_CON);
	return 0;
}
Esempio n. 13
0
int ds_search_rom(uint8_t *id, uint8_t mask)
{
	uint8_t r, i, j;
	uint8_t crc, b1, b2;
	
	/* Reset the sensor(s) */
	r = ds_reset();
	if(r != DS_OK) return(r);
	
	/* Transmit the SEARCH ROM command */
	ds_write_byte(0xF0);
	
	/* An ID is made up of 8 bytes (7 + CRC) */
	for(crc = i = 0; i < 8; i++)
	{
		*id = 0;
		
		for(j = 0; j < 8; j++)
		{
			/* Read the bit and its complement */
			b1 = ds_read_bit();
			b2 = ds_read_bit();
			
			/* Both bits should never be 1 */
			if(b1 && b2) return(DS_ERROR);
			
			/* Both bits are 0 when two or more sensors
			 * respond with a different value */
			if(b1 == b2)
			{
				b1 = mask & 1;
				mask >>= 1;
				
				/* Test if this is the last ID on the bus */
				if(!b1) r = DS_MORE;
			}
			
			/* Let the sensors know which direction we're going */
			ds_write_bit(b1);
			
			*id >>= 1;
			if(b1) *id |= 1 << 7;
		}
		
		/* Update the CRC, compare with last byte */
		if(i != 7) crc = _crc_ibutton_update(crc, *id);
		else if(crc != *id) return(DS_BADCRC);
		
		id++;
	}
Esempio n. 14
0
//*----------------------------------------------------------------------------
//* Name     : ds_configcon_type()
//* Brief    : Config conver BIT type
//* Argument : DS_TYPE  
//* Return   : Error = -1, OK = 0
//*----------------------------------------------------------------------------
static int ds_config_con_type(DS18B20_REG_T * ds, DS_TEMP_T type) {
	uint8 buf = 0;
	
	if(ds == NULL) {
		return -1;	
	}	
	
	// reset DS18B20
	if(ds_reset() < 0) {
		return -1;	
	}
	buf |= (type << 5);	
	
	// Write commands
	ds_write_match_rom(ds);
	ds_write_byte(DS_CMD_WRITE_RAM);
	
	ds_write_byte(ds->usr_byte[0]);
	ds_write_byte(ds->usr_byte[1]);
	ds_write_byte(buf);
	
	// reset DS18B20
	if(ds_reset() < 0) {
		return -1;	
	}
	
	// read back
	if(ds_read_all_ram(ds) < 0) {
		return -1;	
	}
	
	// check if write successed
	if(ds->config != (buf | 0x1f)) {
		return -1;
	} 
	return 0;
}
/* Return 1 if at least one device is present on the bus.
 * Loops 100 times if no device is found.
 */
int ds_check_for_presence (DALLAS_CONTEXT *dc)
{
  int i;

  if (!dc->in_mask) {
    return 0;
  }

  for (i = 0; i < 100; i++) {
    if (ds_reset(dc) == dsError_None) {
      return 1;
    }
  }

  return 0;
} /* ds_check_for_presence */
/* Read data from the memory.
 * This will work for commands READ MEMORY, READ SCRATCHPAD,
 * READ STATUS, READ APPLICATION REG
 */
dsStatus ds_read_string (DALLAS_CONTEXT *dc,
                         unsigned int command,
                         unsigned int address,
                         unsigned char *data,
                         int bufsiz)
{
  unsigned int i;

  if (!dc->in_mask) {
    return dsError_NoDevice;
  }

  ds_write_byte(dc, command);
  ds_write_byte(dc, address);

  for (i = 0; i < bufsiz; i++) {
    *(data + i) = (unsigned char)ds_read_byte(dc);
  }

  return ds_reset(dc);
} /* ds_read_string */
/* Read data from the memory.
 */
static dsStatus ds2430_read_memory (DALLAS_CONTEXT *dc,
                                    unsigned int address,
                                    unsigned char *data,
                                    int bufsiz)
{
  dsStatus stat;

  if (!dc->in_mask) {
    return dsError_NoDevice;
  }

  address &= 0x1F;
  stat = ds_reset(dc);
  if (stat != dsError_None) {
    return stat;
  }

  /* Only one device; skip ROM check */
  ds_write_byte(dc, SKIP_ROM);

  return ds_read_string(dc, READ_MEMORY, address, data, bufsiz );
} /* ds2430_read_memory */
Esempio n. 18
0
/*------------------------------------------------------------*/
boolean
io_fget_ds (FILE *file, dynstring *ds)
     /*
       Read one complete line (up to, but excluding, the '\n' character).
       Return TRUE if successful.
     */
{
  int ch;

  /* pre */
  assert (file);
  assert (ds);
  assert (ds_valid_state (ds));

  ds_reset (ds);
  while ((ch = fgetc (file))) {
    if (ch == '\n') break;
    if (ch == EOF) break;
    ds_add (ds, ch);
  }

  return TRUE;
}