// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
leaq 0xade7(%rip), %r15      # 0xd474
cmpl $0x2, (%r15)
jge 0x281a
movl $0xa0, %ebp
leaq 0x1e(%rsp), %r14
xorl %r12d, %r12d
cmpq $0x8, %r12
je 0x277f
movb %bpl, 0x1e(%rsp)
movl (%rbx), %edi
movq %r14, %rsi
pushq $0x1
popq %rdx
callq 0x20b0
cmpq $0x1, %rax
jne 0x2866
movl $0x2710, %edi           # imm = 0x2710
callq 0x2310
movl (%rbx), %edi
movq %r14, %rsi
pushq $0x2
popq %rdx
callq 0x2180
cmpq $0x2, %rax
jne 0x287b
movq %rbx, %r14
movq %r15, %rbx
cmpl $0x1, (%r15)
movzbl 0x1e(%rsp), %eax
movzwl %ax, %r13d
movzbl 0x1f(%rsp), %eax
movzwl %ax, %r15d
jg 0x272b
shll $0x8, %r13d
orl %r15d, %r13d
movw %r13w, (%rsp,%r12,2)
movq %rbx, %r15
addl $0x2, %ebp
incq %r12
movq %r14, %rbx
leaq 0x1e(%rsp), %r14
jmp 0x26a4
movq 0xa8c6(%rip), %rax      # 0xcff8
movq (%rax), %rdi
leaq 0x59e4(%rip), %rsi      # 0x8120
movl %ebp, %edx
movl %r13d, %ecx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x21c0
shll $0x8, %r13d
orl %r15d, %r13d
movq %rbx, %r15
cmpl $0x2, (%rbx)
movw %r13w, (%rsp,%r12,2)
jl 0x2718
movq 0xa892(%rip), %rax      # 0xcff8
movq (%rax), %rdi
movzwl %r13w, %ecx
leaq 0x59cc(%rip), %rsi      # 0x8140
movl %ebp, %edx
xorl %eax, %eax
callq 0x21c0
jmp 0x2718
movq %rsp, %r14
movq %r14, %rdi
callq 0x2610
movl %eax, %ebp
movb 0xe(%r14), %r14b
andb $0xf, %r14b
cmpl $0x2, (%r15)
movq 0xa859(%rip), %r12      # 0xcff8
jge 0x289f
movl (%r15), %eax
cmpb %r14b, %bpl
jne 0x27c4
cmpl $0x2, %eax
jl 0x27cd
movq (%r12), %rcx
leaq 0x59b7(%rip), %rdi      # 0x8174
pushq $0x3
jmp 0x29af
cmpl $0x2, %eax
jge 0x29a2
movzwl 0x2(%rsp), %eax
shll $0xf, %eax
movl %eax, 0x8(%rbx)
movl 0x4(%rsp), %eax
shll $0x10, %eax
movl %eax, 0xc(%rbx)
movl 0x6(%rsp), %eax
movl %eax, 0x10(%rbx)
movzwl 0xa(%rsp), %eax
shll $0x8, %eax
movl %eax, 0x14(%rbx)
movzwl 0xc(%rsp), %eax
movw %ax, 0x18(%rbx)
xorl %ebp, %ebp
cmpl $0x2, (%r15)
jge 0x28be
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0xa7d7(%rip), %r14      # 0xcff8
movq (%r14), %rdi
cvtss2sd 0x90(%rbx), %xmm0
cvtss2sd 0x8c(%rbx), %xmm1
leaq 0x5841(%rip), %rsi      # 0x807c
movb $0x2, %al
callq 0x21c0
cmpl $0x2, (%r15)
jl 0x2697
movq (%r14), %rcx
leaq 0x585b(%rip), %rdi      # 0x80b1
pushq $0x19
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
jmp 0x2697
movq 0xa78b(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x5854(%rip), %rdi      # 0x80cb
pushq $0x2b
jmp 0x288e
movq 0xa776(%rip), %rax      # 0xcff8
movq (%rax), %rcx
leaq 0x586b(%rip), %rdi      # 0x80f7
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
pushq $0x1
popq %rbp
jmp 0x2809
movq (%r12), %rdi
movzbl %bpl, %edx
movzbl %r14b, %ecx
leaq 0x589d(%rip), %rsi      # 0x814f
xorl %eax, %eax
callq 0x21c0
jmp 0x27a5
movq (%r12), %rcx
leaq 0x58bb(%rip), %rdi      # 0x8184
pushq $0x14
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
cmpl $0x2, (%r15)
jl 0x2809
movq (%r12), %rdi
movl 0x8(%rbx), %edx
leaq 0x58ad(%rip), %rsi      # 0x8199
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, (%r15)
jl 0x2809
movq (%r12), %rdi
movl 0xc(%rbx), %edx
leaq 0x5896(%rip), %rsi      # 0x81a3
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, (%r15)
jl 0x2809
movq (%r12), %rdi
movzwl 0x10(%rbx), %edx
leaq 0x587e(%rip), %rsi      # 0x81ad
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, (%r15)
jl 0x2809
movq (%r12), %rdi
movzwl 0x12(%rbx), %edx
leaq 0x5866(%rip), %rsi      # 0x81b7
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, (%r15)
jl 0x2809
movq (%r12), %rdi
movl 0x14(%rbx), %edx
leaq 0x584f(%rip), %rsi      # 0x81c1
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, (%r15)
jl 0x2809
movq (%r12), %rdi
movzwl 0x18(%rbx), %edx
leaq 0x5837(%rip), %rsi      # 0x81cb
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x21c0
jmp 0x2809
movq (%r12), %rcx
leaq 0x57cb(%rip), %rdi      # 0x8178
pushq $0xb
popq %rsi
pushq $0x1
popq %rdx
callq 0x22b0
jmp 0x27cd