eeprom_test.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009 by Rittal GmbH & Co. KG,
00003  * Ulrich Prinz <prinz.u@rittal.de> All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the copyright holders nor the names of
00015  *    contributors may be used to endorse or promote products derived
00016  *    from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY EMBEDDED IT AND CONTRIBUTORS
00019  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EMBEDDED IT
00022  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * $Log$
00036  *
00037  * Revision 1.0  2009/04/13 ulrichprinz
00038  * First checkin, test application for new AT24C EEPROM driver (currently 
00039  * SAM7X256 is tested only)
00040  *
00041  */
00042                               
00047 /* Test */
00048 #include <stdio.h>
00049 #include <string.h>
00050 #include <io.h>
00051 #include <dev/gpio.h>
00052 
00053 #include <cfg/arch.h>
00054 #include <dev/board.h>
00055 
00056 #include <sys/thread.h>
00057 #include <sys/timer.h>
00058 #include <dev/twif.h>
00059 #include <dev/pca9555.h>
00060 #include <dev/eeprom.h>
00061 #define MYPRINT(fmt, ...) printf ("%s:%s(%d)\t" fmt "\n", \
00062                              __FILE__,__FUNCTION__,__LINE__, ##__VA_ARGS__);
00063 
00064 #define LED_OFF     0
00065 #define LED_ON      1
00066 
00067 #define LED(led,state)  if( state == LED_OFF )IOExpSetBit( 1, led );else IOExpClrBit( 1, led )
00068 #define LED1(state) LED( 0, state )
00069 #define LED2(state) LED( 1, state )
00070 #define LED3(state) LED( 2, state )
00071 #define LED4(state) LED( 3, state )
00072 #define LED5(state) LED( 4, state )
00073 
00074 #define DLED(led,state) if( state == LED_OFF )IOExpSetBit( 0, led + 4 );else IOExpClrBit( 0, led + 4 )
00075 #define DLED1(state)    DLED( 0, state )
00076 #define DLED2(state)    DLED( 1, state )
00077 #define DLED3(state)    DLED( 2, state )
00078 #define DLED4(state)    DLED( 3, state )
00079 
00080 #define KEY1 (1<<0)
00081 #define KEY2 (1<<1)
00082 #define KEY3 (1<<2)
00083 #define KEY4 (1<<3)
00084 
00085 int8_t ledid = 0;
00086 uint8_t up = 0;
00087 
00088 
00089 THREAD(Led, arg)
00090 {
00091     /*
00092      * Endless loop in high priority thread.
00093      */
00094     NutThreadSetPriority(16);
00095     for (;;)
00096     {
00097         if( up == 1 )
00098         {
00099             LED( ledid, LED_OFF );
00100             if( ++ledid > 4 )
00101             {
00102                 ledid = 3;
00103                 up = 0;
00104             }
00105             LED( ledid, LED_ON );
00106         }
00107         else
00108         {
00109             LED( ledid, LED_OFF );
00110             if( --ledid < 0 )
00111             {
00112                 ledid = 1;
00113                 up = 1;
00114             }
00115             LED( ledid, LED_ON );
00116         }
00117         NutSleep(200);
00118     }
00119 }
00120 
00121 void HexDump( uint8_t *rxb, uint16_t len )
00122 {
00123     uint16_t i;
00124     uint16_t f = 1;
00125     char ascii[17];
00126 
00127     for( i = 0; i < len; i++ )
00128     {
00129         if( f == 1 )
00130         {
00131             f = 0;
00132             printf ( "%04x : ", i );
00133         }
00134         printf( "%02X ", rxb[i] );
00135         ascii[ i % 16] = ( rxb[i] > 31 && rxb[i] < 128 ) ? rxb[i] : '.';
00136 
00137         if((( i + 1) % 16 ) == 0 )
00138         {
00139             f = 1;
00140             ascii[16] = 0;
00141             printf( "| %s\n", ascii );
00142         }
00143     }
00144 }
00145 
00146 const uint8_t teststr[64] = { "This is an ultimatly long string that reaches 61 bytes length\0"};
00147 
00148 /*
00149  * Main application thread.
00150  */
00151 int main(void)
00152 {
00153 
00154     uint8_t txBuffer[128];
00155     uint8_t rxBuffer[128];
00156     u_long baud = 115200;
00157 
00158     /*
00159      * Register the UART device, open it, assign stdout to it and set
00160      * the baudrate.
00161      */
00162     NutRegisterDevice(&DEV_DEBUG, 0, 0);
00163     freopen(DEV_DEBUG_NAME, "w", stdout);
00164     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00165 
00166     printf( "\n*** EEPROM Test ***\n");
00167 
00168     baud = 100000;
00169     TwInit( 0 ); /* par = slave address but we are master */
00170     TwIOCtl( TWI_SETSPEED, &baud);
00171 //  IOExpInit();
00172     EEInit();
00173 
00174     /*
00175      * Start two additional threads. All threads are started with
00176      * priority 64.
00177      */
00178     // NutThreadCreate("Led", Led, 0, 512);
00179 
00180 
00181     /****************************** 1st part *******************************/
00182     /*       Before starting the test, fill the EEPROM with 0xFF           */
00183     /***********************************************************************/
00184 
00185     /* Clear the Receive buffer */
00186     memset( rxBuffer, 0x00, sizeof( rxBuffer));
00187     memset( txBuffer, 0xff, sizeof( txBuffer));
00188 
00189     printf( "Read\n" );
00190     EEReadData( 2, rxBuffer, 64);
00191     HexDump( rxBuffer, 64 );
00192 
00193     if( !strncmp( rxBuffer, teststr, strlen( teststr)))
00194         printf( "Test successfull, data is equal!\n");
00195     else
00196         printf( "Test failed, data not equal!\n");
00197 
00198     printf( "\nInit: Fill 0xFF\n" );
00199     EEWriteData( 0, txBuffer, 64);
00200     EEReadData( 0, rxBuffer, 64);
00201     HexDump( rxBuffer, 64 );
00202 
00203     strcpy( txBuffer, "First, " );
00204     EEWriteData( 24, txBuffer, strlen( txBuffer ));
00205     EEReadData( 0, rxBuffer, 64);
00206     printf( "1 Step\n" );
00207     HexDump( rxBuffer, 64 );
00208 
00209     strcpy( txBuffer, "Third." );
00210     EEWriteData( 24+16, txBuffer, strlen( txBuffer ));
00211     EEReadData( 0, rxBuffer, 64);
00212     printf( "2 Step\n" );
00213     HexDump( rxBuffer, 64 );
00214 
00215     strcpy( txBuffer, "Second, " );
00216     EEWriteData( 24+8, txBuffer, strlen( txBuffer ));
00217     EEReadData( 0, rxBuffer, 64);
00218     printf( "3 Step\n" );
00219     HexDump( rxBuffer, 64 );
00220 
00221     EEWriteData( 2, (void*)teststr, strlen( teststr));
00222     EEReadData( 2, rxBuffer, 64);
00223     printf( "4 Step\n" );
00224     HexDump( rxBuffer, 64 );
00225 
00226     /*
00227      * Endless loop in main thread.
00228      */
00229     for (;;)
00230     {
00231         NutSleep(1000);
00232     }
00233     return 0;
00234 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/