// File: /workspace/github_c_makefile_star2_O1/Openvario[P]sensord/humidity.c
// Function: am2321_read
int am2321_read (t_ds2482 *sensor) {
	uint8_t data[8];

	data[0] = 0x00;
	if (write(sensor->fd, NULL, 0)<0)
	return 3;

	struct timespec nstime = {0,1.0e6};
	while (nanosleep (&nstime,&nstime)) ; /* Wait atleast 1.5ms */

	/* write at addr 0x03, start reg = 0x00, num regs = 0x04 */
	data[0] = 0x03;
	data[1] = 0x00;
	data[2] = 0x04;
	if (write(sensor->fd, data, 3) < 0) return 3;

	/* wait for AM2320 */
	nstime.tv_nsec = 1.6e6;
	while (nanosleep (&nstime,&nstime)) ; /* Wait atleast 1.5ms */

	/*
	 * Read out 8 bytes of data
	 * Byte 0: Should be Modbus function code 0x03
	 * Byte 1: Should be number of registers to read (0x04)
	 * Byte 2: Humidity msb
	 * Byte 3: Humidity lsb
	 * Byte 4: Temperature msb
	 * Byte 5: Temperature lsb
	 * Byte 6: CRC lsb byte
	 * Byte 7: CRC msb byte
	*/

  	if (read(sensor->fd, data, 8) < 0) return 4;

	/* Check data[0] and data[1] */
	if (data[0] != 0x03 || data[1] != 0x04) return 9;

	/* Check CRC */
	uint16_t crcdata = _calc_crc16(data, 6);
	uint16_t crcread = _combine_bytes(data[7], data[6]);
	if (crcdata != crcread) return 10;

	uint16_t temp16 = _combine_bytes(data[4], data[5]);
	uint16_t humi16 = _combine_bytes(data[2], data[3]);

	/* Temperature resolution is 16Bit,
	 * temperature highest bit (Bit15) is equal to 1 indicates a
	 * negative temperature, the temperature highest bit (Bit15)
	 * is equal to 0 indicates a positive temperature;
	 * temperature in addition to the most significant bit (Bit14 ~ Bit0)
	 *  indicates the temperature sensor string value.
	 * Temperature sensor value is a string of 10 times the
	 * actual temperature value.
	*/

 	if (temp16 & 0x8000) temp16 = -(temp16 & 0x7FFF);

	if (sensor->temp_present) {
		sensor->temperature = (float)temp16 / 10.0;
		sensor->temp_valid = 1;
	}
	if (sensor->humidity_present) {
		sensor->humidity = (float)humi16 / 10.0;
		sensor->humidity_valid = 1;
	}
	return 0;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
movb $0x0, 0x8(%rsp)
movl (%rdi), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x20b0
testq %rax, %rax
js 0x8df7
movups 0x1e95(%rip), %xmm0     # 0xab48
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
movq %r14, %rdi
movq %r14, %rsi
callq 0x2120
testl %eax, %eax
jne 0x8cbc
leaq 0x8(%rsp), %rsi
movw $0x3, (%rsi)
movb $0x4, 0x2(%rsi)
movl (%rbx), %edi
movl $0x3, %edx
callq 0x20b0
testq %rax, %rax
js 0x8df7
leaq 0x10(%rsp), %r14
movq $0x186a00, 0x8(%r14)    # imm = 0x186A00
movq %r14, %rdi
movq %r14, %rsi
callq 0x2120
testl %eax, %eax
jne 0x8cfb
movl (%rbx), %edi
leaq 0x8(%rsp), %r14
movl $0x8, %edx
movq %r14, %rsi
callq 0x2180
testq %rax, %rax
js 0x8e04
cmpb $0x3, 0x8(%rsp)
movl $0x9, %eax
jne 0x8dfc
cmpb $0x4, 0x9(%rsp)
jne 0x8dfc
movw $0xffff, %cx            # imm = 0xFFFF
movl $0x5, %eax
movzbl (%r14), %edx
xorl %edx, %ecx
movl $0x8, %edx
movzwl %cx, %esi
shrl %esi
movl %esi, %edi
xorl $0xffffa001, %edi       # imm = 0xFFFFA001
testb $0x1, %cl
cmovel %esi, %edi
movl %edi, %ecx
decl %edx
jne 0x8d56
incq %r14
subq $0x1, %rax
jae 0x8d4b
movl $0xa, %eax
cmpw 0xe(%rsp), %cx
jne 0x8dfc
cmpb $0x0, 0x2c(%rbx)
je 0x8dc5
movzbl 0xc(%rsp), %eax
shll $0x8, %eax
movzbl 0xd(%rsp), %ecx
orl %eax, %ecx
movl %ecx, %edx
andl $0x7fff, %edx           # imm = 0x7FFF
negl %edx
testw %ax, %ax
cmovnsl %ecx, %edx
movzwl %dx, %eax
cvtsi2sd %eax, %xmm0
divsd 0x1b60(%rip), %xmm0     # 0xa918
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x24(%rbx)
movb $0x1, 0x2e(%rbx)
xorl %eax, %eax
cmpb $0x0, 0x2d(%rbx)
je 0x8dfc
movzwl 0xa(%rsp), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
divsd 0x1b30(%rip), %xmm0     # 0xa918
cvtsd2ss %xmm0, %xmm0
movss %xmm0, 0x28(%rbx)
movb $0x1, 0x2f(%rbx)
jmp 0x8dfc
movl $0x3, %eax
addq $0x28, %rsp
popq %rbx
popq %r14
retq
movl $0x4, %eax
jmp 0x8dfc