1wire_write.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /****************************************************************************
  2. * drivers/1wire/1wire_write.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_write
  33. *
  34. * Description:
  35. * Send a block of data on 1WIRE. Each write operation will be an 'atomic'
  36. * operation in the sense that any other 1WIRE actions will be serialized
  37. * and pend until this write completes.
  38. *
  39. * Input Parameters:
  40. * master - Device-specific state data
  41. * config - Described the 1WIRE configuration
  42. * buffer - A pointer to the read-only buffer of data to be written to
  43. * device
  44. * buflen - The number of bytes to send from the buffer
  45. *
  46. * Returned Value:
  47. * 0: success, <0: A negated errno
  48. *
  49. ****************************************************************************/
  50. int onewire_write(FAR struct onewire_master_s *master,
  51. FAR const struct onewire_config_s *config,
  52. FAR const uint8_t *buffer, int buflen)
  53. {
  54. int ret;
  55. /* Avoid calling this function from a search callback to prevent a
  56. * deadlock
  57. */
  58. if (master->insearch == true)
  59. {
  60. return -EAGAIN;
  61. }
  62. ret = onewire_sem_wait(master);
  63. if (ret < 0)
  64. {
  65. return ret;
  66. }
  67. ret = onewire_reset_select(master, config->romcode);
  68. if (ret < 0)
  69. {
  70. goto err_unlock;
  71. }
  72. /* Then perform the transfer. */
  73. ret = ONEWIRE_WRITE(master->dev, buffer, buflen);
  74. err_unlock:
  75. onewire_sem_post(master);
  76. return ret;
  77. }