@@ -35,7 +35,9 @@ THE SOFTWARE.
35
35
/* * Default constructor, uses default I2C address.
36
36
* @see TCA6424A_DEFAULT_ADDRESS
37
37
*/
38
- TCA6424A::TCA6424A () {
38
+ TCA6424A::TCA6424A (rtos::Mutex & wire_mtx)
39
+ : _wire_mtx{wire_mtx}
40
+ {
39
41
devAddr = TCA6424A_DEFAULT_ADDRESS;
40
42
}
41
43
@@ -45,8 +47,11 @@ TCA6424A::TCA6424A() {
45
47
* @see TCA6424A_ADDRESS_ADDR_LOW
46
48
* @see TCA6424A_ADDRESS_ADDR_HIGH
47
49
*/
48
- TCA6424A::TCA6424A (uint8_t address) {
49
- devAddr = address;
50
+ TCA6424A::TCA6424A (uint8_t address, rtos::Mutex & wire_mtx)
51
+ : devAddr{address}
52
+ , _wire_mtx{wire_mtx}
53
+ {
54
+
50
55
}
51
56
52
57
/* * Power on and prepare for general usage.
@@ -62,6 +67,7 @@ void TCA6424A::initialize() {
62
67
* @return True if connection is valid, false otherwise
63
68
*/
64
69
bool TCA6424A::testConnection () {
70
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
65
71
return I2Cdev::readBytes (devAddr, TCA6424A_RA_INPUT0, 3 , buffer) == 3 ;
66
72
}
67
73
@@ -71,6 +77,7 @@ bool TCA6424A::testConnection() {
71
77
* @return Pin logic level (0 or 1)
72
78
*/
73
79
bool TCA6424A::readPin (uint16_t pin) {
80
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
74
81
I2Cdev::readBit (devAddr, TCA6424A_RA_INPUT0 + (pin / 8 ), pin % 8 , buffer);
75
82
return buffer[0 ];
76
83
}
@@ -79,6 +86,7 @@ bool TCA6424A::readPin(uint16_t pin) {
79
86
* @return 8 pins' logic levels (0 or 1 for each pin)
80
87
*/
81
88
uint8_t TCA6424A::readBank (uint8_t bank) {
89
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
82
90
I2Cdev::readByte (devAddr, TCA6424A_RA_INPUT0 + bank, buffer);
83
91
return buffer[0 ];
84
92
}
@@ -87,6 +95,7 @@ uint8_t TCA6424A::readBank(uint8_t bank) {
87
95
* @param banks Container for all bank's pin values (P00-P27)
88
96
*/
89
97
void TCA6424A::readAll (uint8_t *banks) {
98
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
90
99
I2Cdev::readBytes (devAddr, TCA6424A_RA_INPUT0, 3 , banks);
91
100
}
92
101
/* * Get all pin logic levels from all banks.
@@ -96,6 +105,7 @@ void TCA6424A::readAll(uint8_t *banks) {
96
105
* @param bank2 Container for Bank 2's pin values (P20-P27)
97
106
*/
98
107
void TCA6424A::readAll (uint8_t *bank0, uint8_t *bank1, uint8_t *bank2) {
108
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
99
109
I2Cdev::readBytes (devAddr, TCA6424A_RA_INPUT0, 3 , buffer);
100
110
*bank0 = buffer[0 ];
101
111
*bank1 = buffer[1 ];
@@ -110,6 +120,7 @@ void TCA6424A::readAll(uint8_t *bank0, uint8_t *bank1, uint8_t *bank2) {
110
120
* @return Pin output setting (0 or 1)
111
121
*/
112
122
bool TCA6424A::getPinOutputLevel (uint16_t pin) {
123
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
113
124
I2Cdev::readBit (devAddr, TCA6424A_RA_OUTPUT0 + (pin / 8 ), pin % 8 , buffer);
114
125
return buffer[0 ];
115
126
}
@@ -120,6 +131,7 @@ bool TCA6424A::getPinOutputLevel(uint16_t pin) {
120
131
* @return 8 pins' output settings (0 or 1 for each pin)
121
132
*/
122
133
uint8_t TCA6424A::getBankOutputLevel (uint8_t bank) {
134
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
123
135
I2Cdev::readByte (devAddr, TCA6424A_RA_OUTPUT0 + bank, buffer);
124
136
return buffer[0 ];
125
137
}
@@ -128,6 +140,7 @@ uint8_t TCA6424A::getBankOutputLevel(uint8_t bank) {
128
140
* @param banks Container for all bank's pin values (P00-P27)
129
141
*/
130
142
void TCA6424A::getAllOutputLevel (uint8_t *banks) {
143
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
131
144
I2Cdev::readBytes (devAddr, TCA6424A_RA_OUTPUT0, 3 , banks);
132
145
}
133
146
/* * Get all pin output settings from all banks.
@@ -139,6 +152,7 @@ void TCA6424A::getAllOutputLevel(uint8_t *banks) {
139
152
* @param bank2 Container for Bank 2's pin values (P20-P27)
140
153
*/
141
154
void TCA6424A::getAllOutputLevel (uint8_t *bank0, uint8_t *bank1, uint8_t *bank2) {
155
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
142
156
I2Cdev::readBytes (devAddr, TCA6424A_RA_OUTPUT0, 3 , buffer);
143
157
*bank0 = buffer[0 ];
144
158
*bank1 = buffer[1 ];
@@ -149,19 +163,22 @@ void TCA6424A::getAllOutputLevel(uint8_t *bank0, uint8_t *bank1, uint8_t *bank2)
149
163
* @param value New pin output logic level (0 or 1)
150
164
*/
151
165
void TCA6424A::writePin (uint16_t pin, bool value) {
166
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
152
167
I2Cdev::writeBit (devAddr, TCA6424A_RA_OUTPUT0 + (pin / 8 ), pin % 8 , value);
153
168
}
154
169
/* * Set all OUTPUT pins' logic levels in one bank.
155
170
* @param bank Which bank to write (0/1/2 for P0*, P1*, P2* respectively)
156
171
* @param value New pins' output logic level (0 or 1 for each pin)
157
172
*/
158
173
void TCA6424A::writeBank (uint8_t bank, uint8_t value) {
174
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
159
175
I2Cdev::writeByte (devAddr, TCA6424A_RA_OUTPUT0 + bank, value);
160
176
}
161
177
/* * Set all OUTPUT pins' logic levels in all banks.
162
178
* @param banks All pins' new logic values (P00-P27) in 3-byte array
163
179
*/
164
180
void TCA6424A::writeAll (uint8_t *banks) {
181
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
165
182
I2Cdev::writeBytes (devAddr, TCA6424A_RA_OUTPUT0 | TCA6424A_AUTO_INCREMENT, 3 , banks);
166
183
}
167
184
/* * Set all OUTPUT pins' logic levels in all banks.
@@ -170,6 +187,7 @@ void TCA6424A::writeAll(uint8_t *banks) {
170
187
* @param bank2 Bank 2's new logic values (P20-P27)
171
188
*/
172
189
void TCA6424A::writeAll (uint8_t bank0, uint8_t bank1, uint8_t bank2) {
190
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
173
191
buffer[0 ] = bank0;
174
192
buffer[1 ] = bank1;
175
193
buffer[2 ] = bank2;
@@ -182,6 +200,7 @@ void TCA6424A::writeAll(uint8_t bank0, uint8_t bank1, uint8_t bank2) {
182
200
* @return Pin polarity setting (0 or 1)
183
201
*/
184
202
bool TCA6424A::getPinPolarity (uint16_t pin) {
203
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
185
204
I2Cdev::readBit (devAddr, TCA6424A_RA_POLARITY0 + (pin / 8 ), pin % 8 , buffer);
186
205
return buffer[0 ];
187
206
}
@@ -190,6 +209,7 @@ bool TCA6424A::getPinPolarity(uint16_t pin) {
190
209
* @return 8 pins' polarity settings (0 or 1 for each pin)
191
210
*/
192
211
uint8_t TCA6424A::getBankPolarity (uint8_t bank) {
212
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
193
213
I2Cdev::readByte (devAddr, TCA6424A_RA_POLARITY0 + bank, buffer);
194
214
return buffer[0 ];
195
215
}
@@ -198,6 +218,7 @@ uint8_t TCA6424A::getBankPolarity(uint8_t bank) {
198
218
* @param banks Container for all bank's pin values (P00-P27)
199
219
*/
200
220
void TCA6424A::getAllPolarity (uint8_t *banks) {
221
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
201
222
I2Cdev::readBytes (devAddr, TCA6424A_RA_POLARITY0, 3 , banks);
202
223
}
203
224
/* * Get all pin polarity (normal/inverted) settings from all banks.
@@ -207,6 +228,7 @@ void TCA6424A::getAllPolarity(uint8_t *banks) {
207
228
* @param bank2 Container for Bank 2's pin values (P20-P27)
208
229
*/
209
230
void TCA6424A::getAllPolarity (uint8_t *bank0, uint8_t *bank1, uint8_t *bank2) {
231
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
210
232
I2Cdev::readBytes (devAddr, TCA6424A_RA_POLARITY0, 3 , buffer);
211
233
*bank0 = buffer[0 ];
212
234
*bank1 = buffer[1 ];
@@ -217,19 +239,22 @@ void TCA6424A::getAllPolarity(uint8_t *bank0, uint8_t *bank1, uint8_t *bank2) {
217
239
* @param polarity New pin polarity setting (0 or 1)
218
240
*/
219
241
void TCA6424A::setPinPolarity (uint16_t pin, bool polarity) {
242
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
220
243
I2Cdev::writeBit (devAddr, TCA6424A_RA_POLARITY0 + (pin / 8 ), pin % 8 , polarity);
221
244
}
222
245
/* * Set all pin polarity (normal/inverted) settings in one bank.
223
246
* @param bank Which bank to write (0/1/2 for P0*, P1*, P2* respectively)
224
247
* @return New pins' polarity settings (0 or 1 for each pin)
225
248
*/
226
249
void TCA6424A::setBankPolarity (uint8_t bank, uint8_t polarity) {
250
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
227
251
I2Cdev::writeByte (devAddr, TCA6424A_RA_POLARITY0 + bank, polarity);
228
252
}
229
253
/* * Set all pin polarity (normal/inverted) settings in all banks.
230
254
* @param banks All pins' new logic values (P00-P27) in 3-byte array
231
255
*/
232
256
void TCA6424A::setAllPolarity (uint8_t *banks) {
257
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
233
258
I2Cdev::writeBytes (devAddr, TCA6424A_RA_POLARITY0 | TCA6424A_AUTO_INCREMENT, 3 , banks);
234
259
}
235
260
/* * Set all pin polarity (normal/inverted) settings in all banks.
@@ -238,6 +263,7 @@ void TCA6424A::setAllPolarity(uint8_t *banks) {
238
263
* @param bank2 Bank 2's new polarity values (P20-P27)
239
264
*/
240
265
void TCA6424A::setAllPolarity (uint8_t bank0, uint8_t bank1, uint8_t bank2) {
266
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
241
267
buffer[0 ] = bank0;
242
268
buffer[1 ] = bank1;
243
269
buffer[2 ] = bank2;
@@ -250,6 +276,7 @@ void TCA6424A::setAllPolarity(uint8_t bank0, uint8_t bank1, uint8_t bank2) {
250
276
* @return Pin direction setting (0 or 1)
251
277
*/
252
278
bool TCA6424A::getPinDirection (uint16_t pin) {
279
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
253
280
I2Cdev::readBit (devAddr, TCA6424A_RA_CONFIG0 + (pin / 8 ), pin % 8 , buffer);
254
281
return buffer[0 ];
255
282
}
@@ -258,6 +285,7 @@ bool TCA6424A::getPinDirection(uint16_t pin) {
258
285
* @return 8 pins' direction settings (0 or 1 for each pin)
259
286
*/
260
287
uint8_t TCA6424A::getBankDirection (uint8_t bank) {
288
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
261
289
I2Cdev::readByte (devAddr, TCA6424A_RA_CONFIG0 + bank, buffer);
262
290
return buffer[0 ];
263
291
}
@@ -266,6 +294,7 @@ uint8_t TCA6424A::getBankDirection(uint8_t bank) {
266
294
* @param banks Container for all bank's pin values (P00-P27)
267
295
*/
268
296
void TCA6424A::getAllDirection (uint8_t *banks) {
297
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
269
298
I2Cdev::readBytes (devAddr, TCA6424A_RA_CONFIG0, 3 , banks);
270
299
}
271
300
/* * Get all pin direction (I/O) settings from all banks.
@@ -275,6 +304,7 @@ void TCA6424A::getAllDirection(uint8_t *banks) {
275
304
* @param bank2 Container for Bank 2's pin values (P20-P27)
276
305
*/
277
306
void TCA6424A::getAllDirection (uint8_t *bank0, uint8_t *bank1, uint8_t *bank2) {
307
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
278
308
I2Cdev::readBytes (devAddr, TCA6424A_RA_CONFIG0, 3 , buffer);
279
309
*bank0 = buffer[0 ];
280
310
*bank1 = buffer[1 ];
@@ -285,19 +315,22 @@ void TCA6424A::getAllDirection(uint8_t *bank0, uint8_t *bank1, uint8_t *bank2) {
285
315
* @param direction Pin direction setting (0 or 1)
286
316
*/
287
317
void TCA6424A::setPinDirection (uint16_t pin, bool direction) {
318
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
288
319
I2Cdev::writeBit (devAddr, TCA6424A_RA_CONFIG0 + (pin / 8 ), pin % 8 , direction);
289
320
}
290
321
/* * Set all pin direction (I/O) settings in one bank.
291
322
* @param bank Which bank to read (0/1/2 for P0*, P1*, P2* respectively)
292
323
* @param direction New pins' direction settings (0 or 1 for each pin)
293
324
*/
294
325
void TCA6424A::setBankDirection (uint8_t bank, uint8_t direction) {
326
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
295
327
I2Cdev::writeByte (devAddr, TCA6424A_RA_CONFIG0 + bank, direction);
296
328
}
297
329
/* * Set all pin direction (I/O) settings in all banks.
298
330
* @param banks All pins' new direction values (P00-P27) in 3-byte array
299
331
*/
300
332
void TCA6424A::setAllDirection (uint8_t *banks) {
333
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
301
334
I2Cdev::writeBytes (devAddr, TCA6424A_RA_CONFIG0 | TCA6424A_AUTO_INCREMENT, 3 , banks);
302
335
}
303
336
/* * Set all pin direction (I/O) settings in all banks.
@@ -306,6 +339,7 @@ void TCA6424A::setAllDirection(uint8_t *banks) {
306
339
* @param bank2 Bank 2's new direction values (P20-P27)
307
340
*/
308
341
void TCA6424A::setAllDirection (uint8_t bank0, uint8_t bank1, uint8_t bank2) {
342
+ mbed::ScopedLock<rtos::Mutex> lock (_wire_mtx);
309
343
buffer[0 ] = bank0;
310
344
buffer[1 ] = bank1;
311
345
buffer[2 ] = bank2;
0 commit comments