Linux Audio

Check our new training course

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * Copyright (c) 2018 Diego Sueiro
 * Copyright (c) 2022 Thomas Stranger
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <stdlib.h>

#include <zephyr/drivers/w1.h>
#include <zephyr/shell/shell.h>
#include <zephyr/kernel.h>

#define BUF_SIZE CONFIG_W1_SHELL_BUFFER_SIZE
static uint8_t msg_buf[BUF_SIZE];

#define W1DEV_X_NOT_FOUND "1-Wire device not found: \"%s\""

#define OPTION_HELP_RESET "-r Perform bus reset before executing cmd."

static const char *w1_settings_name[W1_SETINGS_TYPE_COUNT] = {
	[W1_SETTING_SPEED] = "speed",
	[W1_SETTING_STRONG_PULLUP] = "spu",
};

static int read_io_options(const struct shell *sh, int pos, char **argv,
			   bool *reset)
{
	char *arg = argv[pos];

	if (arg[0] != '-') {
		return pos;
	}

	for (arg = &arg[1]; *arg; arg++) {
		switch (*arg) {
		case 'r':
			*reset = true;
			break;
		default:
			shell_error(sh, "Unknown option %c", *arg);
			return -EINVAL;
		}
	}

	return ++pos;
}

/* 1-Wire reset bus <device> */
static int cmd_w1_reset_bus(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int ret;

	dev = device_get_binding(argv[1]);
	if (!dev) {
		shell_error(sh, W1DEV_X_NOT_FOUND, argv[1]);
		return -EINVAL;
	}

	(void)w1_lock_bus(dev);
	ret = w1_reset_bus(dev);
	if (ret < 0) {
		shell_error(sh, "Failed to reset bus [%d]", ret);
	}

	(void)w1_unlock_bus(dev);
	return ret;
}

/* 1-Wire read_bit <device> */
static int cmd_w1_read_bit(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int ret;

	dev = device_get_binding(argv[1]);
	if (!dev) {
		shell_error(sh, W1DEV_X_NOT_FOUND, argv[1]);
		return -EINVAL;
	}

	(void)w1_lock_bus(dev);
	ret = w1_read_bit(dev);
	if (ret < 0) {
		shell_error(sh, "Failed to read bit [%d]", ret);
	} else {
		shell_print(sh, "Output: 0b%x", ret);
	}

	(void)w1_unlock_bus(dev);
	return ret;
}

/* 1-Wire read_byte <device> */
static int cmd_w1_read_byte(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int ret;

	dev = device_get_binding(argv[1]);
	if (!dev) {
		shell_error(sh, W1DEV_X_NOT_FOUND, argv[1]);
		return -EINVAL;
	}

	(void)w1_lock_bus(dev);
	ret = w1_read_byte(dev);
	if (ret < 0) {
		shell_error(sh, "Failed to read byte [%d]", ret);
	} else {
		shell_print(sh, "Output: 0x%x", ret);
	}

	(void)w1_unlock_bus(dev);
	return ret;
}

/* 1-Wire read_block <device> [num_bytes] */
static int cmd_w1_read_block(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	char *end_ptr;
	size_t read_len;
	int ret;

	dev = device_get_binding(argv[1]);
	if (!dev) {
		shell_error(sh, W1DEV_X_NOT_FOUND, argv[1]);
		return -EINVAL;
	}

	read_len = strtoul(argv[2], &end_ptr, 0);
	if (*end_ptr != '\0') {
		shell_error(sh, "num_bytes is not a number");
		return -EINVAL;
	}
	if (read_len > BUF_SIZE) {
		shell_error(sh, "num_bytes limited to: %u", BUF_SIZE);
		return -EINVAL;
	}

	(void)w1_lock_bus(dev);
	ret = w1_read_block(dev, msg_buf, read_len);
	if (ret < 0) {
		shell_error(sh, "Failed to read byte [%d]", ret);
		goto out;
	}

	shell_fprintf(sh, SHELL_NORMAL, "Output:");
	for (int i = 0; i < read_len; i++) {
		shell_fprintf(sh, SHELL_NORMAL, " 0x%02x", msg_buf[i]);
	}
	shell_fprintf(sh, SHELL_NORMAL, "\n");

out:
	(void)w1_unlock_bus(dev);
	return ret;
}

/* 1-Wire write_bit <device> <bit_value> */
static int cmd_w1_write_bit(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	unsigned long input = strtoul(argv[2], NULL, 0);
	int ret;

	dev = device_get_binding(argv[1]);
	if (!dev) {
		shell_error(sh, W1DEV_X_NOT_FOUND, argv[1]);
		return -EINVAL;
	}
	if (input > 1UL) {
		shell_error(sh, "input must not be > 0b1");
		return -EINVAL;
	}

	(void)w1_lock_bus(dev);
	ret = w1_write_byte(dev, (bool)input);
	if (ret < 0) {
		shell_error(sh, "Failed to write bit [%d]", ret);
	}

	(void)w1_unlock_bus(dev);
	return ret;
}

/* 1-Wire write_byte <device> <byte_value> */
static int cmd_w1_write_byte(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	unsigned long input;
	int pos = 1;
	bool reset = false;
	int ret;

	dev = device_get_binding(argv[pos]);
	if (!dev) {
		shell_error(sh, W1DEV_X_NOT_FOUND, argv[pos]);
		return -EINVAL;
	}
	pos++;

	pos = read_io_options(sh, pos, argv, &reset);
	if (pos < 0) {
		return -EINVAL;
	}
	if (argc <= pos) {
		shell_error(sh, "Missing data to be written.");
		return -EINVAL;
	}

	input = strtoul(argv[pos], NULL, 0);
	if (input > 0xFFUL) {
		shell_error(sh, "input must not be > 0xFF");
		return -EINVAL;
	}

	(void)w1_lock_bus(dev);
	if (reset) {
		ret = w1_reset_bus(dev);
		if (ret <= 0) {
			shell_error(sh, "Failed to reset bus [%d]", ret);
			goto out;
		}
	}

	ret = w1_write_byte(dev, (uint8_t)input);
	if (ret < 0) {
		shell_error(sh, "Failed to write byte [%d]", ret);
	}
out:
	(void)w1_unlock_bus(dev);
	return ret;
}

/* 1-Wire write_block <device> <byt1> [byte2, ...] */
static int cmd_w1_write_block(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int i;
	int pos = 1;
	bool reset = false;
	int ret;

	dev = device_get_binding(argv[pos]);
	if (!dev) {
		shell_error(sh, W1DEV_X_NOT_FOUND, argv[1]);
		return -EINVAL;
	}
	pos++;

	pos = read_io_options(sh, pos, argv, &reset);
	if (pos < 0) {
		return -EINVAL;
	}
	if (argc <= pos) {
		shell_error(sh, "Missing data to be written.");
		return -EINVAL;
	}
	if ((argc - pos) > BUF_SIZE) {
		shell_error(sh, "Too much data to be written.");
		return -EINVAL;
	}

	(void)w1_lock_bus(dev);
	i = 0;
	do {
		msg_buf[i] = (uint8_t)strtoul(argv[i + pos], NULL, 16);
		i++;
	} while ((i + pos) < argc);

	if (reset) {
		ret = w1_reset_bus(dev);
		if (ret <= 0) {
			shell_error(sh, "Failed to reset bus [%d]", ret);
			goto out;
		}
	}

	ret = w1_write_block(dev, msg_buf, i);
	if (ret < 0) {
		shell_error(sh, "Failed to write block [%d]", ret);
	}
out:
	(void)w1_unlock_bus(dev);
	return ret;
}

/* 1-Wire config <device> <type> <value> */
static int cmd_w1_configure(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	char *type_endptr;
	char *type_name = argv[2];
	int ret;
	uint32_t type = strtoul(type_name, &type_endptr, 0);
	uint32_t value = strtoul(argv[3], NULL, 0);

	dev = device_get_binding(argv[1]);
	if (!dev) {
		shell_error(sh, W1DEV_X_NOT_FOUND, argv[1]);
		return -EINVAL;
	}

	/* if type is not given as number, search it via the name */
	if (*type_endptr != '\0') {
		for (type = 0; type < ARRAY_SIZE(w1_settings_name); type++) {
			if (strcmp(type_name, w1_settings_name[type]) == 0) {
				break;
			}
		}

		if (type == ARRAY_SIZE(w1_settings_name)) {
			shell_error(sh, "Unknown config name (%s)", type_name);
			return -ENOTSUP;
		}
	}

	if (type > W1_SETINGS_TYPE_COUNT) {
		shell_error(sh, "invalid type %u", type);
		return -EINVAL;
	}

	(void)w1_lock_bus(dev);
	ret = w1_configure(dev, type, value);
	if (ret < 0) {
		shell_error(sh, "Failed to configure [%d]", ret);
		goto out;
	}

	shell_info(sh, "Applied config: %s = %u (0x%08x)",
		   w1_settings_name[type], value, value);

out:
	(void)w1_unlock_bus(dev);
	return ret;
}

static void search_callback(struct w1_rom rom, void *user_data)
{
	const struct shell *sh = (const struct shell *)user_data;

	shell_print(sh, "ROM found: %016llx", w1_rom_to_uint64(&rom));
}

/* 1-Wire search <device> */
static int cmd_w1_search(const struct shell *sh, size_t argc, char **argv)
{
	const struct device *dev;
	int ret;

	dev = device_get_binding(argv[1]);
	if (!dev) {
		shell_error(sh, W1DEV_X_NOT_FOUND, argv[1]);
		return -EINVAL;
	}

	(void)w1_lock_bus(dev);
	ret = w1_search_rom(dev, search_callback, (void *)sh);
	if (ret < 0) {
		shell_error(sh, "Failed to initiate search [%d]", ret);
	} else {
		shell_print(sh, "Found %d device(s)", ret);
	}

	(void)w1_unlock_bus(dev);
	return ret;
}

SHELL_STATIC_SUBCMD_SET_CREATE(sub_w1,
	SHELL_CMD_ARG(reset, NULL,
		      "Reset 1-Wire bus.\n"
		      "Usage: <device>",
		      cmd_w1_reset_bus, 2, 0),
	SHELL_CMD_ARG(read_bit, NULL,
		      "Read 1-Wire bit.\n"
		      "Usage: <device>",
		      cmd_w1_read_bit, 2, 0),
	SHELL_CMD_ARG(read_byte, NULL,
		      "Read 1-Wire byte.\n"
		      "Usage: <device>",
		      cmd_w1_read_byte, 2, 0),
	SHELL_CMD_ARG(read_block, NULL,
		      "Read 1-Wire block.\n"
		      "Usage: <device> <num_bytes>",
		      cmd_w1_read_block, 3, 0),
	SHELL_CMD_ARG(write_bit, NULL,
		      "Write 1-Wire bit.\n"
		      "Usage: <device> <bit>",
		      cmd_w1_write_bit, 3, 0),
	SHELL_CMD_ARG(write_byte, NULL,
		      "Write 1-Wire byte.\n"
		      "Usage: <device> [-r] <byte>\n"
		      OPTION_HELP_RESET,
		      cmd_w1_write_byte, 3, 1),
	SHELL_CMD_ARG(write_block, NULL,
		      "Write 1-Wire block.\n"
		      "Usage: <device> [-r] <byte1> [<byte2>, ...]\n"
		      OPTION_HELP_RESET,
		      cmd_w1_write_block, 3, BUF_SIZE),
	SHELL_CMD_ARG(config, NULL,
		      "Configure 1-Wire host.\n"
		      "Usage: <device> <type> <value>\n"
		      "<type> is either a name or an id.",
		      cmd_w1_configure, 4, 0),
	SHELL_CMD_ARG(search, NULL,
		      "1-Wire devices.\n"
		      "Usage: <device>",
		      cmd_w1_search, 2, 0),
	SHELL_SUBCMD_SET_END /* Array terminated. */
);

SHELL_CMD_ARG_REGISTER(w1, &sub_w1, "1-Wire commands", NULL, 2, 0);