1 /*
2  * Copyright (c) 2018 Roman Tataurov <diytronic@yandex.ru>
3  * Copyright (c) 2022 Thomas Stranger
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 /**
9  * @file
10  * @brief Public 1-Wire Driver APIs
11  */
12 
13 #ifndef ZEPHYR_INCLUDE_DRIVERS_W1_H_
14 #define ZEPHYR_INCLUDE_DRIVERS_W1_H_
15 
16 #include <zephyr/types.h>
17 #include <zephyr/device.h>
18 #include <zephyr/kernel.h>
19 #include <zephyr/sys/crc.h>
20 #include <zephyr/sys/byteorder.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /**
27  * @brief 1-Wire Interface
28  * @defgroup w1_interface 1-Wire Interface
29  * @ingroup io_interfaces
30  * @{
31  */
32 
33 /**  @cond INTERNAL_HIDDEN */
34 
35 /*
36  * Count the number of slaves expected on the bus.
37  * This can be used to decide if the bus has a multidrop topology or
38  * only a single slave is present.
39  * There is a comma after each ordinal (including the last)
40  * Hence FOR_EACH adds "+1" once too often which has to be subtracted in the end.
41  */
42 #define F1(x) 1
43 #define W1_SLAVE_COUNT(node_id) \
44 		(FOR_EACH(F1, (+), DT_SUPPORTS_DEP_ORDS(node_id)) - 1)
45 #define W1_INST_SLAVE_COUNT(inst)  \
46 		(W1_SLAVE_COUNT(DT_DRV_INST(inst)))
47 
48 /** @endcond */
49 
50 /**
51  * @brief Defines the 1-Wire master settings types, which are runtime configurable.
52  */
53 enum w1_settings_type {
54 	/** Overdrive speed is enabled in case a value of 1 is passed and
55 	 * disabled passing 0.
56 	 */
57 	W1_SETTING_SPEED,
58 	/**
59 	 * The strong pullup resistor is activated immediately after the next
60 	 * written data block by passing a value of 1, and deactivated passing 0.
61 	 */
62 	W1_SETTING_STRONG_PULLUP,
63 
64 	/**
65 	 * Number of different settings types.
66 	 */
67 	W1_SETINGS_TYPE_COUNT,
68 };
69 
70 /**  @cond INTERNAL_HIDDEN */
71 
72 /** Configuration common to all 1-Wire master implementations. */
73 struct w1_master_config {
74 	/* Number of connected slaves */
75 	uint16_t slave_count;
76 };
77 
78 /** Data common to all 1-Wire master implementations. */
79 struct w1_master_data {
80 	/* The mutex used by w1_lock_bus and w1_unlock_bus methods */
81 	struct k_mutex bus_lock;
82 };
83 
84 typedef int (*w1_reset_bus_t)(const struct device *dev);
85 typedef int (*w1_read_bit_t)(const struct device *dev);
86 typedef int (*w1_write_bit_t)(const struct device *dev, bool bit);
87 typedef int (*w1_read_byte_t)(const struct device *dev);
88 typedef int (*w1_write_byte_t)(const struct device *dev, const uint8_t byte);
89 typedef int (*w1_read_block_t)(const struct device *dev, uint8_t *buffer,
90 			       size_t len);
91 typedef int (*w1_write_block_t)(const struct device *dev, const uint8_t *buffer,
92 				size_t len);
93 typedef size_t (*w1_get_slave_count_t)(const struct device *dev);
94 typedef int (*w1_configure_t)(const struct device *dev,
95 			      enum w1_settings_type type, uint32_t value);
96 typedef int (*w1_change_bus_lock_t)(const struct device *dev, bool lock);
97 
98 __subsystem struct w1_driver_api {
99 	w1_reset_bus_t reset_bus;
100 	w1_read_bit_t read_bit;
101 	w1_write_bit_t write_bit;
102 	w1_read_byte_t read_byte;
103 	w1_write_byte_t write_byte;
104 	w1_read_block_t read_block;
105 	w1_write_block_t write_block;
106 	w1_configure_t configure;
107 	w1_change_bus_lock_t change_bus_lock;
108 };
109 /** @endcond */
110 
111 /** @cond INTERNAL_HIDDEN */
112 __syscall int w1_change_bus_lock(const struct device *dev, bool lock);
113 
z_impl_w1_change_bus_lock(const struct device * dev,bool lock)114 static inline int z_impl_w1_change_bus_lock(const struct device *dev, bool lock)
115 {
116 	struct w1_master_data *ctrl_data = (struct w1_master_data *)dev->data;
117 	const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
118 
119 	if (api->change_bus_lock) {
120 		return api->change_bus_lock(dev, lock);
121 	}
122 
123 	if (lock) {
124 		return k_mutex_lock(&ctrl_data->bus_lock, K_FOREVER);
125 	} else {
126 		return k_mutex_unlock(&ctrl_data->bus_lock);
127 	}
128 }
129 /** @endcond */
130 
131 /**
132  * @brief Lock the 1-wire bus to prevent simultaneous access.
133  *
134  * This routine locks the bus to prevent simultaneous access from different
135  * threads. The calling thread waits until the bus becomes available.
136  * A thread is permitted to lock a mutex it has already locked.
137  *
138  * @param[in] dev Pointer to the device structure for the driver instance.
139  *
140  * @retval        0 If successful.
141  * @retval -errno Negative error code on error.
142  */
w1_lock_bus(const struct device * dev)143 static inline int w1_lock_bus(const struct device *dev)
144 {
145 	return w1_change_bus_lock(dev, true);
146 }
147 
148 /**
149  * @brief Unlock the 1-wire bus.
150  *
151  * This routine unlocks the bus to permit access to bus line.
152  *
153  * @param[in] dev Pointer to the device structure for the driver instance.
154  *
155  * @retval 0      If successful.
156  * @retval -errno Negative error code on error.
157  */
w1_unlock_bus(const struct device * dev)158 static inline int w1_unlock_bus(const struct device *dev)
159 {
160 	return w1_change_bus_lock(dev, false);
161 }
162 
163 /**
164  * @brief 1-Wire data link layer
165  * @defgroup w1_data_link 1-Wire data link layer
166  * @ingroup w1_interface
167  * @{
168  */
169 
170 /**
171  * @brief Reset the 1-Wire bus to prepare slaves for communication.
172  *
173  * This routine resets all 1-Wire bus slaves such that they are
174  * ready to receive a command.
175  * Connected slaves answer with a presence pulse once they are ready
176  * to receive data.
177  *
178  * In case the driver supports both standard speed and overdrive speed,
179  * the reset routine takes care of sendig either a short or a long reset pulse
180  * depending on the current state. The speed can be changed using
181  * @a w1_configure().
182  *
183  * @param[in] dev Pointer to the device structure for the driver instance.
184  *
185  * @retval 0      If no slaves answer with a present pulse.
186  * @retval 1      If at least one slave answers with a present pulse.
187  * @retval -errno Negative error code on error.
188  */
189 __syscall int w1_reset_bus(const struct device *dev);
190 
z_impl_w1_reset_bus(const struct device * dev)191 static inline int z_impl_w1_reset_bus(const struct device *dev)
192 {
193 	const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
194 
195 	return api->reset_bus(dev);
196 }
197 
198 /**
199  * @brief Read a single bit from the 1-Wire bus.
200  *
201  * @param[in] dev Pointer to the device structure for the driver instance.
202  *
203  * @retval rx_bit The read bit value on success.
204  * @retval -errno Negative error code on error.
205  */
206 __syscall int w1_read_bit(const struct device *dev);
207 
z_impl_w1_read_bit(const struct device * dev)208 static inline int z_impl_w1_read_bit(const struct device *dev)
209 {
210 	const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
211 
212 	return api->read_bit(dev);
213 }
214 
215 /**
216  * @brief Write a single bit to the 1-Wire bus.
217  *
218  * @param[in] dev Pointer to the device structure for the driver instance.
219  * @param bit     Transmitting bit value 1 or 0.
220  *
221  * @retval 0      If successful.
222  * @retval -errno Negative error code on error.
223  */
224 __syscall int w1_write_bit(const struct device *dev, const bool bit);
225 
z_impl_w1_write_bit(const struct device * dev,bool bit)226 static inline int z_impl_w1_write_bit(const struct device *dev, bool bit)
227 {
228 	const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
229 
230 	return api->write_bit(dev, bit);
231 }
232 
233 /**
234  * @brief Read a single byte from the 1-Wire bus.
235  *
236  * @param[in] dev Pointer to the device structure for the driver instance.
237  *
238  * @retval rx_byte The read byte value on success.
239  * @retval -errno  Negative error code on error.
240  */
241 __syscall int w1_read_byte(const struct device *dev);
242 
z_impl_w1_read_byte(const struct device * dev)243 static inline int z_impl_w1_read_byte(const struct device *dev)
244 {
245 	const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
246 
247 	return api->read_byte(dev);
248 }
249 
250 /**
251  * @brief Write a single byte to the 1-Wire bus.
252  *
253  * @param[in] dev Pointer to the device structure for the driver instance.
254  * @param byte    Transmitting byte.
255  *
256  * @retval 0      If successful.
257  * @retval -errno Negative error code on error.
258  */
259 __syscall int w1_write_byte(const struct device *dev, uint8_t byte);
260 
z_impl_w1_write_byte(const struct device * dev,uint8_t byte)261 static inline int z_impl_w1_write_byte(const struct device *dev, uint8_t byte)
262 {
263 	const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
264 
265 	return api->write_byte(dev, byte);
266 }
267 
268 /**
269  * @brief Read a block of data from the 1-Wire bus.
270  *
271  * @param[in] dev     Pointer to the device structure for the driver instance.
272  * @param[out] buffer Pointer to receive buffer.
273  * @param len         Length of receiving buffer (in bytes).
274  *
275  * @retval 0      If successful.
276  * @retval -errno Negative error code on error.
277  */
278 __syscall int w1_read_block(const struct device *dev, uint8_t *buffer, size_t len);
279 
280 /**
281  * @brief Write a block of data from the 1-Wire bus.
282  *
283  * @param[in] dev    Pointer to the device structure for the driver instance.
284  * @param[in] buffer Pointer to transmitting buffer.
285  * @param len        Length of transmitting buffer (in bytes).
286  *
287  * @retval 0      If successful.
288  * @retval -errno Negative error code on error.
289  */
290 __syscall int w1_write_block(const struct device *dev,
291 			     const uint8_t *buffer, size_t len);
292 
293 /**
294  * @brief Get the number of slaves on the bus.
295  *
296  * @param[in] dev  Pointer to the device structure for the driver instance.
297  *
298  * @retval slave_count  Positive Number of connected 1-Wire slaves on success.
299  * @retval -errno       Negative error code on error.
300  */
301 __syscall size_t w1_get_slave_count(const struct device *dev);
302 
z_impl_w1_get_slave_count(const struct device * dev)303 static inline size_t z_impl_w1_get_slave_count(const struct device *dev)
304 {
305 	const struct w1_master_config *ctrl_cfg =
306 		(const struct w1_master_config *)dev->config;
307 
308 	return ctrl_cfg->slave_count;
309 }
310 
311 /**
312  * @brief Configure parameters of the 1-Wire master.
313  *
314  * Allowed configuration parameters are defined in enum w1_settings_type,
315  * but master devices may not support all types.
316  *
317  * @param[in] dev  Pointer to the device structure for the driver instance.
318  * @param type     Enum specifying the setting type.
319  * @param value    The new value for the passed settings type.
320  *
321  * @retval 0        If successful.
322  * @retval -ENOTSUP The master doesn't support the configuration of the supplied type.
323  * @retval -EIO     General input / output error, failed to configure master devices.
324  */
325 __syscall int w1_configure(const struct device *dev,
326 			   enum w1_settings_type type, uint32_t value);
327 
z_impl_w1_configure(const struct device * dev,enum w1_settings_type type,uint32_t value)328 static inline int z_impl_w1_configure(const struct device *dev,
329 				      enum w1_settings_type type, uint32_t value)
330 {
331 	const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
332 
333 	return api->configure(dev, type, value);
334 }
335 
336 /**
337  * @}
338  */
339 
340 /**
341  * @brief 1-Wire network layer
342  * @defgroup w1_network 1-Wire network layer
343  * @ingroup w1_interface
344  * @{
345  */
346 
347 /**
348  * @name 1-Wire ROM Commands
349  * @{
350  */
351 
352 /**
353  * This command allows the bus master to read the slave devices without
354  * providing their ROM code.
355  */
356 #define W1_CMD_SKIP_ROM			0xCC
357 
358 /**
359  * This command allows the bus master to address a specific slave device by
360  * providing its ROM code.
361  */
362 #define W1_CMD_MATCH_ROM		0x55
363 
364 /**
365  * This command allows the bus master to resume a previous read out from where
366  * it left off.
367  */
368 #define W1_CMD_RESUME			0xA5
369 
370 /**
371  * This command allows the bus master to read the ROM code from a single slave
372  * device.
373  * This command should be used when there is only a single slave device on the
374  * bus.
375  */
376 #define W1_CMD_READ_ROM			0x33
377 
378 /**
379  * This command allows the bus master to discover the addresses (i.e., ROM
380  * codes) of all slave devices on the bus.
381  */
382 #define W1_CMD_SEARCH_ROM		0xF0
383 
384 /**
385  * This command allows the bus master to identify which devices have experienced
386  * an alarm condition.
387  */
388 #define W1_CMD_SEARCH_ALARM		0xEC
389 
390 /**
391  * This command allows the bus master to address all devices on the bus and then
392  * switch them to overdrive speed.
393  */
394 #define W1_CMD_OVERDRIVE_SKIP_ROM	0x3C
395 
396 /**
397  * This command allows the bus master to address a specific device and switch it
398  * to overdrive speed.
399  */
400 #define W1_CMD_OVERDRIVE_MATCH_ROM	0x69
401 
402 /** @} */
403 
404 /**
405  * @name CRC Defines
406  * @{
407  */
408 
409 /** Seed value used to calculate the 1-Wire 8-bit crc. */
410 #define W1_CRC8_SEED		0x00
411 /** Polynomial used to calculate the 1-Wire 8-bit crc. */
412 #define W1_CRC8_POLYNOMIAL	0x8C
413 /** Seed value used to calculate the 1-Wire 16-bit crc. */
414 #define W1_CRC16_SEED		0x0000
415 /** Polynomial used to calculate the 1-Wire 16-bit crc. */
416 #define W1_CRC16_POLYNOMIAL	0xa001
417 
418 /** @} */
419 
420 /** This flag can be passed to searches in order to not filter on family ID. */
421 #define W1_SEARCH_ALL_FAMILIES		0x00
422 
423 /** Intitialize all w1_rom struct members to zero. */
424 #define W1_ROM_INIT_ZERO					\
425 	{							\
426 		.family = 0, .serial = { 0 }, .crc = 0,		\
427 	}
428 
429 /**
430  * @brief w1_rom struct.
431  */
432 struct w1_rom {
433 	/** @brief The 1-Wire family code identifying the slave device type.
434 	 *
435 	 * An incomplete list of family codes is available at:
436 	 * https://www.maximintegrated.com/en/app-notes/index.mvp/id/155
437 	 * others are documented in the respective device data sheet.
438 	 */
439 	uint8_t family;
440 	/** The serial together with the family code composes the unique 56-bit id */
441 	uint8_t serial[6];
442 	/** 8-bit checksum of the 56-bit unique id. */
443 	uint8_t crc;
444 };
445 
446 /**
447  * @brief Node specific 1-wire configuration struct.
448  *
449  * This struct is passed to network functions, such that they can configure
450  * the bus to address the specific slave using the selected speed.
451  */
452 struct w1_slave_config {
453 	/** Unique 1-Wire ROM. */
454 	struct w1_rom rom;
455 	/** overdrive speed is used if set to 1. */
456 	uint32_t overdrive : 1;
457 	/** @cond INTERNAL_HIDDEN */
458 	uint32_t res : 31;
459 	/** @endcond */
460 };
461 
462 /**
463  * @brief Define the application callback handler function signature
464  *        for searches.
465  *
466  * @param rom found The ROM of the found slave.
467  * @param user_data User data provided to the w1_search_bus() call.
468  */
469 typedef void (*w1_search_callback_t)(struct w1_rom rom, void *user_data);
470 
471 /**
472  * @brief Read Peripheral 64-bit ROM.
473  *
474  * This procedure allows the 1-Wire bus master to read the peripherals’
475  * 64-bit ROM without using the Search ROM procedure.
476  * This command can be used as long as not more than a sigle peripheral is
477  * connected to the bus.
478  * Otherwise data collisons occur and a faulty ROM is read.
479  *
480  * @param[in] dev  Pointer to the device structure for the driver instance.
481  * @param[out] rom Pointer to the ROM structure.
482  *
483  * @retval 0       If successful.
484  * @retval -ENODEV In case no slave responds to reset.
485  * @retval -errno  Other negative error code in case of invalid crc and
486  *         communication errors.
487  */
488 int w1_read_rom(const struct device *dev, struct w1_rom *rom);
489 
490 /**
491  * @brief Select a specific slave by broadcasting a selected ROM.
492  *
493  * This routine allows the 1-Wire bus master to select a slave
494  * identified by its unique ROM, such that the next command will target only
495  * this single selected slave.
496  *
497  * This command is only necessary in multidrop environments, otherwise the
498  * Skip ROM command can be issued.
499  * Once a slave has been selected, to reduce the communication overhead, the
500  * resume command can be used instead of this command to communicate with the
501  * selected slave.
502  *
503  * @param[in] dev    Pointer to the device structure for the driver instance.
504  * @param[in] config Pointer to the slave specific 1-Wire config.
505  *
506  * @retval 0       If successful.
507  * @retval -ENODEV In case no slave responds to reset.
508  * @retval -errno  Other negative error code on error.
509  */
510 int w1_match_rom(const struct device *dev, const struct w1_slave_config *config);
511 
512 /**
513  * @brief Select the slave last addressed with a Match ROM or Search ROM commnad.
514  *
515  * This routine allows the 1-Wire bus master to re-select a slave
516  * device that was already addressed using a Match ROM or Search ROM command.
517  *
518  * @param dev     Pointer to the device structure for the driver instance.
519  *
520  * @retval 0       If successful.
521  * @retval -ENODEV In case no slave responds to reset.
522  * @retval -errno  Other negative error code on error.
523  */
524 int w1_resume_command(const struct device *dev);
525 
526 /**
527  * @brief Select all slaves regardless of ROM.
528  *
529  * This routine sets up the bus slaves to receive a command.
530  * It is usually used when there is only one peripheral on the bus
531  * to avoid the overhead of the Match ROM command.
532  * But it can also be used to concurrently write to all slave devices.
533  *
534  * @param[in] dev    Pointer to the device structure for the driver instance.
535  * @param[in] config Pointer to the slave specific 1-Wire config.
536  *
537  * @retval 0       If successful.
538  * @retval -ENODEV In case no slave responds to reset.
539  * @retval -errno  Other negative error code on error.
540  */
541 int w1_skip_rom(const struct device *dev, const struct w1_slave_config *config);
542 
543 /**
544  * @brief In single drop configurations use Skip Select command, otherweise use
545  *        Match ROM command.
546  *
547  * @param[in] dev    Pointer to the device structure for the driver instance.
548  * @param[in] config Pointer to the slave specific 1-Wire config.
549  *
550  * @retval 0       If successful.
551  * @retval -ENODEV In case no slave responds to reset.
552  * @retval -errno  Other negative error code on error.
553  */
554 int w1_reset_select(const struct device *dev, const struct w1_slave_config *config);
555 
556 /**
557  * @brief Write then read data from the 1-Wire slave with matching ROM.
558  *
559  * This routine uses w1_reset_select to select the given ROM.
560  * Then writes given data and reads the response back from the slave.
561  *
562  * @param[in] dev       Pointer to the device structure for the driver instance.
563  * @param[in] config    Pointer to the slave specific 1-Wire config.
564  * @param[in] write_buf Pointer to the data to be written.
565  * @param write_len     Number of bytes to write.
566  * @param[out] read_buf Pointer to storage for read data.
567  * @param read_len      Number of bytes to read.
568  *
569  * @retval 0       If successful.
570  * @retval -ENODEV In case no slave responds to reset.
571  * @retval -errno  Other negative error code on error.
572  */
573 int w1_write_read(const struct device *dev, const struct w1_slave_config *config,
574 		  const uint8_t *write_buf, size_t write_len,
575 		  uint8_t *read_buf, size_t read_len);
576 
577 /**
578  * @brief Search 1-wire slaves on the bus.
579  *
580  * This function searches slaves on the 1-wire bus, with the possibility
581  * to search either all slaves or only slaves that have an active alarm state.
582  * If a callback is passed, the callback is called for each found slave.
583  *
584  * The algorithm mostly follows the suggestions of
585  * https://pdfserv.maximintegrated.com/en/an/AN187.pdf
586  *
587  * Note: Filtering on families is not supported.
588  *
589  * @param[in] dev       Pointer to the device structure for the driver instance.
590  * @param command       Can either be W1_SEARCH_ALARM or W1_SEARCH_ROM.
591  * @param family        W1_SEARCH_ALL_FAMILIES searcheas all families,
592  *                      filtering on a specific family is not yet supported.
593  * @param callback      Application callback handler function to be called
594  *                      for each found slave.
595  * @param[in] user_data User data to pass to the application callback handler
596  *                      function.
597  *
598  * @retval slave_count  Number of slaves found.
599  * @retval -errno       Negative error code on error.
600  */
601 __syscall int w1_search_bus(const struct device *dev, uint8_t command,
602 			    uint8_t family, w1_search_callback_t callback,
603 			    void *user_data);
604 
605 /**
606  * @brief Search for 1-Wire slave on bus.
607  *
608  * This routine can discover unknown slaves on the bus by scanning for the
609  * unique 64-bit registration number.
610  *
611  * @param[in] dev       Pointer to the device structure for the driver instance.
612  * @param callback      Application callback handler function to be called
613  *                      for each found slave.
614  * @param[in] user_data User data to pass to the application callback handler
615  *                      function.
616  *
617  * @retval slave_count  Number of slaves found.
618  * @retval -errno       Negative error code on error.
619  */
w1_search_rom(const struct device * dev,w1_search_callback_t callback,void * user_data)620 static inline int w1_search_rom(const struct device *dev,
621 				w1_search_callback_t callback, void *user_data)
622 {
623 	return w1_search_bus(dev, W1_CMD_SEARCH_ROM, W1_SEARCH_ALL_FAMILIES,
624 			     callback, user_data);
625 }
626 
627 /**
628  * @brief Search for 1-Wire slaves with an active alarm.
629  *
630  * This routine searches 1-Wire slaves on the bus, which currently have
631  * an active alarm.
632  *
633  * @param[in] dev       Pointer to the device structure for the driver instance.
634  * @param callback      Application callback handler function to be called
635  *                      for each found slave.
636  * @param[in] user_data User data to pass to the application callback handler
637  *                      function.
638  *
639  * @retval slave_count  Number of slaves found.
640  * @retval -errno       Negative error code on error.
641  */
w1_search_alarm(const struct device * dev,w1_search_callback_t callback,void * user_data)642 static inline int w1_search_alarm(const struct device *dev,
643 				  w1_search_callback_t callback, void *user_data)
644 {
645 	return w1_search_bus(dev, W1_CMD_SEARCH_ALARM, W1_SEARCH_ALL_FAMILIES,
646 			     callback, user_data);
647 }
648 
649 /**
650  * @brief Function to convert a w1_rom struct to an uint64_t.
651  *
652  * @param[in] rom Pointer to the ROM struct.
653  *
654  * @retval rom64 The ROM converted to an unsigned integer in  endianness.
655  */
w1_rom_to_uint64(const struct w1_rom * rom)656 static inline uint64_t w1_rom_to_uint64(const struct w1_rom *rom)
657 {
658 	return sys_get_be64((uint8_t *)rom);
659 }
660 
661 /**
662  * @brief Function to write an uint64_t to struct w1_rom pointer.
663  *
664  * @param rom64    Unsigned 64 bit integer representing the ROM in host endianness.
665  * @param[out] rom The ROM struct pointer.
666  */
w1_uint64_to_rom(const uint64_t rom64,struct w1_rom * rom)667 static inline void w1_uint64_to_rom(const uint64_t rom64, struct w1_rom *rom)
668 {
669 	sys_put_be64(rom64, (uint8_t *)rom);
670 }
671 
672 /**
673  * @brief Compute CRC-8 chacksum as defined in the 1-Wire specification.
674  *
675  * The 1-Wire of CRC 8 variant is using 0x31 as its polynomial with the initial
676  * value set to 0x00.
677  * This CRC is used to check the correctness of the unique 56-bit ROM.
678  *
679  * @param[in] src Input bytes for the computation.
680  * @param len     Length of the input in bytes.
681  *
682  * @retval crc The computed CRC8 value.
683  */
w1_crc8(const uint8_t * src,size_t len)684 static inline uint8_t w1_crc8(const uint8_t *src, size_t len)
685 {
686 	return crc8(src, len, W1_CRC8_POLYNOMIAL, W1_CRC8_SEED, true);
687 }
688 
689 /**
690  * @brief Compute 1-Wire variant of CRC 16
691  *
692  * The 16-bit 1-Wire crc variant is using the reflected polynomial function
693  * X^16 + X^15 * + X^2 + 1 with the initial value set to 0x0000.
694  * See also APPLICATION NOTE 27:
695  * "UNDERSTANDING AND USING CYCLIC REDUNDANCY CHECKS WITH MAXIM 1-WIRE AND IBUTTON PRODUCTS"
696  * https://www.maximintegrated.com/en/design/technical-documents/app-notes/2/27.html
697  *
698  * @param seed    Init value for the CRC, it is usually set to 0x0000.
699  * @param[in] src Input bytes for the computation.
700  * @param len     Length of the input in bytes.
701  *
702  * @retval crc The computed CRC16 value.
703  */
w1_crc16(const uint16_t seed,const uint8_t * src,const size_t len)704 static inline uint16_t w1_crc16(const uint16_t seed, const uint8_t *src,
705 				const size_t len)
706 {
707 	return crc16_reflect(W1_CRC16_POLYNOMIAL, seed, src, len);
708 }
709 
710 /**
711  * @}
712  */
713 
714 #ifdef __cplusplus
715 }
716 #endif
717 
718 /**
719  * @}
720  */
721 #include <syscalls/w1.h>
722 
723 #endif /* ZEPHYR_INCLUDE_DRIVERS_W1_H_ */
724