blob: a3ca5011e321bc5be8fa0cdd94cac874d499a9a8 [file] [log] [blame]
Christina Quast53b21052014-12-09 15:34:35 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2009, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30#ifndef _BITBANDING_
31#define _BITBANDING_
32
33/*----------------------------------------------------------------------------
34 * \file bitbanding.h
35 * Include Defines & macros for bit-banding.
36 *----------------------------------------------------------------------------*/
37
38/*----------------------------------------------------------------------------
39 * Header files
40 *----------------------------------------------------------------------------*/
41
42#include <stdint.h>
43
44/*----------------------------------------------------------------------------
45 * Global Macros
46 *----------------------------------------------------------------------------*/
47
48/**
49 * \brief Check if the address is in bit banding sram region.
50 *
51 * \note The address should be in area of 0x2000000 ~ 0x200FFFFF
52 *
53 * \param x The address to check.
54 */
55#define IS_BITBAND_SRAM_ADDR(x) \
56 ( ((uint32_t)(x)) >= 0x20000000 && \
57 ((uint32_t)(x)) < (0x20000000+0x100000) )
58
59/**
60 * \brief Check if the address is in bit banding peripheral region
61 *
62 * \note The address should be in area of 0x4000000 ~ 0x400FFFFF
63 * \param x The address to check
64 */
65#define IS_BITBAND_PERIPH_ADDR(x) \
66 ( ((uint32_t)(x)) >= 0x40000000 && \
67 ((uint32_t)(x)) < (0x40000000+0x100000) )
68
69/**
70 * \brief Calculate bit band alias address.
71 *
72 * Calculate the bit band alias address and return a pointer address to word.
73 *
74 * \param addr The byte address of bitbanding bit.
75 * \param bit The bit position of bitbanding bit.
76 * \callergraph
77 */
78#define BITBAND_ALIAS_ADDRESS(addr, bit) \
79 ((volatile uint32_t*)((((uint32_t)(addr) & 0xF0000000) + 0x02000000) \
80 +((((uint32_t)(addr)&0xFFFFF)*32)\
81 +( (uint32_t)(bit)*4))))
82
83/**
84 * \brief Bit write through bit banding.
85 *
86 * \param addr32 32-bit aligned byte address where the bit exists.
87 * \param bit Bit position.
88 * \param val The value that the bit is set to.
89 * \callergraph
90 */
91#define WRITE_BITBANDING(addr32, bit, val) do {\
92 *BITBAND_ALIAS_ADDRESS(addr32,bit) = (val); \
93 } while (0);
94
95/**
96 * \brief Toggle bit through bit banding
97 *
98 * \param addr32 32-bit aligned byte address where the bit exists.
99 * \param bit Bit position.
100 */
101#define TOGGLE_BITBANDING(addr32, bit) do {\
102 volatile uint32_t * p = \
103 BITBAND_ALIAS_ADDRESS(addr32,bit); \
104 if (*p) *p = 0; \
105 else *p = 1; \
106 }while(0);
107
108#endif /* #ifndef _BITBANDING_ */