// File: /workspace/github_c_makefile_star2_O0/Openvario[P]sensord/ms5611.c
// Function: ms5611_init
int ms5611_init(t_ms5611 *sensor)
{
	uint8_t buf[10];
	uint8_t a,i,n_crc, crc;
	//uint16_t prom[8]={0x3132,0x3334,0x3536,0x3738,0x3940,0x4142,0x4344,0x4500};
	uint16_t prom[8];

	// Print debug info
	ddebug_print("Sensor compensation data: Offset: %f, Linearity: %f\n", sensor->offset, sensor->linearity);

	// get calibration data from sensor
	ddebug_print("Get calibration data ...\n");

	for(a = 0xA0, i = 0; a <= 0xAE; a = a +0x02, i++)
	{
		// get calibration values
		buf[0] = a;													// This is the register we want to read from
		if ((write(sensor->fd, buf, 1)) != 1) {								// Send register we want to read from
			fprintf(stderr, "Error writing to i2c slave (write cal reg)\n");
			return(1);
		}
		usleep(10000);
		if (read(sensor->fd, buf, 2) != 2) {								// Read back data into buf[]
			fprintf(stderr, "Unable to read from slave (get cal reg)\n");
			return(1);
		}
		ddebug_print("Read adr: 0x%x data: 0x%x 0x%x\n", a, buf[0], buf[1]);
		prom[i] = (buf[0] * 256) + buf[1];
		ddebug_print("Adr = 0x%x %u\n", a, prom[i]);
	}


	n_crc=crc4(prom);
	crc = prom[7] & 0xF;

	ddebug_print("CRC Check: calc: 0x%x read: 0x%x => ", n_crc, crc);
	// check if CRC is correct
	if (n_crc == crc)
	{
		ddebug_print("OK\n");
	}
	else
	{
		ddebug_print("WRONG !!!!\n");
	}

	sensor->C1s = prom[1] << 15;
	sensor->C2s = prom[2] << 16;
	sensor->C3  = prom[3];
	sensor->C4  = prom[4];
	sensor->C5s = prom[5] << 8;
	sensor->C6  = prom[6];

	// print calibration values if debug is enabled
	ddebug_print("Calibration values:\n");

	ddebug_print("C1s = %u\n", sensor->C1s);
	ddebug_print("C2s = %u\n", sensor->C2s);
	ddebug_print("C3  = %u\n", sensor->C3);
	ddebug_print("C4  = %u\n", sensor->C4);
	ddebug_print("C5s = %u\n", sensor->C5s);
	ddebug_print("C6  = %u\n", sensor->C6);

	return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
leaq 0xcc11(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x28a0
movq 0xc789(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movss 0x90(%rax), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x10(%rbp), %rax
movss 0x8c(%rax), %xmm1
cvtss2sd %xmm1, %xmm1
leaq 0x87ef(%rip), %rsi      # 0xb088
movb $0x2, %al
callq 0x21d0
leaq 0xcbcd(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x28c4
movq 0xc745(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x8800(%rip), %rsi      # 0xb0bd
movb $0x0, %al
callq 0x21d0
movb $-0x60, -0x1b(%rbp)
movb $0x0, -0x1c(%rbp)
movzbl -0x1b(%rbp), %eax
cmpl $0xae, %eax
jg 0x29fb
movb -0x1b(%rbp), %al
movb %al, -0x1a(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0x1a(%rbp), %rsi
movl $0x1, %edx
callq 0x20a0
cmpq $0x1, %rax
je 0x291f
movq 0xc6f6(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x87cb(%rip), %rsi      # 0xb0d7
movb $0x0, %al
callq 0x21d0
movl $0x1, -0x4(%rbp)
jmp 0x2c13
movl $0x2710, %edi           # imm = 0x2710
callq 0x2300
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0x1a(%rbp), %rsi
movl $0x2, %edx
callq 0x2180
cmpq $0x2, %rax
je 0x2967
movq 0xc6ae(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x87af(%rip), %rsi      # 0xb103
movb $0x0, %al
callq 0x21d0
movl $0x1, -0x4(%rbp)
jmp 0x2c13
leaq 0xcb06(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2998
movq 0xc67e(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzbl -0x1b(%rbp), %edx
movzbl -0x1a(%rbp), %ecx
movzbl -0x19(%rbp), %r8d
leaq 0x879b(%rip), %rsi      # 0xb12c
movb $0x0, %al
callq 0x21d0
movzbl -0x1a(%rbp), %eax
shll $0x8, %eax
movzbl -0x19(%rbp), %ecx
addl %ecx, %eax
movw %ax, %cx
movzbl -0x1c(%rbp), %eax
movw %cx, -0x30(%rbp,%rax,2)
leaq 0xcabc(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x29e2
movq 0xc634(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzbl -0x1b(%rbp), %edx
movzbl -0x1c(%rbp), %eax
movzwl -0x30(%rbp,%rax,2), %ecx
leaq 0x8771(%rip), %rsi      # 0xb14c
movb $0x0, %al
callq 0x21d0
jmp 0x29e4
movzbl -0x1b(%rbp), %eax
addl $0x2, %eax
movb %al, -0x1b(%rbp)
movb -0x1c(%rbp), %al
addb $0x1, %al
movb %al, -0x1c(%rbp)
jmp 0x28cc
leaq -0x30(%rbp), %rdi
callq 0x2740
movb %al, -0x1d(%rbp)
movzwl -0x22(%rbp), %eax
andl $0xf, %eax
movb %al, -0x1e(%rbp)
leaq 0xca5c(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2a3d
movq 0xc5d4(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movzbl -0x1d(%rbp), %edx
movzbl -0x1e(%rbp), %ecx
leaq 0x8725(%rip), %rsi      # 0xb15b
movb $0x0, %al
callq 0x21d0
movzbl -0x1d(%rbp), %eax
movzbl -0x1e(%rbp), %ecx
cmpl %ecx, %eax
jne 0x2a6f
leaq 0xca24(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2a6d
movq 0xc59c(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x871a(%rip), %rsi      # 0xb180
movb $0x0, %al
callq 0x21d0
jmp 0x2a95
leaq 0xc9fe(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2a93
movq 0xc576(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x86f8(%rip), %rsi      # 0xb184
movb $0x0, %al
callq 0x21d0
jmp 0x2a95
movzwl -0x2e(%rbp), %ecx
shll $0xf, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movzwl -0x2c(%rbp), %ecx
shll $0x10, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movw -0x2a(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x10(%rax)
movw -0x28(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x12(%rax)
movzwl -0x26(%rbp), %ecx
shll $0x8, %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x14(%rax)
movw -0x24(%rbp), %cx
movq -0x10(%rbp), %rax
movw %cx, 0x18(%rax)
leaq 0xc98a(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2b07
movq 0xc502(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x8690(%rip), %rsi      # 0xb190
movb $0x0, %al
callq 0x21d0
leaq 0xc966(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2b32
movq 0xc4de(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x867a(%rip), %rsi      # 0xb1a5
movb $0x0, %al
callq 0x21d0
leaq 0xc93b(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2b5d
movq 0xc4b3(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0xc(%rax), %edx
leaq 0x8659(%rip), %rsi      # 0xb1af
movb $0x0, %al
callq 0x21d0
leaq 0xc910(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2b89
movq 0xc488(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x10(%rax), %edx
leaq 0x8637(%rip), %rsi      # 0xb1b9
movb $0x0, %al
callq 0x21d0
leaq 0xc8e4(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2bb5
movq 0xc45c(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x12(%rax), %edx
leaq 0x8615(%rip), %rsi      # 0xb1c3
movb $0x0, %al
callq 0x21d0
leaq 0xc8b8(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2be0
movq 0xc430(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movl 0x14(%rax), %edx
leaq 0x85f4(%rip), %rsi      # 0xb1cd
movb $0x0, %al
callq 0x21d0
leaq 0xc88d(%rip), %rax      # 0xf474
cmpl $0x1, (%rax)
jle 0x2c0c
movq 0xc405(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movzwl 0x18(%rax), %edx
leaq 0x85d2(%rip), %rsi      # 0xb1d7
movb $0x0, %al
callq 0x21d0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax)