atom.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.7 2009/01/17 11:26:47 haraldkipp
  36. * Getting rid of two remaining BSD types in favor of stdint.
  37. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  38. *
  39. * Revision 1.6 2008/07/07 11:04:27 haraldkipp
  40. * Configurable ways of handling critical sections for ARM targets.
  41. *
  42. * Revision 1.5 2008/03/17 10:15:36 haraldkipp
  43. * Added memory and cc clobbers to NutEnter/ExitCritical. This keeps
  44. * compiler optimizations inside and outside of critical sections
  45. * seperated.
  46. *
  47. * Revision 1.4 2007/08/17 10:47:03 haraldkipp
  48. * Bug #1757410 fixed. NutEnter/ExitCritical destroyed ARM register R0.
  49. *
  50. * Revision 1.3 2006/03/02 20:03:39 haraldkipp
  51. * Added ICCARM inline assembly for NutEnter/ExitCritical(). Also fixed
  52. * SF 1440949 (FIQ never enabled).
  53. *
  54. * Revision 1.2 2005/07/26 15:47:06 haraldkipp
  55. * AtomicInc() and AtomicDec() are no longer required by Nut/Net.
  56. * Removed to simplify the porting job. Broken applications should
  57. * implement their own version.
  58. *
  59. * Revision 1.1 2005/06/06 10:49:35 haraldkipp
  60. * Building outside the source tree failed. All header files moved from
  61. * arch/cpu/include to include/arch/cpu.
  62. *
  63. * Revision 1.1 2005/05/27 17:41:52 drsung
  64. * Moved the file.
  65. *
  66. * Revision 1.1 2005/05/26 10:08:42 drsung
  67. * Moved the platform dependend code from include/sys/atom.h to this file.
  68. *
  69. *
  70. */
  71. #ifndef _SYS_ATOM_H_
  72. #error "Do not include this file directly. Use sys/atom.h instead!"
  73. #endif
  74. #ifdef __GNUC__
  75. #if defined(NUT_CRITICAL_NESTING)
  76. #if defined(NUT_CRITICAL_NESTING_STACK)
  77. #define NutEnterCritical() \
  78. { \
  79. int temp_; \
  80. asm volatile ( \
  81. "@ NutEnterCritical" "\n\t" \
  82. "mrs %0, cpsr" "\n\t" \
  83. "stmfd sp!, {%0}" "\n\t" \
  84. "orr %0, %0, #0xC0" "\n\t" \
  85. "msr cpsr, %0" "\n\t" \
  86. : "=r" (temp_) : : "memory", "cc"); \
  87. }
  88. #define NutExitCritical() \
  89. { \
  90. int temp_; \
  91. asm volatile ( \
  92. "@ NutExitCritical" "\n\t" \
  93. "ldmfd sp!, {%0}" "\n\t" \
  94. "msr cpsr, %0" "\n\t" \
  95. : "=r" (temp_) : : "memory", "cc"); \
  96. }
  97. #else /* NUT_CRITICAL_NESTING_STACK */
  98. extern unsigned int critical_nesting_level;
  99. #define NutEnterCritical() \
  100. if (critical_nesting_level++ == 0) { \
  101. int temp_; \
  102. asm volatile ( \
  103. "@ NutEnterCritical" "\n\t" \
  104. "mrs %0, cpsr" "\n\t" \
  105. "orr %0, %0, #0xC0" "\n\t" \
  106. "msr cpsr, %0" "\n\t" \
  107. : "=r" (temp_) : : "cc"); \
  108. }
  109. #define NutExitCritical() \
  110. if (critical_nesting_level) { \
  111. int temp_; \
  112. asm volatile ( \
  113. "@ NutExitCritical" "\n\t" \
  114. "mrs %0, cpsr" "\n\t" \
  115. "bic %0, %0, #0xC0" "\n\t" \
  116. "msr cpsr, %0" "\n\t" \
  117. : "=r" (temp_) : : "cc"); \
  118. critical_nesting_level--; \
  119. }
  120. #endif /* NUT_CRITICAL_NESTING_STACK */
  121. #else /* NUT_CRITICAL_NESTING */
  122. #define NutEnterCritical() \
  123. { \
  124. int temp_; \
  125. asm volatile ( \
  126. "@ NutEnterCritical" "\n\t" \
  127. "mrs %0, cpsr" "\n\t" \
  128. "orr %0, %0, #0xC0" "\n\t" \
  129. "msr cpsr, %0" "\n\t" \
  130. : "=r" (temp_) : : "cc"); \
  131. }
  132. #define NutExitCritical() \
  133. { \
  134. int temp_; \
  135. asm volatile ( \
  136. "@ NutExitCritical" "\n\t" \
  137. "mrs %0, cpsr" "\n\t" \
  138. "bic %0, %0, #0xC0" "\n\t" \
  139. "msr cpsr, %0" "\n\t" \
  140. : "=r" (temp_) : : "cc"); \
  141. }
  142. #endif /* NUT_CRITICAL_NESTING */
  143. #define NutJumpOutCritical() NutExitCritical()
  144. #else /* __IMAGECRAFT__ */
  145. #define NutEnterCritical() \
  146. asm("; NutEnterCritical\n" \
  147. "mrs r12, cpsr\n" \
  148. "orr r12, r12, #0xC0\n" \
  149. "msr cpsr_c, r12")
  150. #define NutExitCritical() \
  151. asm("; NutExitCritical\n" \
  152. "mrs r12, cpsr\n" \
  153. "bic r12, r12, #0xC0\n" \
  154. "msr cpsr_c, r12")
  155. #endif