-
Notifications
You must be signed in to change notification settings - Fork 1
/
i2c.c
517 lines (409 loc) · 11.4 KB
/
i2c.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* ************************************************************************** */
/** I2C
@Company
ShimaneJohoshoriCenter.inc
@File Name
i2c.c
@Summary
I2C processing
@Description
mruby/c function army
use I2C2 module and frequency is 100kHz only.
*/
/* ************************************************************************** */
#include "pic32mx.h"
#include "gpio.h"
#include "mrubyc.h"
/* ================================ C codes ================================ */
#define I2CFREQ 100000 // 100kHz
#define I2C_ACK 0
#define I2C_NACK 1
#define I2C_BUS_BUSY -1
#define I2C_PARAM_ERROR -2
//================================================================
/*! Initialize I2C module.
use I2C2 module only. SCL=B3, SDA=B2
*/
void i2c_init(void)
{
// DS60001116G Equation 24-1: Baud Rate Generator Reload Value Calculation
I2C2BRG = (uint16_t)(PBCLK / (2.0 * I2CFREQ) - 1 - PBCLK * 130e-9 / 2);
I2C2CON = 0x8200; // I2C : Enable, 100kHz
I2C2STAT = 0x0;
}
//================================================================
/*! Send START condition
@return not zero if error.
*/
static int i2c_send_start(void)
{
I2C2CONSET = (1 << _I2C2CON_SEN_POSITION);
// Normally loops about 20 times at clock 40MHz.
// thus, for safety 2000 loops.
for( int i = 0; i < 2000; i++ ) {
if( I2C2CONbits.SEN == 0 ) return 0;
}
return I2C_BUS_BUSY;
}
//================================================================
/*! Send RESTART condition
@return not zero if error.
*/
static int i2c_send_restart( void )
{
I2C2CONSET = (1 << _I2C2CON_RSEN_POSITION);
for( int i = 0; i < 2000; i++ ) {
if( I2C2CONbits.RSEN == 0 ) return 0;
}
return I2C_BUS_BUSY;
}
//================================================================
/*! Send STOP condition
@return not zero if error.
*/
static int i2c_send_stop( void )
{
I2C2CONSET = (1 << _I2C2CON_PEN_POSITION);
for( int i = 0; i < 2000; i++ ) {
if( I2C2CONbits.PEN == 0 ) return 0;
}
return I2C_BUS_BUSY;
}
//================================================================
/*! Writes a single byte.
@param data data byte.
@return 0: ACK / 1: NACK / Other: error.
*/
static int i2c_write_byte( uint8_t data )
{
I2C2TRN = data;
// about 100us/byte at 100kHz mode.
// thus, for safety about 10ms.
for( int i = 0; i < 1000; i++ ) {
if( (I2C2STAT & (_I2C2STAT_TBF_MASK|_I2C2STAT_TRSTAT_MASK)) == 0 ) {
return I2C2STATbits.ACKSTAT;
}
__delay_us( 10 );
}
return I2C_BUS_BUSY;
}
//================================================================
/*! Reads a single byte.
@param ack_nack I2C_ACK | I2C_NACK
@return received data byte. or minus value if error.
*/
static int i2c_read_byte( int ack_nack )
{
I2C2CONbits.RCEN = 1;
for( int i = 0; I2C2CONbits.RCEN; ) {
if( ++i > 1000 ) goto TIMEOUT_ERROR;
__delay_us( 10 );
}
int ret = I2C2RCV;
// send ack=0 or nack=1
I2C2CONbits.ACKDT = !!ack_nack;
I2C2CONbits.ACKEN = 1;
for( int i = 0; i < 1000; i++ ) {
if( I2C2CONbits.ACKEN == 0 ) return ret; // normal return
__delay_us(1);
}
TIMEOUT_ERROR:
return I2C_BUS_BUSY;
}
//================================================================
/*! write mrbc value to i2c bus.
*/
static int i2c_write_mrbc_value( const mrbc_value *v, int *n_of_out_bytes )
{
int ret = 0;
switch( v->tt ) {
case MRBC_TT_INTEGER: {
ret = i2c_write_byte( mrbc_fixnum(*v) );
if( ret == 0 ) (*n_of_out_bytes)++;
} break;
case MRBC_TT_ARRAY:
for( int i = 0; i < mrbc_array_size(v); i++ ) {
mrbc_value v1 = mrbc_array_get(v, i);
ret = i2c_write_mrbc_value( &v1, n_of_out_bytes );
}
break;
case MRBC_TT_STRING: {
const char *p = mrbc_string_cstr(v);
for( int i = 0; i < mrbc_string_size(v); i++ ) {
ret = i2c_write_byte( *p++ );
if( ret != 0 ) break;
(*n_of_out_bytes)++;
}
} break;
default:
ret = I2C_PARAM_ERROR;
}
return ret;
}
/* ============================= mruby/c codes ============================= */
//================================================================
/*! read transaction.
(mruby usage)
s = i2c.read( i2c_adrs_7, read_bytes, *param )
s.getbyte(n) # bytes
i2c_adrs_7 = Integer
read_byres = Integer
*param = (option)
(I2C Sequence)
S - adrs W A - [param A...] - Sr - adrs R A - data_1 A... data_n A|N - [P]
S : Start condition
P : Stop condition
Sr: Repeated start condition
A : Ack
N : Nack
*/
static void c_i2c_read(mrb_vm *vm, mrb_value v[], int argc)
{
/*
Get parameter
*/
if( argc < 2 ) goto ERROR_PARAM;
if( mrbc_type(v[1]) != MRBC_TT_INTEGER ) goto ERROR_PARAM;
int i2c_adrs_7 = mrbc_integer(v[1]);
if( mrbc_type(v[2]) != MRBC_TT_INTEGER ) goto ERROR_PARAM;
int read_bytes = mrbc_integer(v[2]);
if( read_bytes < 0 ) goto ERROR_PARAM;
mrbc_value ret = mrbc_string_new(vm, 0, read_bytes);
/*
Start I2C communication
*/
// start condition
if( i2c_send_start() != 0 ) {
mrbc_raise(vm, 0, "i2c#read: start condition failed.");
goto STOP;
}
// send output data if specified.
if( argc > 2 ) {
// send I2C address. address + r/w bit=0 (write).
if( i2c_write_byte( i2c_adrs_7 << 1 ) != 0 ) {
mrbc_raise(vm, 0, "i2c#read: write address failed.");
goto STOP;
}
// send data
for( int i = 3; i <= argc; i++ ) {
int n_of_out_bytes = 0;
switch( i2c_write_mrbc_value( &v[i], &n_of_out_bytes ) ) {
case I2C_NACK:
if( i == argc ) break;
mrbc_raise(vm, 0, "i2c#read: NACK received.");
goto STOP;
case I2C_BUS_BUSY:
mrbc_raise(vm, 0, "i2c#read: send data failed.");
goto STOP;
case I2C_PARAM_ERROR:
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "i2c#read: parameter error.");
goto STOP;
}
}
// send repeated start
if( i2c_send_restart() != 0 ) {
mrbc_raise(vm, 0, "i2c#read: repeated start condition failed.");
goto STOP;
}
}
// send I2C address. address + r/w bit=1 (read).
if( i2c_write_byte( (i2c_adrs_7 << 1) | 1 ) != 0 ) {
mrbc_raise(vm, 0, "i2c#read: write address failed.");
goto STOP;
}
// receive data.
uint8_t *p = mrbc_string_cstr(&ret);
for( int i = read_bytes-1; i >= 0; i-- ) {
int res = i2c_read_byte( i == 0 );
if( res < 0 ) {
mrbc_raise(vm, 0, "i2c#read: read data failed.");
goto STOP;
}
*p++ = res;
}
*p = 0;
// send stop condition.
STOP:
i2c_send_stop();
goto RETURN;
ERROR_PARAM:
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "i2c#read: parameter error.");
RETURN:
SET_RETURN(ret);
}
//================================================================
/*! write transaction.
(mruby usage)
i2c.write( i2c_adrs_7, write_data, ... )
i2c_adrs_7 = Integer
write_data = String, Integer, or Array<Integer>
(I2C Sequence)
S - ADRS W A - data1 A... - P
S : Start condition
P : Stop condition
A : Ack
*/
static void c_i2c_write(mrb_vm *vm, mrb_value v[], int argc)
{
/*
Get parameter
*/
if( argc < 1 ) goto ERROR_PARAM;
if( mrbc_type(v[1]) != MRBC_TT_INTEGER ) goto ERROR_PARAM;
int i2c_adrs_7 = mrbc_integer(v[1]);
/*
Start I2C communication
*/
// start condition
if( i2c_send_start() != 0 ) {
mrbc_raise(vm, 0, "i2c#write: start condition failed.");
goto STOP;
}
// send I2C address. address + r/w bit=0 (write).
if( i2c_write_byte( i2c_adrs_7 << 1 ) != 0 ) {
mrbc_raise(vm, 0, "i2c#write: write address failed.");
goto STOP;
}
// send data
int n_of_out_bytes = 0;
for( int i = 2; i <= argc; i++ ) {
switch( i2c_write_mrbc_value( &v[i], &n_of_out_bytes ) ) {
case I2C_NACK:
if( i == argc ) break;
mrbc_raise(vm, 0, "i2c#write: NACK received.");
goto STOP;
case I2C_BUS_BUSY:
mrbc_raise(vm, 0, "i2c#write: send data failed.");
goto STOP;
case I2C_PARAM_ERROR:
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "i2c#write: parameter error.");
goto STOP;
}
}
// send stop condition.
STOP:
i2c_send_stop();
goto RETURN;
ERROR_PARAM:
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "i2c#write: parameter error.");
RETURN:
SET_RETURN( mrbc_integer_value(n_of_out_bytes) );
}
//================================================================
/*! Send START condition
(mruby usage)
i2c.send_start
@note LOW LEVEL function.
*/
static void c_i2c_send_start(mrb_vm *vm, mrb_value v[], int argc)
{
if( i2c_send_start() != 0 ) {
mrbc_raise(vm, 0, "start condition failed.");
}
}
//================================================================
/*! Send RESTART condition
(mruby usage)
i2c.send_restart
@note LOW LEVEL function.
*/
static void c_i2c_send_restart(mrb_vm *vm, mrb_value v[], int argc)
{
if( i2c_send_restart() != 0 ) {
mrbc_raise(vm, 0, "repeated start condition failed.");
}
}
//================================================================
/*! Send STOP condition
(mruby usage)
i2c.send_stop
@note LOW LEVEL function.
*/
static void c_i2c_send_stop(mrb_vm *vm, mrb_value v[], int argc)
{
if( i2c_send_stop() != 0 ) {
mrbc_raise(vm, 0, "stop condition failed.");
}
}
//================================================================
/*! Low level read
(mruby usage)
str = i2c.raw_read( read_bytes, ack_nack = false )
@note LOW LEVEL function.
*/
static void c_i2c_raw_read(mrb_vm *vm, mrb_value v[], int argc)
{
/*
Get parameter
*/
if( argc < 1 ) goto ERROR_PARAM;
if( mrbc_type(v[1]) != MRBC_TT_INTEGER ) goto ERROR_PARAM;
int read_bytes = mrbc_integer(v[1]);
if( read_bytes < 0 ) goto ERROR_PARAM;
int ack_nack = 0;
if( argc >= 2 ) {
switch( mrbc_type(v[2]) ) {
case MRBC_TT_TRUE: ack_nack = 1; break;
case MRBC_TT_FALSE: ack_nack = 0; break;
default:
goto ERROR_PARAM;
}
}
mrbc_value ret = mrbc_string_new(vm, 0, read_bytes);
// receive data.
uint8_t *p = mrbc_string_cstr(&ret);
for( int i = read_bytes-1; i >= 0; i-- ) {
int res = i2c_read_byte( i == 0 && ack_nack == 0);
if( res < 0 ) {
mrbc_raise(vm, 0, "read data failed.");
return;
}
*p++ = res;
}
*p = 0;
SET_RETURN(ret);
return;
ERROR_PARAM:
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "parameter error.");
}
//================================================================
/*! Low level write
(mruby usage)
i2c.raw_write( *param ) # -> Integer
@note LOW LEVEL function.
*/
static void c_i2c_raw_write(mrb_vm *vm, mrb_value v[], int argc)
{
// send data
int n_of_out_bytes = 0;
for( int i = 1; i <= argc; i++ ) {
switch( i2c_write_mrbc_value( &v[i], &n_of_out_bytes ) ) {
case I2C_NACK:
if( i == argc ) break;
mrbc_raise(vm, 0, "NACK received.");
return;
case I2C_BUS_BUSY:
mrbc_raise(vm, 0, "send data failed.");
return;
case I2C_PARAM_ERROR:
mrbc_raise(vm, MRBC_CLASS(ArgumentError), "parameter error.");
return;
}
}
SET_RETURN( mrbc_integer_value(n_of_out_bytes) );
}
//================================================================
/*! initialize
*/
void mrbc_init_class_i2c(void)
{
i2c_init();
mrbc_class *i2c = mrbc_define_class(0, "I2C", 0);
mrbc_define_method(0, i2c, "read", c_i2c_read);
mrbc_define_method(0, i2c, "write", c_i2c_write);
mrbc_define_method(0, i2c, "send_start", c_i2c_send_start);
mrbc_define_method(0, i2c, "send_restart", c_i2c_send_restart);
mrbc_define_method(0, i2c, "send_stop", c_i2c_send_stop);
mrbc_define_method(0, i2c, "raw_read", c_i2c_raw_read);
mrbc_define_method(0, i2c, "raw_write", c_i2c_raw_write);
}