owibus.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #ifndef _OWIBUS_H_
  2. #define _OWIBUS_H_
  3. /*
  4. * Copyright (C) 2012 by Uwe Bonnes(bon@elektron.ikp.physik.tu-darmstadt.de)
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*!
  37. * \file dev/owibus.c
  38. * \brief Header for the One-Wire API.
  39. *
  40. * \verbatim
  41. * $Id: owibus.h 5528 2014-01-07 18:17:58Z u_bonnes $
  42. * \endverbatim
  43. */
  44. #include <cfg/arch.h>
  45. #include <stdint.h>
  46. #include <sys/device.h>
  47. /*!
  48. * \addtogroup xgOwibus
  49. */
  50. /*@{*/
  51. /* Overdrive Commands as in
  52. * http://www.ovro.caltech.edu/~dwh/correlator/pdf/onewire.pdf
  53. */
  54. /*!
  55. * \brief One-Wire command codes for devices
  56. */
  57. #define OWI_READ_ROM 0x33 /*!< \brief Read 64-bit serial ID (single device only). */
  58. #define OWI_OVERDRIVE_SKIP_ROM 0x3c /*!< \brief As OWI_SKIP_ROM, but with command phase in fast mode. */
  59. #define OWI_CONVERT_T 0x44 /*!< \brief DS18B20: Start conversion on selected device(s). */
  60. #define OWI_MATCH_ROM 0x55 /*!< \brief Qualify command to single device. */
  61. #define OWI_OVERDRIVE_MATCH_ROM 0x69 /*!< \brief As OWI_MATCH_ROM but with command phase in fast mode. */
  62. #define OWI_SKIP_ROM 0xCC /*!< \brief Qualify command as broadcast. */
  63. #define OWI_READ 0xBE /*!< \brief Read data from addressed device. */
  64. #define OWI_SEARCH_ROM 0xF0 /*!< \brief Prepare devices for ID search. */
  65. /*!
  66. * \brief Constanst used for OwiRomSearch
  67. */
  68. #define OWI_LAST_DEVICE 0x00 /*!< \brief No more devices. */
  69. #define OWI_SEARCH_FIRST 0xFF /*!< \brief Start value for ID search. */
  70. /*!
  71. * \brief Known Owi-Wire families
  72. */
  73. #define W1_FAMILY_DEFAULT 0
  74. #define W1_FAMILY_SMEM_01 0x01
  75. #define W1_FAMILY_SMEM_81 0x81
  76. #define W1_THERM_DS18S20 0x10
  77. #define W1_FAMILY_DS28E04 0x1C
  78. #define W1_COUNTER_DS2423 0x1D
  79. #define W1_THERM_DS1822 0x22
  80. #define W1_EEPROM_DS2433 0x23
  81. #define W1_THERM_DS18B20 0x28
  82. #define W1_FAMILY_DS2408 0x29
  83. #define W1_EEPROM_DS2431 0x2D
  84. #define W1_FAMILY_DS2760 0x30
  85. #define W1_FAMILY_DS2780 0x32
  86. #define W1_THERM_DS1825 0x3B
  87. #define W1_FAMILY_DS2781 0x3D
  88. #define W1_THERM_DS28EA00 0x42
  89. /*!
  90. * \brief OWI return codes.
  91. *
  92. * \note The enum type itself is never used.
  93. */
  94. enum OWI_ERRORS {
  95. OWI_SUCCESS = 0, /*!< \brief All fine. */
  96. OWI_PRESENCE_ERR = -1, /*!< \brief No device present. */
  97. OWI_INVALID_HW = -2, /*!< \brief Selected Hardware can't do OWI transactions. */
  98. OWI_OUT_OF_MEM = -3, /*!< \brief No more memory. */
  99. OWI_HW_ERROR = -4, /*!< \brief Unexpected hardware behavior. */
  100. OWI_DATA_ERROR = -5, /*!< \brief Unexpected data. */
  101. OWI_NOT_IMPLEMENTED = -6, /*!< \brief No OWI implementation available. */
  102. };
  103. /*!
  104. * \brief OWI speed modes.
  105. *
  106. * \note The enum type itself is never used.
  107. */
  108. enum OWIBUS_MODE {
  109. OWI_MODE_NORMAL = 0, /*!< \brief Normal speed. */
  110. OWI_MODE_OVERDRIVE = 1, /*!< \brief High speed (only some device types). */
  111. OWI_MODE_NONE = 2, /*!< \brief List end marker. */
  112. };
  113. /*!
  114. * \brief OWI transactions.
  115. *
  116. * \note The enum type itself is never used.
  117. */
  118. enum STM32_OWITIMER_COMMANDS {
  119. OWI_CMD_RESET = 0, /*!< \brief Presence pulse. */
  120. OWI_CMD_RWBIT = 1, /*!< \brief Bit transaction. */
  121. OWI_CMD_NONE = 2, /*!< \brief List end marker. */
  122. };
  123. /*!
  124. * \brief OWI transactions phases.
  125. *
  126. * \note The enum type itself is never used.
  127. */
  128. enum STM32_OWITIMER_PHASES {
  129. OWI_PHASE_SETUP = 0, /*!< \brief Pull bus low. */
  130. OWI_PHASE_SYNC_PULSE = 1, /*!< \brief Release bus high for presence pulse, read and write '1'. */
  131. OWI_PHASE_SYNC_PULSE_LOW = 2,/*!< \brief Release bus high to write '0'. */
  132. OWI_PHASE_RW = 3, /*!< \brief Pull read bus state for presence and read. */
  133. OWI_PHASE_RELEASE = 4, /*!< \brief Unconditionally release bus. */
  134. OWI_PHASE_NONE = 5, /*!< \brief List end marker. */
  135. };
  136. /*!
  137. * \brief OWI bus modes
  138. */
  139. #define OWI_OVERDRIVE 0x1 /*!< \brief Overdrive capable devices on the bus. */
  140. #define OWI_PULLUP 0x2 /*!< \brief Active pull-up for loaded buses. */
  141. typedef struct _NUTOWIBUS NUTOWIBUS;
  142. #include "owibus_bbif.h"
  143. #include "owibus_uart.h"
  144. /*!
  145. * \brief The OWIBUS structure.
  146. *
  147. * Storage provided by the caller.
  148. */
  149. struct _NUTOWIBUS {
  150. uintptr_t owibus_info;
  151. uint32_t mode;
  152. int(*OwiSetup) (NUTOWIBUS *);
  153. int(*OwiTouchReset) (NUTOWIBUS *);
  154. int(*OwiReadBlock) (NUTOWIBUS *bus, uint8_t *data, uint_fast8_t);
  155. int(*OwiWriteBlock) (NUTOWIBUS *bus, uint8_t *data, uint_fast8_t);
  156. };
  157. int OwiInit(NUTOWIBUS *bus);
  158. int OwiRomSearch(NUTOWIBUS *bus, uint8_t *diff, uint64_t *hid);
  159. int OwiCommand(NUTOWIBUS *bus, uint8_t cmd, uint64_t *hid);
  160. int OwiReadBlock(NUTOWIBUS *bus, uint8_t *data, uint_fast8_t len);
  161. int OwiWriteBlock(NUTOWIBUS *bus, uint8_t *data, uint_fast8_t len);
  162. int OwiSetMode(NUTOWIBUS *bus, uint_fast8_t mode);
  163. int OwiGetMode(NUTOWIBUS *bus);
  164. extern NUTOWIBUS owiBus0Uart;
  165. extern NUTOWIBUS owiBus0Gpio;
  166. /*@}*/
  167. #endif