Zephyr API Documentation  3.7.0
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#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>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
37/*
38 * Count the number of slaves expected on the bus.
39 * This can be used to decide if the bus has a multidrop topology or
40 * only a single slave is present.
41 * There is a comma after each ordinal (including the last)
42 * Hence FOR_EACH adds "+1" once too often which has to be subtracted in the end.
43 */
44#define F1(x) 1
45#define W1_SLAVE_COUNT(node_id) \
46 (FOR_EACH(F1, (+), DT_SUPPORTS_DEP_ORDS(node_id)) - 1)
47#define W1_INST_SLAVE_COUNT(inst) \
48 (W1_SLAVE_COUNT(DT_DRV_INST(inst)))
49
65
70};
71
75struct w1_master_config {
76 /* Number of connected slaves */
77 uint16_t slave_count;
78};
79
81struct w1_master_data {
82 /* The mutex used by w1_lock_bus and w1_unlock_bus methods */
83 struct k_mutex bus_lock;
84};
85
86typedef int (*w1_reset_bus_t)(const struct device *dev);
87typedef int (*w1_read_bit_t)(const struct device *dev);
88typedef int (*w1_write_bit_t)(const struct device *dev, bool bit);
89typedef int (*w1_read_byte_t)(const struct device *dev);
90typedef int (*w1_write_byte_t)(const struct device *dev, const uint8_t byte);
91typedef int (*w1_read_block_t)(const struct device *dev, uint8_t *buffer,
92 size_t len);
93typedef int (*w1_write_block_t)(const struct device *dev, const uint8_t *buffer,
94 size_t len);
95typedef size_t (*w1_get_slave_count_t)(const struct device *dev);
96typedef int (*w1_configure_t)(const struct device *dev,
97 enum w1_settings_type type, uint32_t value);
98typedef int (*w1_change_bus_lock_t)(const struct device *dev, bool lock);
99
100__subsystem struct w1_driver_api {
101 w1_reset_bus_t reset_bus;
102 w1_read_bit_t read_bit;
103 w1_write_bit_t write_bit;
104 w1_read_byte_t read_byte;
105 w1_write_byte_t write_byte;
106 w1_read_block_t read_block;
107 w1_write_block_t write_block;
108 w1_configure_t configure;
109 w1_change_bus_lock_t change_bus_lock;
110};
114__syscall int w1_change_bus_lock(const struct device *dev, bool lock);
115
116static inline int z_impl_w1_change_bus_lock(const struct device *dev, bool lock)
117{
118 struct w1_master_data *ctrl_data = (struct w1_master_data *)dev->data;
119 const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
120
121 if (api->change_bus_lock) {
122 return api->change_bus_lock(dev, lock);
123 }
124
125 if (lock) {
126 return k_mutex_lock(&ctrl_data->bus_lock, K_FOREVER);
127 } else {
128 return k_mutex_unlock(&ctrl_data->bus_lock);
129 }
130}
145static inline int w1_lock_bus(const struct device *dev)
146{
147 return w1_change_bus_lock(dev, true);
148}
149
160static inline int w1_unlock_bus(const struct device *dev)
161{
162 return w1_change_bus_lock(dev, false);
163}
164
191__syscall int w1_reset_bus(const struct device *dev);
192
193static inline int z_impl_w1_reset_bus(const struct device *dev)
194{
195 const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
196
197 return api->reset_bus(dev);
198}
199
208__syscall int w1_read_bit(const struct device *dev);
209
210static inline int z_impl_w1_read_bit(const struct device *dev)
211{
212 const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
213
214 return api->read_bit(dev);
215}
216
226__syscall int w1_write_bit(const struct device *dev, const bool bit);
227
228static inline int z_impl_w1_write_bit(const struct device *dev, bool bit)
229{
230 const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
231
232 return api->write_bit(dev, bit);
233}
234
243__syscall int w1_read_byte(const struct device *dev);
244
245static inline int z_impl_w1_read_byte(const struct device *dev)
246{
247 const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
248
249 return api->read_byte(dev);
250}
251
261__syscall int w1_write_byte(const struct device *dev, uint8_t byte);
262
263static inline int z_impl_w1_write_byte(const struct device *dev, uint8_t byte)
264{
265 const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
266
267 return api->write_byte(dev, byte);
268}
269
280__syscall int w1_read_block(const struct device *dev, uint8_t *buffer, size_t len);
281
292__syscall int w1_write_block(const struct device *dev,
293 const uint8_t *buffer, size_t len);
294
303__syscall size_t w1_get_slave_count(const struct device *dev);
304
305static inline size_t z_impl_w1_get_slave_count(const struct device *dev)
306{
307 const struct w1_master_config *ctrl_cfg =
308 (const struct w1_master_config *)dev->config;
309
310 return ctrl_cfg->slave_count;
311}
312
327__syscall int w1_configure(const struct device *dev,
328 enum w1_settings_type type, uint32_t value);
329
330static inline int z_impl_w1_configure(const struct device *dev,
331 enum w1_settings_type type, uint32_t value)
332{
333 const struct w1_driver_api *api = (const struct w1_driver_api *)dev->api;
334
335 return api->configure(dev, type, value);
336}
337
358#define W1_CMD_SKIP_ROM 0xCC
359
364#define W1_CMD_MATCH_ROM 0x55
365
370#define W1_CMD_RESUME 0xA5
371
378#define W1_CMD_READ_ROM 0x33
379
384#define W1_CMD_SEARCH_ROM 0xF0
385
390#define W1_CMD_SEARCH_ALARM 0xEC
391
396#define W1_CMD_OVERDRIVE_SKIP_ROM 0x3C
397
402#define W1_CMD_OVERDRIVE_MATCH_ROM 0x69
403
412#define W1_CRC8_SEED 0x00
414#define W1_CRC8_POLYNOMIAL 0x8C
416#define W1_CRC16_SEED 0x0000
418#define W1_CRC16_POLYNOMIAL 0xa001
419
423#define W1_SEARCH_ALL_FAMILIES 0x00
424
426#define W1_ROM_INIT_ZERO \
427 { \
428 .family = 0, .serial = { 0 }, .crc = 0, \
429 }
430
434struct w1_rom {
446};
447
456 struct w1_rom rom;
460 uint32_t res : 31;
462};
463
471typedef void (*w1_search_callback_t)(struct w1_rom rom, void *user_data);
472
490int w1_read_rom(const struct device *dev, struct w1_rom *rom);
491
512int w1_match_rom(const struct device *dev, const struct w1_slave_config *config);
513
526int w1_resume_command(const struct device *dev);
527
543int w1_skip_rom(const struct device *dev, const struct w1_slave_config *config);
544
556int w1_reset_select(const struct device *dev, const struct w1_slave_config *config);
557
575int w1_write_read(const struct device *dev, const struct w1_slave_config *config,
576 const uint8_t *write_buf, size_t write_len,
577 uint8_t *read_buf, size_t read_len);
578
603__syscall int w1_search_bus(const struct device *dev, uint8_t command,
605 void *user_data);
606
622static inline int w1_search_rom(const struct device *dev,
623 w1_search_callback_t callback, void *user_data)
624{
626 callback, user_data);
627}
628
644static inline int w1_search_alarm(const struct device *dev,
645 w1_search_callback_t callback, void *user_data)
646{
648 callback, user_data);
649}
650
658static inline uint64_t w1_rom_to_uint64(const struct w1_rom *rom)
659{
660 return sys_get_be64((uint8_t *)rom);
661}
662
669static inline void w1_uint64_to_rom(const uint64_t rom64, struct w1_rom *rom)
670{
671 sys_put_be64(rom64, (uint8_t *)rom);
672}
673
686static inline uint8_t w1_crc8(const uint8_t *src, size_t len)
687{
688 return crc8(src, len, W1_CRC8_POLYNOMIAL, W1_CRC8_SEED, true);
689}
690
706static inline uint16_t w1_crc16(const uint16_t seed, const uint8_t *src,
707 const size_t len)
708{
709 return crc16_reflect(W1_CRC16_POLYNOMIAL, seed, src, len);
710}
711
716#ifdef __cplusplus
717}
718#endif
719
723#include <zephyr/syscalls/w1.h>
724
725#endif /* ZEPHYR_INCLUDE_DRIVERS_W1_H_ */
CRC computation function.
#define K_FOREVER
Generate infinite timeout delay.
Definition: kernel.h:1363
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.
w1_settings_type
Defines the 1-Wire master settings types, which are runtime configurable.
Definition: w1.h:55
static int w1_unlock_bus(const struct device *dev)
Unlock the 1-wire bus.
Definition: w1.h:160
static int w1_lock_bus(const struct device *dev)
Lock the 1-wire bus to prevent simultaneous access.
Definition: w1.h:145
@ W1_SETTING_SPEED
Overdrive speed is enabled in case a value of 1 is passed and disabled passing 0.
Definition: w1.h:59
@ W1_SETINGS_TYPE_COUNT
Number of different settings types.
Definition: w1.h:69
@ W1_SETTING_STRONG_PULLUP
The strong pullup resistor is activated immediately after the next written data block by passing a va...
Definition: w1.h:64
static int w1_search_rom(const struct device *dev, w1_search_callback_t callback, void *user_data)
Search for 1-Wire slave on bus.
Definition: w1.h:622
static int w1_search_alarm(const struct device *dev, w1_search_callback_t callback, void *user_data)
Search for 1-Wire slaves with an active alarm.
Definition: w1.h:644
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:658
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 slaves on the bus.
int w1_write_read(const struct device *dev, const struct w1_slave_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 slave with matching ROM.
void(* w1_search_callback_t)(struct w1_rom rom, void *user_data)
Define the application callback handler function signature for searches.
Definition: w1.h:471
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:686
int w1_match_rom(const struct device *dev, const struct w1_slave_config *config)
Select a specific slave by broadcasting a selected ROM.
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:706
#define W1_CMD_SEARCH_ROM
This command allows the bus master to discover the addresses (i.e., ROM codes) of all slave devices o...
Definition: w1.h:384
int w1_resume_command(const struct device *dev)
Select the slave 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:669
#define W1_SEARCH_ALL_FAMILIES
This flag can be passed to searches in order to not filter on family ID.
Definition: w1.h:423
int w1_read_rom(const struct device *dev, struct w1_rom *rom)
Read Peripheral 64-bit ROM.
int w1_reset_select(const struct device *dev, const struct w1_slave_config *config)
In single drop configurations use Skip Select command, otherwise use Match ROM command.
#define W1_CRC8_SEED
Seed value used to calculate the 1-Wire 8-bit crc.
Definition: w1.h:412
#define W1_CRC16_POLYNOMIAL
Polynomial used to calculate the 1-Wire 16-bit crc.
Definition: w1.h:418
int w1_skip_rom(const struct device *dev, const struct w1_slave_config *config)
Select all slaves regardless of ROM.
#define W1_CRC8_POLYNOMIAL
Polynomial used to calculate the 1-Wire 8-bit crc.
Definition: w1.h:414
#define W1_CMD_SEARCH_ALARM
This command allows the bus master to identify which devices have experienced an alarm condition.
Definition: w1.h:390
Public kernel APIs.
Size of off_t must be equal or less than size of size_t
Definition: retained_mem.h:28
__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:403
void * data
Address of the device instance private data.
Definition: device.h:413
const void * api
Address of the API structure exposed by the device instance.
Definition: device.h:409
const void * config
Address of device instance config information.
Definition: device.h:407
Mutex Structure.
Definition: kernel.h:2917
w1_rom struct.
Definition: w1.h:434
uint8_t serial[6]
The serial together with the family code composes the unique 56-bit id.
Definition: w1.h:443
uint8_t crc
8-bit checksum of the 56-bit unique id.
Definition: w1.h:445
uint8_t family
The 1-Wire family code identifying the slave device type.
Definition: w1.h:441
Node specific 1-wire configuration struct.
Definition: w1.h:454
struct w1_rom rom
Unique 1-Wire ROM.
Definition: w1.h:456
uint32_t overdrive
overdrive speed is used if set to 1.
Definition: w1.h:458
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:395
static uint64_t sys_get_be64(const uint8_t src[8])
Get a 64-bit integer stored in big-endian format.
Definition: byteorder.h:576