// File: /workspace/github_c_makefile_star2_O3/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 0xa637(%rip), %r12      # 0xe474
cmpl $0x2, (%r12)
jge 0x3fc1
movl $0xa0, %ebp
leaq 0x1a(%rsp), %r14
xorl %r15d, %r15d
movb %bpl, 0x1a(%rsp)
movl (%rbx), %edi
movl $0x1, %edx
movq %r14, %rsi
callq 0x20b0
cmpq $0x1, %rax
jne 0x3fce
movl $0x2710, %edi           # imm = 0x2710
callq 0x2300
movl (%rbx), %edi
movl $0x2, %edx
movq %r14, %rsi
callq 0x2180
cmpq $0x2, %rax
jne 0x3fd5
movq %rbx, %r14
movq %r12, %rbx
cmpl $0x1, (%r12)
movzbl 0x1a(%rsp), %eax
movzwl %ax, %r13d
movzbl 0x1b(%rsp), %eax
movzwl %ax, %r12d
jg 0x3ede
shll $0x8, %r13d
orl %r12d, %r13d
movw %r13w, (%rsp,%r15,2)
movq %rbx, %r12
addl $0x2, %ebp
incq %r15
cmpq $0x8, %r15
movq %r14, %rbx
leaq 0x1a(%rsp), %r14
jne 0x3e55
jmp 0x3f32
movq 0xa113(%rip), %rax      # 0xdff8
movq (%rax), %rdi
leaq 0x5231(%rip), %rsi      # 0x9120
movl %ebp, %edx
movl %r13d, %ecx
movl %r12d, %r8d
xorl %eax, %eax
callq 0x21c0
shll $0x8, %r13d
orl %r12d, %r13d
movq %rbx, %r12
cmpl $0x2, (%rbx)
movw %r13w, (%rsp,%r15,2)
jl 0x3ec4
movq 0xa0df(%rip), %rax      # 0xdff8
movq (%rax), %rdi
movzwl %r13w, %ecx
leaq 0x5219(%rip), %rsi      # 0x9140
movl %ebp, %edx
xorl %eax, %eax
callq 0x21c0
jmp 0x3ec4
movq %rsp, %r14
movq %r14, %rdi
callq 0x3dc7
movl %eax, %ebp
movb 0xe(%r14), %r14b
andb $0xf, %r14b
cmpl $0x2, (%r12)
movq 0xa0a5(%rip), %r15      # 0xdff8
jge 0x3fe1
movl (%r12), %eax
cmpb %r14b, %bpl
jne 0x3f6e
cmpl $0x2, %eax
jl 0x3f77
callq 0x243c
jmp 0x3f77
cmpl $0x2, %eax
jge 0x40a7
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, (%r12)
jge 0x3fff
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 0x3e48
callq 0x23e8
jmp 0x3fda
callq 0x2404
movl $0x1, %ebp
jmp 0x3fb0
movq (%r15), %rdi
movzbl %bpl, %edx
movzbl %r14b, %ecx
leaq 0x515c(%rip), %rsi      # 0x914f
xorl %eax, %eax
callq 0x21c0
jmp 0x3f59
movq (%r15), %rcx
leaq 0x517b(%rip), %rdi      # 0x9184
movl $0x14, %esi
movl $0x1, %edx
callq 0x22a0
cmpl $0x2, (%r12)
jl 0x3fb0
movq (%r15), %rdi
movl 0x8(%rbx), %edx
leaq 0x516d(%rip), %rsi      # 0x9199
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, (%r12)
jl 0x3fb0
movq (%r15), %rdi
movl 0xc(%rbx), %edx
leaq 0x5156(%rip), %rsi      # 0x91a3
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, (%r12)
jl 0x3fb0
movq (%r15), %rdi
movzwl 0x10(%rbx), %edx
leaq 0x513e(%rip), %rsi      # 0x91ad
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x21c0
cmpl $0x2, (%r12)
jl 0x3fb0
leaq 0x12(%rbx), %rdi
leaq 0x14(%rbx), %rsi
addq $0x18, %rbx
leaq 0x24(%rsp), %r14
movq %rbx, %rdx
movq %r14, %rcx
callq 0x2458
movl (%r14), %ebp
jmp 0x3fb0
callq 0x2420
jmp 0x3f77