📄 eeprom.c
字号:
/* * Copyright (C) 2000-2004 by ETH Zurich * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * For additional information see http://www.ethernut.de/ * *//* * unix_eeprom.c - avr eeprom functions for unix emulation * * 2004.08.05 Matthias Ringwald <matthias.ringwald@inf.ethz.ch> * *//**\brief Provides the avr eeprom api. It uses the file 'btnode_eerpom.bin' to store its content.*//* avoid stdio nut wrapper */#define NO_STDIO_NUT_WRAPPER#include <stdio.h>// eeprom imagestatic FILE *eepromFile = 0;// asserts eepromFile open and positions read/write pointervoid unix_eeprom_acces(const void *addr){ if (eepromFile == 0) { // create file if it doenst exist eepromFile = fopen("eeprom.bin", "a"); if (eepromFile) fclose(eepromFile); // open file for read & write eepromFile = fopen("eeprom.bin", "r+"); if (eepromFile == 0) { printf("ERROR: Open 'eeprom.bin' failed\n\r"); } } if (eepromFile) { fseek(eepromFile, (long) addr, SEEK_SET); }} /** \ingroup unix_eeprom write a byte \c val to EEPROM address \c addr */void eeprom_write_byte(unsigned char *addr, unsigned char val){ unix_eeprom_acces(addr); fwrite(&val, 1, 1, eepromFile); fflush(eepromFile);}/** \ingroup unix_eeprom read a block of \c n bytes from EEPROM address \c addr to \c buf */void eeprom_read_block(void *buf, const void *addr, size_t n){ u_short count; unix_eeprom_acces(addr); count = fread(buf, 1, n, eepromFile); // fill missing values while (count < n) { ((u_char *) buf)[count++] = 0xff; }}/** \ingroup unix_eeprom read one byte from EEPROM address \c addr */u_char eeprom_read_byte(const unsigned char *addr){ u_char result; u_short count; unix_eeprom_acces(addr); count = fread(&result, 1, 1, eepromFile); if (count != 1) return -1; return result;}/** \ingroup unix_eeprom read one 16-bit word (little endian) from EEPROM address \c addr */u_short eeprom_read_word(const unsigned short *addr){ u_short result; u_short count; unix_eeprom_acces(addr); count = fread(&result, 1, 2, eepromFile); if (count != 2) return -1; return result;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -