Skip to content

Commit 3b8b7a7

Browse files
committed
Bugfix for HardWire/I2C + configured/compiled dfu-util to work on Linux again
- Fixed 0 byte payload bug, stopping HardWire from sending only addresses to scan for devices - Fixed I2C bug accessing wrong status register for error flags - Improved Hardwire endTransmission() return flags to correspond with the actual I2C failure status - Removed dos endings from dfu-util autogen script breaking compilation
1 parent 0ec837f commit 3b8b7a7

8 files changed

Lines changed: 28 additions & 13 deletions

File tree

STM32F1/cores/maple/libmaple/i2c.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ int32 i2c_master_xfer(i2c_dev *dev,
235235

236236
i2c_enable_irq(dev, I2C_IRQ_EVENT);
237237
i2c_start_condition(dev);
238-
238+
239239
rc = wait_for_state_change(dev, I2C_STATE_XFER_DONE, timeout);
240240
if (rc < 0) {
241241
goto out;
@@ -347,9 +347,20 @@ void _i2c_irq_handler(i2c_dev *dev) {
347347
* register. We should get another TXE interrupt
348348
* immediately to fill DR again.
349349
*/
350-
if (msg->length != 1) {
350+
if (msg->length > 1) {
351351
i2c_write(dev, msg->data[msg->xferred++]);
352-
}
352+
} else if (msg->length == 0){ /* We're just sending an address */
353+
i2c_stop_condition(dev);
354+
/*
355+
* Turn off event interrupts to keep BTF from firing until
356+
* the end of the stop condition. Why on earth they didn't
357+
* have a start/stop condition request clear BTF is beyond
358+
* me.
359+
*/
360+
i2c_disable_irq(dev, I2C_IRQ_EVENT);
361+
I2C_CRUMB(STOP_SENT, 0, 0);
362+
dev->state = I2C_STATE_XFER_DONE;
363+
} /* else we're just sending one byte */
353364
}
354365
sr1 = sr2 = 0;
355366
}
@@ -456,7 +467,7 @@ void _i2c_irq_handler(i2c_dev *dev) {
456467
void _i2c_irq_error_handler(i2c_dev *dev) {
457468
I2C_CRUMB(ERROR_ENTRY, dev->regs->SR1, dev->regs->SR2);
458469

459-
dev->error_flags = dev->regs->SR2 & (I2C_SR1_BERR |
470+
dev->error_flags = dev->regs->SR1 & (I2C_SR1_BERR |
460471
I2C_SR1_ARLO |
461472
I2C_SR1_AF |
462473
I2C_SR1_OVR);

STM32F1/libraries/Wire/HardWire.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@
4040

4141
uint8 HardWire::process() {
4242
int8 res = i2c_master_xfer(sel_hard, &itc_msg, 1, 0);
43-
if (res != 0) {
43+
if (res == I2C_ERROR_PROTOCOL) {
44+
if (sel_hard->error_flags & I2C_SR1_AF)
45+
res = (sel_hard->error_flags & I2C_SR1_ADDR ? ENACKADDR :
46+
ENACKTRNS);
47+
else if (sel_hard->error_flags & I2C_SR1_OVR) res = EDATA;
48+
else res = EOTHER;
4449
i2c_disable(sel_hard);
4550
i2c_master_enable(sel_hard, (I2C_BUS_RESET | dev_flags));
4651
}
47-
return 0;
52+
return res;
4853
}
4954

5055
// TODO: Add in Error Handling if devsel is out of range for other Maples
@@ -56,7 +61,7 @@ HardWire::HardWire(uint8 dev_sel, uint8 flags) {
5661
} else {
5762
ASSERT(1);
5863
}
59-
dev_flags=flags;
64+
dev_flags = flags;
6065
}
6166

6267
HardWire::~HardWire() {

STM32F1/libraries/Wire/HardWire.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,5 @@ class HardWire : public WireBase {
6767

6868
void begin(uint8 = 0x00);
6969
};
70-
70+
extern HardWire HWire;
7171
#endif // _HARDWIRE_H_

STM32F1/libraries/Wire/WireBase.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ void WireBase::beginTransmission(int slave_address) {
6161

6262
uint8 WireBase::endTransmission(void) {
6363
uint8 retVal;
64-
65-
if (tx_buf_overflow) {
64+
if (tx_buf_overflow) {
6665
return EDATA;
6766
}
68-
retVal=process();// Changed so that the return value from process is returned by this function see also the return line below
67+
retVal = process();// Changed so that the return value from process is returned by this function see also the return line below
6968
tx_buf_idx = 0;
7069
tx_buf_overflow = false;
7170
return retVal;//SUCCESS;

tools/linux/dfu-util/dfu-prefix

12.6 KB
Binary file not shown.

tools/linux/dfu-util/dfu-suffix

12.5 KB
Binary file not shown.

tools/linux/dfu-util/dfu-util

25.9 KB
Binary file not shown.

tools/src/dfu-util/autogen.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#! /bin/sh
2-
autoreconf -v -i
1+
#! /bin/sh
2+
autoreconf -v -i

0 commit comments

Comments
 (0)