146 lines
3.4 KiB
C
146 lines
3.4 KiB
C
#include <signal.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
/* USBSIO header */
|
|
#include <lpcusbsio.h>
|
|
|
|
#define NXP_VID 0x1FC9
|
|
#define MCULINK_PID 0x0143
|
|
|
|
static bool s_running = true;
|
|
|
|
static int aht10_measure(LPC_HANDLE handle, uint8_t slave_addr, double *temp, double *hum);
|
|
|
|
static void signal_handler(int sig);
|
|
|
|
int main(int argc, const char *argv) {
|
|
int ret;
|
|
LPC_HANDLE dev_handle;
|
|
LPC_HANDLE i2c_handle;
|
|
|
|
fprintf(stdout, "USBSIO example - AHT10\n");
|
|
|
|
signal(SIGINT, signal_handler);
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
/* Find all SIO devices. */
|
|
ret = LPCUSBSIO_GetNumPorts(NXP_VID, MCULINK_PID);
|
|
if (ret == 0) {
|
|
fprintf(stderr, "No MCU-Link devices found.\n");
|
|
return -1;
|
|
}
|
|
|
|
/* Open device */
|
|
dev_handle = LPCUSBSIO_Open(0); /* For now, open the first device available. */
|
|
if (dev_handle == NULL) {
|
|
fprintf(stderr, "Failed to open device #0\n");
|
|
return -2;
|
|
}
|
|
|
|
fprintf(stdout, "%s\n", LPCUSBSIO_GetVersion(dev_handle));
|
|
|
|
I2C_PORTCONFIG_T i2c_cfg = {
|
|
.ClockRate = I2C_CLOCK_FAST_MODE,
|
|
.Options = 0U,
|
|
};
|
|
|
|
i2c_handle = I2C_Open(dev_handle, &i2c_cfg, 0);
|
|
|
|
double temperature = 0.00;
|
|
double humidity = 0.00;
|
|
|
|
while (s_running) {
|
|
int ret = aht10_measure(i2c_handle, 0x38, &temperature, &humidity);
|
|
if(ret < 0) {
|
|
fprintf(stderr, "Sensor communication error.\n");
|
|
}
|
|
else {
|
|
fprintf(stdout, "Temperature: %.2fC, humidity: %.2f%%\n", temperature, humidity);
|
|
}
|
|
|
|
usleep(100 * 1000);
|
|
}
|
|
}
|
|
|
|
static int aht10_measure(LPC_HANDLE handle, uint8_t slave_addr, double *temp, double *hum) {
|
|
uint8_t tx_buffer[3] = {0x71, 0x00, 0x00};
|
|
uint8_t rx_buffer[6];
|
|
|
|
I2C_FAST_XFER_T xfer = {
|
|
.slaveAddr = slave_addr,
|
|
.txSz = 1U,
|
|
.txBuff = tx_buffer,
|
|
.rxSz = 1U,
|
|
.rxBuff = rx_buffer,
|
|
.options = 0U,
|
|
};
|
|
|
|
usleep(40 * 1000);
|
|
|
|
int ret = I2C_FastXfer(handle, &xfer);
|
|
if (ret < 0) {
|
|
I2C_Reset(handle);
|
|
return ret;
|
|
}
|
|
|
|
if ((rx_buffer[0] & 0x08) == 0U) {
|
|
/* BIT3 indicates AHT10 not calibrated, send initialization command */
|
|
tx_buffer[0] = 0xE1;
|
|
tx_buffer[1] = 0x08;
|
|
tx_buffer[2] = 0x00;
|
|
|
|
xfer.txSz = 0x03;
|
|
xfer.rxSz = 0x00;
|
|
|
|
ret = I2C_FastXfer(handle, &xfer);
|
|
if (ret < 0) {
|
|
I2C_Reset(handle);
|
|
return ret;
|
|
}
|
|
|
|
usleep(40 * 1000);
|
|
}
|
|
|
|
tx_buffer[0] = 0xAC;
|
|
tx_buffer[1] = 0x33;
|
|
tx_buffer[2] = 0x00;
|
|
|
|
xfer.txSz = 0x03;
|
|
xfer.rxSz = 0x00;
|
|
|
|
ret = I2C_FastXfer(handle, &xfer);
|
|
if (ret < 0) {
|
|
I2C_Reset(handle);
|
|
return ret;
|
|
}
|
|
|
|
usleep(80 * 1000);
|
|
|
|
tx_buffer[0] = 0x71;
|
|
xfer.txSz = 0x01;
|
|
xfer.rxSz = 0x06;
|
|
|
|
ret = I2C_FastXfer(handle, &xfer);
|
|
if (ret < 0) {
|
|
I2C_Reset(handle);
|
|
return ret;
|
|
}
|
|
|
|
uint32_t humid_raw = rx_buffer[1] << 12U;
|
|
humid_raw |= rx_buffer[2] << 4U;
|
|
humid_raw |= rx_buffer[3] >> 4U;
|
|
|
|
uint32_t temp_raw = (rx_buffer[3] & 0x0FU) << 16U;
|
|
temp_raw |= rx_buffer[4] << 8U;
|
|
temp_raw |= rx_buffer[5];
|
|
|
|
*hum = (double)humid_raw * 100.0 / 1048576.0;
|
|
*temp = ((double)temp_raw * 200.0 / 1048576.0) - 50.0;
|
|
|
|
return ret;
|
|
}
|
|
|
|
static void signal_handler(int sig) { s_running = false; } |