summaryrefslogtreecommitdiff
path: root/ucoo/hal/spi
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/spi
parentd415a0646e874848cd06482c5d57916cca5cff4a (diff)
Rename ucoolib modules directory from ucoolib to ucoo
Diffstat (limited to 'ucoo/hal/spi')
-rw-r--r--ucoo/hal/spi/Module1
-rw-r--r--ucoo/hal/spi/spi_soft.cc125
-rw-r--r--ucoo/hal/spi/spi_soft.hh64
-rw-r--r--ucoo/hal/spi/test/Makefile9
-rw-r--r--ucoo/hal/spi/test/test_spi.cc112
5 files changed, 311 insertions, 0 deletions
diff --git a/ucoo/hal/spi/Module b/ucoo/hal/spi/Module
new file mode 100644
index 0000000..a6df136
--- /dev/null
+++ b/ucoo/hal/spi/Module
@@ -0,0 +1 @@
+hal_spi_SOURCES := spi_soft.cc
diff --git a/ucoo/hal/spi/spi_soft.cc b/ucoo/hal/spi/spi_soft.cc
new file mode 100644
index 0000000..3bcc0d5
--- /dev/null
+++ b/ucoo/hal/spi/spi_soft.cc
@@ -0,0 +1,125 @@
+// 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 "spi_soft.hh"
+
+#include "ucoo/utils/delay.hh"
+#include "ucoo/common.hh"
+
+namespace ucoo {
+
+SpiSoftMaster::SpiSoftMaster (Io &sck, Io &mosi, Io &miso)
+ : sck_ (sck), mosi_ (mosi), miso_ (miso), enabled_ (false)
+{
+}
+
+SpiSoftMaster::~SpiSoftMaster ()
+{
+ disable ();
+}
+
+void
+SpiSoftMaster::enable (int speed, SpiMode mode)
+{
+ enabled_ = true;
+ // Take pins ownership, set SCK according to CPOL.
+ sck_.set (mode >= SPI_MODE_2);
+ sck_.output ();
+ mosi_.reset ();
+ mosi_.output ();
+ miso_.input ();
+ // Set clock period and clock phase.
+ half_period_ns_ = 1000 * 1000 * 1000 / 2 / speed;
+ cpha_ = mode & 1;
+}
+
+void
+SpiSoftMaster::disable ()
+{
+ enabled_ = false;
+ // Release pins.
+ sck_.input ();
+ sck_.reset ();
+ mosi_.input ();
+}
+
+void
+SpiSoftMaster::send_and_recv (const char *tx_buf, char *rx_buf, int count)
+{
+ while (count--)
+ *rx_buf++ = send_and_recv (*tx_buf++);
+}
+
+char
+SpiSoftMaster::send_and_recv (char tx)
+{
+ assert (enabled_);
+ uint8_t i, rx = 0;
+ // Depending on clock phase, sample is done on first transition or second
+ // transition.
+ for (i = 0x80; i; i >>= 1)
+ {
+ // Setup stage.
+ if (cpha_)
+ sck_.toggle ();
+ mosi_.set (tx & i);
+ delay_ns (half_period_ns_);
+ // Sample stage.
+ sck_.toggle ();
+ if (miso_.get ())
+ rx |= i;
+ delay_ns (half_period_ns_);
+ // SCK toggle for next setup stage.
+ if (!cpha_)
+ sck_.toggle ();
+ }
+ return rx;
+}
+
+void
+SpiSoftMaster::send (const char *tx_buf, int count)
+{
+ while (count--)
+ send (*tx_buf++);
+}
+
+void
+SpiSoftMaster::send (char tx)
+{
+ send_and_recv (tx);
+}
+
+void
+SpiSoftMaster::recv (char *rx_buf, int count)
+{
+ while (count--)
+ *rx_buf++ = recv ();
+}
+
+char
+SpiSoftMaster::recv ()
+{
+ return send_and_recv (0);
+}
+
+} // namespace ucoo
diff --git a/ucoo/hal/spi/spi_soft.hh b/ucoo/hal/spi/spi_soft.hh
new file mode 100644
index 0000000..2dc1a64
--- /dev/null
+++ b/ucoo/hal/spi/spi_soft.hh
@@ -0,0 +1,64 @@
+#ifndef ucoo_hal_spi_spi_soft_hh
+#define ucoo_hal_spi_spi_soft_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/spi_master.hh"
+#include "ucoo/intf/io.hh"
+
+namespace ucoo {
+
+/// Software implementation of a SPI master.
+class SpiSoftMaster : public SpiMaster
+{
+ public:
+ /// Constructor, need all SPI pins.
+ SpiSoftMaster (Io &sck, Io &mosi, Io &miso);
+ /// Destructor, disable.
+ ~SpiSoftMaster ();
+ /// Enable and setup.
+ void enable (int speed_hz, SpiMode mode = SPI_MODE_0);
+ /// Disable.
+ void disable ();
+ /// See SpiMaster::send_and_recv.
+ void send_and_recv (const char *tx_buf, char *rx_buf, int count);
+ /// Send and receive one byte.
+ char send_and_recv (char tx);
+ /// See SpiMaster::send.
+ void send (const char *tx_buf, int count);
+ /// Send one byte.
+ void send (char tx);
+ /// See SpiMaster::recv.
+ void recv (char *rx_buf, int count);
+ /// Receive one byte.
+ char recv ();
+ private:
+ Io &sck_, &mosi_, &miso_;
+ int half_period_ns_;
+ bool cpha_;
+ bool enabled_;
+};
+
+} // namespace ucoo
+
+#endif // ucoo_hal_spi_spi_soft_hh
diff --git a/ucoo/hal/spi/test/Makefile b/ucoo/hal/spi/test/Makefile
new file mode 100644
index 0000000..76b8230
--- /dev/null
+++ b/ucoo/hal/spi/test/Makefile
@@ -0,0 +1,9 @@
+BASE = ../../../..
+
+TARGETS = stm32f4
+PROGS = test_spi
+test_spi_SOURCES = test_spi.cc
+
+MODULES = hal/spi hal/gpio utils base/test hal/usb
+
+include $(BASE)/build/top.mk
diff --git a/ucoo/hal/spi/test/test_spi.cc b/ucoo/hal/spi/test/test_spi.cc
new file mode 100644
index 0000000..fa26abc
--- /dev/null
+++ b/ucoo/hal/spi/test/test_spi.cc
@@ -0,0 +1,112 @@
+// 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/spi/spi_soft.hh"
+#include "ucoo/hal/gpio/gpio.hh"
+#include "ucoo/utils/delay.hh"
+
+#include "ucoo/arch/arch.hh"
+#include "ucoo/base/test/test.hh"
+
+#include <libopencm3/stm32/f4/rcc.h>
+
+#include <algorithm>
+#include <cstdio>
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::Stream &ts = ucoo::test_stream ();
+ // Use connection to LIS302DL device on discovery board.
+ rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPAEN
+ | RCC_AHB1ENR_IOPEEN);
+ ucoo::Gpio ss (GPIOE, 3);
+ ss.set ();
+ ss.output ();
+ ucoo::Gpio sck (GPIOA, 5), mosi (GPIOA, 7), miso (GPIOA, 6);
+ ucoo::SpiSoftMaster spi (sck, mosi, miso);
+ spi.enable (1000000, ucoo::SPI_MODE_3);
+ // Loop with simple IU.
+ char buf[64];
+ unsigned int r;
+ int8_t x, y, z;
+ while (1)
+ {
+ ucoo::delay_ms (200);
+ // Read X, Y, Z.
+ ss.reset ();
+ spi.send (0xe9);
+ x = spi.recv ();
+ spi.recv ();
+ y = spi.recv ();
+ spi.recv ();
+ z = spi.recv ();
+ ss.set ();
+ ucoo::delay_ns (100);
+ r = snprintf (buf, sizeof (buf), "x: %4d, y: %4d, z: %4d\r", x, y, z);
+ r = std::min (sizeof (buf) - 1, r);
+ ts.write (buf, r);
+ // Simple UI.
+ while (ts.poll ())
+ {
+ char c = ts.getc ();
+ switch (c)
+ {
+ case '?':
+ static const char help[] =
+ "\n? - help\n"
+ "w - who am I\n"
+ "o - turn on\n"
+ "f - turn off\n";
+ ts.write (help, sizeof (help));
+ break;
+ case 'w':
+ {
+ // Read Who am I register.
+ ss.reset ();
+ spi.send (0x8f);
+ char rsp = spi.recv ();
+ ss.set ();
+ ucoo::delay_ns (100);
+ // Report result.
+ r = snprintf (buf, sizeof (buf), "\nI am 0x%02x\n",
+ static_cast<unsigned char> (rsp));
+ r = std::min (sizeof (buf) - 1, r);
+ ts.write (buf, r);
+ }
+ break;
+ case 'o':
+ case 'f':
+ {
+ char cmd[] = { 0x20, (char) (c == 'o' ? 0x47 : 0x07) };
+ ss.reset ();
+ spi.send (cmd, sizeof (cmd));
+ ss.set ();
+ ucoo::delay_ns (100);
+ }
+ break;
+ }
+ }
+ }
+}