This repository has been archived by the owner on May 27, 2020. It is now read-only.
forked from jgarzik/cpuminer
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathi2c-context.c
127 lines (110 loc) · 3.26 KB
/
i2c-context.c
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
/*
* generic I2C slave access interface
*
* Copyright 2014-2016 Mikeqin <Fengling.Qin@gmail.com>
* Copyright 2014 Zefir Kurtisi <zefir.kurtisi@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version. See COPYING for more details.
*/
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <stdint.h>
#include <stdbool.h>
#include <fcntl.h>
#include <assert.h>
#include "miner.h"
#include "i2c-context.h"
static bool i2c_slave_write(struct i2c_ctx *ctx, uint8_t reg, uint8_t val)
{
union i2c_smbus_data data;
data.byte = val;
struct i2c_smbus_ioctl_data args;
args.read_write = I2C_SMBUS_WRITE;
args.command = reg;
args.size = I2C_SMBUS_BYTE_DATA;
args.data = &data;
if (ioctl(ctx->file, I2C_SMBUS, &args) == -1) {
applog(LOG_INFO, "i2c 0x%02x: failed to write to fdesc %d: %s",
ctx->addr, ctx->file, strerror(errno));
return false;
}
applog(LOG_DEBUG, "I2C-W(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, val);
return true;
}
static bool i2c_slave_read(struct i2c_ctx *ctx, uint8_t reg, uint8_t *val)
{
union i2c_smbus_data data;
struct i2c_smbus_ioctl_data args;
args.read_write = I2C_SMBUS_READ;
args.command = reg;
args.size = I2C_SMBUS_BYTE_DATA;
args.data = &data;
if (ioctl(ctx->file, I2C_SMBUS, &args) == -1) {
applog(LOG_INFO, "i2c 0x%02x: failed to read from fdesc %d: %s",
ctx->addr, ctx->file, strerror(errno));
return false;
}
*val = data.byte;
applog(LOG_DEBUG, "I2C-R(0x%02x/0x%02x)=0x%02x", ctx->addr, reg, *val);
return true;
}
static bool i2c_slave_write_raw(struct i2c_ctx *ctx, uint8_t *buf, uint32_t len)
{
/* SMBus cann't support write bytes > 32, use plain i2c write */
if (len != write(ctx->file, buf, len)) {
applog(LOG_INFO, "i2c 0x%02x: failed to write raw to fdesc %d: %s",
ctx->addr, ctx->file, strerror(errno));
return false;
}
applog(LOG_DEBUG, "I2C-W-RAW(0x%02x)", ctx->addr);
return true;
}
static bool i2c_slave_read_raw(struct i2c_ctx *ctx, uint8_t *buf, uint32_t len)
{
/* SMBus cann't support read bytes > 32, use plain i2c read */
if (len != read(ctx->file, buf, len)) {
applog(LOG_INFO, "i2c 0x%02x: failed to read raw from fdesc %d: %s",
ctx->addr, ctx->file, strerror(errno));
return false;
}
applog(LOG_DEBUG, "I2C-R-RAW(0x%02x)", ctx->addr);
return true;
}
static void i2c_slave_exit(struct i2c_ctx *ctx)
{
if (ctx->file == -1)
return;
close(ctx->file);
free(ctx);
}
extern struct i2c_ctx *i2c_slave_open(char *i2c_bus, uint8_t slave_addr)
{
int file = open(i2c_bus, O_RDWR);
if (file < 0) {
applog(LOG_INFO, "Failed to open %s: %s", i2c_bus, strerror(errno));
return NULL;
}
if (ioctl(file, I2C_SLAVE, slave_addr) < 0) {
close(file);
return NULL;
}
struct i2c_ctx *ctx = malloc(sizeof(*ctx));
assert(ctx != NULL);
ctx->addr = slave_addr;
ctx->file = file;
ctx->exit = i2c_slave_exit;
ctx->read = i2c_slave_read;
ctx->write = i2c_slave_write;
ctx->read_raw = i2c_slave_read_raw;
ctx->write_raw = i2c_slave_write_raw;
return ctx;
}