summaryrefslogtreecommitdiff
path: root/ucoo/hal/gpio
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/gpio
parentd415a0646e874848cd06482c5d57916cca5cff4a (diff)
Rename ucoolib modules directory from ucoolib to ucoo
Diffstat (limited to 'ucoo/hal/gpio')
-rw-r--r--ucoo/hal/gpio/Module1
-rw-r--r--ucoo/hal/gpio/gpio.hh35
-rw-r--r--ucoo/hal/gpio/gpio.host.cc181
-rw-r--r--ucoo/hal/gpio/gpio.host.hh69
-rw-r--r--ucoo/hal/gpio/gpio.stm32f4.cc105
-rw-r--r--ucoo/hal/gpio/gpio.stm32f4.hh85
-rw-r--r--ucoo/hal/gpio/test/Makefile9
-rw-r--r--ucoo/hal/gpio/test/test_gpio.cc71
8 files changed, 556 insertions, 0 deletions
diff --git a/ucoo/hal/gpio/Module b/ucoo/hal/gpio/Module
new file mode 100644
index 0000000..8f2ba40
--- /dev/null
+++ b/ucoo/hal/gpio/Module
@@ -0,0 +1 @@
+hal_gpio_SOURCES = gpio.host.cc gpio.stm32f4.cc
diff --git a/ucoo/hal/gpio/gpio.hh b/ucoo/hal/gpio/gpio.hh
new file mode 100644
index 0000000..86a6c91
--- /dev/null
+++ b/ucoo/hal/gpio/gpio.hh
@@ -0,0 +1,35 @@
+#ifndef ucoo_hal_gpio_gpio_hh
+#define ucoo_hal_gpio_gpio_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 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 "gpio.host.hh"
+#elif defined TARGET_stm32f4
+# include "gpio.stm32f4.hh"
+#else
+# error "not implemented for this target"
+#endif
+
+#endif // ucoo_hal_gpio_gpio_hh
diff --git a/ucoo/hal/gpio/gpio.host.cc b/ucoo/hal/gpio/gpio.host.cc
new file mode 100644
index 0000000..ca9065b
--- /dev/null
+++ b/ucoo/hal/gpio/gpio.host.cc
@@ -0,0 +1,181 @@
+// 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 "gpio.host.hh"
+
+#include <cstring>
+
+namespace ucoo {
+
+/// Shared context between instances.
+class GpioShared
+{
+ public:
+ /// Constructor, connect to mex node.
+ GpioShared (Host &host);
+ /// Register a new GPIO.
+ void register_instance (Host &host, Gpio &instance);
+ /// Set output state.
+ void set (Gpio &instance, bool state);
+ /// Set direction.
+ void output (Gpio &instance, bool output);
+ private:
+ /// Send state change message.
+ void send (Gpio &instance);
+ /// Handle input value from Mex.
+ void handle_gpio_input (mex::Msg &msg);
+ private:
+ Host &host_;
+ mex::Node &node_;
+ mex::mtype_t gpio_output_mtype_, gpio_input_mtype_;
+ typedef std::map<std::string, Gpio *> Instances;
+ Instances instances_;
+};
+
+GpioShared::GpioShared (Host &host)
+ : host_ (host), node_ (host.get_node ())
+{
+ std::string instance = host_.get_instance ();
+ gpio_output_mtype_ = node_.reserve (instance + ":gpio_out");
+ gpio_input_mtype_ = node_.reserve (instance + ":gpio_in");
+ node_.handler_register (gpio_input_mtype_, *this,
+ &GpioShared::handle_gpio_input);
+}
+
+void
+GpioShared::register_instance (Host &host, Gpio &instance)
+{
+ assert (&host == &host_);
+ std::pair<Instances::iterator, bool> r =
+ instances_.insert (Instances::value_type (instance.name_, &instance));
+ assert (r.second);
+}
+
+void
+GpioShared::set (Gpio &instance, bool state)
+{
+ if (state != instance.output_)
+ {
+ instance.output_ = state;
+ send (instance);
+ }
+}
+
+void
+GpioShared::output (Gpio &instance, bool output)
+{
+ if (output != instance.direction_output_)
+ {
+ instance.direction_output_ = output;
+ send (instance);
+ }
+}
+
+void
+GpioShared::send (Gpio &instance)
+{
+ mex::Msg msg (gpio_output_mtype_);
+ msg.push ("BB") << instance.direction_output_ << instance.output_;
+ msg.push (instance.name_, std::strlen (instance.name_));
+ node_.send (msg);
+}
+
+void
+GpioShared::handle_gpio_input (mex::Msg &msg)
+{
+ int input, namelen;
+ msg.pop ("B") >> input;
+ namelen = msg.len ();
+ std::string name (msg.pop (namelen), namelen);
+ Instances::iterator i = instances_.find (name);
+ assert (i != instances_.end ());
+ i->second->input_ = input;
+}
+
+GpioShared *Gpio::shared_;
+
+Gpio::Gpio (Host &host, const char *name)
+ : name_ (name), input_ (false), output_ (false), direction_output_ (false)
+{
+ if (!shared_)
+ {
+ static GpioShared shared (host);
+ shared_ = &shared;
+ }
+ shared_->register_instance (host, *this);
+}
+
+Gpio::Gpio ()
+ : name_ (0), input_ (false), output_ (false), direction_output_ (false)
+{
+}
+
+void
+Gpio::set ()
+{
+ if (name_)
+ shared_->set (*this, true);
+}
+
+void
+Gpio::reset ()
+{
+ if (name_)
+ shared_->set (*this, false);
+}
+
+void
+Gpio::set (bool state)
+{
+ if (name_)
+ shared_->set (*this, state);
+}
+
+void
+Gpio::toggle ()
+{
+ if (name_)
+ shared_->set (*this, !output_);
+}
+
+bool
+Gpio::get () const
+{
+ return input_;
+}
+
+void
+Gpio::input ()
+{
+ if (name_)
+ shared_->output (*this, false);
+}
+
+void
+Gpio::output ()
+{
+ if (name_)
+ shared_->output (*this, true);
+}
+
+} // namespace ucoo
diff --git a/ucoo/hal/gpio/gpio.host.hh b/ucoo/hal/gpio/gpio.host.hh
new file mode 100644
index 0000000..fcd0cbf
--- /dev/null
+++ b/ucoo/hal/gpio/gpio.host.hh
@@ -0,0 +1,69 @@
+#ifndef ucoo_hal_gpio_gpio_host_hh
+#define ucoo_hal_gpio_gpio_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/io.hh"
+#include "ucoo/arch/host/host.hh"
+
+namespace ucoo {
+
+class GpioShared;
+
+/// General purpose input/output on host.
+class Gpio : public Io
+{
+ public:
+ /// Initialise GPIO.
+ Gpio (Host &host, const char *name);
+ /// Initialise GPIO, with no mex support.
+ Gpio ();
+ /// See Io::set.
+ void set ();
+ /// See Io::reset.
+ void reset ();
+ /// See Io::set.
+ void set (bool state);
+ /// See Io::toggle.
+ void toggle ();
+ /// See Io::get.
+ bool get () const;
+ /// See Io::input.
+ void input ();
+ /// See Io::output.
+ void output ();
+ private:
+ /// Name.
+ const char *name_;
+ /// Current input/output states.
+ bool input_, output_;
+ /// Current direction, true for output.
+ bool direction_output_;
+ /// Shared context.
+ static GpioShared *shared_;
+ friend class GpioShared;
+};
+
+} // namespace ucoo
+
+#endif // ucoo_hal_gpio_gpio_host_hh
diff --git a/ucoo/hal/gpio/gpio.stm32f4.cc b/ucoo/hal/gpio/gpio.stm32f4.cc
new file mode 100644
index 0000000..1b46fd5
--- /dev/null
+++ b/ucoo/hal/gpio/gpio.stm32f4.cc
@@ -0,0 +1,105 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 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 "gpio.stm32f4.hh"
+
+namespace ucoo {
+
+void
+Gpio::set ()
+{
+ GPIO_BSRR (port_) = mask_;
+}
+
+void
+Gpio::reset ()
+{
+ GPIO_BSRR (port_) = mask_ << 16;
+}
+
+/// Helper to avoid virtual dance.
+static inline void
+Gpio_set (uint32_t port, uint16_t mask, bool state)
+{
+ if (state)
+ GPIO_BSRR (port) = mask;
+ else
+ GPIO_BSRR (port) = mask << 16;
+}
+
+void
+Gpio::set (bool state)
+{
+ Gpio_set (port_, mask_, state);
+}
+
+void
+Gpio::toggle ()
+{
+ // Avoid read/modify/write ODR, to achieve atomic operation.
+ Gpio_set (port_, mask_, !(GPIO_ODR (port_) & mask_));
+}
+
+bool
+Gpio::get () const
+{
+ return GPIO_IDR (port_) & mask_;
+}
+
+/// Set two bits in a register for the corresponding one-bit mask.
+static uint32_t
+dmask_set (uint32_t mask, uint32_t reg, uint32_t bits)
+{
+ uint32_t dmask = mask * mask;
+ reg &= ~(dmask | dmask << 1);
+ reg |= bits * dmask;
+ return reg;
+}
+
+void
+Gpio::input ()
+{
+ GPIO_MODER (port_) = dmask_set (mask_, GPIO_MODER (port_),
+ GPIO_MODE_INPUT);
+}
+
+void
+Gpio::output ()
+{
+ GPIO_MODER (port_) = dmask_set (mask_, GPIO_MODER (port_),
+ GPIO_MODE_OUTPUT);
+}
+
+void
+Gpio::pull (Pull dir)
+{
+ GPIO_PUPDR (port_) = dmask_set (mask_, GPIO_PUPDR (port_), dir);
+}
+
+void
+Gpio::speed (Speed s)
+{
+ GPIO_OSPEEDR (port_) = dmask_set (mask_, GPIO_OSPEEDR (port_), s);
+}
+
+} // namespace ucoo
diff --git a/ucoo/hal/gpio/gpio.stm32f4.hh b/ucoo/hal/gpio/gpio.stm32f4.hh
new file mode 100644
index 0000000..b2dfb16
--- /dev/null
+++ b/ucoo/hal/gpio/gpio.stm32f4.hh
@@ -0,0 +1,85 @@
+#ifndef ucoo_hal_gpio_gpio_stm32f4_hh
+#define ucoo_hal_gpio_gpio_stm32f4_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 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/io.hh"
+
+#include <libopencm3/stm32/f4/gpio.h>
+
+namespace ucoo {
+
+/// General purpose input/output on STM32F4.
+class Gpio : public Io
+{
+ public:
+ enum Pull
+ {
+ PULL_NONE = GPIO_PUPD_NONE,
+ PULL_UP = GPIO_PUPD_PULLUP,
+ PULL_DOWN = GPIO_PUPD_PULLDOWN,
+ };
+ enum Speed
+ {
+ SPEED_2MHZ = GPIO_OSPEED_2MHZ,
+ SPEED_25MHZ = GPIO_OSPEED_25MHZ,
+ SPEED_50MHZ = GPIO_OSPEED_50MHZ,
+ SPEED_100MHZ = GPIO_OSPEED_100MHZ,
+ };
+ public:
+ /// Constructor, take the PORT base address, and pin BIT number.
+ Gpio (uint32_t port, int bit);
+ /// See Io::set.
+ void set ();
+ /// See Io::reset.
+ void reset ();
+ /// See Io::set.
+ void set (bool state);
+ /// See Io::toggle.
+ void toggle ();
+ /// See Io::get.
+ bool get () const;
+ /// See Io::input.
+ void input ();
+ /// See Io::output.
+ void output ();
+ /// Set pull-up or pull-down.
+ void pull (Pull dir);
+ /// Set output speed.
+ void speed (Speed s);
+ private:
+ /// Port register base address.
+ const uint32_t port_;
+ /// IO bitmask.
+ const uint16_t mask_;
+};
+
+inline
+Gpio::Gpio (uint32_t port, int bit)
+ : port_ (port), mask_ (1u << bit)
+{
+}
+
+} // namespace ucoo
+
+#endif // ucoo_hal_gpio_gpio_stm32f4_hh
diff --git a/ucoo/hal/gpio/test/Makefile b/ucoo/hal/gpio/test/Makefile
new file mode 100644
index 0000000..e2f7692
--- /dev/null
+++ b/ucoo/hal/gpio/test/Makefile
@@ -0,0 +1,9 @@
+BASE = ../../../..
+
+TARGETS = stm32f4
+stm32f4_PROGS = test_gpio
+test_gpio_SOURCES = test_gpio.cc
+
+MODULES = hal/gpio utils
+
+include $(BASE)/build/top.mk
diff --git a/ucoo/hal/gpio/test/test_gpio.cc b/ucoo/hal/gpio/test/test_gpio.cc
new file mode 100644
index 0000000..a953505
--- /dev/null
+++ b/ucoo/hal/gpio/test/test_gpio.cc
@@ -0,0 +1,71 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2012 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/arch/arch.hh"
+#include "ucoo/hal/gpio/gpio.hh"
+#include "ucoo/utils/delay.hh"
+
+#include <libopencm3/stm32/f4/rcc.h>
+
+void
+test (ucoo::Io &loop_out, ucoo::Io &loop_in, ucoo::Io &led3, ucoo::Io &led4,
+ ucoo::Io &led5, ucoo::Io &led6)
+{
+ loop_in.input ();
+ loop_out.output ();
+ led3.output ();
+ led4.output ();
+ led5.output ();
+ led6.output ();
+ led3.set ();
+ led6.reset ();
+ bool state = false;
+ while (1)
+ {
+ led3.toggle ();
+ led4.set (state);
+ led5.set (loop_in.get ());
+ led6.toggle ();
+ state = !state;
+ loop_out.set (state);
+ ucoo::delay (1);
+ }
+}
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPBEN
+ | RCC_AHB1ENR_IOPDEN);
+ // For this test, shorten B6 & B7 to have loopback.
+ gpio_mode_setup (GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, GPIO7);
+ ucoo::Gpio loop_out (GPIOB, 6);
+ ucoo::Gpio loop_in (GPIOB, 7);
+ ucoo::Gpio led3 (GPIOD, 13);
+ ucoo::Gpio led4 (GPIOD, 12);
+ ucoo::Gpio led5 (GPIOD, 14);
+ ucoo::Gpio led6 (GPIOD, 15);
+ test (loop_out, loop_in, led3, led4, led5, led6);
+ return 0;
+}