avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-libc-dev] [RFC] New eeprom.h


From: Wouter van Gulik
Subject: Re: [avr-libc-dev] [RFC] New eeprom.h
Date: Wed, 30 Jan 2008 11:41:52 +0100
User-agent: Thunderbird 2.0.0.9 (Windows/20071031)

Weddington, Eric schreef:

Mainly because this is a total rewrite of the implementation. The API
(interface) stays the same. I'm certainly fine with putting the old
copyrights back on.


I thought the address thingy was taken from Bjoern Haasse's implementation. So that's why I thought he should at least be there. But I am no lawyer, and personally I do not mind.


This sounds promising. Do you care to modify the header?


I did. See attachment. (if they do not get to the list, please notify me)
No guarantees. But for me it saves up to 50 bytes testing read/write pairs per type. The only thing I can't make gcc do in a proper manner is the read block. GCC just uses to many instructions. That is a candidate for a special assembler routine.

 > There is a loop, or should be. The loop is checking the EEWE flag (or
equivalent) to make sure that a read or write is possible.

And the old code is not disabling interrupts during eeprom write... which is
no good! Maybe this was the problem of the report for the tiny13?

No, the old code is disabling the interrupts for the write routines. You
have to look in the libc/misc/eeprom.S file in the avr-libc source.


Right, I forgot about the assembler file... shame on me! Sorry for the fuzz.

Wouter

/* Copyright (c) 2007 Atmel Corporation
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   * Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.

   * 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.

   * 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 THE COPYRIGHT HOLDERS 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 THE COPYRIGHT OWNER 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. */

/* $Id$ */


#ifndef _AVR_EEPROM_H_
#define _AVR_EEPROM_H_ 1

#include <avr/io.h>
#include <stdint.h>


/** \def EEMEM
    \ingroup avr_eeprom
    Attribute expression causing a variable to be allocated within the .eeprom
     section.  */
#define EEMEM __attribute__((section(".eeprom")))


/* Register definitions */

/* Check for existence of EEPROM peripheral. */
#if E2END == 0
#error "Device does not have EEPROM available!"
#endif


/* Check for aliases. */
#if !defined(EEWE) && defined(EEPE)
#define EEWE EEPE
#endif

#if !defined(EEMWE) && defined(EEMPE)
#define EEMWE EEMPE
#endif

#if !defined(EECR) && defined(DEECR)
/* AT86RF401 */
#define EECR  DEECR
#define EEAR  DEEAR
#define EEARL DEEAR
#define EEDR  DEEDR
#define EERE  EER
#define EEWE  EEL
#define EEMWE EEU
#endif


#if !defined(EECR) || !defined(EEDR) || !defined(EEARL)

    #ifndef __EEPROM_REG_LOCATIONS__
        /*
        6-byte string denoting where to find the EEPROM registers in memory 
space.
        Adresses denoted in hex syntax with uppercase letters. Used by the 
EEPROM
        subroutines.
        First two letters:  EECR address.
        Second two letters: EEDR address.
        Last two letters:   EEAR address.
        */
        #define __EEPROM_REG_LOCATIONS__ 1C1D1E
    #endif

    /* If needed, override the locations defined in the IO headers. */
    #ifdef EEPROM_REG_LOCATIONS_OVERRIDE
        #undef  __EEPROM_REG_LOCATIONS__
        #define __EEPROM_REG_LOCATIONS__ EEPROM_REG_LOCATIONS_OVERRIDE
    #endif

    #define CONCAT1(a, b) CONCAT2(a, b)
    #define CONCAT2(a, b) a ## b
    #define HEXNR CONCAT1(0x, __EEPROM_REG_LOCATIONS__)

    #undef EECR
    #define EECR _SFR_IO8((HEXNR >> 16) & 0xFF)

    #undef EEDR
    #define EEDR _SFR_IO8((HEXNR >> 8) & 0xFF )

    #undef EEAR
    #define EEAR _SFR_IO8(HEXNR & 0xFF)

    #undef EEARH

    #undef EEARL
    #define EEARL EEAR

#endif


/** \def eeprom_is_ready
    \ingroup avr_eeprom
    \returns 1 if EEPROM is ready for a new read/write operation, 0 if not. */

#if defined(__DOXYGEN__)
# define eeprom_is_ready()
#elif defined(DEECR)
# define eeprom_is_ready() bit_is_clear(DEECR, BSY)
#else
# define eeprom_is_ready() bit_is_clear(EECR, EEWE)
#endif


