1wire_writeread.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /****************************************************************************
  2. * drivers/1wire/1wire_writeread.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/1wire/1wire.h>
  26. #include <nuttx/1wire/1wire_master.h>
  27. #include "1wire_internal.h"
  28. /****************************************************************************
  29. * Public Functions
  30. ****************************************************************************/
  31. /****************************************************************************
  32. * Name: 1wire_writeread
  33. *
  34. * Description:
  35. * Receive a block of data from 1WIRE. Each read operation will be an
  36. * 'atomic' operation in the sense that any other 1WIRE actions will be
  37. * serialized and pend until this read completes.
  38. *
  39. * Input Parameters:
  40. * master - Device-specific state data
  41. * config - Described the 1WIRE configuration
  42. * wbuffer - A pointer to the read-only buffer of data to be written to
  43. * device
  44. * wbuflen - The number of bytes to send from the buffer
  45. * rbuffer - A pointer to a buffer of data to receive the data from the
  46. * device
  47. * rbuflen - The requested number of bytes to be read
  48. *
  49. * Returned Value:
  50. * 0: success, <0: A negated errno
  51. *
  52. ****************************************************************************/
  53. int onewire_writeread(FAR struct onewire_master_s *master,
  54. FAR const struct onewire_config_s *config,
  55. FAR const uint8_t *wbuffer, int wbuflen,
  56. FAR uint8_t *rbuffer, int rbuflen)
  57. {
  58. int ret;
  59. /* Avoid calling this function from a search callback to prevent a
  60. * deadlock
  61. */
  62. if (master->insearch == true)
  63. {
  64. return -EAGAIN;
  65. }
  66. ret = onewire_sem_wait(master);
  67. if (ret < 0)
  68. {
  69. return ret;
  70. }
  71. ret = onewire_reset_select(master, config->romcode);
  72. if (ret < 0)
  73. {
  74. goto err_unlock;
  75. }
  76. /* Perform the transfer. */
  77. ret = ONEWIRE_WRITE(master->dev, wbuffer, wbuflen);
  78. if (ret < 0)
  79. {
  80. goto err_unlock;
  81. }
  82. ret = ONEWIRE_READ(master->dev, rbuffer, rbuflen);
  83. err_unlock:
  84. onewire_sem_post(master);
  85. return ret;
  86. }