Пример #1
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;
}
Пример #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
/** Odczytuje dane ze pamięci czujnika.
	@param buffer bufor, do którego zapisywane są dane
*/
void temp_read_scratchpad(uint8_t* buffer) {
	char i;
	onewire_write_byte(DS18B20_READ_SCRATCHPAD);
	for(i=0; i<9; i++) {
		*buffer++= onewire_read_byte();
	}
}
Пример #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
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
    }
}
Пример #6
0
/** Ustawia kolejna konwersję temperatury
@param id indentyfikator czujnika
*/
void temp_set_next_convert(uint8_t id) {
	if(onewire_present()==IS_PRESENT) {
		onewire_match_rom(&(temp_sensors[id].rom[0]));
		onewire_write_byte(DS18B20_ONEWIRE_CONVERT_T);
		temp_sensors[id].flags |= _BV(TEMP_SENSOR_CONVERT);
	} else {
		temp_set_failure(id,ON);
	}
}
Пример #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
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;
    }
}
Пример #9
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;
	}
}
Пример #10
0
void
send_onewire_rom_commands(unsigned char register_id)
{
  unsigned char i, pinmask = register_pinmask_map[register_id];

  // If no ROM is specified for this device (there is only one device on this bus)
  // issue a "SKIP ROM" otherwise issue a "MATCH ROM" followed by the ROM code
  if (register_rom_map[register_id][0] == SINGLE_DEVICE_ON_BUS)
    onewire_write_byte(CMD_SKIP_ROM, pinmask);
  else
    {
      i = 0;
      // Issue "MATCH ROM"
      onewire_write_byte(CMD_MATCH_ROM, pinmask);

      // Write the 64 bit ROM code
      do
        onewire_write_byte(register_rom_map[register_id][i], pinmask);
      while (i++ < 7);
    }
}
Пример #11
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);
  }
}
Пример #12
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;
}
Пример #13
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;
}
Пример #14
0
void onewire_example(void)
{
	unsigned char i, buf[9], type, pinmask = 0x04;
	int dly, temperature;

//	printf("1-wire Temperature Test:\r\n");
	if (onewire_reset(pinmask)) {
//		printf("presence pulse ok\r\n");
		// It isn't really necessary to read the ROM if
		// you know which chip is connected.  A lot of
		// the code below can be removed if you only
		// use one type of sensor.
///		printf("ROM: ");
		onewire_write_byte(CMD_READ_ROM, pinmask);
		for (i=0; i<8; i++) {
			buf[i] = onewire_read_byte(pinmask);
//			printf("%x ", buf[i]);
		}
//		printf(" CRC=%s\r\n",
			onewire_crc_check(buf, 8);
		type = buf[0];
//		printf("Chip ID: ");
/*
		switch (type) {
		  case 0x10: printf("DS18S20\r\n"); break;
		  case 0x28: printf("DS18B20\r\n"); break;
		  case 0x22: printf("DS1822\r\n"); break;
		  default: printf("unknown\r\n"); break;
		}
*/
		// if it's a configurable resolution, set it for 12 bits
		if ((type == 0x28 || type == 0x22) && (buf[4] & 0x60) != 0x60) {
			onewire_write_byte(CMD_WRITE_SCRATCHPAD, pinmask);
			onewire_write_byte(0, pinmask);
			onewire_write_byte(0, pinmask);
			onewire_write_byte(0x7F, pinmask); // config
		}
//		printf("Begin temperature measurement\r\n");
		onewire_reset(pinmask);
		onewire_write_byte(CMD_SKIP_ROM, pinmask);
		onewire_write_byte(CMD_CONVERT_T, pinmask);
		//STRONG_PULLUP_PIN = 0;  // turn on strong pullup transistor
		for (dly=0; dly<1563; dly++) {
			delay_480us();
		}
		//STRONG_PULLUP_PIN = 1;  // turn off strong pullup
		onewire_reset(pinmask);
		// Temperature measurement is complete, now read it
		onewire_write_byte(CMD_SKIP_ROM, pinmask);
		onewire_write_byte(CMD_READ_SCRATCHPAD, pinmask);
//		printf("Data: ");
		for (i=0; i<9; i++) {
			buf[i] = onewire_read_byte(pinmask);
//			printf("%x ", buf[i]);
		}
//		printf(" CRC=%s\r\n",
//			onewire_crc_check(buf, 9);
		temperature = buf[0] | (buf[1] << 8);
		// if it's DS18S20, scale up to 12 bit resolution
		if (type == 0x10) {
			temperature *= 8;
			if (buf[7] == 0x10) {
				temperature &= 0xFFF0;
				temperature += 12;
				temperature -= buf[6];
			} else {
//				printf("Err: count remain not 0x10\r\n");
			}
		}
#ifdef NICE_OUTPUT
//		printf("Temperature: %.2f Celsius\r\n",
//			(float)temperature * 0.0625;
//		printf("Temperature: %.2f Fahrenheit\r\n",
//			(float)temperature * 0.1125 + 32.0;
#else
		// result is deg_C * 16
		printf("Temperature: %d / 16\r\n", temperature);
#endif
	} else {
//		printf("no 1-wire devices present\r\n");
	}


//	printf_fast("\r\n\r\n\r\npress any key to reboot");
//	pm2_cin();
//	pm2_restart();  /* ESC to quit */
}
Пример #15
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);
        }
