summaryrefslogtreecommitdiff
path: root/ucoo/hal/i2c
diff options
context:
space:
mode:
authorNicolas Schodet2015-06-03 13:46:44 +0200
committerNicolas Schodet2019-10-07 00:44:44 +0200
commit574480980dcffbfa7cd6dbe71e88253eb4c9feba (patch)
tree5f1c1261341a7c2a56de01d8c6d0815b164be8ba /ucoo/hal/i2c
parentd415a0646e874848cd06482c5d57916cca5cff4a (diff)
Rename ucoolib modules directory from ucoolib to ucoo
Diffstat (limited to 'ucoo/hal/i2c')
-rw-r--r--ucoo/hal/i2c/Config5
-rw-r--r--ucoo/hal/i2c/Module1
-rw-r--r--ucoo/hal/i2c/i2c.hh36
-rw-r--r--ucoo/hal/i2c/i2c.host.cc213
-rw-r--r--ucoo/hal/i2c/i2c.host.hh66
-rw-r--r--ucoo/hal/i2c/i2c_hard.stm32.cc396
-rw-r--r--ucoo/hal/i2c/i2c_hard.stm32.hh91
-rw-r--r--ucoo/hal/i2c/i2c_slave_data_buffer.cc80
-rw-r--r--ucoo/hal/i2c/i2c_slave_data_buffer.hh71
-rw-r--r--ucoo/hal/i2c/test/Makefile9
-rw-r--r--ucoo/hal/i2c/test/hub.py6
-rw-r--r--ucoo/hal/i2c/test/test_i2c.cc226
12 files changed, 1200 insertions, 0 deletions
diff --git a/ucoo/hal/i2c/Config b/ucoo/hal/i2c/Config
new file mode 100644
index 0000000..3871f5c
--- /dev/null
+++ b/ucoo/hal/i2c/Config
@@ -0,0 +1,5 @@
+[hal/i2c]
+# Size of slave buffer, used for both reception and transmission.
+slave_buffer_size = 64
+# Activate debug trace.
+trace = false
diff --git a/ucoo/hal/i2c/Module b/ucoo/hal/i2c/Module
new file mode 100644
index 0000000..ae93f22
--- /dev/null
+++ b/ucoo/hal/i2c/Module
@@ -0,0 +1 @@
+hal_i2c_SOURCES := i2c.host.cc i2c_slave_data_buffer.cc i2c_hard.stm32.cc
diff --git a/ucoo/hal/i2c/i2c.hh b/ucoo/hal/i2c/i2c.hh
new file mode 100644
index 0000000..2c06085
--- /dev/null
+++ b/ucoo/hal/i2c/i2c.hh
@@ -0,0 +1,36 @@
+#ifndef ucoo_hal_i2c_i2c_hh
+#define ucoo_hal_i2c_i2c_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+
+#if defined (TARGET_host)
+# include "i2c.host.hh"
+#elif defined (TARGET_stm32)
+# include "i2c_hard.stm32.hh"
+#else
+# error "not implemented for this target"
+#endif
+#include "i2c_slave_data_buffer.hh"
+
+#endif // ucoo_hal_i2c_i2c_hh
diff --git a/ucoo/hal/i2c/i2c.host.cc b/ucoo/hal/i2c/i2c.host.cc
new file mode 100644
index 0000000..4a2553b
--- /dev/null
+++ b/ucoo/hal/i2c/i2c.host.cc
@@ -0,0 +1,213 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "i2c.host.hh"
+
+#include "config/hal/i2c.hh"
+
+namespace ucoo {
+
+/// Shared context between instances.
+class I2cHostShared
+{
+ public:
+ /// Constructor, connect to mex node.
+ I2cHostShared (Host &host);
+ /// Register a new driver instance.
+ void register_instance (Host &host, I2cHost &instance);
+ /// Send message, return new master status.
+ int send (uint8_t addr, const char *buf, int count);
+ /// Receive message, return new master status.
+ int recv (uint8_t addr, char *buf, int count);
+ /// Handle read requests from master.
+ void handle_read (mex::Msg &msg);
+ /// Handle write requests from master.
+ void handle_write (mex::Msg &msg);
+ private:
+ Host &host_;
+ mex::Node &node_;
+ mex::mtype_t read_mtype_, write_mtype_;
+ typedef std::list<I2cHost *> Instances;
+ Instances instances_;
+};
+
+I2cHostShared::I2cHostShared (Host &host)
+ : host_ (host), node_ (host.get_node ())
+{
+ std::string instance = host_.get_instance (1);
+ read_mtype_ = node_.reserve (instance + ":read");
+ write_mtype_ = node_.reserve (instance + ":write");
+ node_.handler_register (read_mtype_, *this, &I2cHostShared::handle_read);
+ node_.handler_register (write_mtype_, *this, &I2cHostShared::handle_write);
+}
+
+void
+I2cHostShared::register_instance (Host &host, I2cHost &instance)
+{
+ assert (&host == &host_);
+ instances_.push_back (&instance);
+}
+
+int
+I2cHostShared::send (uint8_t addr, const char *buf, int count)
+{
+ // Test for this slave in the same program.
+ for (Instances::const_iterator i = instances_.begin ();
+ i != instances_.end (); ++i)
+ {
+ if (addr == (*i)->slave_addr_)
+ {
+ assert (count <= UCOO_CONFIG_HAL_I2C_SLAVE_BUFFER_SIZE);
+ (*i)->slave_data_handler_->to_recv (buf, count);
+ return count;
+ }
+ }
+ // Else, send message.
+ mex::Msg msg (write_mtype_);
+ msg.push ("B") << addr;
+ msg.push (buf, count);
+ node_.send (msg);
+ return count;
+}
+
+int
+I2cHostShared::recv (uint8_t addr, char *buf, int count)
+{
+ // Test for this slave in the same program.
+ for (Instances::const_iterator i = instances_.begin ();
+ i != instances_.end (); ++i)
+ {
+ if (addr == (*i)->slave_addr_)
+ {
+ assert (count <= UCOO_CONFIG_HAL_I2C_SLAVE_BUFFER_SIZE);
+ return (*i)->slave_data_handler_->to_send (buf, count);
+ }
+ }
+ // Else, send request and wait for response.
+ mex::Msg msg (read_mtype_);
+ msg.push ("BB") << addr << count;
+ std::auto_ptr<mex::Msg> rsp = node_.request (msg);
+ int rcount = rsp->len ();
+ assert (rcount <= count);
+ const char *rbuf = rsp->pop (rcount);
+ std::copy (rbuf, rbuf + rcount, buf);
+ return rcount;
+}
+
+void
+I2cHostShared::handle_read (mex::Msg &msg)
+{
+ uint8_t addr;
+ int size;
+ msg.pop ("BB") >> addr >> size;
+ for (Instances::const_iterator i = instances_.begin ();
+ i != instances_.end (); ++i)
+ {
+ if (addr == (*i)->slave_addr_)
+ {
+ assert (size <= UCOO_CONFIG_HAL_I2C_SLAVE_BUFFER_SIZE);
+ char buf[size];
+ int n = (*i)->slave_data_handler_->to_send (buf, size);
+ mex::Msg rsp (read_mtype_);
+ rsp.push (buf, n);
+ node_.response (rsp);
+ break;
+ }
+ }
+}
+
+void
+I2cHostShared::handle_write (mex::Msg &msg)
+{
+ uint8_t addr;
+ msg.pop ("B") >> addr;
+ for (Instances::const_iterator i = instances_.begin ();
+ i != instances_.end (); ++i)
+ {
+ if (addr == (*i)->slave_addr_)
+ {
+ int size = msg.len ();
+ assert (size <= UCOO_CONFIG_HAL_I2C_SLAVE_BUFFER_SIZE);
+ (*i)->slave_data_handler_->to_recv (msg.pop (size), size);
+ break;
+ }
+ }
+}
+
+I2cHostShared *I2cHost::shared_;
+
+I2cHost::I2cHost (Host &host, int n)
+ : n_ (n), slave_addr_ (0), slave_data_handler_ (0),
+ master_status_ (STATUS_ERROR)
+{
+ if (!shared_)
+ {
+ static I2cHostShared shared (host);
+ shared_ = &shared;
+ }
+ shared_->register_instance (host, *this);
+ // n is not used, there is no support for several buses in the MEX
+ // messages at the moment.
+}
+
+void
+I2cHost::send (uint8_t addr, const char *buf, int count)
+{
+ // Update status, there is no background task.
+ master_status_ = shared_->send (addr, buf, count);
+ // If needed, call callback.
+ if (finished_handler_)
+ finished_handler_->finished (master_status_);
+}
+
+void
+I2cHost::recv (uint8_t addr, char *buf, int count)
+{
+ // Update status, there is no background task.
+ master_status_ = shared_->recv (addr, buf, count);
+ // If needed, call callback.
+ if (finished_handler_)
+ finished_handler_->finished (master_status_);
+}
+
+int
+I2cHost::status ()
+{
+ return master_status_;
+}
+
+int
+I2cHost::wait ()
+{
+ // Transfers are immediate.
+ return status ();
+}
+
+void
+I2cHost::register_data (uint8_t addr, DataHandler &data_handler)
+{
+ slave_addr_ = addr;
+ slave_data_handler_ = &data_handler;
+}
+
+} // namespace ucoo
diff --git a/ucoo/hal/i2c/i2c.host.hh b/ucoo/hal/i2c/i2c.host.hh
new file mode 100644
index 0000000..c0bb7f9
--- /dev/null
+++ b/ucoo/hal/i2c/i2c.host.hh
@@ -0,0 +1,66 @@
+#ifndef ucoo_hal_i2c_i2c_host_hh
+#define ucoo_hal_i2c_i2c_host_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/intf/i2c.hh"
+#include "ucoo/arch/host/host.hh"
+
+namespace ucoo {
+
+class I2cHostShared;
+
+/// I2C interface, host version.
+class I2cHost : public I2c
+{
+ public:
+ /// Initialise the Nth I2C.
+ I2cHost (Host &host, int n);
+ /// See I2cMaster::send.
+ void send (uint8_t addr, const char *buf, int count);
+ /// See I2cMaster::recv.
+ void recv (uint8_t addr, char *buf, int count);
+ /// See I2cMaster::status.
+ int status ();
+ /// See I2cMaster::wait.
+ int wait ();
+ /// See I2cSlave::register_data.
+ void register_data (uint8_t addr, DataHandler &data_handler);
+ private:
+ /// I2C number.
+ int n_;
+ /// Slave address.
+ uint8_t slave_addr_;
+ /// Handler called to source or sink data for slave exchanges.
+ DataHandler *slave_data_handler_;
+ /// Current master transfer status.
+ int master_status_;
+ /// Shared context.
+ static I2cHostShared *shared_;
+ friend class I2cHostShared;
+};
+
+} // namespace ucoo
+
+
+#endif // ucoo_hal_i2c_i2c_host_hh
diff --git a/ucoo/hal/i2c/i2c_hard.stm32.cc b/ucoo/hal/i2c/i2c_hard.stm32.cc
new file mode 100644
index 0000000..8122d15
--- /dev/null
+++ b/ucoo/hal/i2c/i2c_hard.stm32.cc
@@ -0,0 +1,396 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "i2c_hard.stm32.hh"
+
+#include <libopencm3/stm32/i2c.h>
+#include <libopencm3/stm32/f4/rcc.h>
+#include <libopencm3/cm3/nvic.h>
+
+#include "ucoo/utils/trace.hh"
+
+namespace ucoo {
+
+/// Local trace.
+static Trace<UCOO_CONFIG_HAL_I2C_TRACE> i2c_trace;
+
+static const int i2c_nb = 3;
+
+/// Information on I2C hardware structure.
+struct i2c_hardware_t
+{
+ /// I2C base address.
+ uint32_t base;
+ /// RCC enable bit.
+ uint32_t rcc_en;
+ /// Corresponding event IRQ (error IRQ is next one).
+ int ev_irq;
+};
+
+/// Information on I2C hardware array, this is zero indexed, I2C1 is at index
+/// 0.
+static const i2c_hardware_t i2c_hardware[i2c_nb] =
+{
+ { I2C1_BASE, RCC_APB1ENR_I2C1EN, NVIC_I2C1_EV_IRQ },
+ { I2C2_BASE, RCC_APB1ENR_I2C2EN, NVIC_I2C2_EV_IRQ },
+ { I2C3_BASE, RCC_APB1ENR_I2C3EN, NVIC_I2C3_EV_IRQ },
+};
+
+static I2cHard *i2c_instances[i2c_nb];
+
+} // namespace ucoo
+
+extern "C" {
+
+void i2c1_ev_isr () { ucoo::I2cHard::ev_isr (0); }
+
+void i2c1_er_isr () { ucoo::I2cHard::er_isr (0); }
+
+void i2c2_ev_isr () { ucoo::I2cHard::ev_isr (1); }
+
+void i2c2_er_isr () { ucoo::I2cHard::er_isr (1); }
+
+void i2c3_ev_isr () { ucoo::I2cHard::ev_isr (2); }
+
+void i2c3_er_isr () { ucoo::I2cHard::er_isr (2); }
+
+}
+
+namespace ucoo {
+
+I2cHard::I2cHard (int n)
+ : n_ (n), enabled_ (false), slave_addr_ (0), slave_data_handler_ (0),
+ master_ (false), master_status_ (STATUS_ERROR), master_buf_ (0)
+{
+ assert (n < i2c_nb);
+ assert (!i2c_instances[n]);
+ i2c_instances[n] = this;
+}
+
+I2cHard::~I2cHard ()
+{
+ disable ();
+ i2c_instances[n_] = 0;
+}
+
+void
+I2cHard::enable (int speed)
+{
+ enabled_ = true;
+ uint32_t base = i2c_hardware[n_].base;
+ // Turn on.
+ rcc_peripheral_enable_clock (&RCC_APB1ENR, i2c_hardware[n_].rcc_en);
+ // Reset.
+ I2C_CR1 (base) = I2C_CR1_SWRST;
+ // TODO: make sure the bus is free!!! How!
+ I2C_CR1 (base) = 0;
+ // Compute clock parameters.
+ int pclk = rcc_ppre1_frequency;
+ int pclk_mhz = pclk / 1000000;
+ uint16_t ccr, tris;
+ if (speed <= 100000)
+ {
+ ccr = pclk / speed / 2;
+ tris = pclk_mhz + 1;
+ }
+ else
+ {
+ assert (speed <= 400000);
+ ccr = I2C_CCR_FS | I2C_CCR_DUTY | (pclk / speed / 25);
+ tris = pclk_mhz * 3 / 10 + 1;
+ }
+ // Set all parameters.
+ I2C_CCR (base) = ccr;
+ I2C_TRISE (base) = tris;
+ I2C_OAR1 (base) = slave_addr_ | (1 << 14);
+ I2C_OAR2 (base) = 0;
+ I2C_CR2 (base) = I2C_CR2_ITEVTEN | I2C_CR2_ITERREN | pclk_mhz;
+ // Enable.
+ nvic_enable_irq (i2c_hardware[n_].ev_irq);
+ nvic_enable_irq (i2c_hardware[n_].ev_irq + 1);
+ I2C_CR1 (base) = I2C_CR1_ACK | I2C_CR1_PE;
+
+}
+
+void
+I2cHard::disable ()
+{
+ if (enabled_)
+ {
+ enabled_ = false;
+ uint32_t base = i2c_hardware[n_].base;
+ // TODO: wait for end of transfer?
+ // Disable.
+ nvic_disable_irq (i2c_hardware[n_].ev_irq);
+ nvic_disable_irq (i2c_hardware[n_].ev_irq + 1);
+ I2C_CR1 (base) = 0;
+ // Turn off.
+ rcc_peripheral_disable_clock (&RCC_APB1ENR,
+ i2c_hardware[n_].rcc_en);
+ }
+}
+
+void
+I2cHard::transfer (uint8_t addr, char *buf, int count)
+{
+ assert (enabled_ && count);
+ // No need to lock, master is not busy.
+ assert (master_status_ != STATUS_BUSY);
+ uint32_t base = i2c_hardware[n_].base;
+ // Wait until STOP condition terminated, polling is the only way.
+ while (I2C_CR1 (base) & I2C_CR1_STOP)
+ barrier ();
+ // Now, program next transfer.
+ master_status_ = STATUS_BUSY;
+ master_slave_addr_ = addr;
+ master_buf_ = buf;
+ master_count_ = count;
+ // TODO: multimaster: about ACK, may have to lock IRQ for multimaster.
+ I2C_CR1 (base) |= I2C_CR1_START;
+}
+
+void
+I2cHard::send (uint8_t addr, const char *buf, int count)
+{
+ transfer (addr, const_cast<char *> (buf), count);
+}
+
+void
+I2cHard::recv (uint8_t addr, char *buf, int count)
+{
+ assert (count >= 2); // TODO: count = 1 not supported, there is no IT!
+ // LSB = 1 for receiver mode.
+ transfer (addr | 1, buf, count);
+}
+
+int
+I2cHard::status ()
+{
+ return master_status_;
+}
+
+int
+I2cHard::wait ()
+{
+ while (master_status_ == STATUS_BUSY)
+ barrier ();
+ return master_status_;
+}
+
+void
+I2cHard::register_data (uint8_t addr, DataHandler &data_handler)
+{
+ assert ((addr & 1) == 0);
+ slave_addr_ = addr;
+ slave_data_handler_ = &data_handler;
+ if (enabled_)
+ {
+ uint32_t base = i2c_hardware[n_].base;
+ // Just in case a transfer is triggered right now.
+ barrier ();
+ // According to datasheet, bit 14 should be 1!
+ I2C_OAR1 (base) = addr | (1 << 14);
+ }
+}
+
+void
+I2cHard::ev_isr (int n)
+{
+ uint32_t base = i2c_hardware[n].base;
+ assert (i2c_instances[n]);
+ I2cHard &i2c = *i2c_instances[n];
+ i2c_trace ("<%d> event isr", n);
+ while (1)
+ {
+ uint16_t sr1 = I2C_SR1 (base);
+ i2c_trace ("<%d> sr1=%04x", n, sr1);
+ // Can not read SR2 because doing so would clear the ADDR bit.
+ if (i2c.master_)
+ {
+ if (sr1 & I2C_SR1_ADDR)
+ {
+ uint16_t sr2;
+ // If only one or two bytes should be received, disable ACK
+ // before reading SR2, and STOP after... Crappy hardware!
+ if ((i2c.master_slave_addr_ & 1) && i2c.buf_count_ == 1)
+ {
+ I2C_CR1 (base) = I2C_CR1_PE;
+ sr2 = I2C_SR2 (base);
+ I2C_CR1 (base) = I2C_CR1_STOP | I2C_CR1_PE;
+ // TODO: what to wait now? Unsupported for now.
+ }
+ else if ((i2c.master_slave_addr_ & 1) && i2c.buf_count_ == 2)
+ {
+ I2C_CR1 (base) = I2C_CR1_POS | I2C_CR1_PE;
+ sr2 = I2C_SR2 (base);
+ // Wait for BTF.
+ }
+ else
+ {
+ sr2 = I2C_SR2 (base);
+ I2C_CR2 (base) |= I2C_CR2_ITBUFEN;
+ }
+ i2c_trace ("<%d> master sr2=%04x", n, sr2);
+ }
+ else if (sr1 & I2C_SR1_TxE
+ && i2c.buf_index_ < i2c.buf_count_)
+ {
+ i2c_trace ("<%d> master tx index=%d", n, i2c.buf_index_);
+ // Send next byte.
+ I2C_DR (base) = i2c.master_buf_[i2c.buf_index_++];
+ // Wait for BTF if last one.
+ if (i2c.buf_index_ == i2c.buf_count_)
+ I2C_CR2 (base) &= ~I2C_CR2_ITBUFEN;
+ }
+ else if (sr1 & I2C_SR1_RxNE
+ && i2c.buf_count_ - i2c.buf_index_ > 3)
+ {
+ i2c_trace ("<%d> master rx index=%d", n, i2c.buf_index_);
+ i2c.master_buf_[i2c.buf_index_++] = I2C_DR (base);
+ if (i2c.buf_count_ - i2c.buf_index_ == 3)
+ // Wait for BTF.
+ I2C_CR2 (base) &= ~I2C_CR2_ITBUFEN;
+ }
+ else if (sr1 & I2C_SR1_BTF)
+ {
+ i2c_trace ("<%d> master btf index=%d", n, i2c.buf_index_);
+ if (!(i2c.master_slave_addr_ & 1))
+ {
+ // End of transmission.
+ I2C_CR1 (base) = I2C_CR1_ACK | I2C_CR1_STOP | I2C_CR1_PE;
+ i2c.master_ = false;
+ i2c.master_status_ = i2c.buf_index_;
+ if (i2c.finished_handler_)
+ i2c.finished_handler_->finished (i2c.master_status_);
+ }
+ else if (i2c.buf_count_ - i2c.buf_index_ == 3)
+ {
+ // Near end of reception.
+ I2C_CR1 (base) = I2C_CR1_PE;
+ i2c.master_buf_[i2c.buf_index_++] = I2C_DR (base);
+ // Wait for BTF.
+ }
+ else
+ {
+ // End of reception.
+ I2C_CR1 (base) = I2C_CR1_ACK | I2C_CR1_STOP | I2C_CR1_PE;
+ if (i2c.buf_count_ - i2c.buf_index_ == 2)
+ i2c.master_buf_[i2c.buf_index_++] = I2C_DR (base);
+ i2c.master_buf_[i2c.buf_index_++] = I2C_DR (base);
+ i2c.master_ = false;
+ i2c.master_status_ = i2c.buf_index_;
+ if (i2c.finished_handler_)
+ i2c.finished_handler_->finished (i2c.master_status_);
+ }
+ }
+ else
+ break;
+ }
+ else
+ {
+ if (sr1 & I2C_SR1_ADDR)
+ {
+ uint16_t sr2 = I2C_SR2 (base);
+ i2c_trace ("<%d> slave sr2=%04x", n, sr2);
+ // Initiate new slave transfer.
+ if (sr2 & I2C_SR2_TRA)
+ {
+ i2c.buf_count_ = i2c.slave_data_handler_->to_send
+ (i2c.slave_buf_, sizeof (i2c.slave_buf_));
+ }
+ else
+ i2c.buf_count_ = sizeof (i2c.slave_buf_);
+ i2c.buf_index_ = 0;
+ I2C_CR2 (base) |= I2C_CR2_ITBUFEN;
+ }
+ else if (sr1 & I2C_SR1_TxE)
+ {
+ i2c_trace ("<%d> slave tx index=%d", n, i2c.buf_index_);
+ uint8_t b = 0xff;
+ if (i2c.buf_index_ < i2c.buf_count_)
+ b = i2c.slave_buf_[i2c.buf_index_++];
+ I2C_DR (base) = b;
+ }
+ else if (sr1 & I2C_SR1_RxNE)
+ {
+ i2c_trace ("<%d> slave rx index=%d", n, i2c.buf_index_);
+ uint8_t b = I2C_DR (base);
+ if (i2c.buf_index_ < i2c.buf_count_)
+ i2c.slave_buf_[i2c.buf_index_++] = b;
+ }
+ else if (sr1 & I2C_SR1_STOPF)
+ {
+ i2c_trace ("<%d> slave stop", n);
+ i2c.slave_data_handler_->to_recv (i2c.slave_buf_, i2c.buf_index_);
+ // TODO: multimaster: there is no way to write in this
+ // register if a START was requested to switch to master mode!
+ I2C_CR1 (base) = I2C_CR1_ACK | I2C_CR1_PE;
+ I2C_CR2 (base) &= ~I2C_CR2_ITBUFEN;
+ }
+ else if (sr1 & I2C_SR1_SB)
+ {
+ i2c_trace ("<%d> master start", n);
+ // Starting master mode.
+ I2C_DR (base) = i2c.master_slave_addr_;
+ i2c.master_ = true;
+ i2c.buf_count_ = i2c.master_count_;
+ i2c.buf_index_ = 0;
+ }
+ else
+ break;
+ }
+
+ }
+}
+
+void
+I2cHard::er_isr (int n)
+{
+ uint32_t base = i2c_hardware[n].base;
+ assert (i2c_instances[n]);
+ I2cHard &i2c = *i2c_instances[n];
+ uint16_t sr1 = I2C_SR1 (base);
+ I2C_SR1 (base) = 0;
+ i2c_trace ("<%d> error isr sr1=%04x", n, sr1);
+ if (i2c.master_)
+ {
+ if (sr1 & I2C_SR1_ARLO)
+ {
+ // Try again.
+ I2C_CR1 (base) = I2C_CR1_ACK | I2C_CR1_START | I2C_CR1_PE;
+ i2c.master_ = false;
+ }
+ else if (sr1 & I2C_SR1_AF)
+ {
+ I2C_CR1 (base) = I2C_CR1_ACK | I2C_CR1_STOP | I2C_CR1_PE;
+ i2c.master_ = false;
+ i2c.master_status_ = i2c.buf_index_;
+ if (i2c.finished_handler_)
+ i2c.finished_handler_->finished (i2c.master_status_);
+ }
+ }
+ I2C_CR2 (base) &= ~I2C_CR2_ITBUFEN;
+ // TODO: handle misplaced STOP errata.
+}
+
+} // namespace ucoo
diff --git a/ucoo/hal/i2c/i2c_hard.stm32.hh b/ucoo/hal/i2c/i2c_hard.stm32.hh
new file mode 100644
index 0000000..94e4259
--- /dev/null
+++ b/ucoo/hal/i2c/i2c_hard.stm32.hh
@@ -0,0 +1,91 @@
+#ifndef ucoo_hal_i2c_i2c_hard_stm32_hh
+#define ucoo_hal_i2c_i2c_hard_stm32_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/intf/i2c.hh"
+
+#include "config/hal/i2c.hh"
+
+namespace ucoo {
+
+/// I2C interface, using dedicated hardware.
+class I2cHard : public I2c
+{
+ public:
+ /// Constructor for the Nth I2C.
+ I2cHard (int n);
+ /// Shutdown.
+ ~I2cHard ();
+ /// Enable and setup
+ void enable (int speed = 100000);
+ /// Disable.
+ void disable ();
+ /// See I2cMaster::send.
+ void send (uint8_t addr, const char *buf, int count);
+ /// See I2cMaster::recv.
+ void recv (uint8_t addr, char *buf, int count);
+ /// See I2cMaster::status.
+ int status ();
+ /// See I2cMaster::wait.
+ int wait ();
+ /// See I2cSlave::register_data.
+ void register_data (uint8_t addr, DataHandler &data_handler);
+ /// Event ISR.
+ static void ev_isr (int n);
+ /// Error ISR.
+ static void er_isr (int n);
+ private:
+ /// Start a master transfer, send or recv, depending on addr LSB.
+ void transfer (uint8_t addr, char *buf, int count);
+ private:
+ /// I2C number.
+ int n_;
+ /// Is it enabled?
+ bool enabled_;
+ /// Slave address.
+ uint8_t slave_addr_;
+ /// Handler called to source or sink data for slave exchanges.
+ DataHandler *slave_data_handler_;
+ /// Slave buffer.
+ char slave_buf_[UCOO_CONFIG_HAL_I2C_SLAVE_BUFFER_SIZE];
+ /// Current buffer count (bytes to send), or buffer size (available room).
+ int buf_count_;
+ /// Current buffer index (position to read byte to send or write received
+ /// byte).
+ int buf_index_;
+ /// Master access granted.
+ bool master_;
+ /// Current master transfer status.
+ int master_status_;
+ /// Current master transfer buffer.
+ char *master_buf_;
+ /// Current master transfer size, copied to buf_count_ when active.
+ int master_count_;
+ /// Current master transfer slave address, LSB is set for receiver mode.
+ uint8_t master_slave_addr_;
+};
+
+} // namespace ucoo
+
+#endif // ucoo_hal_i2c_i2c_hard_stm32_hh
diff --git a/ucoo/hal/i2c/i2c_slave_data_buffer.cc b/ucoo/hal/i2c/i2c_slave_data_buffer.cc
new file mode 100644
index 0000000..1ba3a3a
--- /dev/null
+++ b/ucoo/hal/i2c/i2c_slave_data_buffer.cc
@@ -0,0 +1,80 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "i2c_slave_data_buffer.hh"
+
+#include <algorithm>
+
+namespace ucoo {
+
+I2cSlaveDataBuffer::I2cSlaveDataBuffer (char *send_buf, int send_size,
+ char *recv_buf, int recv_size)
+ : send_buf_ (send_buf), send_size_ (send_size), send_count_ (0),
+ recv_buf_ (recv_buf), recv_size_ (recv_size), recv_count_ (0)
+{
+}
+
+void
+I2cSlaveDataBuffer::update (const char *buf, int count)
+{
+ int r = std::min (count, send_size_);
+ irq_flags_t flags = irq_lock ();
+ std::copy (buf, buf + r, send_buf_);
+ send_count_ = r;
+ irq_restore (flags);
+}
+
+int
+I2cSlaveDataBuffer::poll (char *buf, int count)
+{
+ // Test to avoid superfluous irq lock. This is not a problem if
+ // condition become false before irq is locked.
+ if (recv_count_)
+ {
+ irq_flags_t flags = irq_lock ();
+ int r = std::min (count, recv_count_);
+ recv_count_ = 0;
+ std::copy (recv_buf_, recv_buf_ + r, buf);
+ irq_restore (flags);
+ return r;
+ }
+ else return 0;
+}
+
+int
+I2cSlaveDataBuffer::to_send (char *buf, int count)
+{
+ int r = std::min (count, send_count_);
+ std::copy (send_buf_, send_buf_ + r, buf);
+ return r;
+}
+
+void
+I2cSlaveDataBuffer::to_recv (const char *buf, int count)
+{
+ int r = std::min (count, recv_size_);
+ std::copy (buf, buf + r, recv_buf_);
+ recv_count_ = r;
+}
+
+} // namespace ucoo
diff --git a/ucoo/hal/i2c/i2c_slave_data_buffer.hh b/ucoo/hal/i2c/i2c_slave_data_buffer.hh
new file mode 100644
index 0000000..eae45ae
--- /dev/null
+++ b/ucoo/hal/i2c/i2c_slave_data_buffer.hh
@@ -0,0 +1,71 @@
+#ifndef ucoo_hal_i2c_i2c_slave_data_buffer_hh
+#define ucoo_hal_i2c_i2c_slave_data_buffer_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/intf/i2c.hh"
+
+namespace ucoo {
+
+/// Data sink and source for I2C slave. Store any received data and data to
+/// send in buffers.
+class I2cSlaveDataBuffer : public I2cSlave::DataHandler
+{
+ public:
+ /// Constructor, take buffers.
+ I2cSlaveDataBuffer (char *send_buf, int send_size,
+ char *recv_buf, int recv_size);
+ /// Update slave internal buffer, will be sent to master on request.
+ void update (const char *buf, int count);
+ /// If data has been received, copy it to provided buffer and return its
+ /// size. Else, return 0.
+ int poll (char *buf, int count);
+ /// See I2cSlave::DataHandler::to_send.
+ int to_send (char *buf, int count);
+ /// See I2cSlave::DataHandler::to_recv.
+ void to_recv (const char *buf, int count);
+ private:
+ char *send_buf_;
+ int send_size_, send_count_;
+ char *recv_buf_;
+ int recv_size_, recv_count_;
+};
+
+/// Same as I2cSlaveDataBuffer, but include buffers.
+template<int send_size, int recv_size>
+class I2cSlaveDataBufferSize : public I2cSlaveDataBuffer
+{
+ public:
+ /// Default constructor.
+ I2cSlaveDataBufferSize ()
+ : I2cSlaveDataBuffer (send_array_, sizeof (send_array_),
+ recv_array_, sizeof (recv_array_))
+ { }
+ private:
+ char send_array_[send_size];
+ char recv_array_[recv_size];
+};
+
+} // namespace ucoo
+
+#endif // ucoo_hal_i2c_i2c_slave_data_buffer_hh
diff --git a/ucoo/hal/i2c/test/Makefile b/ucoo/hal/i2c/test/Makefile
new file mode 100644
index 0000000..5046862
--- /dev/null
+++ b/ucoo/hal/i2c/test/Makefile
@@ -0,0 +1,9 @@
+BASE = ../../../..
+
+TARGETS = host stm32f4
+PROGS = test_i2c
+test_i2c_SOURCES = test_i2c.cc
+
+MODULES = hal/i2c utils base/test hal/usb
+
+include $(BASE)/build/top.mk
diff --git a/ucoo/hal/i2c/test/hub.py b/ucoo/hal/i2c/test/hub.py
new file mode 100644
index 0000000..c6c77f7
--- /dev/null
+++ b/ucoo/hal/i2c/test/hub.py
@@ -0,0 +1,6 @@
+# Hub for test.
+from mex.hub import Hub
+def log (x):
+ print x
+h = Hub (min_clients = 1, log = log)
+h.wait ()
diff --git a/ucoo/hal/i2c/test/test_i2c.cc b/ucoo/hal/i2c/test/test_i2c.cc
new file mode 100644
index 0000000..7fc7772
--- /dev/null
+++ b/ucoo/hal/i2c/test/test_i2c.cc
@@ -0,0 +1,226 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the "Software"),
+// to deal in the Software without restriction, including without limitation
+// the rights to use, copy, modify, merge, publish, distribute, sublicense,
+// and/or sell copies of the Software, and to permit persons to whom the
+// Software is furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+// DEALINGS IN THE SOFTWARE.
+//
+// }}}
+#include "ucoo/hal/i2c/i2c.hh"
+
+#include "ucoo/arch/arch.hh"
+#include "ucoo/base/test/test.hh"
+
+#ifdef TARGET_stm32
+# include <libopencm3/stm32/f4/rcc.h>
+# include "ucoo/hal/gpio/gpio.hh"
+#endif
+
+#ifdef TARGET_host
+# define TEST_NOT_THERE 0
+#else
+# define TEST_NOT_THERE 1
+#endif
+
+#include "ucoo/utils/delay.hh"
+
+#include <algorithm>
+#include <cstring>
+
+static const int buffer_size = 16;
+static const int margin = 5;
+static const uint8_t a1 = 0x2c, a2 = 0x2e, a3 = 0x42;
+
+void
+test_basic (ucoo::TestSuite &tsuite, ucoo::I2cMaster &m,
+ ucoo::I2cSlaveDataBuffer &d, uint8_t addr)
+{
+ tsuite.group ("basic");
+ {
+ ucoo::Test test (tsuite, "recv");
+ const char *hello = "Hello world!";
+ char buf[buffer_size + margin];
+ d.update (hello, std::strlen (hello) + 1);
+ char ref[buffer_size + margin];
+ int ref_size = std::strlen (hello) + 1;
+ std::copy (hello, hello + ref_size, ref);
+#ifdef TARGET_stm32
+ // Slave is not able to signal its buffer end. Extra bytes will be
+ // read as 0xff.
+ std::fill (ref + ref_size, ref + sizeof (ref), 0xff);
+ ref_size = sizeof (ref);
+#endif
+ for (int len = 2; len < (int) sizeof (buf); len++)
+ {
+ std::fill (buf, buf + sizeof (buf), 42);
+ m.recv (addr, buf, len);
+ int r = m.wait ();
+ int rexp = std::min (len, ref_size);
+ test_fail_break_unless (test, r == rexp);
+ test_fail_break_unless (test, std::equal (buf, buf + rexp, ref));
+ test_fail_break_unless (test, std::count (buf, buf + sizeof (buf), 42)
+ == (int) sizeof (buf) - rexp);
+ }
+ }
+ {
+ ucoo::Test test (tsuite, "send");
+ // Slave is supposed to signal when it received enough data, but this
+ // is not implemented.
+ char buf[buffer_size + margin];
+ for (int len = 1; len < (int) sizeof (buf); len++)
+ {
+ int r;
+ // Before transfer, no data.
+ r = d.poll (buf, sizeof (buf));
+ test_fail_break_unless (test, r == 0);
+ // Send data.
+ char c = '0' + len % 10;
+ std::fill (buf, buf + len, c);
+ m.send (addr, buf, len);
+ r = m.wait ();
+ test_fail_break_unless (test, r == len);
+ // Let some time for slave to finish reception.
+ ucoo::delay_ms (1);
+ // Check what is received.
+ r = d.poll (buf, sizeof (buf));
+ test_fail_break_unless (test, r == std::min (len, buffer_size));
+ test_fail_break_unless (test, std::count (buf, buf + r, c) == r);
+ }
+ }
+ do {
+ ucoo::Test test (tsuite, "callback");
+ // Callback object will start a reception, then a transmission of what
+ // was received.
+ class TestCallback : public ucoo::I2cMaster::FinishedHandler
+ {
+ ucoo::I2cMaster &m_;
+ uint8_t addr_;
+ int step_;
+ char buf_[buffer_size];
+ public:
+ bool failed;
+ public:
+ TestCallback (ucoo::I2cMaster &m, uint8_t addr)
+ : m_ (m), addr_ (addr), step_ (0), failed (false) { }
+ void finished (int status)
+ {
+ if (status != buffer_size)
+ failed = true;
+ else
+ {
+ switch (step_)
+ {
+ case 0:
+ step_++;
+ m_.recv (addr_, buf_, buffer_size);
+ break;
+ case 1:
+ step_++;
+ m_.send (addr_, buf_, buffer_size);
+ break;
+ case 2:
+ // Nothing, stop.
+ break;
+ }
+ }
+ }
+ };
+ TestCallback callback (m, addr);
+ m.register_finished (callback);
+ // Set slave data.
+ char buf[buffer_size];
+ std::fill (buf, buf + sizeof (buf), 42);
+ d.update (buf, sizeof (buf));
+ // Start transfers.
+ std::fill (buf, buf + sizeof (buf), 21);
+ m.send (addr, buf, sizeof (buf));
+ // Will only return after the last transfer.
+ m.wait ();
+ m.unregister_finished ();
+ test_fail_break_unless (test, !callback.failed);
+ // Let some time for slave to finish reception.
+ ucoo::delay_ms (1);
+ // Check what is received by slave (master should have read 42 from
+ // slave and send it back).
+ int r = d.poll (buf, sizeof (buf));
+ test_fail_break_unless (test, r == buffer_size);
+ test_fail_break_unless (test, std::count (buf, buf + r, 42) == r);
+ } while (0);
+}
+
+void
+test_not_there (ucoo::TestSuite &tsuite, ucoo::I2cMaster &m, uint8_t addr)
+{
+ tsuite.group ("not there");
+ do {
+ ucoo::Test test (tsuite, "recv");
+ char buf[buffer_size];
+ m.recv (addr, buf, buffer_size);
+ int r = m.wait ();
+ test_fail_break_unless (test, r == ucoo::I2cMaster::STATUS_ERROR);
+ } while (0);
+ do {
+ ucoo::Test test (tsuite, "send");
+ char buf[buffer_size];
+ std::fill (buf, buf + buffer_size, 42);
+ m.send (addr, buf, buffer_size);
+ int r = m.wait ();
+ test_fail_break_unless (test, r == ucoo::I2cMaster::STATUS_ERROR);
+ } while (0);
+}
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::TestSuite tsuite ("i2c");
+#if defined (TARGET_host)
+ ucoo::Host host ("test_i2c");
+ host.parse_options ();
+ ucoo::I2cHost i2c1 (host, 0);
+ ucoo::I2cHost i2c2 (host, 1);
+#elif defined (TARGET_stm32)
+ // I2C1: B6: SCL, B9: SDA
+ // I2C3: A8: SCL, C9: SDA
+ rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN);
+ rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPBEN);
+ rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPCEN);
+ gpio_mode_setup (GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO6 | GPIO9);
+ gpio_mode_setup (GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8);
+ gpio_mode_setup (GPIOC, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO9);
+ gpio_set_output_options (GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO6 | GPIO9);
+ gpio_set_output_options (GPIOA, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO8);
+ gpio_set_output_options (GPIOC, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO9);
+ gpio_set_af (GPIOB, GPIO_AF4, GPIO6 | GPIO9);
+ gpio_set_af (GPIOA, GPIO_AF4, GPIO8);
+ gpio_set_af (GPIOC, GPIO_AF4, GPIO9);
+ ucoo::I2cHard i2c1 (0);
+ ucoo::I2cHard i2c2 (2);
+ i2c1.enable ();
+ i2c2.enable ();
+#endif
+ ucoo::I2cSlaveDataBufferSize<16, 16> data1, data2;
+ i2c1.register_data (a1, data1);
+ i2c2.register_data (a2, data2);
+ // Run tests.
+ test_basic (tsuite, i2c1, data2, a2);
+ if (TEST_NOT_THERE)
+ test_not_there (tsuite, i2c1, a3);
+ test_basic (tsuite, i2c2, data1, a1);
+ return tsuite.report () ? 0 : 1;
+}