summaryrefslogtreecommitdiff
path: root/ucoo/utils
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/utils
parentd415a0646e874848cd06482c5d57916cca5cff4a (diff)
Rename ucoolib modules directory from ucoolib to ucoo
Diffstat (limited to 'ucoo/utils')
-rw-r--r--ucoo/utils/Module1
-rw-r--r--ucoo/utils/bytes.hh52
-rw-r--r--ucoo/utils/crc.cc102
-rw-r--r--ucoo/utils/crc.hh70
-rw-r--r--ucoo/utils/delay.arm.cc76
-rw-r--r--ucoo/utils/delay.arm.hh52
-rw-r--r--ucoo/utils/delay.hh76
-rw-r--r--ucoo/utils/dtrace.py40
-rw-r--r--ucoo/utils/fifo.hh76
-rw-r--r--ucoo/utils/fifo.tcc108
-rw-r--r--ucoo/utils/test/Makefile12
-rw-r--r--ucoo/utils/test/test_crc.cc49
-rw-r--r--ucoo/utils/test/test_delay.cc64
-rw-r--r--ucoo/utils/test/test_fifo.cc78
-rw-r--r--ucoo/utils/trace.hh133
15 files changed, 989 insertions, 0 deletions
diff --git a/ucoo/utils/Module b/ucoo/utils/Module
new file mode 100644
index 0000000..a37070f
--- /dev/null
+++ b/ucoo/utils/Module
@@ -0,0 +1 @@
+utils_SOURCES := delay.arm.cc crc.cc
diff --git a/ucoo/utils/bytes.hh b/ucoo/utils/bytes.hh
new file mode 100644
index 0000000..f922720
--- /dev/null
+++ b/ucoo/utils/bytes.hh
@@ -0,0 +1,52 @@
+#ifndef ucoo_utils_bytes_hh
+#define ucoo_utils_bytes_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 {
+
+/// Pack two bytes to one 16 bit word.
+static inline uint16_t
+bytes_pack (uint8_t b1, uint8_t b0)
+{
+ return b1 << 8 | b0;
+}
+
+/// Pack four bytes to one 32 bit word.
+static inline uint32_t
+bytes_pack (uint8_t b3, uint8_t b2, uint8_t b1, uint8_t b0)
+{
+ return b3 << 24 | b2 << 16 | b1 << 8 | b0;
+}
+
+/// Unpack one byte from one 32 bit word.
+static inline uint8_t
+bytes_unpack (uint32_t w, int n)
+{
+ return (w >> (n * 8)) & 0xff;
+}
+
+} // namespace ucoo
+
+#endif // ucoo_utils_bytes_hh
diff --git a/ucoo/utils/crc.cc b/ucoo/utils/crc.cc
new file mode 100644
index 0000000..2627a1d
--- /dev/null
+++ b/ucoo/utils/crc.cc
@@ -0,0 +1,102 @@
+// 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 "crc.hh"
+
+namespace ucoo {
+
+uint8_t
+crc8_compute (const uint8_t *data, int size)
+{
+ uint8_t crc = 0;
+ for (int i = 0; i < size; i++)
+ crc = crc8_update (crc, data[i]);
+ return crc;
+}
+
+static uint32_t const crc32tab[256] =
+{
+ // {{{
+ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
+ 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
+ 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
+ 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
+ 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
+ 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
+ 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
+ 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
+ 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
+ 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
+ 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
+ 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
+ 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
+ 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
+ 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
+ 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
+ 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
+ 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
+ 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
+ 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
+ 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
+ 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
+ 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
+ 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
+ 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
+ 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
+ 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
+ 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
+ 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
+ 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
+ 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
+ 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
+ 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
+ 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
+ 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
+ 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
+ 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
+ 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
+ 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
+ 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
+ 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
+ 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
+ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
+ // }}}
+};
+
+uint32_t
+crc32_update (uint32_t crc, uint8_t data)
+{
+ return crc32tab[(crc ^ data) & 0xff] ^ (crc >> 8);
+}
+
+uint32_t
+crc32_compute (const uint8_t *data, int size)
+{
+ uint32_t crc = 0xffffffff;
+ for (int i = 0; i < size; i++)
+ crc = crc32_update (crc, data[i]);
+ crc = crc ^ 0xffffffff;
+ return crc;
+}
+
+} // namespace ucoo
diff --git a/ucoo/utils/crc.hh b/ucoo/utils/crc.hh
new file mode 100644
index 0000000..b00989c
--- /dev/null
+++ b/ucoo/utils/crc.hh
@@ -0,0 +1,70 @@
+#ifndef ucoo_utils_crc_hh
+#define ucoo_utils_crc_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"
+
+/// Please read "A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS",
+/// http://www.ross.net/crc/download/crc_v3.txt
+
+namespace ucoo {
+
+/// Dallas/Maxim iButton 8bit CRC.
+/// Polynomial: x^8 + x^5 + x^4 + 1
+/// Initial value: 0
+static inline uint8_t
+crc8_update (uint8_t crc, uint8_t data)
+{
+ uint8_t i;
+ crc = crc ^ data;
+ for (i = 0; i < 8; i++)
+ {
+ if (crc & 0x01)
+ crc = (crc >> 1) ^ 0x8C;
+ else
+ crc >>= 1;
+ }
+ return crc;
+}
+
+uint8_t
+crc8_compute (const uint8_t *data, int size);
+
+/// Name : "CRC-32"
+/// Width : 32
+/// Poly : 04C11DB7
+/// Init : FFFFFFFF
+/// RefIn : True
+/// RefOut : True
+/// XorOut : FFFFFFFF
+/// Check : CBF43926
+uint32_t
+crc32_update (uint32_t crc, uint8_t data);
+
+uint32_t
+crc32_compute (const uint8_t *data, int size);
+
+} // namespace ucoo
+
+#endif // ucoo_utils_crc_hh
diff --git a/ucoo/utils/delay.arm.cc b/ucoo/utils/delay.arm.cc
new file mode 100644
index 0000000..294f5ea
--- /dev/null
+++ b/ucoo/utils/delay.arm.cc
@@ -0,0 +1,76 @@
+// 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 "delay.arm.hh"
+
+#include <algorithm>
+
+#include <libopencm3/cm3/systick.h>
+#include <libopencm3/stm32/f4/rcc.h>
+
+namespace ucoo {
+
+void
+delay_us (int us)
+{
+ // Horrible hack: there is no record of the current systick speed, make
+ // guesses based on APB2 clock. Also, suppose that frequency is a multiple
+ // of 1 MHz (to avoid 64 bit division).
+ int systick_mhz = rcc_ppre2_frequency / (4 * 1000000);
+ int cycles = systick_mhz * us;
+ STK_CTRL = 0;
+ // Loop several times if cycles is too big for the systick timer. Some
+ // nanoseconds are lost every second, I can live with that, it simplifies
+ // code.
+ while (cycles)
+ {
+ int loop_cycles = std::min (1 << 24, cycles);
+ STK_LOAD = loop_cycles - 1;
+ STK_VAL = 0;
+ STK_CTRL = STK_CTRL_ENABLE;
+ while (!(STK_CTRL & STK_CTRL_COUNTFLAG))
+ ;
+ STK_CTRL = 0;
+ cycles -= loop_cycles;
+ }
+}
+
+void
+delay_ns (int ns)
+{
+ // Horrible hack: there is no record of the current hclock speed, make
+ // guesses based on APB2 clock. Also, suppose that frequency is a multiple
+ // of 1 MHz (to avoid 64 bit division).
+ int hclock_mhz = rcc_ppre2_frequency / (1000000 / 2);
+ int cycles = (hclock_mhz * ns + 999) / 1000;
+ STK_CTRL = 0;
+ // Loop once, ns is supposed to be small.
+ STK_LOAD = cycles - 1;
+ STK_VAL = 0;
+ STK_CTRL = STK_CTRL_CLKSOURCE_AHB | STK_CTRL_ENABLE;
+ while (!(STK_CTRL & STK_CTRL_COUNTFLAG))
+ ;
+ STK_CTRL = 0;
+}
+
+} // namespace ucoo
diff --git a/ucoo/utils/delay.arm.hh b/ucoo/utils/delay.arm.hh
new file mode 100644
index 0000000..53d2c7e
--- /dev/null
+++ b/ucoo/utils/delay.arm.hh
@@ -0,0 +1,52 @@
+#ifndef ucoo_utils_delay_arm_hh
+#define ucoo_utils_delay_arm_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 {
+
+/// Wait for an integral number of microseconds, used to implement delay.
+void
+delay_us (int us);
+
+/// Wait for a small integral number of nanoseconds, used to implement short
+/// delay.
+void
+delay_ns (int us);
+
+/// Wait for the specified delay in seconds.
+extern inline void
+delay (double s)
+{
+ if (s < 1e-6)
+ delay_ns (static_cast<int> (s * 1e9));
+ else
+ delay_us (static_cast<int> (s * 1e6));
+}
+extern inline void
+delay (double s) __attribute__ ((always_inline));
+
+} // namespace ucoo
+
+#endif // ucoo_utils_delay_arm_hh
diff --git a/ucoo/utils/delay.hh b/ucoo/utils/delay.hh
new file mode 100644
index 0000000..cc8cea3
--- /dev/null
+++ b/ucoo/utils/delay.hh
@@ -0,0 +1,76 @@
+#ifndef ucoo_utils_delay_hh
+#define ucoo_utils_delay_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)
+
+namespace ucoo {
+
+/// No delay on host.
+extern inline void
+delay (double s)
+{
+}
+
+} // namespace ucoo
+
+#elif defined (TARGET_arm)
+# include "delay.arm.hh"
+#else
+# error "not implemented for this target"
+#endif
+
+namespace ucoo {
+
+/// Shortcut for delay with nanosecond.
+extern inline void
+delay_ns (double ns)
+{
+ delay (ns * 1e-9);
+}
+extern inline void
+delay_ns (double ns) __attribute__ ((always_inline));
+
+/// Shortcut for delay with microsecond.
+extern inline void
+delay_us (double us)
+{
+ delay (us * 1e-6);
+}
+extern inline void
+delay_us (double us) __attribute__ ((always_inline));
+
+/// Shortcut for delay with millisecond.
+extern inline void
+delay_ms (double ms)
+{
+ delay (ms * 1e-3);
+}
+extern inline void
+delay_ms (double ms) __attribute__ ((always_inline));
+
+} // namespace ucoo
+
+#endif // ucoo_utils_delay_hh
diff --git a/ucoo/utils/dtrace.py b/ucoo/utils/dtrace.py
new file mode 100644
index 0000000..4180718
--- /dev/null
+++ b/ucoo/utils/dtrace.py
@@ -0,0 +1,40 @@
+"""GDB plugin to dump trace from a Trace object."""
+
+class DTrace(gdb.Command):
+ def __init__(self):
+ gdb.Command.__init__(self, "dtrace", gdb.COMMAND_DATA, gdb.COMPLETE_SYMBOL, True)
+
+ def invoke(self, arg, from_tty):
+ args = gdb.string_to_argv(arg)
+ if len(args) != 1:
+ print 'Usage: dtrace <object>'
+ return
+
+ trace = gdb.parse_and_eval(args[0])
+ args_nb = trace['args_nb'];
+ entries_nb = trace['entries_nb'];
+ index = trace['index'];
+ entries = trace['entries'];
+
+ if entries[index]['str']:
+ r = range(index, entries_nb) + range(index)
+ else:
+ r = range(index)
+
+ for i in r:
+ entry = entries[i]
+ if not entry['str']:
+ break
+ s = entry['str'].string()
+ sargs = s.count('%') - 2 * s.count('%%')
+ args = tuple(entry['args'][i] for i in xrange(sargs))
+ try:
+ s = s % args
+ except:
+ pass
+ if s.startswith('<'):
+ n = int(s[1:].partition('>')[0])
+ s = ' ' * (n * 10) + s
+ print s
+
+DTrace()
diff --git a/ucoo/utils/fifo.hh b/ucoo/utils/fifo.hh
new file mode 100644
index 0000000..f80caf9
--- /dev/null
+++ b/ucoo/utils/fifo.hh
@@ -0,0 +1,76 @@
+#ifndef ucoo_utils_fifo_hh
+#define ucoo_utils_fifo_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/common.hh"
+
+namespace ucoo {
+
+/// First In First Out container. It is implemented as a circular buffer,
+/// providing thread safety if the reader and the writer are in separated
+/// threads.
+///
+/// This is a rather low level container used to implement ucoolib features.
+/// Be careful that there is only room for size - 1 elements in the FIFO.
+template<typename T, int size>
+class Fifo
+{
+ public:
+ /// Constructor, initialise an empty FIFO.
+ Fifo ();
+ /// Test whether the FIFO is empty.
+ bool empty () const { return head_ == tail_; }
+ /// Test whether the FIFO is full.
+ bool full () const { return head_ == next (tail_); }
+ /// Remove an element, do not do that if FIFO is empty!
+ T pop ();
+ /// Add an element, do not do that if FIFO is full!
+ void push (const T &e);
+ /// Pop up to COUNT elements and store them in BUF. Return the number of
+ /// read elements.
+ int read (T *buf, int count);
+ /// Push up to COUNT elements from BUF. Return the number of written
+ /// elements.
+ int write (const T *buf, int count);
+ private:
+ /// Return next index, use unsigned operation for optimisation.
+ int next (int index) const
+ {
+ return (static_cast<unsigned int> (index) + 1) % size;
+ }
+ private:
+ /// Index of the next element to pop, always incremented.
+ int_atomic_t head_;
+ /// Index of the next free space where to put a pushed element, also
+ /// always incremented.
+ int_atomic_t tail_;
+ /// Memory to store elements.
+ T buffer_[size];
+};
+
+} // namespace ucoo
+
+#include "fifo.tcc"
+
+#endif // ucoo_utils_fifo_hh
diff --git a/ucoo/utils/fifo.tcc b/ucoo/utils/fifo.tcc
new file mode 100644
index 0000000..be8f896
--- /dev/null
+++ b/ucoo/utils/fifo.tcc
@@ -0,0 +1,108 @@
+#ifndef ucoo_utils_fifo_tcc
+#define ucoo_utils_fifo_tcc
+// 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 <algorithm>
+
+namespace ucoo {
+
+template<typename T, int size>
+inline
+Fifo<T, size>::Fifo ()
+ : head_ (0), tail_ (0)
+{
+}
+
+template<typename T, int size>
+inline T
+Fifo<T, size>::pop ()
+{
+ // Reader, can only update head.
+ int head = head_;
+ assert (head != tail_);
+ T v = buffer_[head];
+ head = next (head);
+ barrier ();
+ head_ = head;
+ return v;
+}
+
+template<typename T, int size>
+inline void
+Fifo<T, size>::push (const T &e)
+{
+ // Writer, can only update tail.
+ int tail = tail_;
+ buffer_[tail] = e;
+ tail = next (tail);
+ assert (head_ != tail);
+ barrier ();
+ tail_ = tail;
+}
+
+template<typename T, int size>
+inline int
+Fifo<T, size>::read (T *buf, int count)
+{
+ // Reader, can only update head.
+ int r = 0;
+ int head = head_;
+ int tail = access_once (tail_);
+ while (r < count && head != tail)
+ {
+ buf[r] = buffer_[head];
+ head = next (head);
+ r++;
+ }
+ // Ensure data is copied, then update head.
+ barrier ();
+ head_ = head;
+ return r;
+}
+
+template<typename T, int size>
+inline int
+Fifo<T, size>::write (const T *buf, int count)
+{
+ // Writer, can only update tail.
+ int r = 0;
+ int head = access_once (head_);
+ int tail = tail_;
+ int tailp1 = next (tail);
+ while (r < count && tailp1 != head)
+ {
+ buffer_[tail] = buf[r];
+ tail = tailp1;
+ r++;
+ tailp1 = next (tail);
+ }
+ // Ensure data is copied, then update tail.
+ barrier ();
+ tail_ = tail;
+ return r;
+}
+
+} // namespace ucoo
+
+#endif // ucoo_utils_fifo_tcc
diff --git a/ucoo/utils/test/Makefile b/ucoo/utils/test/Makefile
new file mode 100644
index 0000000..01fb2ce
--- /dev/null
+++ b/ucoo/utils/test/Makefile
@@ -0,0 +1,12 @@
+BASE = ../../..
+
+TARGETS = host stm32f4
+PROGS = test_fifo test_crc
+stm32f4_PROGS = test_delay
+test_fifo_SOURCES = test_fifo.cc
+test_delay_SOURCES = test_delay.cc
+test_crc_SOURCES = test_crc.cc
+
+MODULES = utils base/test hal/usb
+
+include $(BASE)/build/top.mk
diff --git a/ucoo/utils/test/test_crc.cc b/ucoo/utils/test/test_crc.cc
new file mode 100644
index 0000000..c20600d
--- /dev/null
+++ b/ucoo/utils/test/test_crc.cc
@@ -0,0 +1,49 @@
+// 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/utils/crc.hh"
+#include "ucoo/arch/arch.hh"
+#include "ucoo/base/test/test.hh"
+
+#include <cstring>
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::TestSuite tsuite ("crc");
+ {
+ ucoo::Test test (tsuite, "crc8 test vector");
+ static const uint8_t test_vector[] = { 0x02, 0x1c, 0xb8, 0x01, 0, 0, 0, 0xa2 };
+ if (ucoo::crc8_compute (test_vector, lengthof (test_vector)) != 0)
+ test.fail ();
+ }
+ {
+ ucoo::Test test (tsuite, "crc32 test vector");
+ const char *check_str = "123456789";
+ if (ucoo::crc32_compute ((const uint8_t *) check_str,
+ std::strlen (check_str)) != 0xCBF43926)
+ test.fail ();
+ }
+ return tsuite.report () ? 0 : 1;
+}
diff --git a/ucoo/utils/test/test_delay.cc b/ucoo/utils/test/test_delay.cc
new file mode 100644
index 0000000..90a53e5
--- /dev/null
+++ b/ucoo/utils/test/test_delay.cc
@@ -0,0 +1,64 @@
+// 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/utils/delay.hh"
+#include "ucoo/arch/arch.hh"
+
+#include <libopencm3/stm32/f4/rcc.h>
+#include <libopencm3/stm32/f4/gpio.h>
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ rcc_peripheral_enable_clock (&RCC_AHB1ENR, RCC_AHB1ENR_IOPDEN);
+ gpio_mode_setup (GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE,
+ GPIO12 | GPIO13 | GPIO14 | GPIO15);
+ gpio_clear (GPIOD, GPIO12 | GPIO13 | GPIO14 | GPIO15);
+ int i, j;
+ while (1)
+ {
+ for (i = 0; i < 4; i++)
+ {
+ gpio_toggle (GPIOD, GPIO12 << (i % 4));
+ ucoo::delay (1);
+ }
+ for (i = 0; i < 16; i++)
+ {
+ gpio_toggle (GPIOD, GPIO12 << (i % 4));
+ ucoo::delay_ms (250);
+ }
+ for (i = 0; i < 16000; i++)
+ {
+ gpio_toggle (GPIOD, GPIO12 << (i % 4));
+ ucoo::delay_us (250);
+ }
+ for (i = 0; i < 16; i++)
+ {
+ gpio_toggle (GPIOD, GPIO12 << (i % 4));
+ for (j = 0; j < 1000; j++)
+ ucoo::delay_us (250);
+ }
+ }
+ return 0;
+}
diff --git a/ucoo/utils/test/test_fifo.cc b/ucoo/utils/test/test_fifo.cc
new file mode 100644
index 0000000..dd9aa8e
--- /dev/null
+++ b/ucoo/utils/test/test_fifo.cc
@@ -0,0 +1,78 @@
+// 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/utils/fifo.hh"
+#include "ucoo/arch/arch.hh"
+#include "ucoo/base/test/test.hh"
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::TestSuite tsuite ("fifo");
+ {
+ ucoo::Test test (tsuite, "push pop");
+ ucoo::Fifo<int, 16> fifo;
+ fifo.push (1);
+ fifo.push (2);
+ fifo.push (3);
+ if (fifo.pop () != 1 || fifo.pop () != 2 || fifo.pop () != 3)
+ test.fail ();
+ }
+ {
+ ucoo::Test test (tsuite, "full empty");
+ ucoo::Fifo<int, 4> fifo;
+ do
+ {
+ test_fail_break_unless (test, fifo.empty () && !fifo.full ());
+ fifo.push (1);
+ test_fail_break_unless (test, !fifo.empty () && !fifo.full ());
+ fifo.push (2);
+ test_fail_break_unless (test, !fifo.empty () && !fifo.full ());
+ fifo.push (3);
+ test_fail_break_unless (test, !fifo.empty () && fifo.full ());
+ } while (0);
+ }
+ {
+ ucoo::Test test (tsuite, "write read");
+ ucoo::Fifo<int, 8> fifo;
+ static const int b[] = { 1, 2, 3, 4, 5 };
+ int c[8];
+ int r;
+ do
+ {
+ r = fifo.write (b, 5);
+ test_fail_break_unless (test, r == 5);
+ r = fifo.read (c, 2);
+ test_fail_break_unless (test, r == 2 && c[0] == 1 && c[1] == 2);
+ r = fifo.write (b, 5);
+ test_fail_break_unless (test, r == 4);
+ r = fifo.read (c, 8);
+ test_fail_break_unless (test, r == 7);
+ test_fail_break_unless (test, c[0] == 3 && c[1] == 4 && c[2] == 5);
+ test_fail_break_unless (test, c[3] == 1 && c[4] == 2 && c[5] == 3
+ && c[6] == 4);
+ } while (0);
+ }
+ return tsuite.report () ? 0 : 1;
+}
diff --git a/ucoo/utils/trace.hh b/ucoo/utils/trace.hh
new file mode 100644
index 0000000..e2d33ac
--- /dev/null
+++ b/ucoo/utils/trace.hh
@@ -0,0 +1,133 @@
+#ifndef ucoo_utils_trace_hh
+#define ucoo_utils_trace_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 {
+
+/// Trace buffer to be read with debugger, no initialisation, should be
+/// instantiated in BSS.
+class TraceBuffer
+{
+ /// Maximum number of arguments.
+ static const int args_nb = 4;
+ /// Maximum number of trace entries.
+ static const int entries_nb = 32;
+ public:
+ /// Trace without argument.
+ inline void operator() (const char *str);
+ /// Trace with N arguments...
+ inline void operator() (const char *str, int a0);
+ inline void operator() (const char *str, int a0, int a1);
+ inline void operator() (const char *str, int a0, int a1, int a2);
+ inline void operator() (const char *str, int a0, int a1, int a2, int a3);
+ private:
+ /// Trace entry, contains all given parameters.
+ struct Entry
+ {
+ const char *str;
+ int args[args_nb];
+ };
+ /// Trace entries array.
+ Entry entries[entries_nb];
+ /// Index of next entry to be written in entries array.
+ unsigned int index;
+};
+
+/// Dummy trace, trace nothing.
+class TraceDummy
+{
+ public:
+ inline void operator() (const char *str) { }
+ inline void operator() (const char *str, int a0) { }
+ inline void operator() (const char *str, int a0, int a1) { }
+ inline void operator() (const char *str, int a0, int a1, int a2) { }
+ inline void operator() (const char *str, int a0, int a1, int a2, int a3) { }
+};
+
+/// Conditional trace, whether it trace or not depends on the template
+/// argument.
+template<bool ENABLED>
+class Trace
+{
+};
+
+template<>
+class Trace<true> : public TraceBuffer
+{
+};
+
+template<>
+class Trace<false> : public TraceDummy
+{
+};
+
+inline void
+TraceBuffer::operator() (const char *str)
+{
+ entries[index].str = str;
+ index = (index + 1) % entries_nb;
+}
+
+inline void
+TraceBuffer::operator() (const char *str, int a0)
+{
+ entries[index].str = str;
+ entries[index].args[0] = a0;
+ index = (index + 1) % entries_nb;
+}
+
+inline void
+TraceBuffer::operator() (const char *str, int a0, int a1)
+{
+ entries[index].str = str;
+ entries[index].args[0] = a0;
+ entries[index].args[1] = a1;
+ index = (index + 1) % entries_nb;
+}
+
+inline void
+TraceBuffer::operator() (const char *str, int a0, int a1, int a2)
+{
+ entries[index].str = str;
+ entries[index].args[0] = a0;
+ entries[index].args[1] = a1;
+ entries[index].args[2] = a2;
+ index = (index + 1) % entries_nb;
+}
+
+inline void
+TraceBuffer::operator() (const char *str, int a0, int a1, int a2, int a3)
+{
+ entries[index].str = str;
+ entries[index].args[0] = a0;
+ entries[index].args[1] = a1;
+ entries[index].args[2] = a2;
+ entries[index].args[3] = a3;
+ index = (index + 1) % entries_nb;
+}
+
+} // namespace ucoo
+
+#endif // ucoo_utils_trace_hh