summaryrefslogtreecommitdiff
path: root/commandline/i2cset.c
blob: bdcd3ccb6292942a953c73b1b772b21dffce2ac8 (plain)
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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "i2c-util.h"

int main(int argc, char **argv)
{
	int i, address, cmdbuf;
	char *conv_err;

	i2c_getopt(argc, argv);

	if (argc < 3) {
		fputs("Usage: i2cset <address> <data ...>", stderr);
		return 1;
	}

	address = strtol(argv[1], &conv_err, 0);

	if (conv_err && *conv_err) {
		fprintf(stderr, "Conversion error at '%s'\n", conv_err);
		return 2;
	}

	i2c_init();
	i2c_hw_start();

	if (!i2c_hw_tx_byte(address << 1)) {
		fprintf(stderr, "Received NAK from slave 0x%02x, aborting\n", address);
		i2c_hw_stop();
		i2c_deinit();
		return 3;
	}

	for (i = 2; i < argc; i++) {
		cmdbuf = strtol(argv[i], &conv_err, 0);
		if (conv_err && *conv_err) {
			fprintf(stderr, "write command: conversion error at '%s'\n", conv_err);
			i2c_hw_stop();
			i2c_deinit();
			return 2;
		}
		if (!i2c_hw_tx_byte(cmdbuf)) {
			fprintf(stderr, "Received NAK after byte %d (0x%02x)\n", i-1, cmdbuf);
			i2c_hw_stop();
			i2c_deinit();
			return 4;
		}
	}

	i2c_hw_stop();
	i2c_deinit();

	return 0;
}