2016-01-27 00:57:51 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/i2c/i2c_read.c
|
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* 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
|
2016-01-27 00:57:51 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-01-27 00:57:51 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* 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.
|
2016-01-27 00:57:51 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2016-01-30 22:00:16 +08:00
|
|
|
#include <nuttx/i2c/i2c_master.h>
|
2016-01-27 00:57:51 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2c_read
|
|
|
|
*
|
|
|
|
* Description:
|
2016-02-02 09:52:16 +08:00
|
|
|
* Receive a block of data from I2C. Each read operation will be an
|
|
|
|
* 'atomic' operation in the sense that any other I2C actions will be
|
|
|
|
* serialized and pend until this read completes.
|
2016-01-27 00:57:51 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
2020-04-01 13:03:22 +08:00
|
|
|
* buffer - A pointer to a buffer of data to receive the data from the
|
|
|
|
* device
|
2016-01-27 00:57:51 +08:00
|
|
|
* buflen - The requested number of bytes to be read
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: success, <0: A negated errno
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-30 22:36:47 +08:00
|
|
|
int i2c_read(FAR struct i2c_master_s *dev,
|
|
|
|
FAR const struct i2c_config_s *config,
|
2016-01-27 00:57:51 +08:00
|
|
|
FAR uint8_t *buffer, int buflen)
|
|
|
|
{
|
|
|
|
struct i2c_msg_s msg;
|
|
|
|
unsigned int flags;
|
2017-08-04 21:31:36 +08:00
|
|
|
int ret;
|
2016-01-27 00:57:51 +08:00
|
|
|
|
|
|
|
/* 7- or 10-bit? */
|
|
|
|
|
|
|
|
flags = (config->addrlen == 10) ? I2C_M_TEN : 0;
|
|
|
|
|
|
|
|
/* Setup for the transfer */
|
|
|
|
|
2016-02-02 04:17:20 +08:00
|
|
|
msg.frequency = config->frequency,
|
|
|
|
msg.addr = config->address,
|
|
|
|
msg.flags = (flags | I2C_M_READ);
|
|
|
|
msg.buffer = buffer;
|
|
|
|
msg.length = buflen;
|
2016-01-27 00:57:51 +08:00
|
|
|
|
2016-02-02 04:17:20 +08:00
|
|
|
/* Then perform the transfer. */
|
2016-01-27 00:57:51 +08:00
|
|
|
|
2017-08-04 21:31:36 +08:00
|
|
|
ret = I2C_TRANSFER(dev, &msg, 1);
|
|
|
|
return (ret >= 0) ? OK : ret;
|
2016-01-27 00:57:51 +08:00
|
|
|
}
|