eeprom_test.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright (C) 2010 by Ulrich Prinz (uprinz2@netscape.net)
  3. * Copyright (C) 2010 by Rittal GmbH & Co. KG GmbH. All rights reserved.
  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. * \verbatim
  37. * $Id: eeprom_test.c 3108 2010-09-15 21:11:15Z Astralix $
  38. * \endverbatim
  39. */
  40. /*!
  41. * \example eeprom/eeprom.c
  42. *
  43. * Test application for new AT24C EEPROM driver.
  44. * (currently SAM7X256 and STM32F is tested only).
  45. *
  46. */
  47. /* Test */
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <io.h>
  51. #include <dev/gpio.h>
  52. #include <cfg/arch.h>
  53. #include <dev/board.h>
  54. #include <sys/thread.h>
  55. #include <sys/timer.h>
  56. #include <dev/twif.h>
  57. #include <dev/eeprom.h>
  58. #if !defined(MCU_AT91) || defined(MCU_AT91R40008) || defined(MCU_GBA)
  59. int main(void)
  60. {
  61. uint32_t baud = 115200;
  62. NutRegisterDevice(&DEV_DEBUG, 0, 0);
  63. freopen(DEV_DEBUG_NAME, "w", stdout);
  64. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  65. puts("TwMasterReg functions are not available on your platform.");
  66. for (;;);
  67. }
  68. #else
  69. void HexDump( char *rxb, uint16_t len )
  70. {
  71. uint16_t i;
  72. uint16_t f = 1;
  73. char ascii[17];
  74. for( i = 0; i < len; i++ )
  75. {
  76. if( f == 1 )
  77. {
  78. f = 0;
  79. printf ( "%04x : ", i );
  80. }
  81. printf( "%02X ", rxb[i] );
  82. ascii[ i % 16] = ( rxb[i] > 31 && rxb[i] < 128 ) ? rxb[i] : '.';
  83. if((( i + 1) % 16 ) == 0 )
  84. {
  85. f = 1;
  86. ascii[16] = 0;
  87. printf( "| %s\n", ascii );
  88. }
  89. }
  90. }
  91. const char teststr[64] = { "This is an ultimatly long string that reaches 61 bytes length\0"};
  92. const char failstr[] = "\033[1;37;41m\033[K*** FAIL rc=%d ***\033[0m\n";
  93. const char infostr[] = "\n\033[30;46m\033[K%s\033[0m\n";
  94. /*
  95. * Main application thread.
  96. */
  97. int main(void)
  98. {
  99. char txBuffer[128];
  100. char rxBuffer[128];
  101. uint32_t baud = 115200;
  102. int rc = 0;
  103. /*
  104. * Register the UART device, open it, assign stdout to it and set
  105. * the baudrate.
  106. */
  107. NutRegisterDevice(&DEV_DEBUG, 0, 0);
  108. freopen(DEV_DEBUG_NAME, "w", stdout);
  109. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  110. /* Clear terminal screen and cursor home */
  111. printf( "\033[2J\033[H");
  112. printf( "\033[1;37;44m\033[K*** EEPROM Test ***\033[0m\n");
  113. baud = 100000;
  114. TwInit( 0 ); /* par = slave address but we are master */
  115. TwIOCtl( TWI_SETSPEED, &baud);
  116. EEInit();
  117. /*
  118. * Start two additional threads. All threads are started with
  119. * priority 64.
  120. */
  121. // NutThreadCreate("Led", Led, 0, 512);
  122. /****************************** 1st part *******************************/
  123. /* Before starting the test, fill the EEPROM with 0xFF */
  124. /***********************************************************************/
  125. /* Clear the Receive buffer */
  126. memset( rxBuffer, 0x00, sizeof( rxBuffer));
  127. memset( txBuffer, 0xff, sizeof( txBuffer));
  128. printf( infostr, "*** Read ***" );
  129. rc = EEReadData( 0x0100, rxBuffer, 64);
  130. HexDump( rxBuffer, 64 );
  131. if( rc) {
  132. printf(failstr, rc);
  133. printf("Please make sure that you have an EEPROM connected on your board.\n");
  134. while(1) NutSleep(1000);
  135. }
  136. if( !strncmp( rxBuffer, teststr, strlen( teststr)))
  137. printf( "Test successfull, data is equal!\n");
  138. else
  139. printf( "Test failed, data not equal!\n");
  140. printf( infostr,"Init: Fill 0xFF" );
  141. rc = EEWriteData( 0x0100, txBuffer, 64);
  142. if( rc) {
  143. printf(failstr, rc);
  144. while(1) NutSleep(1000);
  145. }
  146. rc = EEReadData( 0x0100, rxBuffer, 64);
  147. HexDump( rxBuffer, 64 );
  148. if( rc) {
  149. printf(failstr, rc);
  150. while(1) NutSleep(1000);
  151. }
  152. /*****************/
  153. printf( infostr, "Step 1" );
  154. strcpy( (char*)txBuffer, "First, " );
  155. rc = EEWriteData( 0x0100+24, txBuffer, strlen( (char*)txBuffer ));
  156. if( rc) {
  157. printf(failstr, rc);
  158. while(1) NutSleep(1000);
  159. }
  160. rc = EEReadData( 0x0100, rxBuffer, 64);
  161. HexDump( rxBuffer, 64 );
  162. if( rc) {
  163. printf(failstr, rc);
  164. while(1) NutSleep(1000);
  165. }
  166. /*****************/
  167. printf( infostr, "Step 2" );
  168. strcpy( txBuffer, (const char*)"Third!" );
  169. rc = EEWriteData( 0x0100+24+16, (const char*)txBuffer, strlen( txBuffer ));
  170. if( rc) {
  171. printf(failstr, rc);
  172. while(1) NutSleep(1000);
  173. }
  174. rc = EEReadData( 0x0100, rxBuffer, 64);
  175. HexDump( rxBuffer, 64 );
  176. if( rc) {
  177. printf(failstr, rc);
  178. while(1) NutSleep(1000);
  179. }
  180. /*****************/
  181. printf( infostr, "Step 3" );
  182. strcpy( txBuffer, "Second, " );
  183. rc = EEWriteData( 0x0100+24+8, txBuffer, strlen( txBuffer ));
  184. if( rc) {
  185. printf(failstr, rc);
  186. while(1) NutSleep(1000);
  187. }
  188. rc = EEReadData( 0x0100, rxBuffer, 64);
  189. HexDump( rxBuffer, 64 );
  190. if( rc) {
  191. printf(failstr, rc);
  192. while(1) NutSleep(1000);
  193. }
  194. /*****************/
  195. printf( infostr, "Step 4" );
  196. rc = EEWriteData( 0x0100, (void*)teststr, strlen( teststr));
  197. if( rc) {
  198. printf(failstr, rc);
  199. while(1) NutSleep(1000);
  200. }
  201. rc = EEReadData( 0x0100, rxBuffer, 64);
  202. HexDump( rxBuffer, 64 );
  203. if( rc) {
  204. printf(failstr, rc);
  205. while(1) NutSleep(1000);
  206. }
  207. printf( "\n\033[30;34m\033[K*** END ***\033[0m\n" );
  208. /*
  209. * Endless loop in main thread.
  210. */
  211. for (;;)
  212. {
  213. NutSleep(1000);
  214. }
  215. return 0;
  216. }
  217. #endif