[LIB][ICC][GCC][IAR]AVR Software Framework - EEPROM
[使用说明]A、此版本兼容ATmega系列的AVR单片机对EEPROM的读写
B、兼容编译器为ICC、GCC和IAR
C、对于GCC,请务必将优化选项设置为 -00以上,否则将无法使用该函数库
D、库函数默认只开启EEPROM地读取部分,您可以通过将宏AVR_INTERNAL_EEPROM_READ定义为ES_DISABLED来
关闭这部分功能;如果您想开启EEPROM的写入功能,您可以通过将宏AVR_INTERNAL_EEPROM_WRITE设置为
ES_ENABLED来实现。所有这些宏设置应该在eeprom_cfg.h或者顶层的app_cfg.h中进行。
E、通过ES_EEP_WRITE,可以将大型数据类型,比如结构体写入EEPROM,注意,这里的第二个参数不能为指针。
F、通过ES_EEP_READ,可以从EEPROM中读取大型数据结构,同样,这里的第二个参数不能为指针。
G、您可以直接通过函数EEPROM_Write()或者EEPROM_Read()来读写指定数量的数据。
<font color=red>[目录结构]
|
- HAL
|
- Driver
||
|- EEPROM
| |
| - eeprom_cfg.h
| - eeprom.h
| - eeprom.c
+ UTILS
<font color=blue>eeprom.h
#ifndef _USE_EEPROM_H_
#define _USE_EEPROM_H_
/*-----------------------------*
*include head files *
*----------------------------*/
#include "..\..\..\utils\compiler.h"
#include "eeprom_cfg.h"
/*-----------------------------*
*Macros for others *
*----------------------------*/
#if AVR_INTERNAL_EEPROM_WRITE == ES_ENABLED
# define ES_EEP_WRITE(__ADDR,__OBJECT) \
EEPROM_Write((uint16_t)(__ADDR),&(__OBJECT),sizeof((__OBJECT)))
#endif
#if AVR_INTERNAL_EEPROM_READ != ES_DISABLED
# define ES_EEP_READ(__ADDR,__OBJECT) \
EEPROM_Read((uint16_t)(__ADDR),&(__OBJECT),sizeof((__OBJECT)))
#endif
/*-----------------------------*
*public functions prototypes*
*----------------------------*/
#if AVR_INTERNAL_EEPROM_WRITE == ES_ENABLED
extern ES_BOOL EEPROM_Write(uint16_t hwAddress,void *pData,uint16_t hwLength);
#endif
#if AVR_INTERNAL_EEPROM_READ != ES_DISABLED
extern ES_BOOL EEPROM_Read(uint16_t hwAddress,void *pData,uint16_t hwLength);
#endif
#endif
<font color=blue>eeprom.c
/*-----------------------------*
*include head files *
*----------------------------*/
#include "..\..\..\utils\compiler.h"
#include "eeprom_cfg.h"
/*-----------------------------*
*Macros for constants *
*----------------------------*/
/*-----------------------------*
*Macros for others *
*----------------------------*/
<font color=blue>
#if __IS_COMPILER_IAR__
#if !defined(EEPE)
#define __IAR_SP_EEP_EXTEND_REG_NAME_SPACE__
#endif
#endif
#if (!__IS_COMPILER_IAR__) || ((__IS_COMPILER_IAR__) && defined(__IAR_SP_EEP_EXTEND_REG_NAME_SPACE__))
#ifndef SPMCSR
# define SPMCSR SPMCR
#endif
#endif
#ifndef EEPE
# define EEPE EEWE
#endif
#ifndef EEMPE
# define EEMPE EEMWE
#endif
/*-----------------------------*
*type definitions *
*----------------------------*/
/*-----------------------------*
*structure,union and enum *
*----------------------------*/
/*-----------------------------*
*public functions prototypes*
*----------------------------*/
#if AVR_INTERNAL_EEPROM_WRITE == ES_ENABLED
ES_BOOL EEPROM_Write(uint16_t hwAddress,uint8_t *pchData,uint16_t hwLength);
#endif
#if AVR_INTERNAL_EEPROM_READ != ES_DISABLED
ES_BOOL EEPROM_Read(uint16_t hwAddress,uint8_t *pchData,uint16_t hwLength);
#endif
/*-----------------------------*
*static functions prototypes*
*----------------------------*/
/*-----------------------------*
*public variable declaration*
*----------------------------*/
/*-----------------------------*
*static variable declaration*
*----------------------------*/
#if AVR_INTERNAL_EEPROM_WRITE == ES_ENABLED
/*-----------------------------------------------------------------------------*
*Function Description: *
* Write data to eeprom. *
*Parameters: *
* hwAddress put data to this address in eeprom. Caution, do not *
* try to write datas from zero address. *
* pchData A pointer point to data buffer *
* hwLength data buffer size in byte. *
*Return *
* the result of proccessing (ES_TRUE / ES_FALSE) *
*----------------------------------------------------------------------------*/
ES_BOOL EEPROM_Write(uint16_t hwAddress,uint8_t *pchData,uint16_t hwLength)
{
//! check the write buffer length
if (hwLength == 0)
{
//! nothing to write...
return ES_TRUE;
}
//! check the pointer...
if (pchData == NULL)
{
//! the data buffer pointer is null
return ES_FALSE;
}
if (hwAddress == 0)
{
//! could not write zero address...
return ES_FALSE;
}
while(EECR & BIT(EEPE))
{
RELOAD_WATCHDOG
}
while(SPMCSR & BIT(SPMEN))
{
RELOAD_WATCHDOG
}
EEAR = (hwAddress + hwLength - 1);
if (EEAR != (hwAddress + hwLength - 1))
{
//! Out of eeprom address space
return ES_FALSE;
}
do
{
//! wait for eeprom ready
while(EECR & BIT(EEPE))
{
RELOAD_WATCHDOG
}
while(SPMCSR & BIT(SPMEN))
{
RELOAD_WATCHDOG
}
//! load data address
EEAR = hwAddress;
EEDR = *pchData++;
RELOAD_WATCHDOG
//! eeprom writing sequence...
SAFE_CODE_PERFORMANCE
(
EECR |= BIT(EEMPE);
EECR |= BIT(EEPE);
)
hwAddress++;
}
while (--hwLength);
return ES_TRUE;
}
#endif
#if AVR_INTERNAL_EEPROM_READ != ES_DISABLED
/*-----------------------------------------------------------------------------*
*Function Description: *
* Read data from eeprom. *
*Parameters: *
* hwAddress put datas to this address in eeprom. Caution, do not *
* try to write datas from zero address. *
* pchData A pointer point to data buffer *
* hwLength data buffer size in byte. *
*Return *
* the result of proccessing (ES_TRUE / ES_FALSE) *
*----------------------------------------------------------------------------*/
ES_BOOL EEPROM_Read(uint16_t hwAddress,uint8_t *pchData,uint16_t hwLength)
{
//! check the read buffer length...
if (hwLength == 0)
{
//! nothing to read.
return ES_TRUE;
}
while(EECR & BIT(EEPE))
{
RELOAD_WATCHDOG
}
//! check the read buffer address
if (pchData == NULL)
{
//! the pointer is a null value...
return ES_FALSE;
}
EEAR = (hwAddress + hwLength - 1);
if (EEAR != (hwAddress + hwLength - 1))
{
//! Out of eeprom address space
return ES_FALSE;
}
do
{
//! wait for eeprom ready
while(EECR & BIT(EEPE))
{
RELOAD_WATCHDOG
}
EEAR = hwAddress;
RELOAD_WATCHDOG
EECR |= BIT(EERE);
NOP
*pchData++ = EEDR;
hwAddress++;
}
while (--hwLength);
return ES_TRUE;
}
#endif
[相关下载]
点击此处下载 ourdev_465997.rar(文件大小:14K) <font color=green>(原文件名:EEPROM.rar) 占个沙发再慢慢看 占楼
(广告位出租) 呵呵,来抢个地板。
LZ请看4楼内容。 傻孩子的作品要顶!下下来慢慢消化。 D、库函数模人 D、库函数模人=D、库函数默认 谢谢楼上各位 一定要定起~~ 顶起 立占人立 我晕了,完全看不懂 我有问题要提问:
C、对于GCC,请务必将优化选项设置为 -00以上,否则将无法使用该函数库
为什么,选-00以上,不优化反而不行啊? 不优化,生成的代码就很傻,无法达到“4个周期内”的要求。 Gorgon Meducer 傻孩子
能详细解释一下怎样用吗?
和原来ICCAVR的eeprom读取函数有什么区别。 Gorgon Meducer 傻孩子
谢谢你的回答
以前就是因为这问题卡住了很久,后来改一下优化级别就可以了,由于有很多事要做功能可以了也就没去查原因了,这次看到你的贴子让我回忆起了,就顺便问一下 to 【15楼】 IMTP
这个函数库和以前的我发布的ICC版本,几乎是一样的,只不过针对多个编译器进行了适应性优化。
至于使用方法,直接将压缩包解压缩后,保持里面UTILS文件夹和EEPROM文件夹的相对位置不变,拷贝
到你的工程中。
在你需要使用这个库函数的.c文件中,增加对HAL\Driver\EEPROM\EEPROM.h得包含,然后就可以使用
了。所有的配置信息应该在eeprom_cfg.h或者app_cfg.h中进行。 好好学习一下 谢谢!这得好好研究一下。 谢谢分享
页:
[1]