// File: /workspace/github_c_makefile_star2_O0/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 %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movq %rdi, -0x10(%rbp)
movb $0x0, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x20a0
cmpq $0x0, %rax
jge 0xa184
movl $0x3, -0x4(%rbp)
jmp 0xa336
movq 0x279d(%rip), %rax      # 0xc928
movq %rax, -0x28(%rbp)
movq 0x279a(%rip), %rax      # 0xc930
movq %rax, -0x20(%rbp)
leaq -0x28(%rbp), %rsi
movq %rsi, %rdi
callq 0x2120
cmpl $0x0, %eax
je 0xa1ad
jmp 0xa19a
movb $0x3, -0x18(%rbp)
movb $0x0, -0x17(%rbp)
movb $0x4, -0x16(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0x18(%rbp), %rsi
movl $0x3, %edx
callq 0x20a0
cmpq $0x0, %rax
jge 0xa1df
movl $0x3, -0x4(%rbp)
jmp 0xa336
movq $0x186a00, -0x20(%rbp)  # imm = 0x186A00
leaq -0x28(%rbp), %rsi
movq %rsi, %rdi
callq 0x2120
cmpl $0x0, %eax
je 0xa1fa
jmp 0xa1e7
movq -0x10(%rbp), %rax
movl (%rax), %edi
leaq -0x18(%rbp), %rsi
movl $0x8, %edx
callq 0x2180
cmpq $0x0, %rax
jge 0xa220
movl $0x4, -0x4(%rbp)
jmp 0xa336
movzbl -0x18(%rbp), %eax
cmpl $0x3, %eax
jne 0xa232
movzbl -0x17(%rbp), %eax
cmpl $0x4, %eax
je 0xa23e
movl $0x9, -0x4(%rbp)
jmp 0xa336
leaq -0x18(%rbp), %rdi
movl $0x6, %esi
callq 0xa3b0
movw %ax, -0x2a(%rbp)
movb -0x11(%rbp), %al
movzbl %al, %edi
movzbl -0x12(%rbp), %esi
callq 0xa440
movw %ax, -0x2c(%rbp)
movzwl -0x2a(%rbp), %eax
movzwl -0x2c(%rbp), %ecx
cmpl %ecx, %eax
je 0xa27b
movl $0xa, -0x4(%rbp)
jmp 0xa336
movb -0x14(%rbp), %al
movzbl %al, %edi
movzbl -0x13(%rbp), %esi
callq 0xa440
movw %ax, -0x2e(%rbp)
movb -0x16(%rbp), %al
movzbl %al, %edi
movzbl -0x15(%rbp), %esi
callq 0xa440
movw %ax, -0x30(%rbp)
movzwl -0x2e(%rbp), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0xa2c1
movzwl -0x2e(%rbp), %ecx
andl $0x7fff, %ecx           # imm = 0x7FFF
xorl %eax, %eax
subl %ecx, %eax
movw %ax, -0x2e(%rbp)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2c(%rax)
je 0xa2f8
movzwl -0x2e(%rbp), %eax
cvtsi2ss %eax, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x2759(%rip), %xmm1     # 0xca38
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x24(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x2e(%rax)
movq -0x10(%rbp), %rax
cmpb $0x0, 0x2d(%rax)
je 0xa32f
movzwl -0x30(%rbp), %eax
cvtsi2ss %eax, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x2722(%rip), %xmm1     # 0xca38
divsd %xmm1, %xmm0
cvtsd2ss %xmm0, %xmm0
movq -0x10(%rbp), %rax
movss %xmm0, 0x28(%rax)
movq -0x10(%rbp), %rax
movb $0x1, 0x2f(%rax)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x30, %rsp
popq %rbp
retq
nop