flashtest.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*!
  2. * Copyright (C) 2001-2003 by egnite Software GmbH
  3. * Copyright (C) 2013 Uwe Bonnes
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * $Id: flashtest.c 5592 2014-03-17 10:59:46Z u_bonnes $
  37. */
  38. /*!
  39. * \example uart/uart.c
  40. *
  41. * This sample demonstrates the usage of the parameter memory.
  42. */
  43. #include <string.h>
  44. #include <stdio.h>
  45. #include <io.h>
  46. #include <dev/board.h>
  47. #include <dev/iap_flash.h>
  48. #include <dev/nvmem.h>
  49. #include <sys/timer.h>
  50. static char *banner = "\nNut/OS Flash Sample on " BOARDNAME " " __DATE__ " " __TIME__"\n";
  51. static char *pattern = "0123456789abcdef0123456789ABCDEF";
  52. static char *pattern1 = "FEDCBA9876543210fedcba987654321";
  53. static char *pattern2 = "0123456789abcdef0123456789ABCD";
  54. static char *pattern3 = "abcdef0123456789abcdef0123456";
  55. static char pattern4 = 0x55;
  56. /*
  57. * UART sample.
  58. *
  59. * Some functions do not work with ICCAVR.
  60. */
  61. int main(void)
  62. {
  63. int res;
  64. uint32_t baud = 115200, read_timeout = 10;
  65. FILE *uart;
  66. char buffer[7];
  67. uint32_t iap_flash_end = IapFlashEnd();
  68. void *dword_aligned_end;
  69. uint32_t rd;
  70. int i;
  71. uint32_t *rptr;
  72. NutRegisterDevice(&DEV_CONSOLE, 0, 0);
  73. uart = fopen(DEV_CONSOLE.dev_name, "r+");
  74. _ioctl(_fileno(uart), UART_SETSPEED, &baud);
  75. _ioctl(_fileno(uart), UART_SETREADTIMEOUT, &read_timeout);
  76. freopen(DEV_CONSOLE.dev_name, "r", stdin);
  77. freopen(DEV_CONSOLE.dev_name, "w", stdout);
  78. printf(banner);
  79. NutSleep(10);
  80. res = NutNvMemSave(0x0, "Save0", 6);
  81. if(res)
  82. printf("NutNvMemSave failed: %d\n", res);
  83. else {
  84. NutNvMemLoad(0x0, buffer, 6);
  85. if (memcmp(buffer, "Save0", 5))
  86. printf("NutNvMemSave compare failed: %s vs %s\n", buffer, "Save0");
  87. res = NutNvMemSave(0x0, "Save1", 6);
  88. if(res)
  89. printf("NutNvMemSave failed: %d\n", res);
  90. NutNvMemLoad(0x0, buffer, 6);
  91. if (memcmp(buffer, "Save1", 5))
  92. printf("NutNvMemSave compare failed: %s vs %s\n", buffer, "Save1");
  93. res = NutNvMemSave(0x0, "Save2", 6);
  94. if(res)
  95. printf("NutNvMemSave failed: %d\n", res);
  96. NutNvMemLoad(0x0, buffer, 6);
  97. if (memcmp(buffer, "Save2", 5))
  98. printf("NutNvMemSave compare failed: %s vs %s\n", buffer, "Save2");
  99. res = NutNvMemSave(0x0, "u", 1);
  100. if(res)
  101. printf("NutNvMemSave failed: %d\n", res);
  102. NutNvMemLoad(0x0, buffer, 6);
  103. if (memcmp(buffer, "uave2", 5))
  104. printf("NutNvMemSave compare failed: %s vs %s\n", buffer, "uave2");
  105. }
  106. printf("NutNvMem test done\n");
  107. printf("Application Flash ends at 0x%08lx\n", iap_flash_end);
  108. rptr = (uint32_t *) (iap_flash_end & 0xffffff00);
  109. for(i = 0; i < 64; i++) {
  110. if (rptr[i] != FLASH_ERASED_PATTERN32) {
  111. printf("Not ");
  112. break;
  113. }
  114. }
  115. printf("Empty\n");
  116. memset(buffer, FLASH_ERASED_PATTERN32 & 0xff, sizeof(buffer));
  117. printf("Write to erased flash\n");
  118. res = IapFlashWrite((void*)(iap_flash_end ), &pattern4,
  119. 1, FLASH_ERASE_FIRST_TOUCH);
  120. printf("%40s %3d: ", "0x55 at (address & 3 == 3). Res", res);
  121. rd = *(uint32_t*)(iap_flash_end & ~3);
  122. printf("0x%08lx\n", rd);
  123. NutSleep(10);
  124. res = IapFlashWrite((void*)(iap_flash_end - 5), &pattern4,
  125. 1, FLASH_ERASE_FIRST_TOUCH);
  126. printf("%40s %3d: ", "0x55 at (address & 3 == 2). Res", res);
  127. rd = *(uint32_t*)(iap_flash_end & ~7);
  128. printf("0x%08lx\n", rd);
  129. NutSleep(10);
  130. res = IapFlashWrite((void*)(iap_flash_end - 10), &pattern4,
  131. 1, FLASH_ERASE_NEVER);
  132. printf("%40s %3d: ", "0x55 at (address & 3 == 1). Res", res);
  133. rd = *(uint32_t*)((iap_flash_end - 8) & ~3);
  134. printf("0x%08lx\n", rd);
  135. NutSleep(10);
  136. res = IapFlashWrite((void*)(iap_flash_end - 15), &pattern4,
  137. 1, FLASH_ERASE_NEVER);
  138. printf("%40s %3d: ", "0x55 at (address & 3 == 0). Res", res);
  139. rd = *(uint32_t*)(iap_flash_end & ~15);
  140. printf("0x%08lx\n", rd);
  141. NutSleep(10);
  142. res = IapFlashWrite((void*)(iap_flash_end -0x3f), pattern1,
  143. strlen(pattern1) + 1, FLASH_ERASE_NEVER);
  144. printf("%40s %3d: ", "Up to (address & 3 == 3). Res", res);
  145. printf((char*)(iap_flash_end -0x3f));
  146. printf("\n");
  147. NutSleep(10);
  148. res = IapFlashWrite((void*)(iap_flash_end -0x7f), pattern2,
  149. strlen(pattern2) + 1, FLASH_ERASE_NEVER);
  150. printf("%40s %3d: ", "Up to (address & 3 == 2). Res", res);
  151. printf((char*)(iap_flash_end -0x7f));
  152. printf("\n");
  153. NutSleep(10);
  154. res = IapFlashWrite((void*)(iap_flash_end -0xcf), pattern3,
  155. strlen(pattern2) + 1, FLASH_ERASE_NEVER);
  156. printf("%40s %3d: ", "Up to (address & 3 == 1). Res", res);
  157. printf((char*)(iap_flash_end -0xcf));
  158. printf("\n");
  159. NutSleep(10);
  160. res = IapFlashWrite((void*)(iap_flash_end -0xff), pattern,
  161. strlen(pattern) + 1, FLASH_ERASE_NEVER);
  162. printf("%40s %3d: ", "Up to (address & 3 == 0). Res", res);
  163. printf((char*)(iap_flash_end -0xff));
  164. printf("\n");
  165. NutSleep(10);
  166. dword_aligned_end = (void*)((iap_flash_end - 0xff + strlen(pattern))
  167. & 0xfffffff0);
  168. printf("Write to programmed flash\n");
  169. res = IapFlashWrite(dword_aligned_end - 1, buffer, 1,
  170. FLASH_ERASE_NEVER);
  171. printf("%40s %3d: ", "0x00 at (address & 3 == 3). Res", res);
  172. if ((res == 0) || (res == FLASH_COMPARE))
  173. printf((char*)(iap_flash_end -0xff));
  174. NutSleep(10);
  175. printf("\n");
  176. res = IapFlashWrite(dword_aligned_end - 2, buffer, 1,
  177. FLASH_ERASE_NEVER);
  178. printf("%40s %3d: ", "0x00 at (address & 3 == 2). Res", res);
  179. if ((res == 0) || (res == FLASH_COMPARE))
  180. printf((char*)(iap_flash_end -0xff));
  181. NutSleep(10);
  182. printf("\n");
  183. res = IapFlashWrite(dword_aligned_end - 3, buffer, 1,
  184. FLASH_ERASE_NEVER);
  185. printf("%40s %3d: ", "0x00 at (address & 3 == 1). Res", res);
  186. if ((res == 0) || (res == FLASH_COMPARE))
  187. printf((char*)(iap_flash_end -0xff));
  188. printf("\n");
  189. NutSleep(10);
  190. res = IapFlashWrite(dword_aligned_end - 4, buffer, 1,
  191. FLASH_ERASE_NEVER);
  192. printf("%40s %3d: ", "0x00 at (address & 3 == 0). Res", res);
  193. if ((res == 0) || (res == FLASH_COMPARE))
  194. printf((char*)(iap_flash_end -0xff));
  195. printf("\n");
  196. NutSleep(10);
  197. res = IapFlashWrite(dword_aligned_end - 6, buffer, 2,
  198. FLASH_ERASE_NEVER);
  199. printf("%40s %3d: ", "0x0000 at (address & 3 == 2). Res",
  200. res);
  201. if ((res == 0) || (res == FLASH_COMPARE))
  202. printf((char*)(iap_flash_end -0xff));
  203. printf("\n");
  204. NutSleep(10);
  205. res = IapFlashWrite(dword_aligned_end - 8, buffer, 2,
  206. FLASH_ERASE_NEVER);
  207. printf("%40s %3d: ", "0x0000 at (address & 3 == 0). Res",
  208. res);
  209. if ((res == 0) || (res == FLASH_COMPARE))
  210. printf((char*)(iap_flash_end -0xff));
  211. printf("\n");
  212. NutSleep(10);
  213. res = IapFlashWrite(dword_aligned_end - 11, buffer, 2,
  214. FLASH_ERASE_NEVER);
  215. printf("%40s %3d: ", "0x0000 at (address & 3 == 1). Res",
  216. res);
  217. if ((res == 0) || (res == FLASH_COMPARE))
  218. printf((char*)(iap_flash_end -0xff));
  219. printf("\n");
  220. NutSleep(10);
  221. res = IapFlashWrite(dword_aligned_end - 13, buffer, 2,
  222. FLASH_ERASE_NEVER);
  223. printf("%40s %3d: ", "0x0000 at (address & 3 == 3). Res", res);
  224. if ((res == 0) || (res == FLASH_COMPARE))
  225. printf((char*)(iap_flash_end -0xff));
  226. printf("\n");
  227. NutSleep(10);
  228. res = IapFlashWrite(dword_aligned_end - 17, buffer, 4,
  229. FLASH_ERASE_NEVER);
  230. printf("%40s %3d: ", "0x00000000 at (address & 3 == 3). Res", res);
  231. if ((res == 0) || (res == FLASH_COMPARE))
  232. printf((char*)(iap_flash_end -0xff));
  233. printf("\n");
  234. NutSleep(10);
  235. res = IapFlashWrite(dword_aligned_end - 22, buffer, 4,
  236. FLASH_ERASE_NEVER);
  237. printf("%40s %3d: ", "0x00000000 at (address & 3 == 2). Res", res);
  238. if ((res == 0) || (res == FLASH_COMPARE))
  239. printf((char*)(iap_flash_end -0xff));
  240. printf("\n");
  241. NutSleep(10);
  242. res = IapFlashWrite(dword_aligned_end - 27, buffer, 4,
  243. FLASH_ERASE_NEVER);
  244. printf("%40s %3d: ", "0x00000000 at (address & 3 == 1). Res", res);
  245. if ((res == 0) || (res == FLASH_COMPARE))
  246. printf((char*)(iap_flash_end -0xff));
  247. printf("\n");
  248. NutSleep(10);
  249. res = IapFlashWrite(dword_aligned_end - 32, buffer, 4,
  250. FLASH_ERASE_NEVER);
  251. printf("%40s %3d: ", "0x00000000 at (address & 3 == 0). Res", res);
  252. if ((res == 0) || (res == FLASH_COMPARE))
  253. printf((char*)(iap_flash_end -0xff));
  254. printf("\n");
  255. NutSleep(10);
  256. for (;;) {
  257. NutSleep(100);
  258. }
  259. return 0;
  260. }