@@ -29,7 +29,7 @@ static inline int i2c_status(i2c_t *obj) {
29
29
30
30
// Wait until the Serial Interrupt (SI) is set
31
31
static int i2c_wait_SI (i2c_t * obj ) {
32
- int timeout = 0 ;
32
+ volatile int timeout = 0 ;
33
33
while (!(LPC_I2C0 -> STAT & (1 << 0 ))) {
34
34
timeout ++ ;
35
35
if (timeout > 100000 ) return -1 ;
@@ -41,25 +41,21 @@ static inline void i2c_interface_enable(i2c_t *obj) {
41
41
LPC_I2C0 -> CFG |= (1 << 0 );
42
42
}
43
43
44
- static inline void i2c_power_enable (i2c_t * obj ) {
44
+ void i2c_init (i2c_t * obj , PinName sda , PinName scl ) {
45
+ if ((sda != P0_23 ) | (scl != P0_22 )) {
46
+ error ("I2C pin mapping failed" );
47
+ }
48
+
45
49
// Enables clock for I2C0
46
- LPC_SYSCON -> SYSAHBCLKCTRL1 |= (1 <<13 );
47
- // LPC_SYSCON->PRESETCTRL1 &= ~(0x1<<13);
48
- LPC_SYSCON -> PRESETCTRL1 |= (0x1 <<13 );
49
- LPC_SYSCON -> PRESETCTRL1 &= ~(0x1 << 13 );
50
+ LPC_SYSCON -> SYSAHBCLKCTRL1 |= (1 << 13 );
50
51
51
- }
52
+ LPC_SYSCON -> PRESETCTRL1 |= (1 << 13 );
53
+ LPC_SYSCON -> PRESETCTRL1 &= ~(1 << 13 );
52
54
53
- void i2c_init (i2c_t * obj , PinName sda , PinName scl ) {
54
-
55
- // ピン定義の確認どうしよう…
56
-
57
-
58
- // enable power
59
- i2c_power_enable (obj );
60
55
// pin enable
61
56
LPC_SWM -> PINENABLE1 &= ~(0x3 << 3 );
62
- // set default frequency at 100k
57
+
58
+ // set default frequency at 100kHz
63
59
i2c_frequency (obj , 100000 );
64
60
i2c_interface_enable (obj );
65
61
}
@@ -76,7 +72,7 @@ inline int i2c_start(i2c_t *obj) {
76
72
}
77
73
78
74
inline int i2c_stop (i2c_t * obj ) {
79
- int timeout = 0 ;
75
+ volatile int timeout = 0 ;
80
76
81
77
LPC_I2C0 -> MSTCTL = (1 << 2 ) | (1 << 0 );
82
78
while ((LPC_I2C0 -> STAT & ((1 << 0 ) | (7 << 1 ))) != ((1 << 0 ) | (0 << 1 ))) {
@@ -107,14 +103,12 @@ static inline int i2c_do_read(i2c_t *obj, int last) {
107
103
LPC_I2C0 -> MSTCTL = (1 << 0 );
108
104
109
105
// return the data
110
- //return (I2C_DAT(obj) & 0xFF);
111
106
return (LPC_I2C0 -> MSTDAT & 0xFF );
112
107
}
113
108
114
109
void i2c_frequency (i2c_t * obj , int hz ) {
115
110
// No peripheral clock divider on the M0
116
111
uint32_t PCLK = SystemCoreClock ;
117
-
118
112
uint32_t clkdiv = PCLK / (hz * 4 ) - 1 ;
119
113
120
114
LPC_I2C0 -> DIV = clkdiv ;
@@ -123,59 +117,43 @@ void i2c_frequency(i2c_t *obj, int hz) {
123
117
124
118
int i2c_read (i2c_t * obj , int address , char * data , int length , int stop ) {
125
119
int count , status ;
126
- int timeout = 0 ;
127
120
128
121
i2c_start (obj );
129
122
130
- //status = i2c_do_write(obj, (address | 0x01), 1);
131
123
LPC_I2C0 -> MSTDAT = (address | 0x01 );
132
124
LPC_I2C0 -> MSTCTL |= 0x20 ;
133
- while (!(LPC_I2C0 -> STAT & (1 << 0 ))) {
134
- timeout ++ ;
135
- if (timeout > 100000 ) return -1 ;
136
- }
125
+ if (i2c_wait_SI (obj ) == -1 )
126
+ return -1 ;
127
+
137
128
status = ((LPC_I2C0 -> STAT >> 1 ) & (0x07 ));
138
-
139
129
if (status != 0x01 ) {
140
130
i2c_stop (obj );
141
131
return I2C_ERROR_NO_SLAVE ;
142
132
}
143
133
144
134
// Read in all except last byte
145
135
for (count = 0 ; count < (length - 1 ); count ++ ) {
146
- //int value = i2c_do_read(obj, 0);
147
- while (!(LPC_I2C0 -> STAT & (1 << 0 ))) {
148
- timeout ++ ;
149
- if (timeout > 100000 ) return -1 ;
150
- }
151
- if (!0 )
152
- LPC_I2C0 -> MSTCTL = (1 << 0 );
153
- data [count ] = (LPC_I2C0 -> MSTDAT & 0xFF );
154
- //
155
- status = ((LPC_I2C0 -> STAT >> 1 ) & (0x07 ));
136
+ if (i2c_wait_SI (obj ) == -1 )
137
+ return -1 ;
138
+ LPC_I2C0 -> MSTCTL = (1 << 0 );
139
+ data [count ] = (LPC_I2C0 -> MSTDAT & 0xFF );
140
+ status = ((LPC_I2C0 -> STAT >> 1 ) & (0x07 ));
156
141
if (status != 0x00 ) {
157
142
i2c_stop (obj );
158
143
return count ;
159
144
}
160
- //data[count] = (char) value;
161
145
}
162
146
163
147
// read in last byte
164
- //int value = i2c_do_read(obj, 1);
165
- while (!(LPC_I2C0 -> STAT & (1 << 0 ))) {
166
- timeout ++ ;
167
- if (timeout > 100000 ) return -1 ;
168
- }
148
+ if (i2c_wait_SI (obj ) == -1 )
149
+ return -1 ;
150
+
169
151
data [count ] = (LPC_I2C0 -> MSTDAT & 0xFF );
170
- //
171
152
status = i2c_status (obj );
172
153
if (status != 0x01 ) {
173
154
i2c_stop (obj );
174
155
return length - 1 ;
175
156
}
176
-
177
- //data[count] = (char) value;
178
-
179
157
// If not repeated start, send stop.
180
158
if (stop ) {
181
159
i2c_stop (obj );
@@ -188,35 +166,27 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
188
166
189
167
int i2c_write (i2c_t * obj , int address , const char * data , int length , int stop ) {
190
168
int i , status ;
191
- int timeout = 0 ;
192
169
193
170
i2c_start (obj );
194
171
195
- //status = i2c_do_write(obj, (address & 0xFE), 1);
196
172
LPC_I2C0 -> MSTDAT = (address & 0xFE );
197
173
LPC_I2C0 -> MSTCTL |= 0x20 ;
198
- // wait and return status
199
- while (!(LPC_I2C0 -> STAT & (1 << 0 ))) {
200
- timeout ++ ;
201
- if (timeout > 100000 ) return -1 ;
202
- }
174
+ if (i2c_wait_SI (obj ) == -1 )
175
+ return -1 ;
176
+
203
177
status = ((LPC_I2C0 -> STAT >> 1 ) & (0x07 ));
204
-
205
178
if (status != 0x02 ) {
206
179
i2c_stop (obj );
207
180
return I2C_ERROR_NO_SLAVE ;
208
181
}
209
182
210
183
for (i = 0 ; i < length ; i ++ ) {
211
- //status = i2c_do_write(obj, data[i], 0);
212
184
LPC_I2C0 -> MSTDAT = data [i ];
213
185
LPC_I2C0 -> MSTCTL = (1 << 0 );
214
- // wait and return status
215
- while (!(LPC_I2C0 -> STAT & (1 << 0 ))) {
216
- timeout ++ ;
217
- if (timeout > 100000 ) return -1 ;
218
- }
219
- status = ((LPC_I2C0 -> STAT >> 1 ) & (0x07 ));
186
+ if (i2c_wait_SI (obj ) == -1 )
187
+ return -1 ;
188
+
189
+ status = ((LPC_I2C0 -> STAT >> 1 ) & (0x07 ));
220
190
if (status != 0x02 ) {
221
191
i2c_stop (obj );
222
192
return i ;
@@ -242,17 +212,9 @@ int i2c_byte_read(i2c_t *obj, int last) {
242
212
}
243
213
244
214
int i2c_byte_write (i2c_t * obj , int data ) {
245
- int ack ;
246
- int status = i2c_do_write (obj , (data & 0xFF ), 0 );
247
-
248
- switch (status ) {
249
- case 2 :
250
- ack = 1 ;
251
- break ;
252
- default :
253
- ack = 0 ;
254
- break ;
215
+ if (i2c_do_write (obj , (data & 0xFF ), 0 ) == 2 ) {
216
+ return 1 ;
217
+ } else {
218
+ return 0 ;
255
219
}
256
-
257
- return ack ;
258
220
}
0 commit comments