static inline uint8_t __eeprom_read_byte_address_byte(uint8_t*) 
__attribute__((always_inline));
static inline uint8_t __eeprom_read_byte_address_byte(uint8_t* ee_adr)
{
    do{}while(!eeprom_is_ready());
    uint8_t __result;
    EEARL = (uint16_t)ee_adr & 0xFF;
    EECR |= 1<<EERE;
    __result = EEDR;
    return __result;
}

#if E2END > 0xFF
static inline uint8_t __eeprom_read_byte_address_word(uint8_t* eeptr)
{
    do{}while(!eeprom_is_ready());
    uint16_t __address_word = (uint16_t)(eeptr);
    uint8_t __result;
    EEARL = __address_word & 0xFF;
    EEARH = __address_word >> 8;
    EECR |= 1<<EERE;
    __result = EEDR;
    return __result;
}

#endif

static void inline __eeprom_write_byte_address_byte(uint8_t addr, uint8_t value)
{
    do{}while(!eeprom_is_ready());
    EEARL = addr;
    EEDR  = value;

    __asm__ __volatile__ (
        "/* START EEPROM WRITE CRITICAL SECTION*/\n\t"
        "in r0, %[sreg]\n\t"
        "cli\n\t"
        "sbi %[eecr], %[eemwe]\n\t"
        "sbi %[eecr], %[eewe]\n\t"
        "out %[sreg], r0\n\t"
        "/* END EEPROM WRITE CRITICAL SECTION*/\n\t"
        :
        : [eecr]  "i" (_SFR_IO_ADDR(EECR)),
          [sreg]  "i" (_SFR_IO_ADDR(SREG)),
          [eemwe] "i" (EEMWE),
          [eewe]  "i" (EEWE)
        : "r0"
    );

}

static void inline __eeprom_write_word_address_byte(uint8_t addr, uint16_t 
value)
{
    __eeprom_write_byte_address_byte(addr  , value);
    __eeprom_write_byte_address_byte(addr+1, value>> 8);
}


#if E2END > 0xFF

static void inline __eeprom_write_byte_address_word(uint8_t* addr, uint8_t 
value)
{
    EEARH = (uint16_t)addr >> 8;
    __eeprom_write_byte_address_byte((uint16_t)addr, value);
}

static void inline __eeprom_write_word_address_word(uint8_t* addr, uint16_t 
value)
{
    __eeprom_write_byte_address_word(addr,   value);        //LSB first
    __eeprom_write_byte_address_word(addr+1, value >> 8);   //MSB last
}

#endif


#if E2END <= 0xFF
#define eeprom_read_byte(addr)          
__eeprom_read_byte_address_byte((uint8_t*) addr)
#define eeprom_write_byte(addr, value)  
__eeprom_write_byte_address_byte((uint8_t*), value)
#define eeprom_write_word(addr, value)  
__eeprom_write_word_address_byte((uint8_t*), value)
#else
#define eeprom_read_byte(addr)          
__eeprom_read_byte_address_word((uint8_t*)addr)
#define eeprom_write_byte(addr, value)  
__eeprom_write_byte_address_word((uint8_t*)addr, value)
#define eeprom_write_word(addr, value)  
__eeprom_write_word_address_word((uint8_t*)addr, value)
#endif


#define eeprom_read_word(addr) \
(__extension__({ \
    union \
    { \
        uint16_t word; \
        struct \
        { \
            uint8_t lo; \
            uint8_t hi; \
        } byte; \
    } __result; \
    uint16_t __eeprom_address = (uint16_t)(addr); \
    __result.byte.lo = eeprom_read_byte(__eeprom_address); \
    __result.byte.hi = eeprom_read_byte(__eeprom_address + 1); \
    __result.word; \
}))




/* Assign macro arguments to variables, as the args could be constants. */
#define eeprom_read_block(pointer_ram, pointer_eeprom, n) \
(__extension__({ \
    uint16_t __count = (n); \
    if(__built_in_constant_p(__count) && n< 256) {
    uint16_t __e = (uint16_t)(pointer_eeprom); \
    uint8_t *__r = (uint8_t *)(pointer_ram); \
    while(__count--) \
    { \
        *__r++ = eeprom_read_byte(__e); \
        __e++; \
    } \
}))



/* Assign macro arguments to variables, as the args could be constants. */
#define eeprom_write_block(pointer_ram, pointer_eeprom, n) \
(__extension__({ \
    uint16_t __count = (n); \
    uint16_t __e = (uint16_t)(pointer_eeprom); \
    uint8_t *__r = (uint8_t *)(pointer_ram); \
    while(__count--) \
    { \
        eeprom_write_byte(__e, *__r); \
        __e++; \
        __r++; \
    } \
}))


#endif

reply via email to

[Prev in Thread] Current Thread [Next in Thread]