20
20
#include "error.h"
21
21
22
22
static const PinMap PinMap_I2C_SDA [] = {
23
- {PTE25 , I2C_0 , 5 },
24
- {PTC9 , I2C_0 , 2 },
25
- {PTE0 , I2C_0 , 6 },
26
23
{PTB1 , I2C_0 , 2 },
27
24
{PTB3 , I2C_0 , 2 },
28
- {PTC11 , I2C_0 , 2 },
29
- {PTC2 , I2C_0 , 2 },
30
- {PTA4 , I2C_0 , 2 },
31
25
{NC , NC , 0 }
32
26
};
33
27
34
28
static const PinMap PinMap_I2C_SCL [] = {
35
- {PTE24 , I2C_0 , 5 },
36
- {PTC8 , I2C_0 , 2 },
37
- {PTE1 , I2C_0 , 6 },
38
29
{PTB0 , I2C_0 , 2 },
39
30
{PTB2 , I2C_0 , 2 },
40
- {PTC10 , I2C_0 , 2 },
41
- {PTC1 , I2C_0 , 2 },
42
31
{NC , NC , 0 }
43
32
};
44
33
@@ -66,15 +55,11 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
66
55
I2CName i2c_sda = (I2CName )pinmap_peripheral (sda , PinMap_I2C_SDA );
67
56
I2CName i2c_scl = (I2CName )pinmap_peripheral (scl , PinMap_I2C_SCL );
68
57
obj -> i2c = (I2C_Type * )pinmap_merge (i2c_sda , i2c_scl );
69
- if ((int )obj -> i2c == NC ) {
58
+ if ((int )obj -> i2c == NC )
70
59
error ("I2C pin mapping failed" );
71
- }
72
60
73
- // enable power
74
- switch ((int )obj -> i2c ) {
75
- case I2C_0 : SIM -> SCGC5 |= 1 << 13 ; SIM -> SCGC4 |= 1 << 6 ; break ;
76
- //case I2C_1: SIM->SCGC5 |= 1 << 11; SIM->SCGC4 |= 1 << 7; break;
77
- }
61
+ SIM -> SCGC4 |= SIM_SCGC4_I2C0_MASK ;
62
+ SIM -> SCGC5 |= SIM_SCGC5_PORTB_MASK ;
78
63
79
64
// set default frequency at 100k
80
65
i2c_frequency (obj , 100000 );
@@ -84,12 +69,12 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
84
69
85
70
pinmap_pinout (sda , PinMap_I2C_SDA );
86
71
pinmap_pinout (scl , PinMap_I2C_SCL );
87
-
72
+
88
73
first_read = 1 ;
89
74
}
90
75
91
76
int i2c_start (i2c_t * obj ) {
92
- uint8_t temp ;
77
+ uint32_t temp ;
93
78
volatile int i ;
94
79
// if we are in the middle of a transaction
95
80
// activate the repeat_start flag
@@ -118,19 +103,20 @@ int i2c_stop(i2c_t *obj) {
118
103
// when there is no waiting time after a STOP.
119
104
// This wait is also included on the samples
120
105
// code provided with the freedom board
121
- for (n = 0 ; n < 100 ; n ++ ) __NOP ();
106
+ for (n = 0 ; n < 100 ; n ++ )
107
+ __NOP ();
122
108
first_read = 1 ;
123
109
return 0 ;
124
110
}
125
111
126
112
static int timeout_status_poll (i2c_t * obj , uint32_t mask ) {
127
113
uint32_t i , timeout = 1000 ;
128
-
114
+
129
115
for (i = 0 ; i < timeout ; i ++ ) {
130
116
if (obj -> i2c -> S & mask )
131
117
return 0 ;
132
118
}
133
-
119
+
134
120
return 1 ;
135
121
}
136
122
@@ -139,14 +125,14 @@ static int timeout_status_poll(i2c_t *obj, uint32_t mask) {
139
125
// 1: OK ack not received
140
126
// 2: failure
141
127
static int i2c_wait_end_tx_transfer (i2c_t * obj ) {
142
-
128
+
143
129
// wait for the interrupt flag
144
130
if (timeout_status_poll (obj , I2C_S_IICIF_MASK )) {
145
131
return 2 ;
146
132
}
147
-
133
+
148
134
obj -> i2c -> S |= I2C_S_IICIF_MASK ;
149
-
135
+
150
136
// wait transfer complete
151
137
if (timeout_status_poll (obj , I2C_S_TCF_MASK )) {
152
138
return 2 ;
@@ -164,9 +150,9 @@ static int i2c_wait_end_rx_transfer(i2c_t *obj) {
164
150
if (timeout_status_poll (obj , I2C_S_IICIF_MASK )) {
165
151
return 1 ;
166
152
}
167
-
153
+
168
154
obj -> i2c -> S |= I2C_S_IICIF_MASK ;
169
-
155
+
170
156
return 0 ;
171
157
}
172
158
@@ -301,33 +287,33 @@ void i2c_reset(i2c_t *obj) {
301
287
302
288
int i2c_byte_read (i2c_t * obj , int last ) {
303
289
char data ;
304
-
290
+
305
291
// set rx mode
306
292
obj -> i2c -> C1 &= ~I2C_C1_TX_MASK ;
307
-
293
+
308
294
if (first_read ) {
309
295
// first dummy read
310
296
i2c_do_read (obj , & data , 0 );
311
297
first_read = 0 ;
312
298
}
313
-
299
+
314
300
if (last ) {
315
301
// set tx mode
316
302
obj -> i2c -> C1 |= I2C_C1_TX_MASK ;
317
303
return obj -> i2c -> D ;
318
304
}
319
-
305
+
320
306
i2c_do_read (obj , & data , last );
321
-
307
+
322
308
return data ;
323
309
}
324
310
325
311
int i2c_byte_write (i2c_t * obj , int data ) {
326
312
first_read = 1 ;
327
-
313
+
328
314
// set tx mode
329
315
obj -> i2c -> C1 |= I2C_C1_TX_MASK ;
330
-
316
+
331
317
return !i2c_do_write (obj , (data & 0xFF ));
332
318
}
333
319
@@ -348,10 +334,10 @@ int i2c_slave_receive(i2c_t *obj) {
348
334
switch (obj -> i2c -> S ) {
349
335
// read addressed
350
336
case 0xE6 : return 1 ;
351
-
337
+
352
338
// write addressed
353
339
case 0xE2 : return 3 ;
354
-
340
+
355
341
default : return 0 ;
356
342
}
357
343
}
@@ -360,59 +346,59 @@ int i2c_slave_read(i2c_t *obj, char *data, int length) {
360
346
uint8_t dummy_read ;
361
347
uint8_t * ptr ;
362
348
int count ;
363
-
349
+
364
350
// set rx mode
365
351
obj -> i2c -> C1 &= ~I2C_C1_TX_MASK ;
366
-
352
+
367
353
// first dummy read
368
354
dummy_read = obj -> i2c -> D ;
369
355
if (i2c_wait_end_rx_transfer (obj )) {
370
356
return 0 ;
371
357
}
372
-
358
+
373
359
// read address
374
360
dummy_read = obj -> i2c -> D ;
375
361
if (i2c_wait_end_rx_transfer (obj )) {
376
362
return 0 ;
377
363
}
378
-
364
+
379
365
// read (length - 1) bytes
380
366
for (count = 0 ; count < (length - 1 ); count ++ ) {
381
367
data [count ] = obj -> i2c -> D ;
382
- if (i2c_wait_end_rx_transfer (obj )) {
368
+ if (i2c_wait_end_rx_transfer (obj )) {
383
369
return count ;
384
370
}
385
371
}
386
372
387
373
// read last byte
388
374
ptr = (length == 0 ) ? & dummy_read : (uint8_t * )& data [count ];
389
375
* ptr = obj -> i2c -> D ;
390
-
376
+
391
377
return (length ) ? (count + 1 ) : 0 ;
392
378
}
393
379
394
380
int i2c_slave_write (i2c_t * obj , const char * data , int length ) {
395
381
int i , count = 0 ;
396
-
382
+
397
383
// set tx mode
398
384
obj -> i2c -> C1 |= I2C_C1_TX_MASK ;
399
-
385
+
400
386
for (i = 0 ; i < length ; i ++ ) {
401
387
if (i2c_do_write (obj , data [count ++ ]) == 2 ) {
402
388
return i ;
403
389
}
404
390
}
405
-
391
+
406
392
// set rx mode
407
393
obj -> i2c -> C1 &= ~I2C_C1_TX_MASK ;
408
-
394
+
409
395
// dummy rx transfer needed
410
396
// otherwise the master cannot generate a stop bit
411
397
obj -> i2c -> D ;
412
398
if (i2c_wait_end_rx_transfer (obj ) == 2 ) {
413
399
return count ;
414
400
}
415
-
401
+
416
402
return count ;
417
403
}
418
404
0 commit comments