esp32-warm-water
esp32 based project for the control of a heating element based on temperature
owb.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2017 David Antliff
5  * Copyright (c) 2017 Chris Morgan <chmorgan@gmail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
37 #ifndef ONE_WIRE_BUS_H
38 #define ONE_WIRE_BUS_H
39 
40 #include <stdint.h>
41 #include <stdbool.h>
42 #include <stddef.h>
43 #include "driver/gpio.h"
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 
50 // ROM commands
51 #define OWB_ROM_SEARCH 0xF0
52 #define OWB_ROM_READ 0x33
53 #define OWB_ROM_MATCH 0x55
54 #define OWB_ROM_SKIP 0xCC
55 #define OWB_ROM_SEARCH_ALARM 0xEC
56 
57 #define OWB_ROM_CODE_STRING_LENGTH (17)
58 
59 #ifndef GPIO_NUM_NC
60 # define GPIO_NUM_NC (-1)
61 #endif
62 
63 struct owb_driver;
64 
68 typedef struct
69 {
70  const struct _OneWireBus_Timing * timing;
71  bool use_crc;
73  gpio_num_t strong_pullup_gpio;
74  const struct owb_driver * driver;
75 } OneWireBus;
76 
82 typedef union
83 {
85  struct fields
86  {
87  uint8_t family[1];
88  uint8_t serial_number[6];
89  uint8_t crc[1];
90  } fields;
91 
92  uint8_t bytes[8];
93 
95 
102 typedef struct
103 {
109 
113 typedef enum
114 {
124 
127 {
129  const char* name;
130 
133 
135  owb_status (*reset)(const OneWireBus * bus, bool *is_present);
136 
138  owb_status (*write_bits)(const OneWireBus *bus, uint8_t out, int number_of_bits_to_write);
139 
141  owb_status (*read_bits)(const OneWireBus *bus, uint8_t *in, int number_of_bits_to_read);
142 };
143 
145 #define container_of(ptr, type, member) ({ \
146  const typeof( ((type *)0)->member ) *__mptr = (ptr); \
147  (type *)( (char *)__mptr - offsetof(type,member) );})
149 
156 
163 owb_status owb_use_crc(OneWireBus * bus, bool use_crc);
164 
173 owb_status owb_use_parasitic_power(OneWireBus * bus, bool use_parasitic_power);
174 
183 owb_status owb_use_strong_pullup_gpio(OneWireBus * bus, gpio_num_t gpio);
184 
191 owb_status owb_read_rom(const OneWireBus * bus, OneWireBus_ROMCode * rom_code);
192 
200 owb_status owb_verify_rom(const OneWireBus * bus, OneWireBus_ROMCode rom_code, bool * is_present);
201 
208 owb_status owb_reset(const OneWireBus * bus, bool * is_present);
209 
216 owb_status owb_read_bit(const OneWireBus * bus, uint8_t * out);
217 
224 owb_status owb_read_byte(const OneWireBus * bus, uint8_t * out);
225 
233 owb_status owb_read_bytes(const OneWireBus * bus, uint8_t * buffer, unsigned int len);
234 
241 owb_status owb_write_bit(const OneWireBus * bus, uint8_t bit);
242 
249 owb_status owb_write_byte(const OneWireBus * bus, uint8_t data);
250 
258 owb_status owb_write_bytes(const OneWireBus * bus, const uint8_t * buffer, size_t len);
259 
267 
275 uint8_t owb_crc8_byte(uint8_t crc, uint8_t data);
276 
285 uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t * data, size_t len);
286 
295 owb_status owb_search_first(const OneWireBus * bus, OneWireBus_SearchState * state, bool *found_device);
296 
306 owb_status owb_search_next(const OneWireBus * bus, OneWireBus_SearchState * state, bool *found_device);
307 
317 char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char * buffer, size_t len);
318 
326 owb_status owb_set_strong_pullup(const OneWireBus * bus, bool enable);
327 
328 
329 #include "owb_gpio.h"
330 #include "owb_rmt.h"
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 
336 #endif // ONE_WIRE_BUS_H
owb_status owb_set_strong_pullup(const OneWireBus *bus, bool enable)
Enable or disable the strong-pullup GPIO, if configured.
Definition: owb.c:719
owb_status owb_read_byte(const OneWireBus *bus, uint8_t *out)
Read a single byte from the 1-Wire bus.
Definition: owb.c:502
uint8_t owb_crc8_byte(uint8_t crc, uint8_t data)
1-Wire 8-bit CRC lookup.
Definition: owb.c:647
owb_status owb_read_rom(const OneWireBus *bus, OneWireBus_ROMCode *rom_code)
Read ROM code from device - only works when there is a single device on the bus.
Definition: owb.c:361
owb_status owb_use_parasitic_power(OneWireBus *bus, bool use_parasitic_power)
Enable or disable use of parasitic power on the One Wire Bus. This affects how devices signal on the ...
Definition: owb.c:299
uint8_t owb_crc8_bytes(uint8_t crc, const uint8_t *data, size_t len)
1-Wire 8-bit CRC lookup with accumulation over a block of bytes.
Definition: owb.c:652
owb_status owb_write_rom_code(const OneWireBus *bus, OneWireBus_ROMCode rom_code)
Write a ROM code to the 1-Wire bus ensuring LSB is sent first.
Definition: owb.c:626
owb_status owb_write_bytes(const OneWireBus *bus, const uint8_t *buffer, size_t len)
Write a number of bytes to the 1-Wire bus.
Definition: owb.c:598
char * owb_string_from_rom_code(OneWireBus_ROMCode rom_code, char *buffer, size_t len)
Create a string representation of a ROM code, most significant byte (CRC8) first.
Definition: owb.c:709
owb_status owb_read_bytes(const OneWireBus *bus, uint8_t *buffer, unsigned int len)
Read a number of bytes from the 1-Wire bus.
Definition: owb.c:524
owb_status
Represents the result of OWB API functions.
Definition: owb.h:114
@ OWB_STATUS_PARAMETER_NULL
Function was passed a null pointer.
Definition: owb.h:118
@ OWB_STATUS_CRC_FAILED
CRC failed on data received from a device or devices.
Definition: owb.h:120
@ OWB_STATUS_OK
Operation succeeded.
Definition: owb.h:116
@ OWB_STATUS_TOO_MANY_BITS
Attempt to write an incorrect number of bits to the One Wire Bus.
Definition: owb.h:121
@ OWB_STATUS_HW_ERROR
A hardware error occurred.
Definition: owb.h:122
@ OWB_STATUS_NOT_SET
A status value has not been set.
Definition: owb.h:115
@ OWB_STATUS_NOT_INITIALIZED
Function was passed an uninitialised variable.
Definition: owb.h:117
@ OWB_STATUS_DEVICE_NOT_RESPONDING
No response received from the addressed device or devices.
Definition: owb.h:119
owb_status owb_use_crc(OneWireBus *bus, bool use_crc)
Enable or disable use of CRC checks on device communications.
Definition: owb.c:276
owb_status owb_read_bit(const OneWireBus *bus, uint8_t *out)
Read a single bit from the 1-Wire bus.
Definition: owb.c:480
owb_status owb_uninitialize(OneWireBus *bus)
call to release resources after completing use of the OneWireBus
Definition: owb.c:259
owb_status owb_write_byte(const OneWireBus *bus, uint8_t data)
Write a single byte to the 1-Wire bus.
Definition: owb.c:576
owb_status owb_search_next(const OneWireBus *bus, OneWireBus_SearchState *state, bool *found_device)
Locates the next device on the 1-Wire bus, if present, starting from the provided state....
Definition: owb.c:685
owb_status owb_write_bit(const OneWireBus *bus, uint8_t bit)
Write a bit to the 1-Wire bus.
Definition: owb.c:554
owb_status owb_use_strong_pullup_gpio(OneWireBus *bus, gpio_num_t gpio)
Enable or disable use of extra GPIO to activate strong pull-up circuit. This only has effect if paras...
Definition: owb.c:322
owb_status owb_search_first(const OneWireBus *bus, OneWireBus_SearchState *state, bool *found_device)
Locates the first device on the 1-Wire bus, if present.
Definition: owb.c:657
owb_status owb_reset(const OneWireBus *bus, bool *is_present)
Reset the 1-Wire bus.
Definition: owb.c:460
owb_status owb_verify_rom(const OneWireBus *bus, OneWireBus_ROMCode rom_code, bool *is_present)
Verify the device specified by ROM code is present.
Definition: owb.c:416
Interface definitions for the ESP32 GPIO driver used to communicate with devices on the One Wire Bus.
Interface definitions for ESP32 RMT driver used to communicate with devices on the One Wire Bus.
Provides access via field names.
Definition: owb.h:86
Represents the state of a device search on the 1-Wire bus.
Definition: owb.h:103
int last_family_discrepancy
Bit index that identifies the last discrepancy within the first 8-bit family code of the ROM code.
Definition: owb.h:106
int last_device_flag
Flag to indicate previous search was the last device detected.
Definition: owb.h:107
OneWireBus_ROMCode rom_code
Device ROM code.
Definition: owb.h:104
int last_discrepancy
Bit index that identifies from which bit the next search discrepancy check should start.
Definition: owb.h:105
Structure containing 1-Wire bus information relevant to a single instance.
Definition: owb.h:69
const struct _OneWireBus_Timing * timing
Pointer to timing information.
Definition: owb.h:70
gpio_num_t strong_pullup_gpio
Set if an external strong pull-up circuit is required.
Definition: owb.h:73
bool use_parasitic_power
True if parasitic-powered devices are expected on the bus.
Definition: owb.h:72
const struct owb_driver * driver
Pointer to hardware driver instance.
Definition: owb.h:74
bool use_crc
True if CRC checks are to be used when retrieving information from a device on the bus.
Definition: owb.h:71
owb_status(* read_bits)(const OneWireBus *bus, uint8_t *in, int number_of_bits_to_read)
Definition: owb.h:141
owb_status(* uninitialize)(const OneWireBus *bus)
Definition: owb.h:132
owb_status(* write_bits)(const OneWireBus *bus, uint8_t out, int number_of_bits_to_write)
Definition: owb.h:138
const char * name
Definition: owb.h:129
owb_status(* reset)(const OneWireBus *bus, bool *is_present)
Definition: owb.h:135
Represents a 1-Wire ROM Code. This is a sequence of eight bytes, where the first byte is the family n...
Definition: owb.h:83