nmaggioni commented on code in PR #16838: URL: https://github.com/apache/nuttx/pull/16838#discussion_r2276828365
########## drivers/sensors/tmp112.c: ########## @@ -0,0 +1,402 @@ +/**************************************************************************** + * drivers/sensors/tmp112.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <assert.h> +#include <errno.h> +#include <debug.h> +#include <string.h> +#include <time.h> + +#include <nuttx/arch.h> +#include <nuttx/kmalloc.h> +#include <nuttx/signal.h> +#include <nuttx/fs/fs.h> +#include <nuttx/i2c/i2c_master.h> +#include <nuttx/random.h> + +#include <nuttx/sensors/tmp112.h> + +#if defined(CONFIG_I2C) && defined(CONFIG_SENSORS_TMP112) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct tmp112_dev_s +{ + FAR struct i2c_master_s *i2c; /* I2C interface */ + uint8_t addr; /* TMP112 I2C address */ + int freq; /* TMP112 Frequency */ + uint16_t tmp112_temp_raw; /* Temperature as read from TMP112 */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/* Character driver methods */ + +static ssize_t tmp112_read(FAR struct file *filep, FAR char *buffer, + size_t buflen); + +static ssize_t tmp112_write(FAR struct file *filep, FAR const char *buffer, + size_t buflen); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct file_operations g_tmp112fops = +{ + NULL, /* open */ + NULL, /* close */ + tmp112_read, /* read */ + tmp112_write, /* write */ +}; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +uint8_t tmp112_getreg8(FAR struct tmp112_dev_s *priv, uint8_t regaddr) +{ + struct i2c_config_s config; + uint8_t regval = 0; + int ret; + + /* Set up the I2C configuration */ + + config.frequency = priv->freq; + config.address = priv->addr; + config.addrlen = 7; + + /* Write the register address */ + + ret = i2c_write(priv->i2c, &config, ®addr, 1); + if (ret < 0) + { + snerr("ERROR: i2c_write failed: %s (%d)\n", strerror(-ret), ret); + return ret; + } + + /* Read the register value */ + + ret = i2c_read(priv->i2c, &config, ®val, 1); + if (ret < 0) + { + snerr("ERROR: i2c_read failed: %d\n", ret); + return ret; + } + + return regval; +} + +uint16_t tmp112_getreg16(FAR struct tmp112_dev_s *priv, uint8_t regaddr, + uint8_t ms_delay) +{ + struct i2c_config_s config; + uint16_t msb; + uint16_t lsb; + uint16_t regval = 0; + int ret; + + /* Set up the I2C configuration */ + + config.frequency = priv->freq; + config.address = priv->addr; + config.addrlen = 7; + + /* Register to read */ + + ret = i2c_write(priv->i2c, &config, ®addr, 1); + if (ret < 0) + { + snerr("ERROR: i2c_write failed: %s (%d)\n", strerror(-ret), ret); + return ret; + } + + if (ms_delay > 0) + { + nxsig_usleep(ms_delay * 1000); + } + + /* Read register */ + + ret = i2c_read(priv->i2c, &config, (FAR uint8_t *)®val, 2); + if (ret < 0) + { + snerr("ERROR: i2c_read failed: %d\n", ret); + return ret; + } + + /* MSB and LSB are inverted */ + + msb = (regval & 0xff); + lsb = (regval & 0xff00) >> 8; + + regval = (msb << 4) | (lsb >> 4); + + return regval; +} + +int tmp112_putreg8(FAR struct tmp112_dev_s *priv, uint8_t regaddr, + uint8_t regval) +{ + struct i2c_config_s config; + uint8_t data[2]; + int ret; + + /* Set up the I2C configuration */ + + config.frequency = priv->freq; + config.address = priv->addr; + config.addrlen = 7; + + data[0] = regaddr; + data[1] = regval; + + /* Write the register address and value */ + + ret = i2c_write(priv->i2c, &config, (FAR uint8_t *)&data, 2); + if (ret < 0) + { + snerr("ERROR: i2c_write failed: %s (%d)\n", strerror(-ret), ret); + return ret; + } + + return OK; +} + +int tmp112_putreg16(FAR struct tmp112_dev_s *priv, uint8_t regaddr, + uint16_t regval) +{ + struct i2c_config_s config; + uint8_t data[3]; + int ret; + + /* Set up the I2C configuration */ + + config.frequency = priv->freq; + config.address = priv->addr; + config.addrlen = 7; + + data[0] = regaddr; + data[1] = (regval >> 8) & 0xff; + data[2] = regval & 0xff; + + /* Write the register address and value */ + + ret = i2c_write(priv->i2c, &config, (FAR uint8_t *)&data, 3); Review Comment: This was actually needed, CI doesn't break but in my environment the `incompatible-pointer-types` compiler warning gets triggered (`expected 'const uint8_t *' [...] but argument is of type 'uint8_t (*)[2]' [...]`) and promoted to an error. I think it's cleaner to keep this cast like in the BMP280 code. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org