eeprom.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2000-2004 by ETH Zurich
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
00021  *  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 
00035 /* 
00036  * unix_eeprom.c - avr eeprom functions for unix emulation
00037  *
00038  * 2004.08.05 Matthias Ringwald <matthias.ringwald@inf.ethz.ch>
00039  *
00040  */
00041 
00047 /* avoid stdio nut wrapper */
00048 #define NO_STDIO_NUT_WRAPPER
00049 
00050 #include <stdio.h>
00051 #include <sys/types.h>
00052 // eeprom image
00053 static FILE *eepromFile = 0;
00054 
00055 // asserts eepromFile open and positions read/write pointer
00056 void unix_eeprom_acces(const void *addr)
00057 {
00058     if (eepromFile == 0) {
00059         // create file if it doenst exist
00060         eepromFile = fopen("eeprom.bin", "a");
00061         if (eepromFile)
00062             fclose(eepromFile);
00063         // open file for read & write
00064         eepromFile = fopen("eeprom.bin", "r+");
00065         if (eepromFile == 0) {
00066             printf("ERROR: Open 'eeprom.bin' failed\n\r");
00067         }
00068     }
00069     if (eepromFile) {
00070         fseek(eepromFile, (long) addr, SEEK_SET);
00071     }
00072 }
00073 
00076 void eeprom_write_byte(unsigned char *addr, unsigned char val)
00077 {
00078     unix_eeprom_acces(addr);
00079     fwrite(&val, 1, 1, eepromFile);
00080     fflush(eepromFile);
00081 }
00082 
00086 void eeprom_read_block(void *buf, const void *addr, size_t n)
00087 {
00088     uint16_t count;
00089     unix_eeprom_acces(addr);
00090     count = fread(buf, 1, n, eepromFile);
00091 
00092     // fill missing values
00093     while (count < n) {
00094         ((uint8_t *) buf)[count++] = 0xff;
00095     }
00096 }
00097 
00100 uint8_t eeprom_read_byte(const unsigned char *addr)
00101 {
00102     uint8_t result;
00103     uint16_t count;
00104     unix_eeprom_acces(addr);
00105     count = fread(&result, 1, 1, eepromFile);
00106     if (count != 1)
00107         return -1;
00108     return result;
00109 }
00110 
00113 uint16_t eeprom_read_word(const unsigned short *addr)
00114 {
00115     uint16_t result;
00116     uint16_t count;
00117     unix_eeprom_acces(addr);
00118     count = fread(&result, 1, 2, eepromFile);
00119     if (count != 2)
00120         return -1;
00121     return result;
00122 }
00123 
00129 int NutNvMemLoad(unsigned int addr, void *buff, size_t siz)
00130 {
00131     eeprom_read_block (buff, (void *)addr, siz);
00132     return 0;
00133 }
00134 
00140 int NutNvMemSave(unsigned int addr, const void *buff, size_t len)
00141 {
00142     uint8_t *cp;
00143     size_t i;
00144 
00145     for (cp = (uint8_t *) buff, i = 0; i < len; cp++, i++) {
00146         if (eeprom_read_byte((uint8_t *) (addr + i)) != *cp) {
00147             eeprom_write_byte((uint8_t *) (addr + i), *cp);
00148         }
00149     }
00150     return 0;
00151 }

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