/utility/bitbanding.h

http://github.com/Tinkerforge/bricklib · C Header · 108 lines · 23 code · 11 blank · 74 comment · 5 complexity · 55e894e1dccdedbc4eabcee362d85149 MD5 · raw file

  1. /* ----------------------------------------------------------------------------
  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. #ifndef _BITBANDING_
  30. #define _BITBANDING_
  31. /*----------------------------------------------------------------------------
  32. * \file bitbanding.h
  33. * Include Defines & macros for bit-banding.
  34. *----------------------------------------------------------------------------*/
  35. /*----------------------------------------------------------------------------
  36. * Header files
  37. *----------------------------------------------------------------------------*/
  38. #include <stdint.h>
  39. /*----------------------------------------------------------------------------
  40. * Global Macros
  41. *----------------------------------------------------------------------------*/
  42. /**
  43. * \brief Check if the address is in bit banding sram region.
  44. *
  45. * \note The address should be in area of 0x2000000 ~ 0x200FFFFF
  46. *
  47. * \param x The address to check.
  48. */
  49. #define IS_BITBAND_SRAM_ADDR(x) \
  50. ( ((uint32_t)(x)) >= 0x20000000 && \
  51. ((uint32_t)(x)) < (0x20000000+0x100000) )
  52. /**
  53. * \brief Check if the address is in bit banding peripheral region
  54. *
  55. * \note The address should be in area of 0x4000000 ~ 0x400FFFFF
  56. * \param x The address to check
  57. */
  58. #define IS_BITBAND_PERIPH_ADDR(x) \
  59. ( ((uint32_t)(x)) >= 0x40000000 && \
  60. ((uint32_t)(x)) < (0x40000000+0x100000) )
  61. /**
  62. * \brief Calculate bit band alias address.
  63. *
  64. * Calculate the bit band alias address and return a pointer address to word.
  65. *
  66. * \param addr The byte address of bitbanding bit.
  67. * \param bit The bit position of bitbanding bit.
  68. * \callergraph
  69. */
  70. #define BITBAND_ALIAS_ADDRESS(addr, bit) \
  71. ((volatile uint32_t*)((((uint32_t)(addr) & 0xF0000000) + 0x02000000) \
  72. +((((uint32_t)(addr)&0xFFFFF)*32)\
  73. +( (uint32_t)(bit)*4))))
  74. /**
  75. * \brief Bit write through bit banding.
  76. *
  77. * \param addr32 32-bit aligned byte address where the bit exists.
  78. * \param bit Bit position.
  79. * \param val The value that the bit is set to.
  80. * \callergraph
  81. */
  82. #define WRITE_BITBANDING(addr32, bit, val) do {\
  83. *BITBAND_ALIAS_ADDRESS(addr32,bit) = (val); \
  84. } while (0);
  85. /**
  86. * \brief Toggle bit through bit banding
  87. *
  88. * \param addr32 32-bit aligned byte address where the bit exists.
  89. * \param bit Bit position.
  90. */
  91. #define TOGGLE_BITBANDING(addr32, bit) do {\
  92. volatile uint32_t * p = \
  93. BITBAND_ALIAS_ADDRESS(addr32,bit); \
  94. if (*p) *p = 0; \
  95. else *p = 1; \
  96. }while(0);
  97. #endif /* #ifndef _BITBANDING_ */