diff --git a/STM32F1/cores/maple/libmaple/i2c.c b/STM32F1/cores/maple/libmaple/i2c.c index 9c93d3f..c4ed245 100644 --- a/STM32F1/cores/maple/libmaple/i2c.c +++ b/STM32F1/cores/maple/libmaple/i2c.c @@ -235,7 +235,7 @@ int32 i2c_master_xfer(i2c_dev *dev, i2c_enable_irq(dev, I2C_IRQ_EVENT); i2c_start_condition(dev); - + rc = wait_for_state_change(dev, I2C_STATE_XFER_DONE, timeout); if (rc < 0) { goto out; @@ -347,9 +347,20 @@ void _i2c_irq_handler(i2c_dev *dev) { * register. We should get another TXE interrupt * immediately to fill DR again. */ - if (msg->length != 1) { + if (msg->length > 1) { i2c_write(dev, msg->data[msg->xferred++]); - } + } else if (msg->length == 0) { /* We're just sending an address */ + i2c_stop_condition(dev); + /* + * Turn off event interrupts to keep BTF from firing until + * the end of the stop condition. Why on earth they didn't + * have a start/stop condition request clear BTF is beyond + * me. + */ + i2c_disable_irq(dev, I2C_IRQ_EVENT); + I2C_CRUMB(STOP_SENT, 0, 0); + dev->state = I2C_STATE_XFER_DONE; + } /* else we're just sending one byte */ } sr1 = sr2 = 0; } @@ -456,7 +467,7 @@ void _i2c_irq_handler(i2c_dev *dev) { void _i2c_irq_error_handler(i2c_dev *dev) { I2C_CRUMB(ERROR_ENTRY, dev->regs->SR1, dev->regs->SR2); - dev->error_flags = dev->regs->SR2 & (I2C_SR1_BERR | + dev->error_flags = dev->regs->SR1 & (I2C_SR1_BERR | I2C_SR1_ARLO | I2C_SR1_AF | I2C_SR1_OVR); diff --git a/STM32F1/libraries/Wire/HardWire.cpp b/STM32F1/libraries/Wire/HardWire.cpp index a835991..8a63183 100644 --- a/STM32F1/libraries/Wire/HardWire.cpp +++ b/STM32F1/libraries/Wire/HardWire.cpp @@ -40,11 +40,19 @@ uint8 HardWire::process() { int8 res = i2c_master_xfer(sel_hard, &itc_msg, 1, 0); - if (res != 0) { + if (res == I2C_ERROR_PROTOCOL) { + if (sel_hard->error_flags & I2C_SR1_AF) { /* NACK */ + res = (sel_hard->error_flags & I2C_SR1_ADDR ? ENACKADDR : + ENACKTRNS); + } else if (sel_hard->error_flags & I2C_SR1_OVR) { /* Over/Underrun */ + res = EDATA; + } else { /* Bus or Arbitration error */ + res = EOTHER; + } i2c_disable(sel_hard); i2c_master_enable(sel_hard, (I2C_BUS_RESET | dev_flags)); } - return 0; + return res; } // TODO: Add in Error Handling if devsel is out of range for other Maples @@ -56,7 +64,7 @@ HardWire::HardWire(uint8 dev_sel, uint8 flags) { } else { ASSERT(1); } - dev_flags=flags; + dev_flags = flags; } HardWire::~HardWire() { diff --git a/STM32F1/libraries/Wire/HardWire.h b/STM32F1/libraries/Wire/HardWire.h index e5bca33..6f137b8 100644 --- a/STM32F1/libraries/Wire/HardWire.h +++ b/STM32F1/libraries/Wire/HardWire.h @@ -67,5 +67,5 @@ public: void begin(uint8 = 0x00); }; - +extern HardWire HWire; #endif // _HARDWIRE_H_ diff --git a/STM32F1/libraries/Wire/WireBase.cpp b/STM32F1/libraries/Wire/WireBase.cpp index 36fb8c0..ff0dae3 100644 --- a/STM32F1/libraries/Wire/WireBase.cpp +++ b/STM32F1/libraries/Wire/WireBase.cpp @@ -61,11 +61,10 @@ void WireBase::beginTransmission(int slave_address) { uint8 WireBase::endTransmission(void) { uint8 retVal; - - if (tx_buf_overflow) { + if (tx_buf_overflow) { return EDATA; } - retVal=process();// Changed so that the return value from process is returned by this function see also the return line below + retVal = process();// Changed so that the return value from process is returned by this function see also the return line below tx_buf_idx = 0; tx_buf_overflow = false; return retVal;//SUCCESS; diff --git a/STM32F1/libraries/Wire/examples/i2c_scanner_hwire/i2c_scanner_hwire.ino b/STM32F1/libraries/Wire/examples/i2c_scanner_hwire/i2c_scanner_hwire.ino new file mode 100644 index 0000000..745cae6 --- /dev/null +++ b/STM32F1/libraries/Wire/examples/i2c_scanner_hwire/i2c_scanner_hwire.ino @@ -0,0 +1,76 @@ +// -------------------------------------- +// i2c_scanner +// +// Version 1 +// This program (or code that looks like it) +// can be found in many places. +// For example on the Arduino.cc forum. +// The original author is not know. +// Version 2, Juni 2012, Using Arduino 1.0.1 +// Adapted to be as simple as possible by Arduino.cc user Krodal +// Version 3, Feb 26 2013 +// V3 by louarnold +// Version 4, March 3, 2013, Using Arduino 1.0.3 +// by Arduino.cc user Krodal. +// Changes by louarnold removed. +// Scanning addresses changed from 0...127 to 1...119, +// according to the i2c scanner by Nick Gammon +// http://www.gammon.com.au/forum/?id=10896 +// Version 5, March 28, 2013 +// As version 4, but address scans now to 127. +// A sensor seems to use address 120. +// Version 6, August 1, 2015 +// Modified to support HardWire for STM32duino +// +// This sketch tests the standard 7-bit addresses +// Devices with higher bit address might not be seen properly. +// + +#include + +HardWire HWire(1, I2C_FAST_MODE); // I2c1 + +void setup() { + Serial.begin(115200); + HWire.begin(); + Serial.println("\nI2C Scanner"); +} + + +void loop() { + byte error, address; + int nDevices; + + Serial.println("Scanning..."); + + nDevices = 0; + for(address = 1; address < 127; address++) { + // The i2c_scanner uses the return value of + // the Write.endTransmisstion to see if + // a device did acknowledge to the address. + + HWire.beginTransmission(address); + error = HWire.endTransmission(); + + if (error == 0) { + Serial.print("I2C device found at address 0x"); + if (address < 16) + Serial.print("0"); + Serial.println(address, HEX); + + nDevices++; + } + else if (error == 4) { + Serial.print("Unknown error at address 0x"); + if (address < 16) + Serial.print("0"); + Serial.println(address, HEX); + } + } + if (nDevices == 0) + Serial.println("No I2C devices found"); + else + Serial.println("done"); + + delay(5000); // wait 5 seconds for next scan +} \ No newline at end of file diff --git a/STM32F1/libraries/Wire/examples/i2c_scanner_wire/i2c_scanner_wire.ino b/STM32F1/libraries/Wire/examples/i2c_scanner_wire/i2c_scanner_wire.ino new file mode 100644 index 0000000..30224d2 --- /dev/null +++ b/STM32F1/libraries/Wire/examples/i2c_scanner_wire/i2c_scanner_wire.ino @@ -0,0 +1,74 @@ +// -------------------------------------- +// i2c_scanner +// +// Version 1 +// This program (or code that looks like it) +// can be found in many places. +// For example on the Arduino.cc forum. +// The original author is not know. +// Version 2, Juni 2012, Using Arduino 1.0.1 +// Adapted to be as simple as possible by Arduino.cc user Krodal +// Version 3, Feb 26 2013 +// V3 by louarnold +// Version 4, March 3, 2013, Using Arduino 1.0.3 +// by Arduino.cc user Krodal. +// Changes by louarnold removed. +// Scanning addresses changed from 0...127 to 1...119, +// according to the i2c scanner by Nick Gammon +// http://www.gammon.com.au/forum/?id=10896 +// Version 5, March 28, 2013 +// As version 4, but address scans now to 127. +// A sensor seems to use address 120. +// +// This sketch tests the standard 7-bit addresses +// Devices with higher bit address might not be seen properly. +// + +#include + + +void setup() { + + Serial.begin(115200); + Wire.begin(); + Serial.println("\nI2C Scanner"); +} + + +void loop() { + byte error, address; + int nDevices; + + Serial.println("Scanning..."); + + nDevices = 0; + for(address = 1; address < 127; address++) { + // The i2c_scanner uses the return value of + // the Write.endTransmisstion to see if + // a device did acknowledge to the address. + + Wire.beginTransmission(address); + error = Wire.endTransmission(); + + if (error == 0) { + Serial.print("I2C device found at address 0x"); + if (address < 16) + Serial.print("0"); + Serial.println(address, HEX); + + nDevices++; + } + else if (error == 4) { + Serial.print("Unknown error at address 0x"); + if (address < 16) + Serial.print("0"); + Serial.println(address, HEX); + } + } + if (nDevices == 0) + Serial.println("No I2C devices found"); + else + Serial.println("done"); + + delay(5000); // wait 5 seconds for next scan +} \ No newline at end of file diff --git a/STM32F4/cores/maple/wirish.h b/STM32F4/cores/maple/wirish.h index 75d4aec..4aca74c 100644 --- a/STM32F4/cores/maple/wirish.h +++ b/STM32F4/cores/maple/wirish.h @@ -33,6 +33,7 @@ #ifndef _WIRISH_H_ #define _WIRISH_H_ +#include #include "libmaple.h" #include "wirish_types.h" diff --git a/tools/linux/dfu-util/dfu-prefix b/tools/linux/dfu-util/dfu-prefix index a7e1791..88e2d04 100755 Binary files a/tools/linux/dfu-util/dfu-prefix and b/tools/linux/dfu-util/dfu-prefix differ diff --git a/tools/linux/dfu-util/dfu-suffix b/tools/linux/dfu-util/dfu-suffix index 93b4697..e96df29 100755 Binary files a/tools/linux/dfu-util/dfu-suffix and b/tools/linux/dfu-util/dfu-suffix differ diff --git a/tools/linux/dfu-util/dfu-util b/tools/linux/dfu-util/dfu-util index dd496c5..e9a9599 100755 Binary files a/tools/linux/dfu-util/dfu-util and b/tools/linux/dfu-util/dfu-util differ diff --git a/tools/src/dfu-util/autogen.sh b/tools/src/dfu-util/autogen.sh index 5959508..e67aed3 100755 --- a/tools/src/dfu-util/autogen.sh +++ b/tools/src/dfu-util/autogen.sh @@ -1,2 +1,2 @@ -#! /bin/sh -autoreconf -v -i +#! /bin/sh +autoreconf -v -i