2010-01-08 15:48:25 +00:00
|
|
|
/*
|
2010-12-30 12:19:25 +00:00
|
|
|
* This file is part of the libopencm3 project.
|
2010-01-08 15:48:25 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009 Piotr Esden-Tempski <piotr@esden.net>
|
|
|
|
*
|
2012-03-02 10:23:11 +00:00
|
|
|
* This library is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
2010-01-08 15:48:25 +00:00
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
2012-03-02 10:23:11 +00:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2010-01-08 15:48:25 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2012-03-02 10:23:11 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2010-01-08 15:48:25 +00:00
|
|
|
*
|
2012-03-02 10:23:11 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2010-01-08 15:48:25 +00:00
|
|
|
*/
|
|
|
|
|
2010-12-31 00:11:14 +00:00
|
|
|
#ifndef LIBOPENCM3_TOOLS_H
|
|
|
|
#define LIBOPENCM3_TOOLS_H
|
2010-01-08 15:48:25 +00:00
|
|
|
|
2010-03-06 14:22:09 +00:00
|
|
|
/*
|
2010-01-08 15:48:25 +00:00
|
|
|
* Register accessors / manipulators
|
2010-03-06 14:22:09 +00:00
|
|
|
*/
|
2010-01-08 15:48:25 +00:00
|
|
|
|
2010-03-06 14:22:09 +00:00
|
|
|
/* Get register content. */
|
2015-10-15 01:03:18 +00:00
|
|
|
#define GET_REG(REG) ((uint16_t) *(REG))
|
2010-03-06 14:22:09 +00:00
|
|
|
|
|
|
|
/* Set register content. */
|
2015-10-15 01:03:18 +00:00
|
|
|
#define SET_REG(REG, VAL) (*(REG) = (uint16_t)(VAL))
|
2010-03-06 14:22:09 +00:00
|
|
|
|
|
|
|
/* Clear register bit. */
|
2015-10-15 01:03:18 +00:00
|
|
|
#define CLR_REG_BIT(REG, BIT) SET_REG((REG), (~(BIT)))
|
2010-03-06 14:22:09 +00:00
|
|
|
|
|
|
|
/* Clear register bit masking out some bits that must not be touched. */
|
2014-05-29 19:02:03 +00:00
|
|
|
#define CLR_REG_BIT_MSK_AND_SET(REG, MSK, BIT, EXTRA_BITS) \
|
2015-12-14 21:57:15 +00:00
|
|
|
SET_REG((REG), (GET_REG((REG)) & (MSK) & (~(BIT))) | (EXTRA_BITS))
|
2014-05-29 19:02:03 +00:00
|
|
|
|
2010-03-06 14:22:09 +00:00
|
|
|
#define CLR_REG_BIT_MSK(REG, MSK, BIT) \
|
2015-10-15 01:03:18 +00:00
|
|
|
CLR_REG_BIT_MSK_AND_SET((REG), (MSK), (BIT), 0)
|
2010-03-06 14:22:09 +00:00
|
|
|
|
|
|
|
/* Get masked out bit value. */
|
2015-10-15 01:03:18 +00:00
|
|
|
#define GET_REG_BIT(REG, BIT) (GET_REG(REG) & (BIT))
|
2010-01-08 15:48:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2013-06-16 21:06:37 +00:00
|
|
|
* bit mask BIT then we write 1 and if the bit in the masked window is
|
|
|
|
* matching the bit mask BIT we write 0.
|
2010-01-08 15:48:25 +00:00
|
|
|
*
|
2010-03-06 14:22:09 +00:00
|
|
|
* TODO: We may need a faster implementation of that one?
|
2010-01-08 15:48:25 +00:00
|
|
|
*/
|
2014-05-29 19:02:03 +00:00
|
|
|
#define TOG_SET_REG_BIT_MSK_AND_SET(REG, MSK, BIT, EXTRA_BITS) \
|
2010-11-02 01:02:21 +00:00
|
|
|
do { \
|
2013-06-13 02:11:22 +00:00
|
|
|
register uint16_t toggle_mask = GET_REG(REG) & (MSK); \
|
2016-07-31 10:39:08 +00:00
|
|
|
toggle_mask ^= BIT; \
|
2015-10-15 01:03:18 +00:00
|
|
|
SET_REG((REG), toggle_mask | (EXTRA_BITS)); \
|
2013-06-13 00:44:07 +00:00
|
|
|
} while (0)
|
2010-01-08 15:48:25 +00:00
|
|
|
|
2014-05-29 19:02:03 +00:00
|
|
|
#define TOG_SET_REG_BIT_MSK(REG, MSK, BIT) \
|
2015-10-15 01:03:18 +00:00
|
|
|
TOG_SET_REG_BIT_MSK_AND_SET((REG), (MSK), (BIT), 0)
|
2014-05-29 19:02:03 +00:00
|
|
|
|
2010-02-23 23:34:18 +00:00
|
|
|
#endif
|