summaryrefslogtreecommitdiff
path: root/digital/ucoolib/ucoolib
diff options
context:
space:
mode:
authorNicolas Schodet2013-03-18 22:03:16 +0100
committerNicolas Schodet2013-03-18 22:03:16 +0100
commit10bdbf4b826b1755cb6a22894c0bcf9bf6296f4b (patch)
treecca1e20113131b7223e20468c1445d2258c68d8c /digital/ucoolib/ucoolib
parent5cd8807e90a33e1584df899161d127934f68d74d (diff)
digital/ucoolib/ucoolib/utils: add CRC functions
Diffstat (limited to 'digital/ucoolib/ucoolib')
-rw-r--r--digital/ucoolib/ucoolib/utils/Module2
-rw-r--r--digital/ucoolib/ucoolib/utils/crc.cc37
-rw-r--r--digital/ucoolib/ucoolib/utils/crc.hh53
-rw-r--r--digital/ucoolib/ucoolib/utils/test/Makefile3
-rw-r--r--digital/ucoolib/ucoolib/utils/test/test_crc.cc40
5 files changed, 133 insertions, 2 deletions
diff --git a/digital/ucoolib/ucoolib/utils/Module b/digital/ucoolib/ucoolib/utils/Module
index fe3f9fd0..a37070f3 100644
--- a/digital/ucoolib/ucoolib/utils/Module
+++ b/digital/ucoolib/ucoolib/utils/Module
@@ -1 +1 @@
-utils_SOURCES := delay.arm.cc
+utils_SOURCES := delay.arm.cc crc.cc
diff --git a/digital/ucoolib/ucoolib/utils/crc.cc b/digital/ucoolib/ucoolib/utils/crc.cc
new file mode 100644
index 00000000..e00b8c74
--- /dev/null
+++ b/digital/ucoolib/ucoolib/utils/crc.cc
@@ -0,0 +1,37 @@
+// 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 "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;
+}
+
+} // namespace ucoo
diff --git a/digital/ucoolib/ucoolib/utils/crc.hh b/digital/ucoolib/ucoolib/utils/crc.hh
new file mode 100644
index 00000000..0e97f4a8
--- /dev/null
+++ b/digital/ucoolib/ucoolib/utils/crc.hh
@@ -0,0 +1,53 @@
+#ifndef ucoolib_utils_crc_hh
+#define ucoolib_utils_crc_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/common.hh"
+
+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);
+
+} // namespace ucoo
+
+#endif // ucoolib_utils_crc_hh
diff --git a/digital/ucoolib/ucoolib/utils/test/Makefile b/digital/ucoolib/ucoolib/utils/test/Makefile
index 874bc325..01fb2cef 100644
--- a/digital/ucoolib/ucoolib/utils/test/Makefile
+++ b/digital/ucoolib/ucoolib/utils/test/Makefile
@@ -1,10 +1,11 @@
BASE = ../../..
TARGETS = host stm32f4
-PROGS = test_fifo
+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
diff --git a/digital/ucoolib/ucoolib/utils/test/test_crc.cc b/digital/ucoolib/ucoolib/utils/test/test_crc.cc
new file mode 100644
index 00000000..19d6ca8e
--- /dev/null
+++ b/digital/ucoolib/ucoolib/utils/test/test_crc.cc
@@ -0,0 +1,40 @@
+// 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/utils/crc.hh"
+#include "ucoolib/arch/arch.hh"
+#include "ucoolib/base/test/test.hh"
+
+int
+main (int argc, const char **argv)
+{
+ ucoo::arch_init (argc, argv);
+ ucoo::TestSuite tsuite ("crc");
+ {
+ ucoo::Test test (tsuite, "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 ();
+ }
+ return tsuite.report () ? 0 : 1;
+}