forked from leoz/rpic-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib-juice.c
274 lines (227 loc) · 5.23 KB
/
lib-juice.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include "../raspy-juice-read-only/firmware/juice.h"
#define I2C_NUM_RETRIES 3
#define RETRY_TIMEOUT 20000
static int rj_file = 0;
static int rj_errno;
int rj_readbyte(int subreg);
int rj_writebyte(int subreg, int data);
int rj_readword(int subreg);
int rj_writeword(int subreg, int data);
int rj_readblock(int subreg, void *inbuf);
#define BUFSIZE 64
char version_str[BUFSIZE];
int rj_open(const char *devbusname, int i2caddr)
{
int rval;
rj_file = open(devbusname, O_RDWR);
if (rj_file < 0) {
rj_errno = rj_file;
return -1;
}
if ((rval = ioctl(rj_file, I2C_SLAVE, i2caddr)) < 0) {
rj_errno = rval;
return -2;
}
return rj_file;
}
char *rj_getversion(void)
{
int i;
i = 0;
rj_writebyte(RJ_VERSION, 0);
do {
version_str[i] = rj_readbyte(RJ_VERSION);
} while ((version_str[i] != 0) && (i++ < (BUFSIZE - 2) ) );
version_str[i] = 0;
return version_str;
}
int rj_setservo(int chan, int usec)
{
return rj_writeword(chan, usec);
}
int rj_readstat(void)
{
return rj_readbyte(GSTAT);
}
int rj_readadc(unsigned char mux)
{
int rval;
rval = rj_writebyte(ADCMUX, mux);
rval = rj_readword(ADCDAT);
return rval;
}
int rj232_getc(void)
{
return rj_readbyte(RS232D);
}
int rj232_read(unsigned char *buf, int maxlen)
{
int i = 0;
while ((rj_readstat() & RXA232) && (i < maxlen)) {
buf[i++] = rj232_getc();
}
buf[i] = 0;
return i;
}
int rj485_getc(void)
{
return rj_readbyte(RS485D);
}
int rj485_read(unsigned char *buf, int maxlen)
{
int i = 0;
while ((rj_readstat() & RXA485) && (i < maxlen)) {
buf[i++] = rj485_getc();
}
buf[i] = 0;
return i;
}
int rj232_send(unsigned char *buf, int len)
{
int i, rval;
for (i = 0; i < len; i++)
rval = rj_writebyte(RS232D, *buf++);
return rval;
}
int rj485_send(unsigned char *buf, int len)
{
int i, rval;
for (i = 0; i < len; i++)
rval = rj_writebyte(RS485D, *buf++);
return rval;
}
/**********************************************************************
*
**********************************************************************/
int rj_readbyte(int subreg)
{
int rval, retry = 3;
do {
rval = i2c_smbus_read_byte_data(rj_file, subreg);
if (rval < 0) {
retry--;
usleep(RETRY_TIMEOUT);
}
} while ((rval < 0) && (retry > 0));
if (rval < 0)
fprintf(stderr, "i2c_smbus_read_byte_data failed.\n");
return rval;
}
int rj_readword(int subreg)
{
int rval, retry = 3;
do {
rval = i2c_smbus_read_word_data(rj_file, subreg);
if (rval < 0) {
retry--;
usleep(RETRY_TIMEOUT);
}
} while ((rval < 0) && (retry > 0));
if (rval < 0)
fprintf(stderr, "i2c_smbus_read_word_data failed.\n");
return rval;
}
int rj_readblock(int subreg, void *inbuf)
{
int rval, retry = 3;
do {
rval = i2c_smbus_read_block_data(rj_file, subreg, inbuf);
if (rval < 0) {
retry--;
usleep(RETRY_TIMEOUT);
}
} while ((rval < 0) && (retry > 0));
if (rval < 0)
fprintf(stderr, "i2c_smbus_read_block_data failed.\n");
return rval;
}
int rj_writebyte(int subreg, int data)
{
int rval, retry = 3;
do {
rval = i2c_smbus_write_byte_data(rj_file, subreg, data);
if (rval < 0) {
retry--;
usleep(RETRY_TIMEOUT);
}
} while ((rval < 0) && (retry > 0));
if (rval < 0)
fprintf(stderr, "i2c_smbus_write_byte_data failed.\n");
return rval;
}
int rj_writeword(int subreg, int data)
{
int rval, retry = 3;
do {
rval = i2c_smbus_write_word_data(rj_file, subreg, data);
if (rval < 0) {
retry--;
usleep(RETRY_TIMEOUT);
}
} while ((rval < 0) && (retry > 0));
if (rval < 0)
fprintf(stderr, "i2c_smbus_write_word_data failed.\n");
return rval;
}
struct avr_swuart_baud_setting {
int bps;
unsigned char prescaler;
unsigned char ticks;
} avr_swuart_baud_setting;
struct avr_swuart_baud_setting avr_rates[] = {
{ 1200, 0x05, 96 },
{ 2400, 0x04, 96 },
{ 4800, 0x04, 48 },
{ 9600, 0x04, 24 },
{ 14400, 0x04, 16 },
{ 19200, 0x04, 12 },
{ 38400, 0x04, 6 },
};
int avr_rates_max = sizeof(avr_rates) / sizeof (avr_swuart_baud_setting);
int rj232_setbaud(int bps)
{
int i = 0;
while (i < avr_rates_max) {
if (avr_rates[i].bps == bps) {
return rj_writeword(BPS232, (avr_rates[i].prescaler << 8) |
(avr_rates[i].ticks));
}
i++;
}
return -1;
}
struct avr_usart_baud_setting {
int bps;
int ubrr;
} avr_usart_baud_setting;
#define F_CPU 14745600UL
#define UBRR_V(BAUD) (F_CPU / 16 / (unsigned long)(BAUD) - 1)
struct avr_usart_baud_setting avr_ubrrs[] = {
{ 1200, UBRR_V(1200) },
{ 2400, UBRR_V(2400) },
{ 4800, UBRR_V(4800) },
{ 9600, UBRR_V(9600) },
{ 14400, UBRR_V(14400) },
{ 19200, UBRR_V(19200) },
{ 38400, UBRR_V(38400) },
{ 57600, UBRR_V(57600) },
{ 115200, UBRR_V(115200) },
{ 230400, UBRR_V(230400) },
};
int avr_ubrrs_max = sizeof(avr_ubrrs) / sizeof (avr_usart_baud_setting);
int rj485_setbaud(int bps)
{
int i = 0;
while (i < avr_ubrrs_max) {
if (avr_ubrrs[i].bps == bps)
return rj_writeword(BPS485, avr_ubrrs[i].ubrr);
i++;
}
return -1;
}