summaryrefslogtreecommitdiff
path: root/ucoo/hal/i2c/test
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/test
parentd415a0646e874848cd06482c5d57916cca5cff4a (diff)
Rename ucoolib modules directory from ucoolib to ucoo
Diffstat (limited to 'ucoo/hal/i2c/test')
-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
3 files changed, 241 insertions, 0 deletions
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;
+}