0
votes

Can someone point me to a good tutorial that explains storing data in program flash for Atmega 328P ? I am planning to receieve 8 bit data from portB at 1 Mhz frequency and I want to store first 19200 bytes of data recieved in program flash which has 32k memory. I am new to AVR assembly and I am a bit confused after reading all that I could google, so a direct answer or some good tutorial to read would be greatly appreciated.

As of now I have coded it as

LDI XL, LOW(NRWW_START_ADDR)
LDI XH, HIGH(NRWW_START_ADDR)
loadBuffer:
IN R0,PORTB
ST X+,R0
CPI XH,0x4B
BRNE loadBuffer

But I am not sure if this is the correct thing to do. I would like not to overwrite the bootloader. Also, I am looking for a solution in assembler.

Any help or pointer is much appreciated.

1
The ST instruction doesn't write to program memory. You need to use the STM instruction to write to program memory, but it's much more complicated than that. You also can't read serial data from PORTB that way. The PORTB register is for output only and it's a parallel port output. Finally your program, if it did work, would both overwrite the boot loader section, since its located in the NRWW section, and overrun the end of program memory. The NRWW section is only 2K and exists at the end of program memory. - Ross Ridge
Thanks very much. Can you please tell me at what location can I start writing into flash ? I am thinking that I somehow need to figure out the free zone given the bootloader and the application will be written to flash. Do you know how to figure that out ? - DrBug
You need to reserve 19200 bytes (9600 words) in your application for the buffer. The buffer needs to be page aligned (128 bytes, 64 words) because you have to erase and write program memory in pages. You also need to put the STM instruction in the boot loader section as it won't work in the application section. You're also going to need to reserve a big chunk of SRAM to use a buffer to store the incoming data in while the page write operations occur. These operations take between 3.7 ms and 4.5 ms to perform. The code required to do all this is much more complicated than your example code. - Ross Ridge

1 Answers

1
votes

Two notes:

  1. You're making a common mistake in this example: shuffling input and output on the same port. If you want to read from the B port, then you have to read PINB; if you want to write to it, you must write to PORTB. (Assuming DDRB was set before.)

  2. Are you sure you want to use flash memory for data storage? Data sheet says:

    Write/Erase Cycles: 10,000