components/esp32-owb/include/owb.h

changeset 72
acc1904cd70d
parent 0
88d965579617
equal deleted inserted replaced
71:995557380e5f 72:acc1904cd70d
28 * @brief Interface definitions for the 1-Wire bus component. 28 * @brief Interface definitions for the 1-Wire bus component.
29 * 29 *
30 * This component provides structures and functions that are useful for communicating 30 * This component provides structures and functions that are useful for communicating
31 * with devices connected to a Maxim Integrated 1-Wire® bus via a single GPIO. 31 * with devices connected to a Maxim Integrated 1-Wire® bus via a single GPIO.
32 * 32 *
33 * Currently only externally powered devices are supported. Parasitic power is not supported. 33 * Externally powered and "parasite-powered" devices are supported.
34 * Please consult your device's datasheet for power requirements.
34 */ 35 */
35 36
36 #ifndef ONE_WIRE_BUS_H 37 #ifndef ONE_WIRE_BUS_H
37 #define ONE_WIRE_BUS_H 38 #define ONE_WIRE_BUS_H
38 39
39 #include <stdint.h> 40 #include <stdint.h>
40 #include <stdbool.h> 41 #include <stdbool.h>
41 #include <stddef.h> 42 #include <stddef.h>
43 #include "driver/gpio.h"
42 44
43 #ifdef __cplusplus 45 #ifdef __cplusplus
44 extern "C" { 46 extern "C" {
45 #endif 47 #endif
46 48
47 49
48 // ROM commands 50 // ROM commands
49 #define OWB_ROM_SEARCH 0xF0 ///< Command ROM search 51 #define OWB_ROM_SEARCH 0xF0 ///< Perform Search ROM cycle to identify devices on the bus
50 #define OWB_ROM_READ 0x33 ///< ROM read 52 #define OWB_ROM_READ 0x33 ///< Read device ROM (single device on bus only)
51 #define OWB_ROM_MATCH 0x55 ///< ROM address match 53 #define OWB_ROM_MATCH 0x55 ///< Address a specific device on the bus by ROM
52 #define OWB_ROM_SKIP 0xCC ///< Skip device 54 #define OWB_ROM_SKIP 0xCC ///< Address all devices on the bus simultaneously
53 #define OWB_ROM_SEARCH_ALARM 0xEC ///< Search device with alarm set 55 #define OWB_ROM_SEARCH_ALARM 0xEC ///< Address all devices on the bus with a set alarm flag
54 56
55 #define OWB_ROM_CODE_STRING_LENGTH (17) ///< Typical length of OneWire bus ROM ID as ASCII hex string, including null terminator 57 #define OWB_ROM_CODE_STRING_LENGTH (17) ///< Typical length of OneWire bus ROM ID as ASCII hex string, including null terminator
58
59 #ifndef GPIO_NUM_NC
60 # define GPIO_NUM_NC (-1) ///< ESP-IDF prior to v4.x does not define GPIO_NUM_NC
61 #endif
56 62
57 struct owb_driver; 63 struct owb_driver;
58 64
59 /** 65 /**
60 * @brief Structure containing 1-Wire bus information relevant to a single instance. 66 * @brief Structure containing 1-Wire bus information relevant to a single instance.
61 */ 67 */
62 typedef struct 68 typedef struct
63 { 69 {
64 const struct _OneWireBus_Timing * timing; ///< Pointer to timing information 70 const struct _OneWireBus_Timing * timing; ///< Pointer to timing information
65 bool use_crc; ///< True if CRC checks are to be used when retrieving information from a device on the bus 71 bool use_crc; ///< True if CRC checks are to be used when retrieving information from a device on the bus
66 72 bool use_parasitic_power; ///< True if parasitic-powered devices are expected on the bus
67 const struct owb_driver *driver; 73 gpio_num_t strong_pullup_gpio; ///< Set if an external strong pull-up circuit is required
74 const struct owb_driver * driver; ///< Pointer to hardware driver instance
68 } OneWireBus; 75 } OneWireBus;
69 76
70 /** 77 /**
71 * @brief Represents a 1-Wire ROM Code. This is a sequence of eight bytes, where 78 * @brief Represents a 1-Wire ROM Code. This is a sequence of eight bytes, where
72 * the first byte is the family number, then the following 6 bytes form the 79 * the first byte is the family number, then the following 6 bytes form the
92 * Pass a pointer to this structure to owb_search_first() and 99 * Pass a pointer to this structure to owb_search_first() and
93 * owb_search_next() to iterate through detected devices on the bus. 100 * owb_search_next() to iterate through detected devices on the bus.
94 */ 101 */
95 typedef struct 102 typedef struct
96 { 103 {
97 OneWireBus_ROMCode rom_code; 104 OneWireBus_ROMCode rom_code; ///< Device ROM code
98 int last_discrepancy; 105 int last_discrepancy; ///< Bit index that identifies from which bit the next search discrepancy check should start
99 int last_family_discrepancy; 106 int last_family_discrepancy; ///< Bit index that identifies the last discrepancy within the first 8-bit family code of the ROM code
100 int last_device_flag; 107 int last_device_flag; ///< Flag to indicate previous search was the last device detected
101 } OneWireBus_SearchState; 108 } OneWireBus_SearchState;
102 109
103 /** 110 /**
104 * @brief Status codes 111 * @brief Represents the result of OWB API functions.
105 */ 112 */
106 typedef enum 113 typedef enum
107 { 114 {
108 OWB_STATUS_OK, ///< Ok 115 OWB_STATUS_NOT_SET = -1, ///< A status value has not been set
109 OWB_STATUS_NOT_INITIALIZED, ///< Init error 116 OWB_STATUS_OK = 0, ///< Operation succeeded
110 OWB_STATUS_PARAMETER_NULL, ///< NULL parameter 117 OWB_STATUS_NOT_INITIALIZED, ///< Function was passed an uninitialised variable
111 OWB_STATUS_DEVICE_NOT_RESPONDING, ///< Device does not respond 118 OWB_STATUS_PARAMETER_NULL, ///< Function was passed a null pointer
112 OWB_STATUS_CRC_FAILED, ///< CRC error 119 OWB_STATUS_DEVICE_NOT_RESPONDING, ///< No response received from the addressed device or devices
113 OWB_STATUS_TOO_MANY_BITS, ///< Too many bits 120 OWB_STATUS_CRC_FAILED, ///< CRC failed on data received from a device or devices
114 OWB_STATUS_HW_ERROR ///< Hardware error 121 OWB_STATUS_TOO_MANY_BITS, ///< Attempt to write an incorrect number of bits to the One Wire Bus
122 OWB_STATUS_HW_ERROR ///< A hardware error occurred
115 } owb_status; 123 } owb_status;
116 124
117 /** NOTE: Driver assumes that (*init) was called prior to any other methods */ 125 /** NOTE: Driver assumes that (*init) was called prior to any other methods */
118 struct owb_driver 126 struct owb_driver
119 { 127 {
120 const char* name; 128 const char* name;
121
122 owb_status (*uninitialize)(const OneWireBus * bus); 129 owb_status (*uninitialize)(const OneWireBus * bus);
123 130 owb_status (*reset)(const OneWireBus *bus, bool *is_present);
124 owb_status (*reset)(const OneWireBus * bus, bool *is_present);
125
126 /** NOTE: The data is shifted out of the low bits, eg. it is written in the order of lsb to msb */
127 owb_status (*write_bits)(const OneWireBus *bus, uint8_t out, int number_of_bits_to_write); 131 owb_status (*write_bits)(const OneWireBus *bus, uint8_t out, int number_of_bits_to_write);
128 132 owb_status (*write_bytes)(const OneWireBus *bus, uint8_t *bytes, int number_of_bytes_to_write);
129 /** NOTE: Data is read into the high bits, eg. each bit read is shifted down before the next bit is read */
130 owb_status (*read_bits)(const OneWireBus *bus, uint8_t *in, int number_of_bits_to_read); 133 owb_status (*read_bits)(const OneWireBus *bus, uint8_t *in, int number_of_bits_to_read);
134 owb_status (*read_bytes)(const OneWireBus *bus, uint64_t *in, int number_of_bytes_to_read);
131 }; 135 };
132 136
137 /// @cond ignore
138 // note: the new owb_rmt driver uses the ESP-IDF's built-in `__containerof()` macro instead of this
133 #define container_of(ptr, type, member) ({ \ 139 #define container_of(ptr, type, member) ({ \
134 const typeof( ((type *)0)->member ) *__mptr = (ptr); \ 140 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
135 (type *)( (char *)__mptr - offsetof(type,member) );}) 141 (type *)( (char *)__mptr - offsetof(type,member) );})
142 /// @endcond
136 143
137 /** 144 /**
138 * @brief call to release resources after completing use of the OneWireBus 145 * @brief call to release resources after completing use of the OneWireBus
139 * @param[in] bus Pointer to initialised bus instance. 146 * @param[in] bus Pointer to initialised bus instance.
140 * @return status 147 * @return status
148 * @return status 155 * @return status
149 */ 156 */
150 owb_status owb_use_crc(OneWireBus * bus, bool use_crc); 157 owb_status owb_use_crc(OneWireBus * bus, bool use_crc);
151 158
152 /** 159 /**
160 * @brief Enable or disable use of parasitic power on the One Wire Bus.
161 * This affects how devices signal on the bus, as devices cannot
162 * signal by pulling the bus low when it is pulled high.
163 * @param[in] bus Pointer to initialised bus instance.
164 * @param[in] use_parasitic_power True to enable parasitic power, false to disable.
165 * @return status
166 */
167 owb_status owb_use_parasitic_power(OneWireBus * bus, bool use_parasitic_power);
168
169 /**
170 * @brief Enable or disable use of extra GPIO to activate strong pull-up circuit.
171 * This only has effect if parasitic power mode is enabled.
172 * signal by pulling the bus low when it is pulled high.
173 * @param[in] bus Pointer to initialised bus instance.
174 * @param[in] gpio Set to GPIO number to use, or GPIO_NUM_NC to disable.
175 * @return status
176 */
177 owb_status owb_use_strong_pullup_gpio(OneWireBus * bus, gpio_num_t gpio);
178
179 /**
153 * @brief Read ROM code from device - only works when there is a single device on the bus. 180 * @brief Read ROM code from device - only works when there is a single device on the bus.
154 * @param[in] bus Pointer to initialised bus instance. 181 * @param[in] bus Pointer to initialised bus instance.
155 * @param[out] rom_code the value read from the device's rom 182 * @param[out] rom_code the value read from the device's rom
156 * @return status 183 * @return status
157 */ 184 */
158 owb_status owb_read_rom(const OneWireBus * bus, OneWireBus_ROMCode *rom_code); 185 owb_status owb_read_rom(const OneWireBus * bus, OneWireBus_ROMCode * rom_code);
159 186
160 /** 187 /**
161 * @brief Verify the device specified by ROM code is present. 188 * @brief Verify the device specified by ROM code is present.
162 * @param[in] bus Pointer to initialised bus instance. 189 * @param[in] bus Pointer to initialised bus instance.
163 * @param[in] rom_code ROM code to verify. 190 * @param[in] rom_code ROM code to verify.
164 * @param[out] is_present set to true if a device is present, false if not 191 * @param[out] is_present Set to true if a device is present, false if not
165 * @return status 192 * @return status
166 */ 193 */
167 owb_status owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code, bool* is_present); 194 owb_status owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code, bool * is_present);
168 195
169 /** 196 /**
170 * @brief Reset the 1-Wire bus. 197 * @brief Reset the 1-Wire bus.
171 * @param[in] bus Pointer to initialised bus instance. 198 * @param[in] bus Pointer to initialised bus instance.
172 * @param[out] a_device_present set to true if at least one device is present on the bus 199 * @param[out] is_present set to true if at least one device is present on the bus
173 * @return status 200 * @return status
174 */ 201 */
175 owb_status owb_reset(const OneWireBus * bus, bool* a_device_present); 202 owb_status owb_reset(const OneWireBus * bus, bool * is_present);
176 203
177 /** 204 /**
178 * @brief Write a single byte to the 1-Wire bus. 205 * @brief Read a single bit from the 1-Wire bus.
179 * @param[in] bus Pointer to initialised bus instance. 206 * @param[in] bus Pointer to initialised bus instance.
180 * @param[in] data Byte value to write to bus. 207 * @param[out] out The bit value read from the bus.
181 * @return status 208 * @return status
182 */ 209 */
183 owb_status owb_write_byte(const OneWireBus * bus, uint8_t data); 210 owb_status owb_read_bit(const OneWireBus * bus, uint8_t * out);
184 211
185 /** 212 /**
186 * @brief Read a single byte from the 1-Wire bus. 213 * @brief Read a single byte from the 1-Wire bus.
187 * @param[in] bus Pointer to initialised bus instance. 214 * @param[in] bus Pointer to initialised bus instance.
188 * @param[out] out The byte value read from the bus. 215 * @param[out] out The byte value read from the bus (lsb only).
189 * @return status 216 * @return status
190 */ 217 */
191 owb_status owb_read_byte(const OneWireBus * bus, uint8_t *out); 218 owb_status owb_read_byte(const OneWireBus * bus, uint8_t * out);
192 219
193 /** 220 /**
194 * @brief Read a number of bytes from the 1-Wire bus. 221 * @brief Read a number of bytes from the 1-Wire bus.
195 * @param[in] bus Pointer to initialised bus instance. 222 * @param[in] bus Pointer to initialised bus instance.
196 * @param[in, out] buffer Pointer to buffer to receive read data. 223 * @param[in, out] buffer Pointer to buffer to receive read data.
197 * @param[in] len Number of bytes to read, must not exceed length of receive buffer. 224 * @param[in] len Number of bytes to read, must not exceed length of receive buffer.
198 * @return status. 225 * @return status.
199 */ 226 */
200 owb_status owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, size_t len); 227 owb_status owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len);
228
229 /**
230 * @brief Write a bit to the 1-Wire bus.
231 * @param[in] bus Pointer to initialised bus instance.
232 * @param[in] bit Value to write (lsb only).
233 * @return status
234 */
235 owb_status owb_write_bit(const OneWireBus * bus, uint8_t bit);
236
237 /**
238 * @brief Write a single byte to the 1-Wire bus.
239 * @param[in] bus Pointer to initialised bus instance.
240 * @param[in] data Byte value to write to bus.
241 * @return status
242 */
243 owb_status owb_write_byte(const OneWireBus * bus, uint8_t data);
201 244
202 /** 245 /**
203 * @brief Write a number of bytes to the 1-Wire bus. 246 * @brief Write a number of bytes to the 1-Wire bus.
204 * @param[in] bus Pointer to initialised bus instance. 247 * @param[in] bus Pointer to initialised bus instance.
205 * @param[in] buffer Pointer to buffer to write data from. 248 * @param[in] buffer Pointer to buffer to write data from.
265 * See OWB_ROM_CODE_STRING_LENGTH. 308 * See OWB_ROM_CODE_STRING_LENGTH.
266 * @return pointer to the byte beyond the last byte written 309 * @return pointer to the byte beyond the last byte written
267 */ 310 */
268 char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len); 311 char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len);
269 312
313 /**
314 * @brief Enable or disable the strong-pullup GPIO, if configured.
315 * @param[in] bus Pointer to initialised bus instance.
316 * @param[in] enable If true, enable the external strong pull-up by setting the GPIO high.
317 * If false, disable the external strong pull-up by setting the GPIO low.
318 * @return status
319 */
320 owb_status owb_set_strong_pullup(const OneWireBus * bus, bool enable);
321
322
270 #include "owb_gpio.h" 323 #include "owb_gpio.h"
271 #include "owb_rmt.h" 324 #include "owb_rmt.h"
272 325
273 #ifdef __cplusplus 326 #ifdef __cplusplus
274 } 327 }

mercurial