// File: /workspace/github_c_makefile_star2_O3/Openvario[P]sensord/humidity.c
// Function: si7021_open
int si7021_open (t_ds2482 *sensor, unsigned char i2c_address) {

	int sernuma, sernumb;
	uint8_t i;

	sensor->fd = open(I2C_DEVICE, O_RDWR);
	if (sensor->fd < 0) {
		fprintf(stderr, "Error opening file: %s\n", strerror(errno));
		return 1;
	}
	if (ioctl(sensor->fd, I2C_SLAVE, i2c_address) < 0) {
		fprintf(stderr, "ioctl error: %s\n", strerror(errno));
		return 1;
	}

	uint8_t data[8]={0xfe,0xe6,131,0,0xe7,0x1e,0x08,0x0a};
	uint8_t data2[4]={0xfa,0x0f,0xfc,0xc9};

	if (write(sensor->fd,data+5,1)!=1) {                                    // Do HTU31D soft reset
		struct timespec nstime = {0,15e6};
		while (nanosleep (&nstime,&nstime)) ;                                           // Wait 15 microseconds after reset
		if (write(sensor->fd,data+6,1)!=1) {					// Request Status byte
			if (read(sensor->fd,data+6,1)==1) {
				fprintf(stderr, "In4\n");
				if (data[6]!=0) return 3;						// Confirm status is good
				if (write(sensor->fd,data+7,1)!=1) return 3;				// Request Serial Number
				if (read(sensor->fd,data2,4)!=4) return 3;				// Read Serial Number
				if (si7021_crc_check((data2[0]<<16)|(data2[1]<<8)|(data2[2]),data2[2])>0) return 4; // Verify CRC on serial number
				switch (sensor->databits) {                                             // Set proper bit configuration
					case 11 : sensor->databits=0x40; break;
					case 12 : sensor->databits=0x4a; break;
					case 13 : sensor->databits=0x54; break;
					case 14 :
					default : sensor->databits=0x5e; break;
				}
				fprintf(stderr, "HTU31D, Serial Number 0x%x%x%x - ",data[0],data[1],data[2]);
				sensor->sensor_type=HTU31D;
				if (g_debug > 0) fprintf(stderr, "Opened HTU31D on 0x%x\n", i2c_address);         // Debug info
				sensor->address = i2c_address;
				return 0;
			}
		}
	}
	if (write(sensor->fd,data,1)<0) return 2;					// Do soft reset for HTU21D/Si7021
	struct timespec nstime = {0,15e6};
	while (nanosleep (&nstime,&nstime)) ;						// Wait 15 microseconds after reset
	if (write(sensor->fd,data+1,2)<0) return 2;					// Program for 11 bits
	if (write(sensor->fd,data+4,1)<0) return 2;					// Initiate readback configuration
	if (read (sensor->fd,data+3,1)<0) return 3;					// Readback configuration
	if ((data[2]&129)!=(data[3]&129)) return 3;					// Is it valid?

	switch (sensor->databits) {							// Set proper bit configuration
		case 11 : data[2] = 131; break;
		case 12 : data[2] = 3;   break;
		case 13 : data[2] = 130; break;
		case 14 :
		default : data[2] = 2;   break;
	}
	if (write(sensor->fd,data+1,2)<0) return 4;					// Program proper bit configuration
	if (write(sensor->fd,data+4,1)<0) return 4;					// Initiate readback configuration
	if (read(sensor->fd,data+3,1)<0) return 4;					// Read back configuration
	if ((data[2]&129)!=(data[3]&129)) return 4; 					// Is it valid?
	if (write(sensor->fd,data2,2)<0) return 4;					// Initiate Serial Number A readback
	if (read (sensor->fd,data,8)<0) return 4;					// Readback Serial Number A
	for (i=sernuma=0 ; i<8 ; i+=2) {
		if (si7021_crc_check(data[i],data[i+1])==1) return 4;			// Check for valid CRC
		sernuma=(sernuma<<8)|(data[i]);
	}

	if (write(sensor->fd,data2+2,2)<0) return 4;					// Initiate Serial Number B readback
	if (read (sensor->fd,data,6)<0) return 4;					// Readback Serial Number B
	for (i=sernumb=0 ; i<6 ; i+=3) {
		if (si7021_crc_check((data[i]<<8)|(data[i+1]),data[i+2])==1) return 4;	// Check for valid CRC
		sernumb=(sernumb<<16)|(data[i]<<8)|(data[i+1]);				// Concatenate Serial Number B
	}

	switch (data[0]) {								// Display Serial number and set sensor type
		case 0x00 :
		case 0xff : sensor->sensor_type=SI7021;
			    fprintf(stderr, "SI7021, Engineering Sample, Serial Number: 0x%x%x\n",sernuma,sernumb); break;
		case 0x0d :
		case 0x14 :
		case 0x15 : sensor->sensor_type=SI7021;
			    fprintf(stderr, "SI70%d, Serial Number: 0x%x%x\n",data[0],sernuma,sernumb); break;
		case 0x32 : sensor->sensor_type=HTU21D;
			    if ((sernumb&0xffff)!=0x4854)
				fprintf(stderr, "HTU21D, but serial number is invalid - ");
			    else
				fprintf(stderr, "HTU21D, Serial Number: 0x%x%x%x%x%x - ",data[3],data[4],sernuma,data[0],data[1]);
			    break;
		default   : sensor->sensor_type=SI7021;
			    fprintf(stderr, "SI70%d, UNKNOWN SENSOR, Serial Number 0x%x%x\n",data[0],sernuma,sernumb);
	}
	if (sensor->sensor_type==SI7021) {						// If it's a SI7021 get the firmware revision
		data[0]=0x84;
		data[1]=0xb8;
		if (write(sensor->fd,data,2)<0) return 4;
		if (read(sensor->fd,data,1)<0) return 4;
		switch (data[0]) {
			case 0xff : fprintf(stderr, "Firmware revision 1.0 - "); break;
			case 0x20 : fprintf(stderr, "Firmware revision 2.0 - "); break;
			default   : fprintf(stderr, "Firmware revision UNKNOWN 0x%x - ",data[0]);
		}
	}

	if (g_debug > 0) {
		if (sensor->sensor_type==SI7021) fprintf(stderr, "Opened SI7021/HTU21D on 0x%x\n", i2c_address);		// Debug info
		else fprintf(stderr, "Opened HTU21D on 0x%x\n",i2c_address);
	}
	sensor->address = i2c_address;
	return (0);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movl %esi, %r14d
movq %rdi, %rbx
leaq 0x16a9(%rip), %rdi      # 0x93d8
movl $0x2, %esi
xorl %eax, %eax
callq 0x2230
movl %eax, (%rbx)
testl %eax, %eax
js 0x8064
movzbl %r14b, %ebp
movl $0x703, %esi            # imm = 0x703
movl %eax, %edi
movl %ebp, %edx
xorl %eax, %eax
callq 0x2140
testl %eax, %eax
js 0x806b
movabsq $0xa081ee70083e6fe, %rax # imm = 0xA081EE70083E6FE
leaq 0x15(%rsp), %rsi
movq %rax, -0x5(%rsi)
movl $0xc9fc0ffa, 0x20(%rsp) # imm = 0xC9FC0FFA
movl (%rbx), %edi
movl $0x1, %edx
callq 0x20b0
cmpq $0x1, %rax
je 0x7de0
movups 0x2e23(%rip), %xmm0     # 0xabb8
leaq 0x30(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
jne 0x7d9e
movl (%rbx), %edi
leaq 0x16(%rsp), %r15
movl $0x1, %edx
movq %r15, %rsi
callq 0x20b0
cmpq $0x1, %rax
je 0x7de0
movl (%rbx), %edi
movl $0x1, %edx
movq %r15, %rsi
callq 0x2180
cmpq $0x1, %rax
je 0x8088
movl (%rbx), %edi
leaq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x20b0
testq %rax, %rax
js 0x805c
movups 0x2db7(%rip), %xmm0     # 0xabb8
leaq 0x30(%rsp), %r15
movaps %xmm0, (%r15)
movq %r15, %rdi
movq %r15, %rsi
callq 0x2120
testl %eax, %eax
jne 0x7e0a
movl (%rbx), %edi
leaq 0x11(%rsp), %r12
movl $0x2, %edx
movq %r12, %rsi
callq 0x20b0
movl $0x2, %r15d
testq %rax, %rax
js 0x8076
movl (%rbx), %edi
leaq 0x14(%rsp), %r13
movl $0x1, %edx
movq %r13, %rsi
callq 0x20b0
testq %rax, %rax
js 0x8076
movl (%rbx), %edi
leaq 0x13(%rsp), %rsi
movl $0x1, %edx
callq 0x2180
movl $0x3, %r15d
testq %rax, %rax
js 0x8076
movb 0x13(%rsp), %al
xorb 0x12(%rsp), %al
testb $-0x7f, %al
jne 0x8076
movl 0x3c(%rbx), %eax
leal -0x58(,%rax,8), %ecx
movl $0x820383, %edx         # imm = 0x820383
shrl %cl, %edx
addl $-0xb, %eax
cmpl $0x3, %eax
movl $0x2, %eax
cmovbl %edx, %eax
movb %al, 0x12(%rsp)
movl (%rbx), %edi
movl $0x2, %edx
movq %r12, %rsi
callq 0x20b0
movl $0x4, %r15d
testq %rax, %rax
js 0x8076
movl (%rbx), %edi
movl $0x1, %edx
movq %r13, %rsi
callq 0x20b0
testq %rax, %rax
js 0x8076
movl (%rbx), %edi
movl $0x1, %edx
leaq 0x13(%rsp), %rsi
callq 0x2180
testq %rax, %rax
js 0x8076
movb 0x13(%rsp), %al
xorb 0x12(%rsp), %al
testb $-0x7f, %al
jne 0x8076
movl (%rbx), %edi
leaq 0x20(%rsp), %rsi
movl $0x2, %edx
callq 0x20b0
testq %rax, %rax
js 0x8076
movl (%rbx), %edi
leaq 0x10(%rsp), %rsi
movl $0x8, %edx
callq 0x2180
testq %rax, %rax
js 0x8076
xorl %eax, %eax
xorl %r12d, %r12d
movzbl 0x10(%rsp,%rax), %ecx
movzbl 0x11(%rsp,%rax), %edx
movl %ecx, %esi
shll $0x8, %esi
movl $0x988000, %edi         # imm = 0x988000
movl $0x800000, %r8d         # imm = 0x800000
movl $0xff8000, %r9d         # imm = 0xFF8000
testl %esi, %r8d
je 0x7f73
movl %edi, %r10d
andl %r9d, %r10d
xorl %r10d, %esi
shrl %r8d
shrl %r9d
shrl %edi
cmpl $0x80, %r8d
jne 0x7f65
cmpl %edx, %esi
jne 0x8076
shll $0x8, %r12d
orl %ecx, %r12d
leaq 0x2(%rax), %rcx
cmpq $0x6, %rax
movq %rcx, %rax
jb 0x7f45
movl (%rbx), %edi
leaq 0x22(%rsp), %rsi
movl $0x2, %edx
callq 0x20b0
testq %rax, %rax
js 0x8076
movl (%rbx), %edi
leaq 0x10(%rsp), %rsi
movl $0x6, %edx
callq 0x2180
testq %rax, %rax
js 0x8076
movl %r12d, 0x24(%rsp)
movl $0x0, 0x1c(%rsp)
movq $-0x3, %r13
movzbl 0x13(%rsp,%r13), %r12d
shll $0x8, %r12d
movzbl 0x14(%rsp,%r13), %eax
movq %rax, 0x28(%rsp)
leal (%r12,%rax), %edi
movzbl 0x15(%rsp,%r13), %esi
callq 0x7a04
testl %eax, %eax
jne 0x8076
movl 0x1c(%rsp), %eax
shll $0x10, %eax
orl %r12d, %eax
orl 0x28(%rsp), %eax
movl %eax, 0x1c(%rsp)
addq $0x3, %r13
jb 0x7fe8
movzbl 0x10(%rsp), %edx
cmpl $0x13, %edx
jle 0x8128
leal -0x14(%rdx), %eax
cmpl $0x2, %eax
jb 0x8131
cmpl $0x32, %edx
je 0x81ef
cmpl $0xff, %edx
je 0x8148
jmp 0x816e
movl $0x2, %r15d
jmp 0x8076
callq 0x3aa4
jmp 0x8070
callq 0x3a78
movl $0x1, %r15d
movl %r15d, %eax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5f69(%rip), %rax      # 0xdff8
movq (%rax), %rcx
leaq 0x296f(%rip), %rdi      # 0xaa08
movl $0x4, %esi
movl $0x1, %edx
callq 0x22a0
movl $0x3, %r15d
cmpb $0x0, 0x16(%rsp)
jne 0x8076
movl (%rbx), %edi
leaq 0x17(%rsp), %rsi
movl $0x1, %edx
callq 0x20b0
cmpq $0x1, %rax
jne 0x8076
movl (%rbx), %edi
leaq 0x20(%rsp), %rsi
movl $0x4, %edx
callq 0x2180
cmpq $0x4, %rax
jne 0x8076
movzbl 0x20(%rsp), %eax
shll $0x10, %eax
movzbl 0x21(%rsp), %edi
shll $0x8, %edi
orl %eax, %edi
movzbl 0x22(%rsp), %esi
orl %esi, %edi
callq 0x7a04
movl $0x4, %r15d
testl %eax, %eax
jne 0x8076
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
movl %ebp, %edx
movl %ebp, %ecx
callq 0x396c
xorl %r15d, %r15d
jmp 0x8076
testl %edx, %edx
je 0x8148
cmpl $0xd, %edx
jne 0x816e
movb $0x3, 0x30(%rbx)
movq 0x5ebc(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x2933(%rip), %rsi      # 0xaa79
jmp 0x8183
movb $0x3, 0x30(%rbx)
movq 0x5ea5(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x28e9(%rip), %rsi      # 0xaa46
movl 0x24(%rsp), %edx
movl 0x1c(%rsp), %ecx
xorl %eax, %eax
callq 0x21c0
jmp 0x8193
movb $0x3, 0x30(%rbx)
movq 0x5e7f(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x2964(%rip), %rsi      # 0xaae7
movl 0x24(%rsp), %ecx
movl 0x1c(%rsp), %r8d
xorl %eax, %eax
callq 0x21c0
cmpb $0x3, 0x30(%rbx)
jne 0x8262
leaq 0x10(%rsp), %rsi
movw $0xb884, (%rsi)         # imm = 0xB884
movl (%rbx), %edi
movl $0x2, %edx
callq 0x20b0
testq %rax, %rax
js 0x8076
movl (%rbx), %edi
leaq 0x10(%rsp), %rsi
movl $0x1, %edx
callq 0x2180
testq %rax, %rax
js 0x8076
movzbl 0x10(%rsp), %edx
cmpl $0x20, %edx
je 0x8243
cmpl $0xff, %edx
jne 0x824a
callq 0x3a28
jmp 0x8262
movb $0x1, 0x30(%rbx)
movzwl 0x1c(%rsp), %eax
movq 0x5df9(%rip), %rcx      # 0xdff8
movq (%rcx), %rdi
cmpl $0x4854, %eax           # imm = 0x4854
jne 0x8239
movzbl 0x13(%rsp), %edx
movzbl 0x14(%rsp), %ecx
movzbl 0x11(%rsp), %eax
movl %eax, (%rsp)
leaq 0x289e(%rip), %rsi      # 0xaac0
movl 0x24(%rsp), %r8d
movl $0x32, %r9d
xorl %eax, %eax
callq 0x21c0
jmp 0x8193
callq 0x39eb
jmp 0x8193
callq 0x3a06
jmp 0x8262
movq 0x5da7(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x28ec(%rip), %rsi      # 0xab47
xorl %eax, %eax
callq 0x21c0
leaq 0x620b(%rip), %rax      # 0xe474
cmpl $0x0, (%rax)
jg 0x8277
movb %r14b, 0x4(%rbx)
jmp 0x8120
leaq 0x30(%rbx), %rdi
movl %ebp, %esi
callq 0x3a4a
jmp 0x826e