1 /*
2 * Copyright (c) 2022 Thomas Stranger
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/sys/crc.h>
9 #include <zephyr/types.h>
10 #include <zephyr/drivers/w1.h>
11
12
z_impl_w1_read_block(const struct device * dev,uint8_t * buffer,size_t len)13 int z_impl_w1_read_block(const struct device *dev, uint8_t *buffer, size_t len)
14 {
15 const struct w1_driver_api *api = dev->api;
16 int ret;
17
18 if (api->read_block != NULL) {
19 return api->read_block(dev, buffer, len);
20 }
21 for (int i = 0; i < len; ++i) {
22 ret = w1_read_byte(dev);
23 if (ret < 0) {
24 return ret;
25 }
26 buffer[i] = ret;
27 }
28
29 return 0;
30 }
31
z_impl_w1_write_block(const struct device * dev,const uint8_t * buffer,size_t len)32 int z_impl_w1_write_block(const struct device *dev, const uint8_t *buffer,
33 size_t len)
34 {
35 const struct w1_driver_api *api = dev->api;
36 int ret;
37
38 if (api->write_block != NULL) {
39 return api->write_block(dev, buffer, len);
40 }
41 for (int i = 0; i < len; ++i) {
42 ret = w1_write_byte(dev, buffer[i]);
43 if (ret < 0) {
44 return ret;
45 }
46 }
47
48 return 0;
49 }
50