搜索
bottom↓
回复: 21

[LIB][ICC][GCC][IAR]AVR Software Framework - EEPROM

[复制链接]

出0入296汤圆

发表于 2009-7-30 23:37:39 | 显示全部楼层 |阅读模式

[使用说明]

    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>[目录结构]


    [Project]
       |
       - 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)

出0入0汤圆

发表于 2009-7-30 23:38:40 | 显示全部楼层
占个沙发再慢慢看

出0入0汤圆

发表于 2009-7-31 00:11:02 | 显示全部楼层
占楼
(广告位出租)

出0入0汤圆

发表于 2009-7-31 05:24:06 | 显示全部楼层
呵呵,来抢个地板。
LZ请看4楼内容。

出0入0汤圆

发表于 2009-7-31 08:43:25 | 显示全部楼层
傻孩子的作品要顶!下下来慢慢消化。

出0入0汤圆

发表于 2009-7-31 08:49:04 | 显示全部楼层
D、库函数模人

出0入0汤圆

发表于 2009-7-31 09:04:52 | 显示全部楼层
D、库函数模人=D、库函数默认

出0入296汤圆

 楼主| 发表于 2009-7-31 19:23:27 | 显示全部楼层
谢谢楼上各位

出0入0汤圆

发表于 2009-8-1 11:23:10 | 显示全部楼层
一定要定起~~

出0入0汤圆

发表于 2009-8-1 17:48:00 | 显示全部楼层
顶起

出0入0汤圆

发表于 2009-8-1 21:01:17 | 显示全部楼层
立占人立

出0入0汤圆

发表于 2009-8-15 21:37:57 | 显示全部楼层
我晕了,完全看不懂

出0入4汤圆

发表于 2009-9-2 21:15:46 | 显示全部楼层
我有问题要提问:
C、对于GCC,请务必将优化选项设置为 -00以上,否则将无法使用该函数库

为什么,选-00以上,不优化反而不行啊?

出0入296汤圆

 楼主| 发表于 2009-9-4 19:03:52 | 显示全部楼层
不优化,生成的代码就很傻,无法达到“4个周期内”的要求。

出0入0汤圆

发表于 2009-9-4 21:58:16 | 显示全部楼层
Gorgon Meducer 傻孩子
能详细解释一下怎样用吗?
和原来ICCAVR的eeprom读取函数有什么区别。

出0入4汤圆

发表于 2009-9-5 10:21:48 | 显示全部楼层
Gorgon Meducer 傻孩子
谢谢你的回答

以前就是因为这问题卡住了很久,后来改一下优化级别就可以了,由于有很多事要做功能可以了也就没去查原因了,这次看到你的贴子让我回忆起了,就顺便问一下

出0入296汤圆

 楼主| 发表于 2009-9-5 20:14:25 | 显示全部楼层
to 【15楼】 IMTP
    这个函数库和以前的我发布的ICC版本,几乎是一样的,只不过针对多个编译器进行了适应性优化。
至于使用方法,直接将压缩包解压缩后,保持里面UTILS文件夹和EEPROM文件夹的相对位置不变,拷贝
到你的工程中。
    在你需要使用这个库函数的.c文件中,增加对HAL\Driver\EEPROM\EEPROM.h得包含,然后就可以使用
了。所有的配置信息应该在eeprom_cfg.h或者app_cfg.h中进行。

出0入0汤圆

发表于 2012-3-27 09:47:39 | 显示全部楼层
好好学习一下

出0入0汤圆

发表于 2012-3-28 18:30:54 | 显示全部楼层
谢谢!这得好好研究一下。

出0入0汤圆

发表于 2012-7-20 15:09:51 | 显示全部楼层
谢谢分享
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-1 15:42

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表