aboutsummaryrefslogtreecommitdiff
path: root/include/libopenstm32/tools.h
diff options
context:
space:
mode:
authorPiotr Esden-Tempski2010-01-08 16:48:25 +0100
committerPiotr Esden-Tempski2010-01-15 17:07:19 +0100
commitcde7d1ce4d15ccb23db99ace2c0ac959bea7d864 (patch)
treef4ee6d906f31437053a11cce0882d0b3be2d8edf /include/libopenstm32/tools.h
parent0c6b661502c48baed8f7d4e36205d95c8ace663e (diff)
Added usb peripheral register definitions. It uses some tool macros defined in tools.h
Diffstat (limited to 'include/libopenstm32/tools.h')
-rw-r--r--include/libopenstm32/tools.h58
1 files changed, 58 insertions, 0 deletions
diff --git a/include/libopenstm32/tools.h b/include/libopenstm32/tools.h
new file mode 100644
index 0000000..91c16b5
--- /dev/null
+++ b/include/libopenstm32/tools.h
@@ -0,0 +1,58 @@
+/*
+ * This file is part of the libopenstm32 project.
+ *
+ * Copyright (C) 2009 Piotr Esden-Tempski <piotr@esden.net>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef LIBOPENSTM32_TOOLS_H
+#define LIBOPENSTM32_TOOLS_H
+
+/******************************************************************************
+ * Register accessors / manipulators
+ ******************************************************************************/
+
+/* Get register content */
+#define GET_REG(REG) ((u16) *REG)
+/* Set register content */
+#define SET_REG(REG, VAL) (*REG = (u16)VAL)
+/* Clear register bit */
+#define CLR_REG_BIT(REG, BIT) SET_REG(REG, (~BIT))
+/* Clear register bit masking out some bits that must not be touched */
+#define CLR_REG_BIT_MSK(REG, MSK, BIT) SET_REG(REG, (GET_REG(REG) & \
+ MSK & (~BIT)))
+/* Get masked out bit value */
+#define GET_REG_BIT(REG, BIT) (GET_REG(REG) & BIT)
+
+/*
+ * Set/reset a bit in a masked window by using toggle mechanism.
+ *
+ * This means that we look at the bits in the bit window designated by
+ * the mask. If the bit in the masked window is not matching the
+ * bitmask BIT then we write 1 and if the bit in the masked window is
+ * matching the bitmask BIT we write 0.
+ *
+ * TODO: we may need a faster implementation of that one?
+ */
+#define TOG_SET_REG_BIT_MSK(REG, MSK, BIT) { \
+ register u16 toggle_mask = GET_REG(REG) & MSK; \
+ register u16 bit_selector; \
+ for(bit_selector = 1; bit_selector; bit_selector <<= 1){ \
+ if((bit_selector & BIT) != 0) toggle_mask ^= bit_selector; \
+ } \
+ SET_REG(REG, toggle_mask); \
+ }
+
+#endif /* LIBOPENSTM32_TOOLS_H */