Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
w1.h
Go to the documentation of this file.
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
13
14#ifndef ZEPHYR_INCLUDE_DRIVERS_W1_H_
15#define ZEPHYR_INCLUDE_DRIVERS_W1_H_
16
17#include <zephyr/types.h>
18#include <zephyr/device.h>
19#include <zephyr/kernel.h>
20#include <zephyr/sys/crc.h>
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
35
37
38/*
39 * Count the number of peripherals expected on the bus.
40 * This can be used to decide if the bus has a multidrop topology or
41 * only a single peripheral is present.
42 * There is a comma after each ordinal (including the last)
43 * Hence FOR_EACH adds "+1" once too often which has to be subtracted in the end.
44 */
45#define F1(x) 1
46#define W1_PERIPHERAL_COUNT(node_id) \
47 (FOR_EACH(F1, (+), DT_SUPPORTS_DEP_ORDS(node_id)) - 1)
48#define W1_INST_PERIPHERAL_COUNT(inst) \
49 (W1_PERIPHERAL_COUNT(DT_DRV_INST(inst)))
50
51#define W1_SLAVE_COUNT(node_id) W1_PERIPHERAL_COUNT(node_id) __DEPRECATED_MACRO
52#define W1_INST_SLAVE_COUNT(inst) W1_INST_PERIPHERAL_COUNT(inst) __DEPRECATED_MACRO
53
55
75
80
90 union {
97 __deprecated uint16_t slave_count;
98 };
99};
100
105#define w1_master_config w1_controller_config __DEPRECATED_MACRO
106
118
123#define w1_master_data w1_controller_data __DEPRECATED_MACRO
124
129typedef int (*w1_reset_bus_t)(const struct device *dev);
130
135typedef int (*w1_read_bit_t)(const struct device *dev);
136
141typedef int (*w1_write_bit_t)(const struct device *dev, bool bit);
142
147typedef int (*w1_read_byte_t)(const struct device *dev);
148
153typedef int (*w1_write_byte_t)(const struct device *dev, const uint8_t byte);
154
159typedef int (*w1_read_block_t)(const struct device *dev, uint8_t *buffer,
160 size_t len);
161
166typedef int (*w1_write_block_t)(const struct device *dev, const uint8_t *buffer,
167 size_t len);
168
173typedef size_t (*w1_get_peripheral_count_t)(const struct device *dev);
174
180
185typedef int (*w1_configure_t)(const struct device *dev,
186 enum w1_settings_type type, uint32_t value);
187
192typedef int (*w1_change_bus_lock_t)(const struct device *dev, bool lock);
193
231
233
235__syscall int w1_change_bus_lock(const struct device *dev, bool lock);
236
237static inline int z_impl_w1_change_bus_lock(const struct device *dev, bool lock)
238{
239 struct w1_controller_data *ctrl_data = (struct w1_controller_data *)dev->data;
240 const struct w1_driver_api *api = DEVICE_API_GET(w1, dev);
241
242 if (api->change_bus_lock) {
243 return api->change_bus_lock(dev, lock);
244 }
245
246 if (lock) {
247 return k_mutex_lock(&ctrl_data->bus_lock, K_FOREVER);
248 } else {
249 return k_mutex_unlock(&ctrl_data->bus_lock);
250 }
251}
253
265static inline int w1_lock_bus(const struct device *dev)
266{
267 return w1_change_bus_lock(dev, true);
268}
269
279static inline int w1_unlock_bus(const struct device *dev)
280{
281 return w1_change_bus_lock(dev, false);
282}
283
290
310__syscall int w1_reset_bus(const struct device *dev);
311
312static inline int z_impl_w1_reset_bus(const struct device *dev)
313{
314 return DEVICE_API_GET(w1, dev)->reset_bus(dev);
315}
316
324__syscall int w1_read_bit(const struct device *dev);
325
326static inline int z_impl_w1_read_bit(const struct device *dev)
327{
328 return DEVICE_API_GET(w1, dev)->read_bit(dev);
329}
330
339__syscall int w1_write_bit(const struct device *dev, const bool bit);
340
341static inline int z_impl_w1_write_bit(const struct device *dev, bool bit)
342{
343 return DEVICE_API_GET(w1, dev)->write_bit(dev, bit);
344}
345
353__syscall int w1_read_byte(const struct device *dev);
354
355static inline int z_impl_w1_read_byte(const struct device *dev)
356{
357 return DEVICE_API_GET(w1, dev)->read_byte(dev);
358}
359
368__syscall int w1_write_byte(const struct device *dev, uint8_t byte);
369
370static inline int z_impl_w1_write_byte(const struct device *dev, uint8_t byte)
371{
372 return DEVICE_API_GET(w1, dev)->write_byte(dev, byte);
373}
374
384__syscall int w1_read_block(const struct device *dev, uint8_t *buffer, size_t len);
385
395__syscall int w1_write_block(const struct device *dev,
396 const uint8_t *buffer, size_t len);
397
406__syscall size_t w1_get_peripheral_count(const struct device *dev);
407
408static inline size_t z_impl_w1_get_peripheral_count(const struct device *dev)
409{
410 const struct w1_controller_config *ctrl_cfg =
411 (const struct w1_controller_config *)dev->config;
412
413 return ctrl_cfg->peripheral_count;
414}
415
426__deprecated __syscall size_t w1_get_slave_count(const struct device *dev);
427
428static inline size_t z_impl_w1_get_slave_count(const struct device *dev)
429{
430 return z_impl_w1_get_peripheral_count(dev);
431}
432
447__syscall int w1_configure(const struct device *dev,
448 enum w1_settings_type type, uint32_t value);
449
450static inline int z_impl_w1_configure(const struct device *dev,
451 enum w1_settings_type type, uint32_t value)
452{
453 return DEVICE_API_GET(w1, dev)->configure(dev, type, value);
454}
455
459
466
471
476#define W1_CMD_SKIP_ROM 0xCC
477
482#define W1_CMD_MATCH_ROM 0x55
483
488#define W1_CMD_RESUME 0xA5
489
496#define W1_CMD_READ_ROM 0x33
497
502#define W1_CMD_SEARCH_ROM 0xF0
503
508#define W1_CMD_SEARCH_ALARM 0xEC
509
514#define W1_CMD_OVERDRIVE_SKIP_ROM 0x3C
515
520#define W1_CMD_OVERDRIVE_MATCH_ROM 0x69
521
523
528
530#define W1_CRC8_SEED 0x00
532#define W1_CRC8_POLYNOMIAL 0x8C
534#define W1_CRC16_SEED 0x0000
536#define W1_CRC16_POLYNOMIAL 0xa001
537
539
541#define W1_SEARCH_ALL_FAMILIES 0x00
542
544#define W1_ROM_INIT_ZERO \
545 { \
546 .family = 0, .serial = { 0 }, .crc = 0, \
547 }
548
565
574 struct w1_rom rom;
578 uint32_t res : 31;
580};
581
586#define w1_slave_config w1_peripheral_config __DEPRECATED_MACRO
587
595typedef void (*w1_search_callback_t)(struct w1_rom rom, void *user_data);
596
612int w1_read_rom(const struct device *dev, struct w1_rom *rom);
613
633int w1_match_rom(const struct device *dev, const struct w1_peripheral_config *config);
634
646int w1_resume_command(const struct device *dev);
647
662int w1_skip_rom(const struct device *dev, const struct w1_peripheral_config *config);
663
674int w1_reset_select(const struct device *dev, const struct w1_peripheral_config *config);
675
692int w1_write_read(const struct device *dev, const struct w1_peripheral_config *config,
693 const uint8_t *write_buf, size_t write_len,
694 uint8_t *read_buf, size_t read_len);
695
719__syscall int w1_search_bus(const struct device *dev, uint8_t command,
721 void *user_data);
722
737static inline int w1_search_rom(const struct device *dev,
738 w1_search_callback_t callback, void *user_data)
739{
741 callback, user_data);
742}
743
758static inline int w1_search_alarm(const struct device *dev,
759 w1_search_callback_t callback, void *user_data)
760{
762 callback, user_data);
763}
764
772static inline uint64_t w1_rom_to_uint64(const struct w1_rom *rom)
773{
774 return sys_get_be64((uint8_t *)rom);
775}
776
783static inline void w1_uint64_to_rom(const uint64_t rom64, struct w1_rom *rom)
784{
785 sys_put_be64(rom64, (uint8_t *)rom);
786}
787
800static inline uint8_t w1_crc8(const uint8_t *src, size_t len)
801{
802 return crc8(src, len, W1_CRC8_POLYNOMIAL, W1_CRC8_SEED, true);
803}
804
820static inline uint16_t w1_crc16(const uint16_t seed, const uint8_t *src,
821 const size_t len)
822{
823 return crc16_reflect(W1_CRC16_POLYNOMIAL, seed, src, len);
824}
825
829
830#ifdef __cplusplus
831}
832#endif
833
837#include <zephyr/syscalls/w1.h>
838
839#endif /* ZEPHYR_INCLUDE_DRIVERS_W1_H_ */
APIs and macros for the Zephyr device model.
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1505
#define K_FOREVER
Generate infinite timeout delay.
Definition kernel.h:1727
uint8_t crc8(const uint8_t *src, size_t len, uint8_t polynomial, uint8_t initial_value, bool reversed)
Generic function for computing CRC 8.
uint16_t crc16_reflect(uint16_t poly, uint16_t seed, const uint8_t *src, size_t len)
Generic function for computing a CRC-16 with input and output reflection.
int k_mutex_unlock(struct k_mutex *mutex)
Unlock a mutex.
int k_mutex_lock(struct k_mutex *mutex, k_timeout_t timeout)
Lock a mutex.
size_t(* w1_get_peripheral_count_t)(const struct device *dev)
Get the number of peripherals on the bus.
Definition w1.h:173
int(* w1_write_bit_t)(const struct device *dev, bool bit)
Write a single bit to the 1-Wire bus.
Definition w1.h:141
int(* w1_write_byte_t)(const struct device *dev, const uint8_t byte)
Write a single byte to the 1-Wire bus.
Definition w1.h:153
int(* w1_read_byte_t)(const struct device *dev)
Read a single byte from the 1-Wire bus.
Definition w1.h:147
int(* w1_reset_bus_t)(const struct device *dev)
Reset the 1-Wire bus to prepare peripherals for communication.
Definition w1.h:129
int(* w1_change_bus_lock_t)(const struct device *dev, bool lock)
Lock or unlock the 1-Wire bus.
Definition w1.h:192
int(* w1_configure_t)(const struct device *dev, enum w1_settings_type type, uint32_t value)
Configure parameters of the 1-Wire controller.
Definition w1.h:185
int(* w1_read_bit_t)(const struct device *dev)
Read a single bit from the 1-Wire bus.
Definition w1.h:135
w1_get_peripheral_count_t w1_get_slave_count_t
Get the number of peripherals on the bus.
Definition w1.h:179
int(* w1_read_block_t)(const struct device *dev, uint8_t *buffer, size_t len)
Read a block of data from the 1-Wire bus.
Definition w1.h:159
int(* w1_write_block_t)(const struct device *dev, const uint8_t *buffer, size_t len)
Write a block of data to the 1-Wire bus.
Definition w1.h:166
w1_settings_type
Defines the 1-Wire controller settings types, which are runtime configurable.
Definition w1.h:59
static int w1_unlock_bus(const struct device *dev)
Unlock the 1-wire bus.
Definition w1.h:279
static int w1_lock_bus(const struct device *dev)
Lock the 1-wire bus to prevent simultaneous access.
Definition w1.h:265
@ W1_SETTING_SPEED
Overdrive speed is enabled in case a value of 1 is passed and disabled passing 0.
Definition w1.h:63
@ W1_SETINGS_TYPE_COUNT
Number of different settings types.
Definition w1.h:73
@ W1_SETTING_STRONG_PULLUP
The strong pullup resistor is activated immediately after the next written data block by passing a va...
Definition w1.h:68
static int w1_search_rom(const struct device *dev, w1_search_callback_t callback, void *user_data)
Search for 1-Wire peripheral on bus.
Definition w1.h:737
static int w1_search_alarm(const struct device *dev, w1_search_callback_t callback, void *user_data)
Search for 1-Wire peripherals with an active alarm.
Definition w1.h:758
static uint64_t w1_rom_to_uint64(const struct w1_rom *rom)
Function to convert a w1_rom struct to an uint64_t.
Definition w1.h:772
int w1_search_bus(const struct device *dev, uint8_t command, uint8_t family, w1_search_callback_t callback, void *user_data)
Search 1-wire peripherals on the bus.
void(* w1_search_callback_t)(struct w1_rom rom, void *user_data)
Define the application callback handler function signature for searches.
Definition w1.h:595
static uint8_t w1_crc8(const uint8_t *src, size_t len)
Compute CRC-8 chacksum as defined in the 1-Wire specification.
Definition w1.h:800
static uint16_t w1_crc16(const uint16_t seed, const uint8_t *src, const size_t len)
Compute 1-Wire variant of CRC 16.
Definition w1.h:820
#define W1_CMD_SEARCH_ROM
This command allows the bus controller to discover the addresses (i.e., ROM codes) of all peripheral ...
Definition w1.h:502
int w1_resume_command(const struct device *dev)
Select the peripheral last addressed with a Match ROM or Search ROM command.
static void w1_uint64_to_rom(const uint64_t rom64, struct w1_rom *rom)
Function to write an uint64_t to struct w1_rom pointer.
Definition w1.h:783
#define W1_SEARCH_ALL_FAMILIES
This flag can be passed to searches in order to not filter on family ID.
Definition w1.h:541
int w1_read_rom(const struct device *dev, struct w1_rom *rom)
Read Peripheral 64-bit ROM.
#define W1_CRC8_SEED
Seed value used to calculate the 1-Wire 8-bit crc.
Definition w1.h:530
#define W1_CRC16_POLYNOMIAL
Polynomial used to calculate the 1-Wire 16-bit crc.
Definition w1.h:536
#define W1_CRC8_POLYNOMIAL
Polynomial used to calculate the 1-Wire 8-bit crc.
Definition w1.h:532
int w1_write_read(const struct device *dev, const struct w1_peripheral_config *config, const uint8_t *write_buf, size_t write_len, uint8_t *read_buf, size_t read_len)
Write then read data from the 1-Wire peripheral with matching ROM.
int w1_match_rom(const struct device *dev, const struct w1_peripheral_config *config)
Select a specific peripheral by broadcasting a selected ROM.
int w1_reset_select(const struct device *dev, const struct w1_peripheral_config *config)
In single drop configurations use Skip Select command, otherwise use Match ROM command.
#define W1_CMD_SEARCH_ALARM
This command allows the bus controller to identify which devices have experienced an alarm condition.
Definition w1.h:508
int w1_skip_rom(const struct device *dev, const struct w1_peripheral_config *config)
Select all peripherals regardless of ROM.
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Runtime device structure (in ROM) per driver instance.
Definition device.h:543
void * data
Address of the device instance private data.
Definition device.h:553
const void * config
Address of device instance config information.
Definition device.h:547
Kernel mutex structure.
Definition kernel.h:3764
Configuration common to all 1-Wire controller implementations.
Definition w1.h:88
uint16_t slave_count
Number of connected peripherals.
Definition w1.h:97
uint16_t peripheral_count
Number of connected peripherals.
Definition w1.h:92
Data common to all 1-Wire controller implementations.
Definition w1.h:114
struct k_mutex bus_lock
The mutex used by w1_lock_bus() and w1_unlock_bus() methods.
Definition w1.h:116
<span class="mlabel">Driver Operations</span> 1-Wire driver operations
Definition w1.h:197
w1_write_block_t write_block
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition w1.h:221
w1_configure_t configure
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition w1.h:223
w1_reset_bus_t reset_bus
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition w1.h:199
w1_change_bus_lock_t change_bus_lock
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition w1.h:229
w1_read_block_t read_block
<span class="op-badge op-opt" title="This operation MAY optionally be implemented by the driver....
Definition w1.h:214
w1_write_bit_t write_bit
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition w1.h:203
w1_read_bit_t read_bit
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition w1.h:201
w1_write_byte_t write_byte
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition w1.h:207
w1_read_byte_t read_byte
<span class="op-badge op-req" title="This operation MUST be implemented by the driver....
Definition w1.h:205
Node specific 1-wire configuration struct.
Definition w1.h:572
uint32_t overdrive
overdrive speed is used if set to 1.
Definition w1.h:576
struct w1_rom rom
Unique 1-Wire ROM.
Definition w1.h:574
w1_rom struct.
Definition w1.h:552
uint8_t serial[6]
The serial together with the family code composes the unique 56-bit id.
Definition w1.h:561
uint8_t crc
8-bit checksum of the 56-bit unique id.
Definition w1.h:563
uint8_t family
The 1-Wire family code identifying the peripheral device type.
Definition w1.h:559
Byte order helpers.
static void sys_put_be64(uint64_t val, uint8_t dst[8])
Put a 64-bit integer as big-endian to arbitrary location.
Definition byteorder.h:417
static uint64_t sys_get_be64(const uint8_t src[8])
Get a 64-bit integer stored in big-endian format.
Definition byteorder.h:598
CRC computation function.