memtest.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2012 by Ole Reinhardt (ole.reinhardt@embedded-it.de)
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. ***********************************************************************
  35. *
  36. * The test algorithms are taken from the original public domain source
  37. * code from Michael Barr:
  38. *
  39. * Filename: memtest.c
  40. *
  41. * Description: General-purpose memory testing functions.
  42. *
  43. * Notes: This software can be easily ported to systems with
  44. * different data bus widths by redefining 'datum'.
  45. *
  46. * Copyright (c) 1998 by Michael Barr. This software is placed into
  47. * the public domain and may be used for any purpose. However, this
  48. * notice must not be changed or removed and no warranty is either
  49. * expressed or implied by its publication or distribution.
  50. *
  51. */
  52. /*
  53. * \verbatim
  54. * $Id: $
  55. * \endverbatim
  56. */
  57. /*
  58. * The following functions implement three well known memory test algorithm
  59. * that can test most common memory problems.
  60. * These tree algorithms are in the public domain.
  61. *
  62. * A nice description of these algorithms can be found at:
  63. * http://www.esacademy.com/en/library/technical-articles-and-documents/miscellaneous/software-based-memory-testing.html
  64. *
  65. * And also at:
  66. * [1] Barr, Michael. "Software-Based Memory Testing," Embedded Systems Programming, July 2000, pp. 28-40.
  67. */
  68. /*!
  69. * \brief Check data bus wiring on a 8 bit databus
  70. *
  71. * Test the data bus wiring in a memory region by performing a walking 1's
  72. * test at a fixed address within that region. The address (and hence the
  73. * memory region) is selected by the caller.
  74. *
  75. * \param addr memory address that shall be used for this test
  76. *
  77. * \return 0 on success, a non-zero result is the first pattern that failed
  78. */
  79. datum MemtestDataBus(volatile datum* addr)
  80. {
  81. datum pattern;
  82. /*
  83. * Perform a walking 1's test at the given address.
  84. */
  85. for (pattern = 1; pattern != 0; pattern <<= 1) {
  86. /*
  87. * Write the test pattern.
  88. */
  89. *addr = pattern;
  90. /*
  91. * Read it back (immediately is okay for this test).
  92. */
  93. if (*addr != pattern) {
  94. return (pattern);
  95. }
  96. }
  97. return 0;
  98. }
  99. /*!
  100. * \brief Check address bus wiring on a 8 bit databus
  101. *
  102. * Test the address bus wiring in a memory region by performing a walking
  103. * 1's test on the relevant bits of the address and checking for aliasing.
  104. * This test will find single-bit address failures such as stuck
  105. * -high, stuck-low, and shorted pins.
  106. *
  107. * The base address and size of the region are selected by the caller.
  108. *
  109. * \notes For best results, the selected base address should
  110. * have enough LSB 0's to guarantee single address bit
  111. * changes. For example, to test a 64-Kbyte region,
  112. * select a base address on a 64-Kbyte boundary. Also,
  113. * select the region size as a power-of-two--if at all
  114. * possible.
  115. *
  116. * \param base memory base address that shall be used for this test
  117. * \param size size of the memory region
  118. *
  119. * \return NULL if the test succeeds.
  120. * A non-zero result is the first address at which an
  121. * aliasing problem was uncovered. By examining the
  122. * contents of memory, it may be possible to gather
  123. * additional information about the problem.
  124. */
  125. datum *MemtestAddrBus(volatile datum *base, size_t size)
  126. {
  127. uint32_t addressMask = (size/sizeof(datum) - 1);
  128. uint32_t offset;
  129. uint32_t testOffset;
  130. datum pattern = (datum) 0xAAAAAAAA;
  131. datum antipattern = (datum) 0x55555555;
  132. /*
  133. * Write the default pattern at each of the power-of-two offsets.
  134. */
  135. for (offset = 1; (offset & addressMask) != 0; offset <<= 1) {
  136. base[offset] = pattern;
  137. }
  138. /*
  139. * Check for address bits stuck high.
  140. */
  141. testOffset = 0;
  142. base[testOffset] = antipattern;
  143. for (offset = 1; (offset & addressMask) != 0; offset <<= 1) {
  144. if (base[offset] != pattern) {
  145. return ((datum *) &base[offset]);
  146. }
  147. }
  148. base[testOffset] = pattern;
  149. /*
  150. * Check for address bits stuck low or shorted.
  151. */
  152. for (testOffset = 1; (testOffset & addressMask) != 0; testOffset <<= 1) {
  153. base[testOffset] = antipattern;
  154. if (base[0] != pattern) {
  155. return ((datum *) &base[testOffset]);
  156. }
  157. for (offset = 1; (offset & addressMask) != 0; offset <<= 1) {
  158. if ((base[offset] != pattern) && (offset != testOffset)) {
  159. return ((datum *) &base[testOffset]);
  160. }
  161. }
  162. base[testOffset] = pattern;
  163. }
  164. return NULL;
  165. }
  166. /*!
  167. * \brief Check address bus wiring on a 8 bit databus
  168. *
  169. * Test the integrity of a physical memory device by performing an
  170. * increment/decrement test over the entire region. In the process
  171. * every storage bit in the device is tested as a zero and a one.
  172. *
  173. * The base address and size of the region are selected by the caller.
  174. *
  175. * \param base memory base address that shall be used for this test
  176. * \param size size of the memory region
  177. *
  178. * \return NULL if the test succeeds. Also, in that case, the
  179. * entire memory region will be filled with zeros.
  180. *
  181. * A non-zero result is the first address at which an
  182. * incorrect value was read back. By examining the
  183. * contents of memory, it may be possible to gather
  184. * additional information about the problem.
  185. */
  186. datum * MemtestDevice(volatile datum * base, size_t size)
  187. {
  188. unsigned long offset;
  189. size_t nWords = size / sizeof(datum);
  190. datum pattern;
  191. datum antipattern;
  192. /*
  193. * Fill memory with a known pattern.
  194. */
  195. for (pattern = 1, offset = 0; offset < nWords; pattern++, offset++) {
  196. base[offset] = pattern;
  197. }
  198. /*
  199. * Check each location and invert it for the second pass.
  200. */
  201. for (pattern = 1, offset = 0; offset < nWords; pattern++, offset++) {
  202. if (base[offset] != pattern) {
  203. return ((datum *) &base[offset]);
  204. }
  205. antipattern = ~pattern;
  206. base[offset] = antipattern;
  207. }
  208. /*
  209. * Check each location for the inverted pattern and zero it.
  210. */
  211. for (pattern = 1, offset = 0; offset < nWords; pattern++, offset++) {
  212. antipattern = ~pattern;
  213. if (base[offset] != antipattern) {
  214. return ((datum *) &base[offset]);
  215. }
  216. }
  217. return NULL;
  218. }