Skip to content

Commit 793102d

Browse files
committed
Fix WriteRegister
1 parent ce584a8 commit 793102d

1 file changed

Lines changed: 12 additions & 26 deletions

File tree

src/dev/mpr121.h

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,18 @@ class Mpr121I2CTransport
6060
}
6161

6262
/** \return Did the transaction error? i.e. Return true if error, false if ok */
63-
bool Write(uint8_t *data, uint16_t size)
63+
bool ReadDataAtAddress(uint8_t address, uint8_t *data, uint16_t size)
6464
{
6565
return I2CHandle::Result::OK
66-
!= i2c_.TransmitBlocking(config_.dev_addr, data, size, 10);
66+
!= i2c_.ReadDataAtAddress(
67+
config_.dev_addr, address, 1, data, size, 10);
6768
}
6869

6970
/** \return Did the transaction error? i.e. Return true if error, false if ok */
70-
bool Read(uint8_t *data, uint16_t size)
71+
bool WriteDataAtAddress(uint8_t address, uint8_t *data, uint16_t size)
7172
{
7273
return I2CHandle::Result::OK
73-
!= i2c_.ReceiveBlocking(config_.dev_addr, data, size, 10);
74-
}
75-
76-
bool ReadDataAtAddress(uint16_t address, uint8_t *data, uint16_t size)
77-
{
78-
return I2CHandle::Result::OK
79-
!= i2c_.ReadDataAtAddress(
74+
!= i2c_.WriteDataAtAddress(
8075
config_.dev_addr, address, 1, data, size, 10);
8176
}
8277

@@ -262,36 +257,27 @@ class Mpr121
262257
{
263258
// MPR121 must be put in Stop Mode to write to most registers
264259
bool stop_required = true;
265-
266-
// first get the current set value of the MPR121_ECR register
267-
uint8_t ecr_reg = MPR121_ECR;
268-
uint8_t buff[2] = {ecr_reg, 0x00};
269-
270-
SetTransportErr(transport_.Write(buff, 1));
271-
272-
uint8_t ecr_backup;
273-
SetTransportErr(transport_.Read(&ecr_backup, 1));
274260
if((reg == MPR121_ECR) || ((0x73 <= reg) && (reg <= 0x7A)))
275261
{
276262
stop_required = false;
277263
}
278264

265+
uint8_t ecr_backup;
266+
279267
if(stop_required)
280268
{
269+
ecr_backup = ReadRegister8(MPR121_ECR);
270+
uint8_t zero = 0x00;
281271
// clear this register to set stop mode
282-
SetTransportErr(transport_.Write(buff, 2));
272+
SetTransportErr(transport_.WriteDataAtAddress(MPR121_ECR, &zero, 1));
283273
}
284274

285-
buff[0] = reg;
286-
buff[1] = value;
287-
SetTransportErr(transport_.Write(buff, 2));
275+
SetTransportErr(transport_.WriteDataAtAddress(reg, &value, 1));
288276

289277
if(stop_required)
290278
{
291279
// write back the previous set ECR settings
292-
buff[0] = ecr_reg;
293-
buff[1] = ecr_backup;
294-
SetTransportErr(transport_.Write(buff, 2));
280+
SetTransportErr(transport_.WriteDataAtAddress(MPR121_ECR, &ecr_backup, 1));
295281
}
296282
}
297283

0 commit comments

Comments
 (0)