/* CRC calculation algorithm, as specified in the datasheet (page 13). */ static u16 am2315_crc(u8 *data, u8 nr_bytes)
{ int i;
u16 crc = 0xffff;
while (nr_bytes--) {
crc ^= *data++; for (i = 0; i < 8; i++) { if (crc & 0x01) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
return crc;
}
/* Simple function that sends a few bytes to the device to wake it up. */ staticvoid am2315_ping(struct i2c_client *client)
{
i2c_smbus_read_byte_data(client, AM2315_REG_HUM_MSB);
}
/* First wake up the device. */
am2315_ping(data->client);
mutex_lock(&data->lock);
ret = i2c_master_send(data->client, tx_buf, sizeof(tx_buf)); if (ret < 0) {
dev_err(&data->client->dev, "failed to send read request\n"); goto exit_unlock;
} /* Wait 2-3 ms, then read back the data sent by the device. */
usleep_range(2000, 3000); /* Do a bulk data read, then pick out what we need. */
ret = i2c_master_recv(data->client, rx_buf, sizeof(rx_buf)); if (ret < 0) {
dev_err(&data->client->dev, "failed to read sensor data\n"); goto exit_unlock;
}
mutex_unlock(&data->lock); /* * Do a CRC check on the data and compare it to the value * calculated by the device.
*/
crc = am2315_crc(rx_buf, sizeof(rx_buf) - 2); if ((crc & 0xff) != rx_buf[6] || (crc >> 8) != rx_buf[7]) {
dev_err(&data->client->dev, "failed to verify sensor data\n"); return -EIO;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.