forked from wareck/cgminer-gekko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf16-ctrldevice.c
182 lines (153 loc) · 4.16 KB
/
bf16-ctrldevice.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
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
#include "bf16-ctrldevice.h"
#include "miner.h"
#define MAX_TOKENS_ALLOWED 10
char *ctrl_device_name = "ctrl";
static char *d_state_on = "ON";
static char *d_state_off = "OFF";
static char* D_STATE(int state)
{
return (state == 0) ? d_state_off : d_state_on;
}
char* get_ctrl_data(int channel, int state, int fn)
{
char *request = malloc(CTRL_BUFFER_SIZE);
memset(request, 0, CTRL_BUFFER_SIZE);
int index = (state == 0) ? 0 : 1;
switch (fn) {
case F_BUZZER:
sprintf(request, "%s=%s\n", BUZZER, D_STATE(index));
break;
case F_LED1:
sprintf(request, "%s=%s\n", LED_GREEN, D_STATE(index));
break;
case F_LED2:
sprintf(request, "%s=%s\n", LED_RED, D_STATE(index));
break;
case F_BRST:
(channel == 1) ?
sprintf(request, "%s=%s\n", CH1_MSP_RST, D_STATE(index)) :
sprintf(request, "%s=%s\n", CH2_MSP_RST, D_STATE(index));
break;
case F_RST:
(channel == 1) ?
sprintf(request, "%s=%s\n", CH1_SPI_RES, D_STATE(index)) :
sprintf(request, "%s=%s\n", CH2_SPI_RES, D_STATE(index));
break;
case F_BDET:
(channel == 1) ?
sprintf(request, "%s=%s\n", CH1_BRD_DET, D_STATE(get_ch1_det())) :
sprintf(request, "%s=%s\n", CH2_BRD_DET, D_STATE(get_ch2_det()));
break;
case F_STAT:
default:
request[0] = '\n';
}
return request;
}
static int D_VALUE(char *value)
{
if (strcasecmp(value, d_state_on) == 0)
return 1;
if (strcasecmp(value, d_state_off) == 0)
return 0;
return -1;
}
static int8_t D_FUNCTION(uint8_t value, char *cmd)
{
if (strcmp(cmd, BUZZER) == 0)
return set_buzzer(value);
else if (strcmp(cmd, LED_GREEN) == 0)
return set_led_green(value);
else if (strcmp(cmd, LED_RED) == 0)
return set_led_red(value);
else if (strcmp(cmd, CH1_MSP_RST) == 0)
return set_ch1_rst(value);
else if (strcmp(cmd, CH2_MSP_RST) == 0)
return set_ch2_rst(value);
else if (strcmp(cmd, CH1_SPI_RES) == 0)
return set_ch1_spi(value);
else if (strcmp(cmd, CH2_SPI_RES) == 0)
return set_ch2_spi(value);
else
applog(LOG_ERR, "CTRL DEVICE, D_FUNCTION: unknown command: [%s]", cmd);
return -1;
}
static int split_tokens(char* cmd, char token, char** tokens)
{
int len=0;
char *src, *ptr;
src = cmd;
ptr = cmd;
while ((*src != '\n') && (*src != '\r') && (*src != '\0') &&
(len < MAX_TOKENS_ALLOWED)) {
if (*src == token) {
*src = 0;
tokens[len++] = ptr;
ptr = src + 1;
}
src++;
}
*src = 0;
if (src != ptr)
tokens[len++] = ptr;
return len;
}
int8_t ctrl_init(device_t* attr, char *device, uint16_t size)
{
attr->device = device;
attr->mode = 0;
attr->speed = 0;
attr->bits = 0;
attr->size = size;
attr->rx = malloc(size);
attr->tx = malloc(size);
brd_init();
return 0;
}
int8_t ctrl_transfer(device_t *attr)
{
char* cmd = (char*) attr->tx;
char* tokens[MAX_TOKENS_ALLOWED];
int8_t ret = 0;
int len = split_tokens(cmd, ';', tokens);
cmd = (char*) attr->rx;
if (len == 0) {
len += snprintf(cmd + len, attr->size, "%s=%d;", HW_VER, get_hw_ver());
len += snprintf(cmd + len, attr->size, "%s=%s;", BTN_FR, D_STATE(get_btn_fr()));
len += snprintf(cmd + len, attr->size, "%s=%s;", BTN_DISCOVERY, D_STATE(get_btn_discovery()));
len += snprintf(cmd + len, attr->size, "%s=%s;", CH1_DET, D_STATE(get_ch1_det()));
len += snprintf(cmd + len, attr->size, "%s=%s\n",CH2_DET, D_STATE(get_ch2_det()));
attr->datalen = len;
} else {
char* values[2];
int i;
int datalen = 0;
for (i = 0; i < len; i++) {
char *tok = tokens[i];
int n = split_tokens(tok, '=', values);
if (n == 2) {
int state = D_VALUE(values[1]);
if (state != -1) {
applog(LOG_DEBUG, "BF16: CTRL: token applying: %s>%s", values[0], values[1]);
if (strcasecmp(values[0], CH1_BRD_DET) == 0)
datalen += snprintf(cmd + datalen, attr->size, "%s:%s\n", CH1_DET, D_STATE(get_ch1_det()));
else if (strcasecmp(values[0], CH2_BRD_DET) == 0)
datalen += snprintf(cmd + datalen, attr->size, "%s:%s\n", CH2_DET, D_STATE(get_ch2_det()));
else
ret = D_FUNCTION(state, values[0]);
}
}
}
if (datalen == 0) {
datalen = 1;
cmd[0] = '\n';
} else
attr->datalen = datalen;
}
return ret;
}
void ctrl_release(device_t *attr)
{
free(attr->rx);
free(attr->tx);
}