summaryrefslogtreecommitdiff
path: root/ucoo/intf
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/intf
parentd415a0646e874848cd06482c5d57916cca5cff4a (diff)
Rename ucoolib modules directory from ucoolib to ucoo
Diffstat (limited to 'ucoo/intf')
-rw-r--r--ucoo/intf/Module1
-rw-r--r--ucoo/intf/adc.hh45
-rw-r--r--ucoo/intf/i2c.hh124
-rw-r--r--ucoo/intf/io.hh54
-rw-r--r--ucoo/intf/spi_master.cc48
-rw-r--r--ucoo/intf/spi_master.hh70
-rw-r--r--ucoo/intf/stream.cc63
-rw-r--r--ucoo/intf/stream.hh65
8 files changed, 470 insertions, 0 deletions
diff --git a/ucoo/intf/Module b/ucoo/intf/Module
new file mode 100644
index 0000000..7477f00
--- /dev/null
+++ b/ucoo/intf/Module
@@ -0,0 +1 @@
+intf_SOURCES = stream.cc spi_master.cc
diff --git a/ucoo/intf/adc.hh b/ucoo/intf/adc.hh
new file mode 100644
index 0000000..83c0520
--- /dev/null
+++ b/ucoo/intf/adc.hh
@@ -0,0 +1,45 @@
+#ifndef ucoo_intf_adc_hh
+#define ucoo_intf_adc_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.
+//
+// }}}
+
+namespace ucoo {
+
+/// Analog to digital converter. This is a really simple interface which
+/// should be enough for simple needs.
+class Adc
+{
+ public:
+ /// 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 // ucoo_intf_adc_hh
diff --git a/ucoo/intf/i2c.hh b/ucoo/intf/i2c.hh
new file mode 100644
index 0000000..7d3a624
--- /dev/null
+++ b/ucoo/intf/i2c.hh
@@ -0,0 +1,124 @@
+#ifndef ucoo_intf_i2c_hh
+#define ucoo_intf_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.
+//
+// }}}
+#include "ucoo/common.hh"
+
+namespace ucoo {
+
+/// Master side of I2C interface.
+class I2cMaster
+{
+ public:
+ /// Master transfer status, any other value is the transfer size in case
+ /// of success.
+ enum Status
+ {
+ /// Transfer is still ongoing.
+ STATUS_BUSY = -1,
+ /// Transfer finished with error.
+ STATUS_ERROR = 0,
+ };
+ /// Callback used when master transfer is finised.
+ class FinishedHandler
+ {
+ public:
+ /// Called when transfer is finished, may be called in interrupt
+ /// context. I2C send and recv can be called from this handler.
+ virtual void finished (int status) = 0;
+ };
+ public:
+ /// Send data to selected slave. This is asynchronous, you will have to
+ /// poll status or use a callback to determine if transfer is finished.
+ /// Input buffer is not copied and should be kept valid until transfer is
+ /// finished.
+ ///
+ /// If asynchronous transfer is not supported, this will block.
+ virtual void send (uint8_t addr, const char *buf, int count) = 0;
+ /// Receive data from selected slave. This is asynchronous, you will have
+ /// to poll status or use a callback to determine if transfer is finished.
+ /// Buffer is directly written and should be kept valid until transfer is
+ /// finished.
+ ///
+ /// If asynchronous transfer is not supported, this will block.
+ virtual void recv (uint8_t addr, char *buf, int count) = 0;
+ /// Return last transfer status.
+ virtual int status () = 0;
+ /// Wait until transfer is finished and return status.
+ virtual int wait () = 0;
+ /// Register a handler called when transfer is finished.
+ void register_finished (FinishedHandler &finished_handler)
+ { finished_handler_ = &finished_handler; }
+ /// Remove registered handler.
+ void unregister_finished (void)
+ { finished_handler_ = 0; }
+ protected:
+ /// Default constructor.
+ I2cMaster () : finished_handler_ (0) { }
+ protected:
+ /// Handler called when transfer is finished.
+ FinishedHandler *finished_handler_;
+};
+
+/// Slave side of I2C interface.
+class I2cSlave
+{
+ public:
+ /// Data source and sink.
+ ///
+ /// When master requests data from a slave or send data to it, slave is
+ /// expected to reply as quickly as possible as the bus is stalled until a
+ /// response is done.
+ ///
+ /// This interface provides data to send to master and receives data from
+ /// master. It has to be fast and may be run under an interrupt context.
+ class DataHandler
+ {
+ public:
+ /// Request data to send to master, should write to provided buffer of
+ /// size COUNT and return the writen size.
+ virtual int to_send (char *buf, int count) = 0;
+ /// Provide data sent by master, should make a copy if needed as data will
+ /// be discarded.
+ virtual void to_recv (const char *buf, int count) = 0;
+ };
+ public:
+ /// Register data source and sink, along with 7 bit address (in MSB).
+ virtual void register_data (uint8_t addr, DataHandler &data_handler) = 0;
+ protected:
+ /// Default constructor.
+ I2cSlave () { }
+};
+
+/// Both side of I2C interface.
+class I2c : public I2cMaster, public I2cSlave
+{
+ protected:
+ /// Default constructor.
+ I2c () { }
+};
+
+} // namespace ucoo
+
+#endif // ucoo_intf_i2c_hh
diff --git a/ucoo/intf/io.hh b/ucoo/intf/io.hh
new file mode 100644
index 0000000..34246a1
--- /dev/null
+++ b/ucoo/intf/io.hh
@@ -0,0 +1,54 @@
+#ifndef ucoo_intf_io_hh
+#define ucoo_intf_io_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.
+//
+// }}}
+
+namespace ucoo {
+
+/// One bit input/output interface.
+class Io
+{
+ public:
+ /// Set output to 1.
+ virtual void set () = 0;
+ /// Reset output to 0.
+ virtual void reset () = 0;
+ /// Set or reset output.
+ virtual void set (bool state) = 0;
+ /// Toggle output.
+ virtual void toggle () = 0;
+ /// Get input state (not the same as output latch!).
+ virtual bool get () const = 0;
+ /// Set as input.
+ virtual void input () = 0;
+ /// Set as output.
+ virtual void output () = 0;
+ protected:
+ /// Default constructor.
+ Io () { }
+};
+
+} // namespace ucoo
+
+#endif // ucoo_intf_io_hh
diff --git a/ucoo/intf/spi_master.cc b/ucoo/intf/spi_master.cc
new file mode 100644
index 0000000..38f749b
--- /dev/null
+++ b/ucoo/intf/spi_master.cc
@@ -0,0 +1,48 @@
+// 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_master.hh"
+
+namespace ucoo {
+
+char
+SpiMaster::send_and_recv (char tx)
+{
+ char rx;
+ send_and_recv (&tx, &rx, 1);
+ return rx;
+}
+
+void
+SpiMaster::send (char tx)
+{
+ send_and_recv (tx);
+}
+
+char
+SpiMaster::recv ()
+{
+ return send_and_recv (0);
+}
+
+} // namespace ucoo
diff --git a/ucoo/intf/spi_master.hh b/ucoo/intf/spi_master.hh
new file mode 100644
index 0000000..5363aee
--- /dev/null
+++ b/ucoo/intf/spi_master.hh
@@ -0,0 +1,70 @@
+#ifndef ucoo_intf_spi_master_hh
+#define ucoo_intf_spi_master_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.
+//
+// }}}
+
+namespace ucoo {
+
+/// SPI clock polarity and phase modes.
+enum SpiMode
+{
+ /// Mode 0, sample on rising edge, setup on falling edge.
+ SPI_MODE_0,
+ /// Mode 1, setup on rising edge, sample on falling edge.
+ SPI_MODE_1,
+ /// Mode 2, sample on falling edge, setup on rising edge.
+ SPI_MODE_2,
+ /// Mode 3, setup on falling edge, sample on rising edge.
+ SPI_MODE_3,
+};
+
+/// Interface to the master side of an SPI bus. Do not handle slave select,
+/// this should be done with a regular IO.
+class SpiMaster
+{
+ public:
+ /// Send and receive COUNT bytes of data. Send data from TX_BUF, store
+ /// received data in RX_BUF. TX_BUF and RX_BUF can be the same memory.
+ virtual void send_and_recv (const char *tx_buf, char *rx_buf, int count)
+ = 0;
+ /// Send and receive one byte, shortcut.
+ virtual char send_and_recv (char tx);
+ /// Send, and ignore received data. Same as send_and_recv, but received
+ /// data is discarded.
+ virtual void send (const char *tx_buf, int count) = 0;
+ /// Send one byte, shortcut.
+ virtual void send (char tx);
+ /// Send zeros, and receive data. Same as send_and_recv, but transmit
+ /// zeros.
+ virtual void recv (char *rx_buf, int count) = 0;
+ /// Receive one byte, shortcut.
+ virtual char recv ();
+ protected:
+ /// Default constructor.
+ SpiMaster () { }
+};
+
+} // namespace ucoo
+
+#endif // ucoo_intf_spi_master_hh
diff --git a/ucoo/intf/stream.cc b/ucoo/intf/stream.cc
new file mode 100644
index 0000000..31cea01
--- /dev/null
+++ b/ucoo/intf/stream.cc
@@ -0,0 +1,63 @@
+// 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 "stream.hh"
+
+namespace ucoo {
+
+Stream::Stream ()
+ : block_ (true)
+{
+}
+
+void
+Stream::block (bool block/*true*/)
+{
+ block_ = block;
+}
+
+int
+Stream::getc ()
+{
+ int r;
+ char c;
+ r = read (&c, 1);
+ if (r == 1)
+ return static_cast<unsigned char> (c);
+ else
+ return -1;
+}
+
+int
+Stream::putc (int c)
+{
+ int r;
+ char b = c;
+ r = write (&b, 1);
+ if (r == 1)
+ return 0;
+ else
+ return -1;
+}
+
+} // namespace ucoo
diff --git a/ucoo/intf/stream.hh b/ucoo/intf/stream.hh
new file mode 100644
index 0000000..77eba92
--- /dev/null
+++ b/ucoo/intf/stream.hh
@@ -0,0 +1,65 @@
+#ifndef ucoo_intf_stream_hh
+#define ucoo_intf_stream_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.
+//
+// }}}
+
+namespace ucoo {
+
+/// Interface to an object providing a stream oriented flow, like serial port,
+/// connected socket, or higher encapsulated protocol.
+class Stream
+{
+ public:
+ /// Set whether operations on this stream should block when no data is
+ /// available.
+ virtual void block (bool block = true);
+ /// Read up to COUNT bytes of data from stream and store them in BUF.
+ /// Return the number of read bytes, -1 on error or -2 on EOF.
+ ///
+ /// If blocking, will return as soon as there is some data available. If
+ /// not blocking, may return 0 if no data is available.
+ virtual int read (char *buf, int count) = 0;
+ /// Write up to COUNT bytes of data from BUF to stream. Return the number
+ /// of written bytes or -1 on error.
+ ///
+ /// If blocking, will try its best to write all data provided.
+ virtual int write (const char *buf, int count) = 0;
+ /// Shortcut to read one character. Return -1 on error, on EOF, or if no
+ /// character is available.
+ int getc ();
+ /// Shortcut to write one character. Return -1 on error.
+ int putc (int c);
+ /// Return the number of available bytes to be read.
+ virtual int poll () = 0;
+ protected:
+ /// Default constructor.
+ Stream ();
+ protected:
+ /// Current blocking operation flag.
+ bool block_;
+};
+
+} // namespace ucoo
+
+#endif // ucoo_intf_stream_hh