Write to PROGMEM

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

Hello everyone. This is my first time.

I have an array of bytes as a buffer for serial communications in PROGMEM. I am having problems writing to the array.

uint8_t CommsData[1024] PROGMEM;


if (USARTC0.STATUS & USART_RXCIF_bm) {
		 
   static uint8_t x = 0;
   uint8_t CommsByte;	
   	  		   
   CommsByte = USARTC0.DATA;

   CommsData[x++] = CommsByte;

   // more code here  

}


uint8_t cmd = pgm_read_byte(CommsData[3]); // cmd gets wrong value

Think generously about people who know less than you about code. Don't think they should understand some technical concept. They don't, and they're not stupid for not understanding.

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

You can't write to flash at runtime like that. You can do it for a bootloader, and that's it. You'll have to use an external memory chip.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

For such a dynamically accessed buffer you want it in a fast read/write memory with infinite write possibility, neither code flash nor even EEPROM would be a good idea here.

But ask yourself whether this buffer really needs to be 1024 bytes. In 8bit AVRs most people use 16 or 32 bytes for a UART buffer. It only needs to be big enough to buffer the maximum bytes that might arrive before you've had a chance to process them. If you really think the software might get a back-log of 1024 unprocessed bytes the chances are the program has serious problems elsewhere!

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

The 1k buffer is actualy 10k in my code.

The RAM is 80% full with the data from a structure. The serial comms data is sent in a 9k packet with check sum on the end. Need to read in the whole packet first, do the check sum, and then put it in the struture.

Think generously about people who know less than you about code. Don't think they should understand some technical concept. They don't, and they're not stupid for not understanding.

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

What about putting the structure in PROGMEM and the buffer in RAM. Then write to the structure with memcpy_P everytime new data is recived.

Think generously about people who know less than you about code. Don't think they should understand some technical concept. They don't, and they're not stupid for not understanding.

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

Do you think memcpy_P() was writing in C or magic fairy dust ? If the latter, then yeah why not ( BUT even then... only in YOUR world ) :? . :roll:

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Quote:
Do you think memcpy_P() was writing in C or magic fairy dust ? If the latter, then yeah why not ( BUT even then... only in YOUR world ) Confused . Rolling Eyes

I do not understand your answer. I think you are making fun of me. Not a nice start for me on this forum.

Can anyone else answer if my idea would work or another way to achieve what I need to do.

Think generously about people who know less than you about code. Don't think they should understand some technical concept. They don't, and they're not stupid for not understanding.

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

You CANNOT write to the flash, like I told you the FIRST time.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

What use is memcpy_P if does not do what it name suggests, which is copy data to program memory.

Think generously about people who know less than you about code. Don't think they should understand some technical concept. They don't, and they're not stupid for not understanding.

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

Memcpy() copies from 1 memory to another, the std. function has both memories in RAM. Memcpy_P copies data FROM flash TO ram. Read about it here:

http://www.nongnu.org/avr-libc/u...

Do you have to permanently store data received from the usart ? What are you doing with this 9K -10K of data ultimately, after CRC, etc. ?

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

If you need 10K of dynamically accessible memory then you need to either pick a larger AVR with more RAM or you need to consider adding an external RAM device.

You are kidding yourself if you think you can use either flash or EEPROM as dynamic buffer memory. Apart from anything else (such as speed of access) you have to realise that flash can only ever be written 10,000 times (and must be done in 64/128/256 byte pages!) and EEPROM can only be written 100,000 times. If you were only writing to this buffer every hour then the numbers might work (especially with wear levelling) but if a typical UART (or whatever) receive routines is getting a new character every 100us or whatever then you will wear out the memory in a week.

Bottom line: forget writing to PROGMEM it is NOT the solution here!