summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--digital/ucoolib/ucoolib/hal/adc/Module1
-rw-r--r--digital/ucoolib/ucoolib/hal/adc/adc.hh35
-rw-r--r--digital/ucoolib/ucoolib/hal/adc/adc.host.cc92
-rw-r--r--digital/ucoolib/ucoolib/hal/adc/adc.host.hh52
-rw-r--r--digital/ucoolib/ucoolib/hal/adc/adc_hard.stm32f4.cc69
-rw-r--r--digital/ucoolib/ucoolib/hal/adc/adc_hard.stm32f4.hh73
-rw-r--r--digital/ucoolib/ucoolib/hal/adc/test/Makefile9
-rw-r--r--digital/ucoolib/ucoolib/hal/adc/test/test_adc.cc51
-rw-r--r--digital/ucoolib/ucoolib/intf/adc.hh44
-rw-r--r--host/simu/link/mex_adc_channel.py63
10 files changed, 489 insertions, 0 deletions
diff --git a/digital/ucoolib/ucoolib/hal/adc/Module b/digital/ucoolib/ucoolib/hal/adc/Module
new file mode 100644
index 00000000..f633ffde
--- /dev/null
+++ b/digital/ucoolib/ucoolib/hal/adc/Module
@@ -0,0 +1 @@
+hal_adc_SOURCES = adc.host.cc adc_hard.stm32f4.cc
diff --git a/digital/ucoolib/ucoolib/hal/adc/adc.hh b/digital/ucoolib/ucoolib/hal/adc/adc.hh
new file mode 100644
index 00000000..c60f4982
--- /dev/null
+++ b/digital/ucoolib/ucoolib/hal/adc/adc.hh
@@ -0,0 +1,35 @@
+#ifndef ucoolib_hal_adc_adc_hh
+#define ucoolib_hal_adc_adc_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+
+#if defined (TARGET_host)
+# include "adc.host.hh"
+#elif defined (TARGET_stm32f4)
+# include "adc_hard.stm32f4.hh"
+#else
+# error "not implemented for this target"
+#endif
+
+#endif // ucoolib_hal_adc_adc_hh
diff --git a/digital/ucoolib/ucoolib/hal/adc/adc.host.cc b/digital/ucoolib/ucoolib/hal/adc/adc.host.cc
new file mode 100644
index 00000000..d6aa2695
--- /dev/null
+++ b/digital/ucoolib/ucoolib/hal/adc/adc.host.cc
@@ -0,0 +1,92 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "adc.host.hh"
+
+#include <string>
+
+namespace ucoo {
+
+/// Shared context between instances.
+class AdcHostShared
+{
+ public:
+ /// Constructor, connect to mex node.
+ AdcHostShared (Host &host);
+ /// Register a new instance.
+ void register_instance (Host &host, AdcHost &instance, const char *name);
+ private:
+ /// Handle value from Mex.
+ void handle_adc_channel (mex::Msg &msg);
+ private:
+ Host &host_;
+ typedef std::map<std::string, AdcHost *> Instances;
+ Instances instances_;
+};
+
+AdcHostShared::AdcHostShared (Host &host)
+ : host_ (host)
+{
+ std::string instance = host_.get_instance ();
+ mex::Node &node = host_.get_node ();
+ mex::mtype_t mtype = node.reserve (instance + ":adc_channel");
+ node.handler_register (mtype, *this, &AdcHostShared::handle_adc_channel);
+}
+
+void
+AdcHostShared::register_instance (Host &host, AdcHost &instance,
+ const char *name)
+{
+ assert (&host == &host_);
+ std::pair<Instances::iterator, bool> r =
+ instances_.insert (Instances::value_type (name, &instance));
+ assert (r.second);
+}
+
+void
+AdcHostShared::handle_adc_channel (mex::Msg &msg)
+{
+ int ivalue, namelen;
+ msg.pop ("l") >> ivalue;
+ namelen = msg.len ();
+ std::string name (msg.pop (namelen), namelen);
+ Instances::iterator i = instances_.find (name);
+ assert (i != instances_.end ());
+ double fvalue = ivalue / (double) (1u << 31);
+ i->second->value_ = fvalue * i->second->resolution_;
+}
+
+AdcHostShared *AdcHost::shared_;
+
+AdcHost::AdcHost (Host &host, const char *name, int resolution)
+ : resolution_ (resolution), value_ (0)
+{
+ if (!shared_)
+ {
+ static AdcHostShared shared (host);
+ shared_ = &shared;
+ }
+ shared_->register_instance (host, *this, name);
+}
+
+} // namespace ucoo
diff --git a/digital/ucoolib/ucoolib/hal/adc/adc.host.hh b/digital/ucoolib/ucoolib/hal/adc/adc.host.hh
new file mode 100644
index 00000000..28522106
--- /dev/null
+++ b/digital/ucoolib/ucoolib/hal/adc/adc.host.hh
@@ -0,0 +1,52 @@
+#ifndef ucoolib_hal_adc_adc_host_hh
+#define ucoolib_hal_adc_adc_host_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "ucoolib/intf/adc.hh"
+#include "ucoolib/arch/host/host.hh"
+
+namespace ucoo {
+
+class AdcHostShared;
+
+/// Host ADC channel.
+class AdcHost : public Adc
+{
+ public:
+ /// Constructor.
+ AdcHost (Host &host, const char *name, int resolution);
+ /// See Adc::read.
+ int read () { return value_; }
+ /// See Adc::get_resolution.
+ int get_resolution () const { return resolution_; }
+ private:
+ int resolution_;
+ int value_;
+ static AdcHostShared *shared_;
+ friend class AdcHostShared;
+};
+
+} // namespace ucoo
+
+#endif // ucoolib_hal_adc_adc_host_hh
diff --git a/digital/ucoolib/ucoolib/hal/adc/adc_hard.stm32f4.cc b/digital/ucoolib/ucoolib/hal/adc/adc_hard.stm32f4.cc
new file mode 100644
index 00000000..83235366
--- /dev/null
+++ b/digital/ucoolib/ucoolib/hal/adc/adc_hard.stm32f4.cc
@@ -0,0 +1,69 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "adc_hard.stm32f4.hh"
+
+#include <libopencm3/stm32/f4/adc.h>
+#include <libopencm3/stm32/f4/rcc.h>
+
+namespace ucoo {
+
+AdcHard::AdcHard (int n)
+ : n_ (n)
+{
+ static const uint32_t bases[] = { ADC1, ADC2, ADC3 };
+ assert (n < (int) lengthof (bases));
+ base_ = bases[n];
+}
+
+AdcHard::~AdcHard ()
+{
+ disable ();
+}
+
+void
+AdcHard::enable ()
+{
+ rcc_peripheral_enable_clock (&RCC_APB2ENR, RCC_APB2ENR_ADC1EN << n_);
+ ADC_CR2 (base_) = ADC_CR2_ADON;
+}
+
+void
+AdcHard::disable ()
+{
+ ADC_CR2 (base_) = 0;
+ rcc_peripheral_disable_clock (&RCC_APB2ENR, RCC_APB2ENR_ADC1EN << n_);
+}
+
+int
+AdcHard::read (int channel)
+{
+ ADC_SQR3 (base_) = channel;
+ ADC_CR2 (base_) |= ADC_CR2_SWSTART;
+ while (!(ADC_SR (base_) & ADC_SR_EOC))
+ yield ();
+ ADC_SR (base_) = ~ADC_SR_EOC;
+ return ADC_DR (base_);
+}
+
+} // namespace ucoo
diff --git a/digital/ucoolib/ucoolib/hal/adc/adc_hard.stm32f4.hh b/digital/ucoolib/ucoolib/hal/adc/adc_hard.stm32f4.hh
new file mode 100644
index 00000000..b81ad717
--- /dev/null
+++ b/digital/ucoolib/ucoolib/hal/adc/adc_hard.stm32f4.hh
@@ -0,0 +1,73 @@
+#ifndef ucoolib_hal_adc_adc_hard_stm32f4_hh
+#define ucoolib_hal_adc_adc_hard_stm32f4_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "ucoolib/intf/adc.hh"
+#include "ucoolib/common.hh"
+
+namespace ucoo {
+
+/// ADC interface. This control a full ADC, use AdcHardChannel for a single
+/// channel.
+class AdcHard
+{
+ public:
+ static const int resolution = 1 << 12;
+ public:
+ /// Constructor for the Nth ADC.
+ AdcHard (int n);
+ /// Shutdown.
+ ~AdcHard ();
+ /// Enable, power on.
+ void enable ();
+ /// Disable.
+ void disable ();
+ /// Make a single measure.
+ int read (int channel);
+ private:
+ /// ADC index.
+ int n_;
+ /// ADC base address.
+ uint32_t base_;
+};
+
+/// Single ADC channel.
+class AdcHardChannel : public Adc
+{
+ public:
+ /// Constructor.
+ AdcHardChannel (AdcHard &adc, int channel)
+ : adc_ (adc), channel_ (channel) { }
+ /// See Adc::read.
+ int read () { return adc_.read (channel_); }
+ /// See Adc::get_resolution.
+ int get_resolution () const { return AdcHard::resolution; }
+ private:
+ AdcHard &adc_;
+ int channel_;
+};
+
+} // namespace ucoo
+
+#endif // ucoolib_hal_adc_adc_hard_stm32f4_hh
diff --git a/digital/ucoolib/ucoolib/hal/adc/test/Makefile b/digital/ucoolib/ucoolib/hal/adc/test/Makefile
new file mode 100644
index 00000000..fc46e0dd
--- /dev/null
+++ b/digital/ucoolib/ucoolib/hal/adc/test/Makefile
@@ -0,0 +1,9 @@
+BASE = ../../../..
+
+TARGETS = stm32f4
+stm32f4_PROGS = test_adc
+test_adc_SOURCES = test_adc.cc
+
+MODULES = hal/adc base/test hal/usb utils
+
+include $(BASE)/build/top.mk
diff --git a/digital/ucoolib/ucoolib/hal/adc/test/test_adc.cc b/digital/ucoolib/ucoolib/hal/adc/test/test_adc.cc
new file mode 100644
index 00000000..75a120fb
--- /dev/null
+++ b/digital/ucoolib/ucoolib/hal/adc/test/test_adc.cc
@@ -0,0 +1,51 @@
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+#include "ucoolib/hal/adc/adc.hh"
+
+#include "ucoolib/arch/arch.hh"
+#include "ucoolib/base/test/test.hh"
+#include "ucoolib/utils/delay.hh"
+
+#include <libopencm3/stm32/f4/adc.h>
+#include "ucoolib/hal/gpio/gpio.hh"
+
+#include <cstdio>
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::test_stream_setup ();
+ // Have fun with temperature sensor.
+ ucoo::AdcHard adc (0);
+ adc.enable ();
+ ADC_CCR = ADC_CCR_TSVREFE;
+ ucoo::AdcHardChannel c (adc, 16);
+ while (1)
+ {
+ int r = c.read ();
+ std::printf ("ADC = %d\n", r);
+ ucoo::delay (0.5);
+ }
+}
diff --git a/digital/ucoolib/ucoolib/intf/adc.hh b/digital/ucoolib/ucoolib/intf/adc.hh
new file mode 100644
index 00000000..a002188f
--- /dev/null
+++ b/digital/ucoolib/ucoolib/intf/adc.hh
@@ -0,0 +1,44 @@
+#ifndef ucoolib_intf_adc_hh
+#define ucoolib_intf_adc_hh
+// ucoolib - Microcontroller object oriented library. {{{
+//
+// Copyright (C) 2013 Nicolas Schodet
+//
+// APBTeam:
+// Web: http://apbteam.org/
+// Email: team AT apbteam DOT org
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+
+namespace ucoo {
+
+/// Analog to digital converter. This is a really simple interface which
+/// should be enough for simple needs.
+class Adc
+{
+ /// Make a conversion and return the value.
+ virtual int read () = 0;
+ /// Get resolution (number of possible different values).
+ virtual int get_resolution () const = 0;
+ protected:
+ /// Default constructor.
+ Adc () { }
+};
+
+} // namespace ucoo
+
+#endif // ucoolib_intf_adc_hh
diff --git a/host/simu/link/mex_adc_channel.py b/host/simu/link/mex_adc_channel.py
new file mode 100644
index 00000000..3c0321d4
--- /dev/null
+++ b/host/simu/link/mex_adc_channel.py
@@ -0,0 +1,63 @@
+# simu - Robot simulation. {{{
+#
+# Copyright (C) 2013 Nicolas Schodet
+#
+# APBTeam:
+# Web: http://apbteam.org/
+# Email: team AT apbteam DOT org
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# }}}
+"""MEX ADC link."""
+from utils.observable import Observable
+import mex.msg
+
+class MexAdcChannel (Observable):
+ """Analog to Digital Converter channel.
+
+ - value: input value, in range [0.0, 1.0].
+
+ """
+
+ def __init__ (self, pack, name):
+ Observable.__init__ (self)
+ self.pack = pack
+ assert name not in self.pack.channels
+ self.pack.channels[name] = self
+ self.name = name
+ self.value = 0.0
+ self.register (self.__notified)
+
+ def __notified (self):
+ self.pack.send (self)
+
+ class Pack:
+ """Centralise ADC handling."""
+
+ def __init__ (self, node, instance):
+ self.node = node
+ self.mtype = node.reserve (instance + ':adc_channel')
+ self.channels = { }
+
+ def send (self, channel):
+ ivalue = int ((1 << 31) * channel.value)
+ ivalue = min (ivalue, (1 << 31) - 1)
+ ivalue = max (ivalue, 0)
+ m = mex.msg.Msg (self.mtype)
+ namelen = len (channel.name)
+ m.push ('l%ds' % namelen, ivalue, channel.name)
+ self.node.send (m)
+