libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
tools.h
Go to the documentation of this file.
1/*
2 * This file is part of the libopencm3 project.
3 *
4 * Copyright (C) 2009 Piotr Esden-Tempski <piotr@esden.net>
5 *
6 * This library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBOPENCM3_TOOLS_H
21#define LIBOPENCM3_TOOLS_H
22
23/*
24 * Register accessors / manipulators
25 */
26
27/* Get register content. */
28#define GET_REG(REG) ((uint16_t) *(REG))
29
30/* Set register content. */
31#define SET_REG(REG, VAL) (*(REG) = (uint16_t)(VAL))
32
33/* Clear register bit. */
34#define CLR_REG_BIT(REG, BIT) SET_REG((REG), (~(BIT)))
35
36/* Clear register bit masking out some bits that must not be touched. */
37#define CLR_REG_BIT_MSK_AND_SET(REG, MSK, BIT, EXTRA_BITS) \
38 SET_REG((REG), (GET_REG((REG)) & (MSK) & (~(BIT))) | (EXTRA_BITS))
39
40#define CLR_REG_BIT_MSK(REG, MSK, BIT) \
41 CLR_REG_BIT_MSK_AND_SET((REG), (MSK), (BIT), 0)
42
43/* Get masked out bit value. */
44#define GET_REG_BIT(REG, BIT) (GET_REG(REG) & (BIT))
45
46/*
47 * Set/reset a bit in a masked window by using toggle mechanism.
48 *
49 * This means that we look at the bits in the bit window designated by
50 * the mask. If the bit in the masked window is not matching the
51 * bit mask BIT then we write 1 and if the bit in the masked window is
52 * matching the bit mask BIT we write 0.
53 *
54 * TODO: We may need a faster implementation of that one?
55 */
56#define TOG_SET_REG_BIT_MSK_AND_SET(REG, MSK, BIT, EXTRA_BITS) \
57do { \
58 register uint16_t toggle_mask = GET_REG(REG) & (MSK); \
59 toggle_mask ^= BIT; \
60 SET_REG((REG), toggle_mask | (EXTRA_BITS)); \
61} while (0)
62
63#define TOG_SET_REG_BIT_MSK(REG, MSK, BIT) \
64 TOG_SET_REG_BIT_MSK_AND_SET((REG), (MSK), (BIT), 0)
65
66#endif