21
21
#include "pinmap.h"
22
22
#include "gpio_include.h"
23
23
24
+ #if DEVICE_I2C_ASYNCH
25
+ #define I2C_S (obj ) (struct i2c_s *) (&((obj)->i2c))
26
+ #else
27
+ #define I2C_S (obj ) (struct i2c_s *) (obj)
28
+ #endif
29
+
24
30
static const PinMap PinMap_I2C_SDA [] = {
25
31
{PC1 , I2C_0 , PIN_DATA (1 , 2 )},
26
32
{PA5 , I2C_1 , PIN_DATA (1 , 2 )},
@@ -56,7 +62,9 @@ static void i2c_start_bit(i2c_t *obj);
56
62
// Initialize the I2C peripheral. It sets the default parameters for I2C
57
63
void i2c_init (i2c_t * obj , PinName sda , PinName scl )
58
64
{
59
- MBED_ASSERT (obj != NULL );
65
+ struct i2c_s * obj_s = I2C_S (obj );
66
+ MBED_ASSERT (obj_s != NULL );
67
+
60
68
I2CName i2c_sda = (I2CName )pinmap_peripheral (sda , PinMap_I2C_SDA );
61
69
I2CName i2c_scl = (I2CName )pinmap_peripheral (scl , PinMap_I2C_SCL );
62
70
I2CName i2c_name = (I2CName )pinmap_merge (i2c_sda , i2c_scl );
@@ -66,21 +74,21 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
66
74
case I2C_0 :
67
75
TSB_CG_FSYSENB_IPENB11 = ENABLE ;
68
76
TSB_CG_FSYSENA_IPENA02 = ENABLE ;
69
- obj -> i2c = TSB_I2C0 ;
77
+ obj_s -> i2c = TSB_I2C0 ;
70
78
break ;
71
79
case I2C_1 :
72
80
TSB_CG_FSYSENB_IPENB12 = ENABLE ;
73
81
TSB_CG_FSYSENA_IPENA00 = ENABLE ;
74
- obj -> i2c = TSB_I2C1 ;
82
+ obj_s -> i2c = TSB_I2C1 ;
75
83
break ;
76
84
case I2C_2 :
77
85
TSB_CG_FSYSENB_IPENB13 = ENABLE ;
78
86
TSB_CG_FSYSENA_IPENA10 = ENABLE ;
79
- obj -> i2c = TSB_I2C2 ;
87
+ obj_s -> i2c = TSB_I2C2 ;
80
88
case I2C_3 :
81
89
TSB_CG_FSYSENB_IPENB14 = ENABLE ;
82
90
TSB_CG_FSYSENA_IPENA15 = ENABLE ;
83
- obj -> i2c = TSB_I2C3 ;
91
+ obj_s -> i2c = TSB_I2C3 ;
84
92
break ;
85
93
default :
86
94
error ("I2C is not available" );
@@ -97,15 +105,16 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl)
97
105
98
106
i2c_reset (obj );
99
107
i2c_frequency (obj , 100000 );
100
- obj -> i2c -> CR2 = (I2CxCR2_I2CM_ENABLE | I2CxCR2_TRX | I2CxCR2_PIN_CLEAR |
108
+ obj_s -> i2c -> CR2 = (I2CxCR2_I2CM_ENABLE | I2CxCR2_TRX | I2CxCR2_PIN_CLEAR |
101
109
I2CxCR2_INIT );
102
- obj -> i2c -> OP = I2CxOP_INIT ;
103
- obj -> i2c -> IE = I2CxIE_CLEAR ;
110
+ obj_s -> i2c -> OP = I2CxOP_INIT ;
111
+ obj_s -> i2c -> IE = I2CxIE_CLEAR ;
104
112
}
105
113
106
114
// Configure the I2C frequency
107
115
void i2c_frequency (i2c_t * obj , int hz )
108
116
{
117
+ struct i2c_s * obj_s = I2C_S (obj );
109
118
uint64_t sck , tmp_sck ;
110
119
uint64_t prsck , tmp_prsck ;
111
120
uint64_t fscl , tmp_fscl ;
@@ -134,8 +143,8 @@ void i2c_frequency(i2c_t *obj, int hz)
134
143
clk .prsck = (tmp_prsck < 32 ) ? (uint32_t )(tmp_prsck - 1 ) : 0 ;
135
144
}
136
145
137
- obj -> i2c -> CR1 = (I2CxCR1_ACK | clk .sck );
138
- obj -> i2c -> PRS = (I2CxPRS_PRCK & clk .prsck );
146
+ obj_s -> i2c -> CR1 = (I2CxCR1_ACK | clk .sck );
147
+ obj_s -> i2c -> PRS = (I2CxPRS_PRCK & clk .prsck );
139
148
}
140
149
141
150
int i2c_start (i2c_t * obj )
@@ -146,10 +155,11 @@ int i2c_start(i2c_t *obj)
146
155
147
156
int i2c_stop (i2c_t * obj )
148
157
{
158
+ struct i2c_s * obj_s = I2C_S (obj );
149
159
uint32_t timeout = I2C_TIMEOUT ;
150
160
151
- obj -> i2c -> CR2 = I2CxCR2_STOP_CONDITION ;
152
- while ((obj -> i2c -> SR & I2CxSR_BB ) == I2CxSR_BB ) {
161
+ obj_s -> i2c -> CR2 = I2CxCR2_STOP_CONDITION ;
162
+ while ((obj_s -> i2c -> SR & I2CxSR_BB ) == I2CxSR_BB ) {
153
163
if (timeout == 0 )
154
164
break ;
155
165
timeout -- ;
@@ -159,8 +169,9 @@ int i2c_stop(i2c_t *obj)
159
169
160
170
void i2c_reset (i2c_t * obj )
161
171
{
162
- obj -> i2c -> CR2 = I2CxCR2_SWRES_10 ;
163
- obj -> i2c -> CR2 = I2CxCR2_SWRES_01 ;
172
+ struct i2c_s * obj_s = I2C_S (obj );
173
+ obj_s -> i2c -> CR2 = I2CxCR2_SWRES_10 ;
174
+ obj_s -> i2c -> CR2 = I2CxCR2_SWRES_01 ;
164
175
}
165
176
166
177
int i2c_read (i2c_t * obj , int address , char * data , int length , int stop )
@@ -218,41 +229,43 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
218
229
219
230
int i2c_byte_read (i2c_t * obj , int last )
220
231
{
232
+ struct i2c_s * obj_s = I2C_S (obj );
221
233
int32_t result ;
222
234
223
- obj -> i2c -> ST = I2CxST_CLEAR ;
235
+ obj_s -> i2c -> ST = I2CxST_CLEAR ;
224
236
if (last ) {
225
- obj -> i2c -> OP |= I2CxOP_MFACK ;
237
+ obj_s -> i2c -> OP |= I2CxOP_MFACK ;
226
238
} else {
227
- obj -> i2c -> OP &= ~I2CxOP_MFACK ;
239
+ obj_s -> i2c -> OP &= ~I2CxOP_MFACK ;
228
240
}
229
- obj -> i2c -> DBR = (0 & I2CxDBR_DB_MASK );
241
+ obj_s -> i2c -> DBR = (0 & I2CxDBR_DB_MASK );
230
242
if (wait_status (obj ) < 0 ) {
231
243
result = -1 ;
232
244
} else {
233
- result = (int32_t )(obj -> i2c -> DBR & I2CxDBR_DB_MASK );
245
+ result = (int32_t )(obj_s -> i2c -> DBR & I2CxDBR_DB_MASK );
234
246
}
235
247
return (result );
236
248
}
237
249
238
250
int i2c_byte_write (i2c_t * obj , int data )
239
251
{
252
+ struct i2c_s * obj_s = I2C_S (obj );
240
253
int32_t result ;
241
254
242
- obj -> i2c -> ST = I2CxST_CLEAR ;
255
+ obj_s -> i2c -> ST = I2CxST_CLEAR ;
243
256
if (start_flag == 1 ) {
244
- obj -> i2c -> DBR = (data & I2CxDBR_DB_MASK );
257
+ obj_s -> i2c -> DBR = (data & I2CxDBR_DB_MASK );
245
258
i2c_start_bit (obj );
246
259
start_flag = 0 ;
247
260
} else {
248
- obj -> i2c -> DBR = (data & I2CxDBR_DB_MASK );
261
+ obj_s -> i2c -> DBR = (data & I2CxDBR_DB_MASK );
249
262
}
250
263
251
264
if (wait_status (obj ) < 0 ) {
252
265
return (-1 );
253
266
}
254
267
255
- if (!((obj -> i2c -> SR & I2CxSR_LRB ) == I2CxSR_LRB )) {
268
+ if (!((obj_s -> i2c -> SR & I2CxSR_LRB ) == I2CxSR_LRB )) {
256
269
result = 1 ;
257
270
} else {
258
271
result = 0 ;
@@ -262,21 +275,23 @@ int i2c_byte_write(i2c_t *obj, int data)
262
275
263
276
static void i2c_start_bit (i2c_t * obj ) // Send START command
264
277
{
278
+ struct i2c_s * obj_s = I2C_S (obj );
265
279
uint32_t opreg ;
266
- opreg = obj -> i2c -> OP ;
280
+ opreg = obj_s -> i2c -> OP ;
267
281
opreg &= ~(I2CxOP_RSTA | I2CxOP_SREN );
268
- if ((obj -> i2c -> SR & I2CxSR_BB )) {
282
+ if ((obj_s -> i2c -> SR & I2CxSR_BB )) {
269
283
opreg |= I2CxOP_SREN ;
270
284
}
271
- obj -> i2c -> OP = opreg ;
272
- obj -> i2c -> CR2 |= I2CxCR2_START_CONDITION ;
285
+ obj_s -> i2c -> OP = opreg ;
286
+ obj_s -> i2c -> CR2 |= I2CxCR2_START_CONDITION ;
273
287
}
274
288
275
289
static int32_t wait_status (i2c_t * p_obj )
276
290
{
291
+ struct i2c_s * p_obj_s = I2C_S (p_obj );
277
292
volatile int32_t timeout ;
278
293
timeout = I2C_TIMEOUT ;
279
- while (!((p_obj -> i2c -> ST & I2CxST_I2C ) == I2CxST_I2C )) {
294
+ while (!((p_obj_s -> i2c -> ST & I2CxST_I2C ) == I2CxST_I2C )) {
280
295
if ((timeout -- ) == 0 ) {
281
296
return (-1 );
282
297
}
@@ -286,32 +301,34 @@ static int32_t wait_status(i2c_t *p_obj)
286
301
287
302
void i2c_slave_mode (i2c_t * obj , int enable_slave )
288
303
{
304
+ struct i2c_s * obj_s = I2C_S (obj );
289
305
if (enable_slave ) {
290
- obj -> i2c -> OP = I2CxOP_SLAVE_INIT ;
291
- obj -> i2c -> CR1 = (I2CxCR1_ACK | clk .sck );
292
- obj -> i2c -> CR2 = (I2CxCR2_INIT | I2CxCR2_PIN_CLEAR );
293
- obj -> i2c -> PRS = (I2CxPRS_PRCK & clk .prsck );
294
- obj -> i2c -> AR = (obj -> address & I2CAR_SA_MASK );
295
- obj -> i2c -> IE = I2CxIE_INTI2C ;
306
+ obj_s -> i2c -> OP = I2CxOP_SLAVE_INIT ;
307
+ obj_s -> i2c -> CR1 = (I2CxCR1_ACK | clk .sck );
308
+ obj_s -> i2c -> CR2 = (I2CxCR2_INIT | I2CxCR2_PIN_CLEAR );
309
+ obj_s -> i2c -> PRS = (I2CxPRS_PRCK & clk .prsck );
310
+ obj_s -> i2c -> AR = (obj_s -> address & I2CAR_SA_MASK );
311
+ obj_s -> i2c -> IE = I2CxIE_INTI2C ;
296
312
} else {
297
313
i2c_reset (obj );
298
- obj -> i2c -> CR2 = (I2CxCR2_I2CM_ENABLE | I2CxCR2_TRX | I2CxCR2_PIN_CLEAR |
314
+ obj_s -> i2c -> CR2 = (I2CxCR2_I2CM_ENABLE | I2CxCR2_TRX | I2CxCR2_PIN_CLEAR |
299
315
I2CxCR2_INIT );
300
- obj -> i2c -> OP = I2CxOP_INIT ;
301
- obj -> i2c -> CR1 = (I2CxCR1_ACK | clk .sck );
302
- obj -> i2c -> PRS = (I2CxPRS_PRCK & clk .prsck );
303
- NVIC_DisableIRQ (obj -> IRQn );
304
- NVIC_ClearPendingIRQ (obj -> IRQn );
305
- obj -> i2c -> ST = I2CxST_CLEAR ;
316
+ obj_s -> i2c -> OP = I2CxOP_INIT ;
317
+ obj_s -> i2c -> CR1 = (I2CxCR1_ACK | clk .sck );
318
+ obj_s -> i2c -> PRS = (I2CxPRS_PRCK & clk .prsck );
319
+ NVIC_DisableIRQ (obj_s -> IRQn );
320
+ NVIC_ClearPendingIRQ (obj_s -> IRQn );
321
+ obj_s -> i2c -> ST = I2CxST_CLEAR ;
306
322
}
307
323
}
308
324
309
325
int i2c_slave_receive (i2c_t * obj )
310
326
{
327
+ struct i2c_s * obj_s = I2C_S (obj );
311
328
int32_t result = I2C_NO_DATA ;
312
329
313
- if ((obj -> i2c -> ST & I2CxST_I2C ) && (obj -> i2c -> OP & I2CxOP_SAST )) {
314
- if ((obj -> i2c -> SR & I2CxSR_TRX ) == I2CxSR_TRX ) {
330
+ if ((obj_s -> i2c -> ST & I2CxST_I2C ) && (obj_s -> i2c -> OP & I2CxOP_SAST )) {
331
+ if ((obj_s -> i2c -> SR & I2CxSR_TRX ) == I2CxSR_TRX ) {
315
332
result = I2C_READ_ADDRESSED ;
316
333
} else {
317
334
result = I2C_WRITE_ADDRESSED ;
@@ -322,11 +339,12 @@ int i2c_slave_receive(i2c_t *obj)
322
339
323
340
int i2c_slave_read (i2c_t * obj , char * data , int length )
324
341
{
342
+ struct i2c_s * obj_s = I2C_S (obj );
325
343
int32_t count = 0 ;
326
344
327
345
while (count < length ) {
328
346
int32_t pdata = i2c_byte_read (obj , ((count < (length - 1 )) ? 0 : 1 ));
329
- if ((obj -> i2c -> SR & I2CxSR_TRX )) {
347
+ if ((obj_s -> i2c -> SR & I2CxSR_TRX )) {
330
348
return (count );
331
349
} else {
332
350
if (pdata < 0 ) {
@@ -354,7 +372,8 @@ int i2c_slave_write(i2c_t *obj, const char *data, int length)
354
372
355
373
void i2c_slave_address (i2c_t * obj , int idx , uint32_t address , uint32_t mask )
356
374
{
357
- obj -> address = address & I2CAR_SA_MASK ;
375
+ struct i2c_s * obj_s = I2C_S (obj );
376
+ obj_s -> address = address & I2CAR_SA_MASK ;
358
377
i2c_slave_mode (obj ,1 );
359
378
}
360
379
0 commit comments