@@ -62,6 +62,16 @@ static uint8_t INIT_CMD[3] = {0xbe, 0x08, 0x00};
62
62
*/
63
63
static uint8_t MEASURE_CMD [3 ] = {0xac , 0x33 , 0x00 };
64
64
65
+ /*
66
+ * acknowledge signal
67
+ */
68
+ static uint8_t ACK_CMD = 0x06 ;
69
+
70
+ /*
71
+ * not acknowledge signal
72
+ */
73
+ static uint8_t NACK_CMD = 0x15 ;
74
+
65
75
/*
66
76
* calculates crc8 for given data
67
77
*/
@@ -119,39 +129,27 @@ aht20_status_t aht20_calibrate(I2C_HandleTypeDef *hi2c, uint8_t status_word) {
119
129
* Datasheet: AHT20 Product manuals
120
130
* 5.4 Sensor reading process, paragraph 2
121
131
*/
122
- aht20_status_t aht20_measure (I2C_HandleTypeDef * hi2c , uint8_t * measured_data ) {
123
- uint8_t received_crc = 0 ;
124
-
132
+ aht20_status_t aht20_measure (I2C_HandleTypeDef * hi2c , uint8_t * measured_data , uint16_t measured_data_size ) {
125
133
if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , MEASURE_CMD , (uint16_t )sizeof (MEASURE_CMD ), HAL_MAX_DELAY )) {
126
134
return AHT20_STATUS_NOT_TRANSMITTED ;
127
135
}
128
136
HAL_Delay (80 );
129
137
130
- uint8_t measuring_status = 0 ;
131
- HAL_I2C_Master_Receive (hi2c , DEVICE_ADDRESS , & measuring_status , (uint16_t )sizeof (measuring_status ), HAL_MAX_DELAY );
132
-
133
- uint8_t all_data [7 ];
134
- if (measuring_status & (1 << 7 )) {
135
- return AHT20_STATUS_NOT_MEASURED ;
136
- } else {
137
- HAL_I2C_Master_Receive (hi2c , DEVICE_ADDRESS , all_data , (uint16_t )sizeof (all_data ), HAL_MAX_DELAY );
138
+ if (HAL_OK != HAL_I2C_Master_Receive (hi2c , DEVICE_ADDRESS , measured_data , measured_data_size , HAL_MAX_DELAY )) {
139
+ return AHT20_STATUS_NOT_RECEIVED ;
138
140
}
139
141
140
- // Copy 6 data bytes to measured_data
141
- for (uint8_t i = 0 ; i < 6 ; ++ i ) {
142
- measured_data [i ] = all_data [i ];
142
+ if (measured_data [0 ] & (1 << 7 )) {
143
+ return AHT20_STATUS_NOT_MEASURED ;
143
144
}
144
- received_crc = all_data [6 ]; // CRC is the 7th byte
145
145
146
146
uint8_t calculated_crc = calculate_crc (measured_data );
147
- if (calculated_crc == received_crc ) {
148
- uint8_t ack = 0x06 ;
149
- if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & ack , (uint16_t )sizeof (ack ), HAL_MAX_DELAY )) {
147
+ if (calculated_crc == measured_data [6 ]) {
148
+ if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & ACK_CMD , (uint16_t )sizeof (ACK_CMD ), HAL_MAX_DELAY )) {
150
149
return AHT20_STATUS_NOT_TRANSMITTED ;
151
150
}
152
151
} else {
153
- uint8_t nack = 0x15 ;
154
- if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & nack , (uint16_t )sizeof (nack ), HAL_MAX_DELAY )) {
152
+ if (HAL_OK != HAL_I2C_Master_Transmit (hi2c , DEVICE_ADDRESS , & NACK_CMD , (uint16_t )sizeof (NACK_CMD ), HAL_MAX_DELAY )) {
155
153
return AHT20_STATUS_NOT_TRANSMITTED ;
156
154
}
157
155
0 commit comments