First you should verify whether you mean SDRAM or SRAM. I don't think this microcontroller supports SDRAM and I suspect you should plan to use an external SRAM.
Refer to the microcontroller documentation for examples on how to connect the microcontroller to the external SRAM and flash. You could also find an evaluation board that contains external memories and reference the eval board's schematic.
You will need to configure the microcontroller's External Memory Controller in order for the microcontroller to be able to use the external memories. Refer to the microcontroller's User Guide for details on how to configure the External Memory Controller. Basically each memory will be associated with a chip select pin and you will have to configure all the settings associated with the chip selects that you are using. Typically the External Memory Controller gets configured with some instructions in the startup code for your application. (It's configured by the startup code so that the external memories are accessible sooner rather than later.) You will likely have to provide, or at least customize, this portion of the startup code. Here again, if you can find an eval board with external memories then the example program that comes with the eval board will be a great reference.
Once the External Memory Controller is configured properly, the microcontroller should be able to read and write to the external SRAM without any additional driver code. The microcontroller should also be able to read from the external flash without any special driver code. However, writing to the external flash will require some special driver code that you will have to incorporate in your program. Refer to the flash part's datasheet for the erase and program algorithms that are required to reprogram the flash. Once again, an eval board example would be a good reference.
(If you don't need to reprogram the external flash at run time then you may not need the flash driver code. For example, you may be able to get by with reprogramming the flash via JTAG with a special flash programming application on your PC.)
I'm not familiar with the internal bootloader provided with this microcontroller. I suspect it reads code from the UART and copies it into internal SRAM and then executes it. I doubt that this bootloader will support your external memories automatically. But you may be able to get the source code for the internal bootloader and then customize it by adding support for your external memories.
I have never heard of a flash memory chip (or processor with internal flash) that has sufficient energy storage internally to complete a write (or erase) cycle if external power should be removed. In other words, if you don't have control over when your system powers down, you always need to create a protocol that can detect and deal with any individual flash update operation that might have been interrupted.
One way around this is to provide the necessary energy storage (e.g., an electrolytic capacitor) on your board, such that you can detect an external power failure, yet still complete any write/erase operation that may have already started.
EDIT: Your write buffer concept could be used with the external flash, but it needs to be modified to take into account the larger erase granularity. According to the datasheet, the minimum erase size is one "sector" (4K bytes).
You'll need to reserve three sectors for your write buffer. One of these will hold your READY flag (call this the WB_R wector). The second will hold the sector address of the sector being updated (call this the WB_A sector). The third will hold the updated data for that sector (call this the WB_D sector).
To update any particular byte (or a group of bytes in a single sector), follow the following steps. We assume that WB_R is already erased.
- Erase WB_A.
- Locate the flash sector that contains the byte you want to change (call this the DEST sector).
- Write the sector address of DEST to WB_A.
- Erase WB_D.
- Copy the contents of DEST to WB_D, but when you get to the byte(s) that you're changing, write the new value(s) to WB_D instead of the old value(s).
- Set the READY flag in WB_R. Note that this means you change it to its non-erased state. Since the erased state is 0xFF, this means that you write 0x00.
- Erase DEST (getting the sector address from WB_A).
- Copy the contents of WB_D to DEST.
- Erase WB_R.
On power up, check the READY flag, and if it's set (anything other than 0xFF — it may have been only partially written or partially erased), jump directly to step 7.
Note that with this algorithm, each of the write buffer sectors gets written and erased at least once for each write operation you do. This could become a problem if you do a lot (more than 100,000) of writes over the lifetime of the product. If that's the case, you'll need a more sophisticated wear-leveling algorithm.
Best Answer
When you set a "protection mode" that allows writing some flash, you can boot into or jump into an address where flash-writing is allowed, and you can boot into or jump into an address where flash-writing is not allowed: on these processors you can't write the flash region you are currently executing.
One use of this feature on the AVR processors is to boot into a mode and address that allows writing flash, update the flash, then jump into the updated flash: that allows updates at boot time, but no updates after the jump into the non-boot code region.