memcpy_P and eeprom_write_block

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If I'd have enough RAM
char array_rom [100] PROGMEM;
char array_eeprom[100] __attribute__ ((section (".eeprom")));
void *p = malloc ( sizeof(array_rom));

memcpy_P ( p, array_rom, sizeof(array_rom));
eeprom_write_block ( array_eeprom, p, sizeof(array_rom));

but because I don't, do you guys have any ideas?
Should I have to write memcpy_P2E?
May be somebody already has it?

Thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

char tmp;
int i;
for ( i = 0; i < sizeof(array_rom); i++){
memcpy_P(&tmp,&array_rom[i],sizeof(char));
eeprom_write_byte(&array_eeprom[i], &tmp, sizeof(char));
}

This way I can save ! byte of RAM!!!And if array size is less then 256, i can be char and I will save 2 bytes. But Do you like it?

It should be better way!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Artur,
Thanks, but I was hoping that avr-gcc already has function that allows me to copy data from ROM to EEPROM. All our exercises were just sort a joke. RAM is crucial for my project because I just don't have it quite enough but plenty of ROM instead. I don't want to write this kind a function by myself. I'm wondering why there are no memcpy or movmem of such flavor is not exist.

Local variables ( if they are not static) will be in stack. Stack is RAM too! I'm very-very short of RAM. I have multytask mode, so stacksize for each task is a very important and I have to save on it.

Good luck!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are right! But most cases is NOT always. Task has a lot of variables by itself, so creating temporary ones is just wasted RAM.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#include
#include
#include

#ifndef EEPROM
#define EEPROM __attribute__ ((section (".eeprom")))
#endif

typedef void * eeprom_ptr EEPROM;
typedef void * rom_ptr PROGMEM;

void memcpy_P2E ( eeprom_ptr dst_eeprom, rom_ptr src_rom, int len ) {
int i;

for ( i = 0; i < len; i++ )
eeprom_write_byte ((*( unsigned char * )dst_eepromn++ ),
pgm_read_byte ((*( unsigned char * ) src_rom++ )));
}

I can't compile this.
Who can?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Artur,

First of all I'd like to create my own type via typedef it makes code safer.
Actually compiler should create warning if there would be another type. GCC doesn't, but still typedef is preferable cause some day change will be made.

typedef void * eeprom_ptr;
typedef void * rom_ptr PROGMEM;

void memcpy_P2E ( eeprom_ptr dst_eeprom, rom_ptr src_rom, int len ) {

while ( len-- ) eeprom_write_byte ( ( unsigned char * )dst_eeprom++,
pgm_read_byte ((( unsigned char * )src_rom++ )));
}

Secondary, if inline than why static? Macro vs. inline issue. Marco will never produce call, inline can.

Thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1. should always use void* - disagree.
Don't want to go deep into specific. SHOULD and ALWAYS is not for C - programmer! In most cases - yes. My types are specific for rom and eeprom!
2. Don't know how to force GCC to perform inlining. Sometimes macro works better but IN MOST CASES inline is preferable.