3.4 Flash Program Memory Access
When the part has been identified, it is time to start accessing the Flash memory. A Chip Erase should be performed before programming the Flash memory. Depending on the target device the Flash is programmed using “Byte” or “Page” mode.
For devices with Byte Programming mode each Flash location is dressed and programmed individually. In Page Programming mode, a temporary Page buffer is first filled, and then programmed in one single write cycle. This mode reduces the total Flash programming time. A device will only have one of these modes available. A device with Byte Programming mode do not have the Page Programming option. A device with Page Programming mode of the Flash will, however, use byte programming for the EEPROM memory.
Regardless if the device uses Byte Programming mode or Page Programming mode the Flash will be read one byte at the time using the “Read Flash Program Memory” command. The command sends a memory address ($aa bb) to select a 16-bit word, and selects low or high byte with the H bit in the command byte (0 is low, 1 is high byte). The byte stored at this address is then returned from the target AVR microcontroller in byte 4.
Usually, each 16-bit word in Flash contains one AVR instruction. Assuming the instruction stored at address $104 is “add r16,r17”, the op-code for this instruction would be stored as $0F01. Reading address $104 serially, the expected result returned in byte 4 will be $0F from the high byte, and $01 from the low byte. The data on the MISO and MOSI lines will look like as shown in the table below.
Writing to the Flash memory will, however, differ depending on the available programming mode.
For devices using Byte Programming mode bytes are written with the “Write Program Flash Memory” command. This command sends a memory address ($aa bb) to select a 16-bit word, and selects low or high byte with the H bit (0 is low, 1 is high byte). The byte to be stored is then sent to the target AVR microcontroller in byte 4.
For devices using Page Programming mode the Flash is programmed in two steps. First, a temporary Page buffer is filled using the “Load Program Memory Page” command. Each byte in this buffer can be directly accessed. Once the entire Page buffer is filled, it can be written to the Flash Memory using the “Write Program Memory Page” command.
In some devices, there is no method to detect when the Flash write cycle has ended. For this reason, the programmer presented in this application note waits N ms before attempting to send another command to the interface (the delay N will depend on target device, and can be found in the programming section of the datasheet). For some devices it is possible to use polling. When a byte is being programmed into the Flash or EEPROM, reading the addressed location being programmed will give a value M (often $FF). At the time the device is ready for a new byte, the programmed value will read correctly. This can be used to determine when the next byte can be written. When programming the value M polling will not work, and a delay N should be used before writing the next value. Polled mode will decrease the time required to program a device.
Action | MOSI, sent to the target AVR | MISO, returned from the target AVR |
---|---|---|
Read $01 at address $104, low byte | $20 01 04 xx | $zz 20 01 01 |
Read $0F at address $104, high byte | $28 01 04 yy | $xx 28 01 0F |
Action | MOSI, sent to the target AVR | MISO, returned from the target AVR |
---|---|---|
Write $12 at address $10C, low byte | $60 01 0C 12 | $zz 60 01 0C |
Wait N ms | ||
Write $0F at address $10C, high byte | $68 01 0C 0F | $xx 68 01 0C |
Wait N ms |