// File: /workspace/github_c_makefile_star2_O1/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 0xa617(%rip), %r13      # 0xe474
cmpl $0x2, (%r13)
jge 0x4003
cmpl $0x2, (%r13)
jge 0x4010
movl $0xa0, %ebp
leaq 0x1e(%rsp), %r14
movq 0xa174(%rip), %r15      # 0xdff8
xorl %r12d, %r12d
movb %bpl, 0x1e(%rsp)
movl (%rbx), %edi
movl $0x1, %edx
movq %r14, %rsi
callq 0x20b0
cmpq $0x1, %rax
jne 0x401a
movl $0x2710, %edi           # imm = 0x2710
callq 0x2300
movl (%rbx), %edi
movl $0x2, %edx
movq %r14, %rsi
callq 0x2180
cmpq $0x2, %rax
jne 0x4021
cmpl $0x2, (%r13)
jge 0x3ef2
movzwl 0x1e(%rsp), %eax
rolw $0x8, %ax
movw %ax, (%rsp,%r12,2)
cmpl $0x2, (%r13)
jge 0x3f12
addl $0x2, %ebp
incq %r12
cmpq $0x8, %r12
jne 0x3e87
jmp 0x3f2a
movq (%r15), %rdi
movzbl 0x1e(%rsp), %ecx
movzbl 0x1f(%rsp), %r8d
leaq 0x5219(%rip), %rsi      # 0x9120
movl %ebp, %edx
xorl %eax, %eax
callq 0x21c0
jmp 0x3ecf
movzwl %ax, %ecx
movq (%r15), %rdi
leaq 0x5221(%rip), %rsi      # 0x9140
movl %ebp, %edx
xorl %eax, %eax
callq 0x21c0
jmp 0x3ee4
movq %rsp, %r14
movq %r14, %rdi
callq 0x3de7
movl %eax, %ebp
movb 0xe(%r14), %r14b
andb $0xf, %r14b
cmpl $0x2, (%r13)
jge 0x402d
movl (%r13), %eax
cmpb %r14b, %bpl
jne 0x3f5f
cmpl $0x2, %eax
jl 0x3f68
callq 0x2433
jmp 0x3f68
cmpl $0x2, %eax
jge 0x40ac
movzwl 0x2(%rsp), %eax
shll $0xf, %eax
movl %eax, 0x8(%rbx)
movl 0x4(%rsp), %eax
shll $0x10, %eax
movl %eax, 0xc(%rbx)
movzwl 0x6(%rsp), %eax
movw %ax, 0x10(%rbx)
movzwl 0x8(%rsp), %eax
movw %ax, 0x12(%rbx)
movzwl 0xa(%rsp), %eax
shll $0x8, %eax
movl %eax, 0x14(%rbx)
movzwl 0xc(%rsp), %eax
movw %ax, 0x18(%rbx)
cmpl $0x2, (%r13)
jge 0x404b
cmpl $0x2, (%r13)
jge 0x4055
cmpl $0x2, (%r13)
jge 0x4063
cmpl $0x2, (%r13)
jge 0x4071
cmpl $0x2, (%r13)
jge 0x407f
cmpl $0x2, (%r13)
jge 0x408d
xorl %ebp, %ebp
cmpl $0x2, (%r13)
jge 0x409b
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rdi
callq 0x2398
jmp 0x3e68
callq 0x23c3
jmp 0x3e73
callq 0x23df
jmp 0x4026
callq 0x23fb
movl $0x1, %ebp
jmp 0x3ff2
movq (%r15), %rdi
movzbl %bpl, %edx
movzbl %r14b, %ecx
leaq 0x5110(%rip), %rsi      # 0x914f
xorl %eax, %eax
callq 0x21c0
jmp 0x3f4a
callq 0x244f
jmp 0x3fae
leaq 0x8(%rbx), %rdi
callq 0x246b
jmp 0x3fb9
leaq 0xc(%rbx), %rdi
callq 0x2488
jmp 0x3fc4
leaq 0x10(%rbx), %rdi
callq 0x24a5
jmp 0x3fcf
leaq 0x12(%rbx), %rdi
callq 0x24c3
jmp 0x3fda
leaq 0x14(%rbx), %rdi
callq 0x24e1
jmp 0x3fe5
addq $0x18, %rbx
movq %rbx, %rdi
callq 0x24fe
jmp 0x3ff2
callq 0x2417
jmp 0x3f68