Пример #16
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;
}
Пример #17
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)
{

  // Messaging variables
  unsigned char response_opcode = RESPONSE_UNDEFINED, p;

  bool got_message;

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

      got_message = get_device_message();

/*
 *    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_phase1();
            init_device_comm(HOST_ID, COMM_SPEED_9600_H);
            device_specific_init_phase2();

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

      // Take care of messaging
      if (got_message && !process_generic_messages())
        {
          // Set response opcode to undefined to filter response opcode programming issues
          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<=4 )
              {
/*          Address 1:  HW temp sensor
*           Address 2:  Basement temp sensor
*           Address 3:  Return temp sensor
*           Address 4:  Hidr Shift temp sensor
*/
              response_opcode = COMMAND_FAIL;
              } else if( p <= 12 ) {
/*
*           Address 5:  Buffertop valve - Contact 2
*           Address 6:  Return valve - Contact 3
*           Address 7:  Radiator pump - Contact 4
*           Address 8:  Floor pump - Contact 5
*           Address 9:  Hidraulic Shifter pump - Contact 6
*           Address 10:  HW pump - Contact 7
*           Address 11:  Basement radiator valve Contact 8
*           Address 12:  Basement floor valve - Contact 9
*/
               write_extender(p-5, message_buffer.content[PARAMETER_START + 1] > 0);
              } else if(p == 13) {
/*          Address 13: Heater relay P3_5 - Heater contact */
              HEATER_RELAY_PIN = message_buffer.content[PARAMETER_START + 1] > 0;
              } else if(p <= 15) {
/*
*           Temp wipers - expected data format:
*           Byte 1 & 2 - 9 bits of data holdiong the desired wiper setting
*           Byte 3 - bool flag - is volatile
*           Address 14: HW Wiper
*           Address 15: Heating Wiper
*/
              if (!write_wiper(
                  (message_buffer.content[PARAMETER_START+1] << 8) | message_buffer.content[PARAMETER_START+2] ,
                  message_buffer.content[PARAMETER_START + 3]>0,
                  p == 14 ? WIPER_HW : WIPER_HEAT))
                response_opcode = COMMAND_FAIL;
              } else {
/*          Any other address fails */
              response_opcode = COMMAND_FAIL;
            }

            message_buffer.index = PARAMETER_START-1;

            break;

          case READ_REGISTER:
            // p holds register to read
            p = message_buffer.content[PARAMETER_START];

            // Preset response opcode to success
            response_opcode = COMMAND_SUCCESS;

            if ( p>0 && p<=4 )
              {
/*          Address 1:  HW temp sensor
*           Address 2:  Basement temp sensor
*           Address 3:  Return temp sensor
*           Address 4:  Hidr Shift 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 <= 12 ) {
/*
*           Address 5:  Buffertop valve - Contact 2
*           Address 6:  Return valve - Contact 3
*           Address 7:  Radiator pump - Contact 4
*           Address 8:  Floor pump - Contact 5
*           Address 9:  Hidraulic Shifter pump - Contact 6
*           Address 10:  HW pump - Contact 7
*           Address 11:  Basement radiator valve Contact 8
*           Address 12:  Basement floor valve - Contact 9
*/
              message_buffer.content[PARAMETER_START] = get_extender_value(p-5);
              message_buffer.index = PARAMETER_START;
              } else if(p == 13) {
 /*          Address 13: Heater relay P3_5 - Heater contact */
              message_buffer.content[PARAMETER_START] = HEATER_RELAY_PIN;
              message_buffer.index = PARAMETER_START;
              } else if(p <= 15) {
/*
*             Temp wipers - expected data format:
*             Byte 1 - bool flag - is volatile
*             Address 14: HW Wiper
*             Address 15: Heating Wiper
*/
              read_wiper((unsigned int*)(message_buffer.content+PARAMETER_START), message_buffer.content[PARAMETER_START+1]>0, p == 14 ? WIPER_HW : WIPER_HEAT);
              message_buffer.index = PARAMETER_START+1;
              } else if(p == 16) {
/* Test address to read rom on onewire bus - a single device should be connected to the bus in this case */

              onewire_reset(0x04);
              onewire_write_byte(CMD_READ_ROM, 0x04);

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

              message_buffer.index = PARAMETER_START+7;
              } else {
              response_opcode = COMMAND_FAIL;
              message_buffer.index = PARAMETER_START-1;
              }
            break;

          // Any other message code fails
          default:
            response_opcode = COMMAND_FAIL;
            message_buffer.index = PARAMETER_START-1;
            break;
            }
Пример #18
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);
}
Пример #19
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;
            }