#include <string.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <util/atomic.h>
#include "onewire.h"
uint8_t err=0;
ONEWIRE_PORT &= ~ONEWIRE_MASK;
ONEWIRE_DDR |= ONEWIRE_MASK;
_delay_us(480);
uint8_t sreg_tmp=SREG; cli();
ONEWIRE_DDR &= ~ONEWIRE_MASK;
_delay_us(66);
if( (ONEWIRE_PIN & ONEWIRE_MASK)) {
err = 1;
}
SREG = sreg_tmp;
_delay_us(480);
if( !(ONEWIRE_PIN & ONEWIRE_MASK)) {
err = 2;
}
return err;
}
uint8_t sreg_tmp=SREG; cli();
if (!(wrbit & 1)) {
ONEWIRE_PORT &= ~ONEWIRE_MASK;
ONEWIRE_DDR |= ONEWIRE_MASK;
_delay_us(95);
ONEWIRE_DDR &= ~ONEWIRE_MASK;
_delay_us(5);
} else {
ONEWIRE_PORT &= ~ONEWIRE_MASK;
ONEWIRE_DDR |= ONEWIRE_MASK;
_delay_us(10);
ONEWIRE_DDR &= ~ONEWIRE_MASK;
_delay_us(90);
}
SREG = sreg_tmp;
}
uint8_t rebit;
uint8_t sreg_tmp=SREG; cli();
ONEWIRE_PORT &= ~ONEWIRE_MASK;
ONEWIRE_DDR |= ONEWIRE_MASK;
_delay_us(3);
ONEWIRE_DDR &= ~ONEWIRE_MASK;
_delay_us(10);
rebit = ONEWIRE_PIN & ONEWIRE_MASK;
_delay_us(87);
SREG = sreg_tmp;
if (rebit) {
return 1;
} else {
return 0;
}
}
uint8_t data=0;
uint8_t i;
for (i=0; i<8; i++) {
data >>= 1;
data |= 0x80;
}
}
return data;
}
uint8_t i;
for (i=0; i<8; i++) {
data >>= 1;
}
}
memset(buffer, 0, 8);
}
}
}
uint8_t mask, i, j, bit, rom_tmp;
uint8_t max_conf_zero=0;
static uint8_t max_conf_old;
uint8_t branch_flag=0;
if (buffer == NULL) {
max_conf_old=64;
return 0;
}
return 1;
} else {
rom_tmp = buffer[0];
i=0;
mask=1;
for(j=0; j<64; j++) {
switch(bit) {
case 0:
if (j < max_conf_old) {
if (rom_tmp & mask) {
bit = 1;
} else {
bit = 0;
max_conf_zero = j;
branch_flag = 1;
}
} else if (j == max_conf_old) {
bit = 1;
} else {
bit = 0;
max_conf_zero = j;
branch_flag = 1;
}
break;
case 1:
case 2:
break;
case 3:
return 3;
break;
}
if (bit & 1) {
rom_tmp |= mask;
} else {
rom_tmp &= ~mask;
}
mask <<= 1;
if (mask == 0) {
mask = 1;
buffer[i] = rom_tmp;
i++;
if (i<8) {
rom_tmp = buffer[i];
}
}
}
}
max_conf_old = max_conf_zero;
if (branch_flag) {
return 0;
} else {
return 4;
}
}
uint8_t mask, tmp=0, i, j;
return 1;
} else {
mask=0;
i=0;
for (j=0; j<64; j++) {
mask <<= 1;
if (mask == 0) {
mask = 1;
tmp = rom[i];
i++;
}
if (tmp & mask) {
} else {
}
}
}
return 0;
}
return 1;
} else {
}
return 0;
}
uint8_t mask=1, tmp=0, i=0, j;
return 1;
} else {
for (j=0; j<64; j++) {
tmp |= mask;
}
mask <<= 1;
if (mask == 0) {
mask = 1;
rom[i]=tmp;
tmp=0;
i++;
}
}
return 2;
}
}
return 0;
}
uint8_t
onewire_crc(
const uint8_t *data, uint8_t cnt) {
static const uint8_t crc_table[16] PROGMEM =
{0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8,
0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74};
uint8_t crc, i, tmp;
crc = *data++;
cnt--;
for(; cnt>0; cnt--) {
tmp = *data++;
i = crc & 0x0F;
crc = (crc >> 4) | (tmp << 4);
crc ^= pgm_read_byte(&crc_table[i]);
i = crc & 0x0F;
crc = (crc >> 4) | (tmp & 0xF0);
crc ^= pgm_read_byte(&crc_table[i]);
}
return crc;
}