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
/
A1-trimpot-mcp4x.c
116 lines (93 loc) · 2.46 KB
/
A1-trimpot-mcp4x.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
/*
* support for MCP46x digital trimpot used in Bitmine's products
*
* 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 <assert.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 <sys/ioctl.h>
#include <stdint.h>
#include <stdbool.h>
#include <fcntl.h>
#include "miner.h"
#include "A1-trimpot-mcp4x.h"
static bool mcp4x_check_status(int file)
{
union i2c_smbus_data data;
struct i2c_smbus_ioctl_data args;
args.read_write = I2C_SMBUS_READ;
args.command = ((5 & 0x0f) << 4) | 0x0c;
args.size = I2C_SMBUS_WORD_DATA;
args.data = &data;
return ioctl(file, I2C_SMBUS, &args) >= 0;
}
static uint16_t mcp4x_get_wiper(struct mcp4x *me, uint8_t id)
{
assert(id < 2);
union i2c_smbus_data data;
struct i2c_smbus_ioctl_data args;
args.read_write = I2C_SMBUS_READ;
args.command = ((id & 0x0f) << 4) | 0x0c;
args.size = I2C_SMBUS_WORD_DATA;
args.data = &data;
if (ioctl(me->file, I2C_SMBUS, &args) < 0) {
applog(LOG_ERR, "Failed to read id %d: %s\n", id,
strerror(errno));
return 0xffff;
}
return htobe16(data.word & 0xffff);
}
static bool mcp4x_set_wiper(struct mcp4x *me, uint8_t id, uint16_t w)
{
assert(id < 2);
union i2c_smbus_data data;
data.word = w;
struct i2c_smbus_ioctl_data args;
args.read_write = I2C_SMBUS_WRITE;
args.command = (id & 0x0f) << 4;
args.size = I2C_SMBUS_WORD_DATA;
args.data = &data;
if (ioctl(me->file, I2C_SMBUS, &args) < 0) {
applog(LOG_ERR, "Failed to read id %d: %s\n", id,
strerror(errno));
return false;
}
return me->get_wiper(me, id) == w;
}
void mcp4x_exit(struct mcp4x *me)
{
close(me->file);
free(me);
}
struct mcp4x *mcp4x_init(uint8_t addr)
{
struct mcp4x *me;
int file = open("/dev/i2c-1", O_RDWR);
if (file < 0) {
applog(LOG_INFO, "Failed to open i2c-1: %s\n", strerror(errno));
return NULL;
}
if (ioctl(file, I2C_SLAVE, addr) < 0)
return NULL;
if (!mcp4x_check_status(file))
return NULL;
me = malloc(sizeof(*me));
assert(me != NULL);
me->addr = addr;
me->file = file;
me->exit = mcp4x_exit;
me->get_wiper = mcp4x_get_wiper;
me->set_wiper = mcp4x_set_wiper;
return me;
}