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

© 2000-2010 by contributors - visit http://www.ethernut.de/