io.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2008-2012, Freescale Semiconductor, Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*!
  31. * @file io.h
  32. * @brief Register access macros.
  33. *
  34. * @ingroup diag_init
  35. */
  36. #ifndef __IO_H__
  37. #define __IO_H__
  38. #include "sdk_types.h"
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include "sdk.h"
  43. ////////////////////////////////////////////////////////////////////////////////
  44. // Definitions
  45. ////////////////////////////////////////////////////////////////////////////////
  46. //! @name Register read functions
  47. //@{
  48. #define reg8_read(addr) *((volatile uint8_t *)(addr))
  49. #define reg16_read(addr) *((volatile uint16_t *)(addr))
  50. #define reg32_read(addr) *((volatile uint32_t *)(addr))
  51. //@}
  52. //! @name Register write functions
  53. //@{
  54. #define reg8_write(addr,val) *((volatile uint8_t *)(addr)) = (val)
  55. #define reg16_write(addr,val) *((volatile uint16_t *)(addr)) = (val)
  56. #define reg32_write(addr,val) *((volatile uint32_t *)(addr)) = (val)
  57. //@}
  58. //! @name Memory read functions
  59. //@{
  60. #define mem8_read(addr) *((volatile uint8_t *)(addr))
  61. #define mem16_read(addr) *((volatile uint16_t *)(addr))
  62. #define mem32_read(addr) *((volatile uint32_t *)(addr))
  63. //@}
  64. //! @name Memory write functions
  65. //@{
  66. #define mem8_write(addr,val) *((volatile uint8_t *)(addr)) = (val)
  67. #define mem16_write(addr,val) *((volatile uint16_t *)(addr)) = (val)
  68. #define mem32_write(addr,val) *((volatile uint32_t *)(addr)) = (val)
  69. //@}
  70. //! @name Read functions
  71. //@{
  72. #define readb(a) reg8_read(a)
  73. #define readw(a) reg16_read(a)
  74. #define readl(a) reg32_read(a)
  75. //@}
  76. //! @name Write functrions
  77. //!
  78. //! The prefered method to access registers.
  79. //@{
  80. #define writeb(v, a) reg8_write(a, v)
  81. #define writew(v, a) reg16_write(a, v)
  82. #define writel(v, a) reg32_write(a, v)
  83. //@}
  84. //! @name Bit set/clear functions
  85. //@{
  86. #define reg8setbit(addr,bitpos) \
  87. reg8_write((addr),(reg8_read((addr)) | (1<<(bitpos))))
  88. #define reg16setbit(addr,bitpos) \
  89. reg16_write((addr),(reg16_read((addr)) | (1<<(bitpos))))
  90. #define reg32setbit(addr,bitpos) \
  91. reg32_write((addr),(reg32_read((addr)) | (1<<(bitpos))))
  92. #define reg8clrbit(addr,bitpos) \
  93. reg8_write((addr),(reg8_read((addr)) & (0xFF ^ (1<<(bitpos)))))
  94. #define reg16clrbit(addr,bitpos) \
  95. reg16_write((addr),(reg16_read((addr)) & (0xFFFF ^ (1<<(bitpos)))))
  96. #define reg32clrbit(addr,bitpos) \
  97. reg32_write((addr),(reg32_read((addr)) & (0xFFFFFFFF ^ (1<<(bitpos)))))
  98. //@}
  99. //! @name Masked write functions
  100. //@{
  101. #define reg8_write_mask(addr, data, mask) \
  102. reg8_write((addr),((reg8_read(addr) & (~mask)) | (mask & data)))
  103. #define reg16_write_mask(addr, data, mask) \
  104. reg16_write((addr),((reg16_read(addr) & (~mask)) | (mask & data)))
  105. #define reg32_write_mask(addr, data, mask) \
  106. reg32_write((addr),((reg32_read(addr) & (~mask)) | (mask & data)))
  107. #define gen_msk32(start, end) ((0xFFFFFFFF << (start)) ^ (0xFFFFFFFF << ((end + 1))))
  108. #define reg32_set_field(addr, start, end, val) \
  109. reg32_write_mask(addr, (val) << (start), gen_msk32((start, end)))
  110. //@}
  111. /*!
  112. * This macro is used to get certain bit field from a number
  113. */
  114. #define GET_FIELD(val, len, sh) ((val >> sh) & ((1 << len) - 1))
  115. /*!
  116. * This macro is used to set certain bit field inside a number
  117. */
  118. #define SET_FIELD(val, len, sh, nval) ((val & ~(((1 << len) - 1) << sh)) | (nval << sh))
  119. #endif // __IO_H__
  120. ////////////////////////////////////////////////////////////////////////////////
  121. // EOF
  122. ////////////////////////////////////////////////////////////////////////////////