From e0e3a637e92dbe3c7700cc541410ff19b3b0c80c Mon Sep 17 00:00:00 2001 From: guowenxue <guowenxue@gmail.com> Date: Wed, 21 Apr 2021 12:44:30 +0800 Subject: [PATCH] update iotd hal functions --- iotd/hal/hal.c | 11 iotd/conf/conf.c | 28 +- iotd/hal/gpio.c | 92 +++++++ iotd/hal/gpio.h | 4 iotd/hal/sht20.c | 496 ++++++++++++++++++++++---------------------- iotd/hal/tsl2561.c | 16 6 files changed, 371 insertions(+), 276 deletions(-) diff --git a/iotd/conf/conf.c b/iotd/conf/conf.c index de7ee2e..6c3ebcf 100644 --- a/iotd/conf/conf.c +++ b/iotd/conf/conf.c @@ -266,14 +266,16 @@ if( !(str=iniparser_getstring(ini, "subsciber:subTopic", NULL)) ) { - log_err("ERROR: Parser MQTT subscribe topic failure\n"); - return -2; + log_warn("WARNNING: Parser MQTT subscribe topic failure\n"); } - strncpy(mqtt_ctx->subTopic, str, sizeof(mqtt_ctx->subTopic) ); + else + { + strncpy(mqtt_ctx->subTopic, str, sizeof(mqtt_ctx->subTopic) ); + mqtt_ctx->subQos = iniparser_getint(ini, "subsciber:subQos", 0); + mqtt_ctx->sub_enable = 1; - mqtt_ctx->subQos = iniparser_getint(ini, "subsciber:subQos", 0); - log_nrml("Parser subscriber topic \"%s\" with Qos[%d]\n", mqtt_ctx->subTopic, mqtt_ctx->subQos); - mqtt_ctx->sub_enable = 1; + log_nrml("Parser subscriber topic \"%s\" with Qos[%d]\n", mqtt_ctx->subTopic, mqtt_ctx->subQos); + } /*+------------------------------------------------------+ *| parser publisher settings | @@ -281,14 +283,16 @@ if( !(str=iniparser_getstring(ini, "publisher:pubTopic", NULL)) ) { - log_err("ERROR: Parser MQTT publisher topic failure\n"); - return -2; + log_warn("WARNNING: Parser MQTT publisher topic failure\n"); } - strncpy(mqtt_ctx->pubTopic, str, sizeof(mqtt_ctx->pubTopic) ); + else + { + strncpy(mqtt_ctx->pubTopic, str, sizeof(mqtt_ctx->pubTopic) ); + mqtt_ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", 0); + mqtt_ctx->pub_enable = 1; - mqtt_ctx->pubQos = iniparser_getint(ini, "publisher:pubQos", 0); - log_nrml("Parser publisher topic \"%s\" with Qos[%d]\n", mqtt_ctx->pubTopic, mqtt_ctx->pubQos); - mqtt_ctx->pub_enable = 1; + log_nrml("Parser publisher topic \"%s\" with Qos[%d]\n", mqtt_ctx->pubTopic, mqtt_ctx->pubQos); + } return 0; diff --git a/iotd/hal/gpio.c b/iotd/hal/gpio.c index e50ae3b..bd050b1 100644 --- a/iotd/hal/gpio.c +++ b/iotd/hal/gpio.c @@ -21,18 +21,100 @@ int gpio_init(gpio_t *gpio) { + int i; + gpio_info_t *gpioinfo; + + + s_gpio = gpio; + + if( !gpio ) { log_err("Invalid input arguments $gpio\n"); return -1; } - s_gpio = gpio; + + if( !gpio->incnt && !gpio->outcnt ) + { + log_warn("WARNNING: No GPIO pins configured\n"); + return 0; + } + + + /* gpiod open chip */ + s_chip = gpiod_chip_open_by_name(RPI_GPIONAME); + if( !s_chip ) + { + log_err("gpiod open chip failure, maybe you need running as root\n"); + return -2; + } + log_nrml("gpiod initialise open chip ok\n"); + + + /* gpiod request all output pins */ + for(i=0; i<gpio->outcnt; i++) + { + gpio->output[i].line = gpiod_chip_get_line(s_chip, gpio->output[i].pin); + if( !gpio->output[i].line ) + { + log_err("gpiod get line for '%s' pin[#%d] failure\n", gpio->output[i].name, gpio->output[i].pin ); + return -2; + } + + gpiod_line_request_output(gpio->output[i].line, gpio->output[i].name, !gpio->output[i].active_level); + log_nrml("gpiod request '%s' pin[#%d] output ok\n", gpio->output[i].name, gpio->output[i].pin); + } + + + /* gpiod request all input pins */ + for(i=0; i<gpio->incnt; i++) + { + gpio->input[i].line = gpiod_chip_get_line(s_chip, gpio->input[i].pin); + if( !gpio->input[i].line ) + { + log_err("gpiod get line for '%s' pin[#%d] failure\n", gpio->input[i].name, gpio->input[i].pin ); + return -2; + } + + if( gpio->output[i].active_level ) + { + gpiod_line_request_rising_edge_events(gpio->input[i].line, gpio->input[i].name); + log_nrml("gpiod request '%s' pin[#%d] for rising edge event ok\n", gpio->input[i].name, gpio->input[i].pin); + } + else + { + gpiod_line_request_falling_edge_events(gpio->input[i].line, gpio->input[i].name); + log_nrml("gpiod request '%s' pin[#%d] for falling edge event ok\n", gpio->input[i].name, gpio->input[i].pin); + } + } } + void gpio_term(void) { - return ; + int i; + + log_nrml("start teriminated GPIO\n"); + + if( !s_gpio->incnt && !s_gpio->outcnt ) + { + return ; + } + + + for(i=0; i<s_gpio->outcnt; i++) + { + gpiod_line_release(s_gpio->output[i].line); + } + + + for(i=0; i<s_gpio->incnt; i++) + { + gpiod_line_release(s_gpio->input[i].line); + } + + gpiod_chip_close(s_chip); } void gpio_out(char *name, int cmd) @@ -51,17 +133,17 @@ if( !found ) { - log_err("GPIO output for [%s] pin not found\n", name); + log_err("GPIO output for '%s' pin not found\n", name); return ; } if( OFF == cmd ) { - gpiod_line_set_value(s_gpio->output[i].lines, s_gpio->output[i].active_level); + gpiod_line_set_value(s_gpio->output[i].line, s_gpio->output[i].active_level); } else { - gpiod_line_set_value(s_gpio->output[i].lines, !s_gpio->output[i].active_level); + gpiod_line_set_value(s_gpio->output[i].line, !s_gpio->output[i].active_level); } return ; diff --git a/iotd/hal/gpio.h b/iotd/hal/gpio.h index d1a2a23..2a12d84 100644 --- a/iotd/hal/gpio.h +++ b/iotd/hal/gpio.h @@ -25,9 +25,9 @@ typedef struct gpio_info_s { char name[32]; /* GPIO connected module name */ - int pin; /* GPIO BCM pin number */ + int pin; /* GPIO BCM pin number */ int active_level; /* active power level */ - struct gpiod_line *lines; /* gpiod lines */ + struct gpiod_line *line; /* gpiod line */ } gpio_info_t; diff --git a/iotd/hal/hal.c b/iotd/hal/hal.c index 7763e60..7643b24 100644 --- a/iotd/hal/hal.c +++ b/iotd/hal/hal.c @@ -17,7 +17,6 @@ int hal_init(hal_ctx_t *ctx) { int i; - gpio_info_t *gpio = NULL; if(!ctx) { @@ -32,15 +31,23 @@ log_err("R&H sensor SHT2X initialise failure\n"); return -2; } + else + { + log_nrml("R&H sensor SHT2X initialise okay\n"); + } } if( ctx->lux_enable ) { - if( tsl2561_init()< 0 ) + if( tsl2561_init() < 0 ) { log_err("LUX sensor TSL2561 initialise failure\n"); return -2; } + else + { + log_nrml("LUX sensor TSL2561 initialise okay\n"); + } } gpio_init(&ctx->gpio); diff --git a/iotd/hal/sht20.c b/iotd/hal/sht20.c index d881b43..fed3000 100644 --- a/iotd/hal/sht20.c +++ b/iotd/hal/sht20.c @@ -68,57 +68,58 @@ int sht2x_softreset(int fd) { - uint8_t buf[4]; + uint8_t buf[4]; - if( fd<0 ) - { - log_err("Invalid input arguments\n"); - return -1; - } + if( fd<0 ) + { + log_err("Invalid input arguments\n"); + return -1; + } - /* software reset SHT2x */ - memset(buf, 0, sizeof(buf)); + /* software reset SHT2x */ + memset(buf, 0, sizeof(buf)); - buf[0] = SOFTRESET; - write(fd, buf, 1); + buf[0] = SOFTRESET; + write(fd, buf, 1); - msleep(50); + msleep(50); - return 0; + return 0; } int sht2x_init(void) { - - if( (s_sht2x_fd=open("/dev/i2c-1", O_RDWR)) < 0) - { - log_fatal("i2c device open failed: %s\n", strerror(errno)); + + if( (s_sht2x_fd=open("/dev/i2c-1", O_RDWR)) < 0) + { + log_fatal("i2c device open failed: %s\n", strerror(errno)); return -1; - } - - /* set I2C mode and SHT2x slave address */ - ioctl(s_sht2x_fd, I2C_TENBIT, 0); /* Not 10-bit but 7-bit mode */ - ioctl(s_sht2x_fd, I2C_SLAVE, 0x40); /* set SHT2x slava address 0x40*/ + } - if( sht2x_softreset(s_sht2x_fd) < 0 ) - { - log_err("SHT2x softreset failure\n"); + /* set I2C mode and SHT2x slave address */ + ioctl(s_sht2x_fd, I2C_TENBIT, 0); /* Not 10-bit but 7-bit mode */ + ioctl(s_sht2x_fd, I2C_SLAVE, 0x40); /* set SHT2x slava address 0x40*/ + + if( sht2x_softreset(s_sht2x_fd) < 0 ) + { + log_err("SHT2x softreset failure\n"); sht2x_term(); - return -2; - } + return -2; + } - return s_sht2x_fd; + log_dbg("SHT2X initialise ok, s_sht2x_fd=%d\n", s_sht2x_fd); + return s_sht2x_fd; } int sht2x_get_temp_humidity(float *temp, float *rh) { - uint8_t buf[4]; + uint8_t buf[4]; - if( !temp || !rh ) - { - log_err("Invalid input arguments\n"); - return -1; - } + if( !temp || !rh ) + { + log_err("Invalid input arguments\n"); + return -1; + } if( s_sht2x_fd < 0 ) { @@ -129,42 +130,42 @@ } } - /* send trigger temperature measure command and read the data */ - memset(buf, 0, sizeof(buf)); - buf[0]=TRIGGER_TEMPERATURE_NO_HOLD; - write(s_sht2x_fd, buf, 1); + /* send trigger temperature measure command and read the data */ + memset(buf, 0, sizeof(buf)); + buf[0]=TRIGGER_TEMPERATURE_NO_HOLD; + write(s_sht2x_fd, buf, 1); - msleep(85); /* datasheet: typ=66, max=85 */ + msleep(85); /* datasheet: typ=66, max=85 */ - memset(buf, 0, sizeof(buf)); - read(s_sht2x_fd, buf, 3); - //dump_buf("Temperature sample data: ", buf, 3); - *temp = 175.72 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 46.85; - - /* send trigger humidity measure command and read the data */ - memset(buf, 0, sizeof(buf)); - buf[0] = TRIGGER_HUMIDITY_NO_HOLD; - write(s_sht2x_fd, buf, 1); + memset(buf, 0, sizeof(buf)); + read(s_sht2x_fd, buf, 3); + //dump_buf("Temperature sample data: ", buf, 3); + *temp = 175.72 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 46.85; - msleep(29); /* datasheet: typ=22, max=29 */ - memset(buf, 0, sizeof(buf)); + /* send trigger humidity measure command and read the data */ + memset(buf, 0, sizeof(buf)); + buf[0] = TRIGGER_HUMIDITY_NO_HOLD; + write(s_sht2x_fd, buf, 1); - read(s_sht2x_fd, buf, 3); - //dump_buf("Relative humidity sample data: ", buf, 3); - *rh = 125 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 6; + msleep(29); /* datasheet: typ=22, max=29 */ + memset(buf, 0, sizeof(buf)); - return 0; + read(s_sht2x_fd, buf, 3); + //dump_buf("Relative humidity sample data: ", buf, 3); + *rh = 125 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 6; + + return 0; } int sht2x_get_serialnumber(uint8_t *serialnumber, int size) { - uint8_t buf[4]; + uint8_t buf[4]; - if( !serialnumber || size!=8 ) - { - log_err("Invalid input arguments\n"); - return -1; - } + if( !serialnumber || size!=8 ) + { + log_err("Invalid input arguments\n"); + return -1; + } if( s_sht2x_fd < 0 ) { @@ -175,37 +176,37 @@ } } - /* Read SerialNumber from Location 1 */ - memset(buf, 0, sizeof(buf)); - buf[0] = 0xfa; /* command for readout on-chip memory */ - buf[1] = 0x0f; /* on-chip memory address */ - write(s_sht2x_fd, buf, 2); + /* Read SerialNumber from Location 1 */ + memset(buf, 0, sizeof(buf)); + buf[0] = 0xfa; /* command for readout on-chip memory */ + buf[1] = 0x0f; /* on-chip memory address */ + write(s_sht2x_fd, buf, 2); - memset(buf, 0, sizeof(buf)); - read(s_sht2x_fd, buf, 4); + memset(buf, 0, sizeof(buf)); + read(s_sht2x_fd, buf, 4); - serialnumber[5]=buf[0]; /* Read SNB_3 */ - serialnumber[4]=buf[1]; /* Read SNB_2 */ - serialnumber[3]=buf[2]; /* Read SNB_1 */ - serialnumber[2]=buf[3]; /* Read SNB_0 */ - - /* Read SerialNumber from Location 2 */ - memset(buf, 0, sizeof(buf) ); - buf[0]=0xfc; /* command for readout on-chip memory */ - buf[1]=0xc9; /* on-chip memory address */ - write(s_sht2x_fd, buf, 2); + serialnumber[5]=buf[0]; /* Read SNB_3 */ + serialnumber[4]=buf[1]; /* Read SNB_2 */ + serialnumber[3]=buf[2]; /* Read SNB_1 */ + serialnumber[2]=buf[3]; /* Read SNB_0 */ - memset(buf, 0, sizeof(buf) ); - read(s_sht2x_fd, buf, 4); + /* Read SerialNumber from Location 2 */ + memset(buf, 0, sizeof(buf) ); + buf[0]=0xfc; /* command for readout on-chip memory */ + buf[1]=0xc9; /* on-chip memory address */ + write(s_sht2x_fd, buf, 2); - serialnumber[1]=buf[0]; /* Read SNC_1 */ - serialnumber[0]=buf[1]; /* Read SNC_0 */ - serialnumber[7]=buf[2]; /* Read SNA_1 */ - serialnumber[6]=buf[3]; /* Read SNA_0 */ + memset(buf, 0, sizeof(buf) ); + read(s_sht2x_fd, buf, 4); - //dump_buf("SHT2x Serial number: ", serialnumber, 8); + serialnumber[1]=buf[0]; /* Read SNC_1 */ + serialnumber[0]=buf[1]; /* Read SNC_0 */ + serialnumber[7]=buf[2]; /* Read SNA_1 */ + serialnumber[6]=buf[3]; /* Read SNA_0 */ - return 0; + //dump_buf("SHT2x Serial number: ", serialnumber, 8); + + return 0; } #elif (defined I2C_API_IOCTL) /* Use I2C userspace driver read/write API */ @@ -213,67 +214,68 @@ int sht2x_softreset(int fd) { struct i2c_msg msg; - struct i2c_rdwr_ioctl_data sht2x_data; - uint8_t buf[2]; + struct i2c_rdwr_ioctl_data sht2x_data; + uint8_t buf[2]; - if( fd<0 ) - { - log_err("Invalid input arguments\n"); - return -1; - } + if( fd<0 ) + { + log_err("Invalid input arguments\n"); + return -1; + } - msg.addr= 0x40; - msg.flags=0; //write - msg.len= 1; - msg.buf= buf; - msg.buf[0]=SOFTRESET; + msg.addr= 0x40; + msg.flags=0; //write + msg.len= 1; + msg.buf= buf; + msg.buf[0]=SOFTRESET; - sht2x_data.nmsgs= 1; - sht2x_data.msgs= &msg; + sht2x_data.nmsgs= 1; + sht2x_data.msgs= &msg; - if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 ) - { - log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); + if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 ) + { + log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); sht2x_term(); - return -2; - } + return -2; + } - msleep(50); + msleep(50); - return 0; + return 0; } int sht2x_init(void) { - if( (s_sht2x_fd=open("/dev/i2c-1", O_RDWR)) < 0) - { + if( (s_sht2x_fd=open("/dev/i2c-1", O_RDWR)) < 0) + { log_err("i2c device open failed: %s\n", strerror(errno)); return -1; - } + } - if( sht2x_softreset(s_sht2x_fd) < 0 ) - { - log_err("SHT2x softreset failure\n"); + if( sht2x_softreset(s_sht2x_fd) < 0 ) + { + log_err("SHT2x softreset failure\n"); sht2x_term(); - return -2; - } + return -2; + } + log_dbg("SHT2X initialise ok, s_sht2x_fd=%d\n", s_sht2x_fd); return 0; } int sht2x_get_serialnumber(uint8_t *serialnumber, int size) { struct i2c_msg msgs[2]; - struct i2c_rdwr_ioctl_data sht2x_data; - uint8_t sbuf[2]; - uint8_t rbuf[4]; + struct i2c_rdwr_ioctl_data sht2x_data; + uint8_t sbuf[2]; + uint8_t rbuf[4]; - if( !serialnumber || size!=8 ) - { - log_err("Invalid input arguments\n"); - return -1; - } + if( !serialnumber || size!=8 ) + { + log_err("Invalid input arguments\n"); + return -1; + } if( s_sht2x_fd < 0 ) { @@ -284,86 +286,86 @@ } } - /*+------------------------------------------+ - *| Read SerialNumber from Location 1 | - *+------------------------------------------+*/ + /*+------------------------------------------+ + *| Read SerialNumber from Location 1 | + *+------------------------------------------+*/ - msgs[0].addr= 0x40; - msgs[0].flags=0; //write - msgs[0].len= 2; - msgs[0].buf= sbuf; - msgs[0].buf[0]=0xfa; /* command for readout on-chip memory */ - msgs[0].buf[1]=0x0f; /* on-chip memory address */ - - msgs[1].addr=0x40; - msgs[1].flags=I2C_M_RD; //write - msgs[1].len= 4; - msgs[1].buf= rbuf; + msgs[0].addr= 0x40; + msgs[0].flags=0; //write + msgs[0].len= 2; + msgs[0].buf= sbuf; + msgs[0].buf[0]=0xfa; /* command for readout on-chip memory */ + msgs[0].buf[1]=0x0f; /* on-chip memory address */ - sht2x_data.nmsgs= 2; - sht2x_data.msgs= msgs; + msgs[1].addr=0x40; + msgs[1].flags=I2C_M_RD; //write + msgs[1].len= 4; + msgs[1].buf= rbuf; - if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) - { - log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); + sht2x_data.nmsgs= 2; + sht2x_data.msgs= msgs; + + if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) + { + log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); sht2x_term(); - return -2; - } - - serialnumber[5]=rbuf[0]; /* Read SNB_3 */ + return -2; + } + + serialnumber[5]=rbuf[0]; /* Read SNB_3 */ serialnumber[4]=rbuf[1]; /* Read SNB_2 */ serialnumber[3]=rbuf[2]; /* Read SNB_1 */ serialnumber[2]=rbuf[3]; /* Read SNB_0 */ - /*+------------------------------------------+ - *| Read SerialNumber from Location 2 | - *+------------------------------------------+*/ - - msgs[0].addr= 0x40; - msgs[0].flags=0; //write - msgs[0].len= 2; - msgs[0].buf= sbuf; - msgs[0].buf[0]=0xfc; /* command for readout on-chip memory */ - msgs[0].buf[1]=0xc9; /* on-chip memory address */ - - msgs[1].addr=0x40; - msgs[1].flags=I2C_M_RD; //write - msgs[1].len= 4; - msgs[1].buf= rbuf; + /*+------------------------------------------+ + *| Read SerialNumber from Location 2 | + *+------------------------------------------+*/ - sht2x_data.nmsgs= 2; - sht2x_data.msgs= msgs; + msgs[0].addr= 0x40; + msgs[0].flags=0; //write + msgs[0].len= 2; + msgs[0].buf= sbuf; + msgs[0].buf[0]=0xfc; /* command for readout on-chip memory */ + msgs[0].buf[1]=0xc9; /* on-chip memory address */ - if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) - { - log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); + msgs[1].addr=0x40; + msgs[1].flags=I2C_M_RD; //write + msgs[1].len= 4; + msgs[1].buf= rbuf; + + sht2x_data.nmsgs= 2; + sht2x_data.msgs= msgs; + + if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) + { + log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); sht2x_term(); - return -2; - } - - serialnumber[1]=rbuf[0]; /* Read SNC_1 */ - serialnumber[0]=rbuf[1]; /* Read SNC_0 */ - serialnumber[7]=rbuf[2]; /* Read SNA_1 */ - serialnumber[6]=rbuf[3]; /* Read SNA_0 */ + return -2; + } - //dump_buf("SHT2x Serial number: ", serialnumber, 8); + serialnumber[1]=rbuf[0]; /* Read SNC_1 */ + serialnumber[0]=rbuf[1]; /* Read SNC_0 */ + serialnumber[7]=rbuf[2]; /* Read SNA_1 */ + serialnumber[6]=rbuf[3]; /* Read SNA_0 */ - return 0; + //dump_buf("SHT2x Serial number: ", serialnumber, 8); + + return 0; } int sht2x_get_temp_humidity(float *temp, float *rh) { struct i2c_msg msg; - struct i2c_rdwr_ioctl_data sht2x_data; - uint8_t buf[4]; + struct i2c_rdwr_ioctl_data sht2x_data; + uint8_t buf[4]; - if( !temp || !rh ) - { - log_err("Invalid input arguments\n"); - return -1; - } + if( !temp || !rh ) + { + log_err("Invalid input arguments\n"); + return -1; + } if( s_sht2x_fd < 0 ) { @@ -373,91 +375,91 @@ return -2; } } - - /*+------------------------------------------+ - *| measure and get temperature | - *+------------------------------------------+*/ - msg.addr= 0x40; - msg.flags=0; //write - msg.len= 1; - msg.buf= buf; - msg.buf[0]=TRIGGER_TEMPERATURE_NO_HOLD; /* trigger temperature without hold I2C bus */ + /*+------------------------------------------+ + *| measure and get temperature | + *+------------------------------------------+*/ - sht2x_data.nmsgs= 1; - sht2x_data.msgs= &msg; + msg.addr= 0x40; + msg.flags=0; //write + msg.len= 1; + msg.buf= buf; + msg.buf[0]=TRIGGER_TEMPERATURE_NO_HOLD; /* trigger temperature without hold I2C bus */ - if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) - { - log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); + sht2x_data.nmsgs= 1; + sht2x_data.msgs= &msg; + + if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) + { + log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); sht2x_term(); - return -2; - } + return -2; + } - msleep(85); - - memset(buf, 0, sizeof(buf)); - msg.addr=0x40; - msg.flags=I2C_M_RD; //write - msg.len= 3; - msg.buf= buf; + msleep(85); - sht2x_data.nmsgs= 1; - sht2x_data.msgs= &msg; + memset(buf, 0, sizeof(buf)); + msg.addr=0x40; + msg.flags=I2C_M_RD; //write + msg.len= 3; + msg.buf= buf; - if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) - { - log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); + sht2x_data.nmsgs= 1; + sht2x_data.msgs= &msg; + + if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) + { + log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); sht2x_term(); - return -2; - } - - //dump_buf("Temperature sample data: ", buf, 3); - *temp = 175.72 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 46.85; + return -2; + } - - /*+------------------------------------------+ - *| measure and get relative humidity | - *+------------------------------------------+*/ + //dump_buf("Temperature sample data: ", buf, 3); + *temp = 175.72 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 46.85; - msg.addr= 0x40; - msg.flags=0; //write - msg.len= 1; - msg.buf= buf; - msg.buf[0]=TRIGGER_HUMIDITY_NO_HOLD; /* trigger humidity without hold I2C bus */ - sht2x_data.nmsgs= 1; - sht2x_data.msgs= &msg; + /*+------------------------------------------+ + *| measure and get relative humidity | + *+------------------------------------------+*/ - if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) - { - log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); + msg.addr= 0x40; + msg.flags=0; //write + msg.len= 1; + msg.buf= buf; + msg.buf[0]=TRIGGER_HUMIDITY_NO_HOLD; /* trigger humidity without hold I2C bus */ + + sht2x_data.nmsgs= 1; + sht2x_data.msgs= &msg; + + if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) + { + log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); sht2x_term(); - return -2; - } + return -2; + } - msleep(29); - - memset(buf, 0, sizeof(buf)); - msg.addr=0x40; - msg.flags=I2C_M_RD; //write - msg.len= 3; - msg.buf= buf; + msleep(29); - sht2x_data.nmsgs= 1; - sht2x_data.msgs= &msg; + memset(buf, 0, sizeof(buf)); + msg.addr=0x40; + msg.flags=I2C_M_RD; //write + msg.len= 3; + msg.buf= buf; - if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) - { - log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); + sht2x_data.nmsgs= 1; + sht2x_data.msgs= &msg; + + if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 ) + { + log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno)); sht2x_term(); - return -2; - } + return -2; + } - //dump_buf("Relative humidity sample data: ", buf, 3); - *rh = 125 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 6; - - return 0; + //dump_buf("Relative humidity sample data: ", buf, 3); + *rh = 125 * (((((int) buf[0]) << 8) + buf[1]) / 65536.0) - 6; + + return 0; } #endif diff --git a/iotd/hal/tsl2561.c b/iotd/hal/tsl2561.c index 5f2c9d0..6ffc165 100644 --- a/iotd/hal/tsl2561.c +++ b/iotd/hal/tsl2561.c @@ -43,10 +43,10 @@ if( (s_tsl_fd=open("/dev/i2c-1", O_RDWR)) < 0) { log_err("TSL2561 I2C device setup failure: %s\n", strerror(errno)); - return -1; + return -1; } - log_nrml("TSL2561 initialise ok, s_tsl_fd=%d\n", s_tsl_fd); + log_dbg("TSL2561 initialise ok, s_tsl_fd=%d\n", s_tsl_fd); return s_tsl_fd; } @@ -79,7 +79,7 @@ if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 ) { log_err("%s() ioctl failure: %s\n", __func__, strerror(errno)); - return -1; + return -1; } @@ -91,7 +91,7 @@ if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 ) { log_err("%s() ioctl failure: %s\n", __func__, strerror(errno)); - return -1; + return -1; } return 0; @@ -115,7 +115,7 @@ if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 ) { log_err("%s() ioctl failure: %s\n", __func__, strerror(errno)); - return -1; + return -1; } memset(buf, 0, sizeof(buf)); @@ -131,7 +131,7 @@ if( ioctl(s_tsl_fd, I2C_RDWR, &data) < 0 ) { log_err("%s() ioctl failure: %s\n", __func__, strerror(errno)); - return -1; + return -1; } *regval = msg.buf[0]; @@ -152,7 +152,7 @@ float div = 0.0; float lux = 0.0; - + tsl2561_power(ON); msleep(410); /* t(CONV) MAX 400ms */ @@ -160,7 +160,7 @@ /* Read register Channel0 and channel1 data from register */ for(i=0; i<REG_COUNT; i++) { - tsl2561_readreg(regs_addr[i], ®_data[i]); + tsl2561_readreg(regs_addr[i], ®_data[i]); } chn0_data = reg_data[1]*256 + reg_data[0]; /* Channel0 = DATA0HIGH<<8 + DATA0LOW */ -- Gitblit v1.9.1