i2c_read.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /****************************************************************************
  2. * drivers/i2c/i2c_read.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <assert.h>
  25. #include <nuttx/i2c/i2c_master.h>
  26. /****************************************************************************
  27. * Public Functions
  28. ****************************************************************************/
  29. /****************************************************************************
  30. * Name: i2c_read
  31. *
  32. * Description:
  33. * Receive a block of data from I2C. Each read operation will be an
  34. * 'atomic' operation in the sense that any other I2C actions will be
  35. * serialized and pend until this read completes.
  36. *
  37. * Input Parameters:
  38. * dev - Device-specific state data
  39. * buffer - A pointer to a buffer of data to receive the data from the
  40. * device
  41. * buflen - The requested number of bytes to be read
  42. *
  43. * Returned Value:
  44. * 0: success, <0: A negated errno
  45. *
  46. ****************************************************************************/
  47. int i2c_read(FAR struct i2c_master_s *dev,
  48. FAR const struct i2c_config_s *config,
  49. FAR uint8_t *buffer, int buflen)
  50. {
  51. struct i2c_msg_s msg;
  52. unsigned int flags;
  53. int ret;
  54. /* 7- or 10-bit? */
  55. flags = (config->addrlen == 10) ? I2C_M_TEN : 0;
  56. /* Setup for the transfer */
  57. msg.frequency = config->frequency,
  58. msg.addr = config->address,
  59. msg.flags = (flags | I2C_M_READ);
  60. msg.buffer = buffer;
  61. msg.length = buflen;
  62. /* Then perform the transfer. */
  63. ret = I2C_TRANSFER(dev, &msg, 1);
  64. return (ret >= 0) ? OK : ret;
  65. }