2016-01-26 23:58:18 +08:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/i2c/i2c_writeread.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-26 23:58:18 +08:00
|
|
|
*
|
2021-03-04 14:10:42 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-01-26 23:58:18 +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-26 23:58:18 +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-26 23:58:18 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
2016-01-27 00:57:51 +08:00
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: i2c_writeread
|
2016-01-26 23:58:18 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2016-02-02 09:52:16 +08:00
|
|
|
* Send a block of data on I2C followed by restarted read access. This
|
|
|
|
* provides a convenient wrapper to the transfer function.
|
2016-01-27 00:57:51 +08:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* dev - Device-specific state data
|
|
|
|
* config - Described the I2C configuration
|
2020-04-01 13:03:22 +08:00
|
|
|
* wbuffer - A pointer to the read-only buffer of data to be written to
|
|
|
|
* device
|
2016-01-27 00:57:51 +08:00
|
|
|
* wbuflen - The number of bytes to send from the buffer
|
2020-04-01 13:03:22 +08:00
|
|
|
* rbuffer - A pointer to a buffer of data to receive the data from the
|
|
|
|
* device
|
2016-01-27 00:57:51 +08:00
|
|
|
* rbuflen - The requested number of bytes to be read
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: success, <0: A negated errno
|
2016-01-26 23:58:18 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2016-01-30 22:36:47 +08:00
|
|
|
int i2c_writeread(FAR struct i2c_master_s *dev,
|
|
|
|
FAR const struct i2c_config_s *config,
|
2016-01-26 23:58:18 +08:00
|
|
|
FAR const uint8_t *wbuffer, int wbuflen,
|
|
|
|
FAR uint8_t *rbuffer, int rbuflen)
|
|
|
|
{
|
|
|
|
struct i2c_msg_s msg[2];
|
|
|
|
unsigned int flags;
|
2017-08-04 21:31:36 +08:00
|
|
|
int ret;
|
2016-01-26 23:58:18 +08:00
|
|
|
|
|
|
|
/* 7- or 10-bit address? */
|
|
|
|
|
|
|
|
DEBUGASSERT(config->addrlen == 10 || config->addrlen == 7);
|
|
|
|
flags = (config->addrlen == 10) ? I2C_M_TEN : 0;
|
|
|
|
|
2018-08-03 22:51:55 +08:00
|
|
|
/* Format two messages: The first is a write which is never terminated
|
|
|
|
* with STOP condition.
|
|
|
|
*/
|
2016-01-26 23:58:18 +08:00
|
|
|
|
2018-08-03 21:43:57 +08:00
|
|
|
msg[0].frequency = config->frequency,
|
|
|
|
msg[0].addr = config->address;
|
2018-08-03 22:51:55 +08:00
|
|
|
msg[0].flags = flags | I2C_M_NOSTOP;
|
2018-08-03 21:43:57 +08:00
|
|
|
msg[0].buffer = (FAR uint8_t *)wbuffer; /* Override const */
|
|
|
|
msg[0].length = wbuflen;
|
|
|
|
|
|
|
|
/* The second is either a read (rbuflen > 0) with a repeated start or a
|
|
|
|
* write (rbuflen < 0) with no restart.
|
2016-01-26 23:58:18 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (rbuflen > 0)
|
|
|
|
{
|
2018-08-03 21:43:57 +08:00
|
|
|
msg[1].flags = (flags | I2C_M_READ);
|
2016-01-26 23:58:18 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-03 22:51:55 +08:00
|
|
|
msg[1].flags = (flags | I2C_M_NOSTART);
|
|
|
|
rbuflen = -rbuflen;
|
2016-01-26 23:58:18 +08:00
|
|
|
}
|
|
|
|
|
2018-08-03 21:43:57 +08:00
|
|
|
msg[1].frequency = config->frequency,
|
|
|
|
msg[1].addr = config->address;
|
|
|
|
msg[1].buffer = rbuffer;
|
|
|
|
msg[1].length = rbuflen;
|
2016-01-26 23:58:18 +08:00
|
|
|
|
2016-02-02 04:17:20 +08:00
|
|
|
/* Then perform the transfer. */
|
2016-01-26 23:58:18 +08:00
|
|
|
|
2017-08-04 21:31:36 +08:00
|
|
|
ret = I2C_TRANSFER(dev, msg, 2);
|
|
|
|
return (ret >= 0) ? OK : ret;
|
2016-01-26 23:58:18 +08:00
|
|
|
}
|