FLASH Memory In System Programmer for Infineon C509 MCU's

A product developed by Felix Daners Engineering
Felix Daners Engineering

Code Bank Switching

This section applies to the full version only. Demo versions do not support code bank switching.

Bank switching is a mean to increase the addressable memory. 8051 based micro controllers support a 16-bit wide address space for code and xdata. In this document, we are interested in code space only.
To increase the address range, the 16 bit wide address is patched with one or more bits. This way, the code space becomes segmented. The additional address bits act as segment (code bank) selectors while the original address bits select the offset into the selected segment. The program counter still is controlled by the opcode fetch of the CPU. The selection of the segments (code banks) must be managed in the user program code. When implementing code bank switching, some special conditions must be considered. If an interrupt vector is loaded into the program counter, the hardware does not care about the selection of the code bank. Because vectoring the interrupt may not be altered by user code, the service routine must be accessible directly from all code banks. Programs written for Keil C51 compilers use a common area, which is copied into each bank. Program parts, which must be accessible from each bank are placed into this area. Function calls across bank boundaries are implemented in a jump table, which is also located in the common area. This table glues together bank selection and function call. Please check the documentation that comes with your compiler system on how to build code banked applications.

Examples of a Hardware setu-ps

There are many possible hardware configurations to realize code bank switching. Flash509win supports the two most common solutions:

The requirements for Flash509win were defined as to offer good flexibility with a relatively simple user interface. The goal was to fit most hardware configurations. The current release of Flash509win has some minor limitations.

For bank switching with one or more port lines:

For bank switching with a XRAM register:

Bank switching using Ports

This is probably the most straightforward approach to achieve code bank switching. In this example, we use a 512k Flash with eight 64k sectors. This way the Flash sector borders exactly equal the eight 64k code bank borders (each code bank fits exactly into one Flash sector). It would be also possible to use a Flash with smaller sectors as long as each bank border meets one of the Flash sectors (each code bank consists of more than one whole flash sector).

Example Code Bank Switching with PORT(S)

The straightforward solution, which includes the common area physically in each bank, might seem to be inefficient. There are other possibilities that logically create a similar memory map with the help of an address decoder but store the common area physically only once.

Bank switching using an XRAM Register

The following example also uses the 512k Flash with eight 64k sectors. For the discussion about the code memory layout read the section Bank switching using Ports above.

Example Code Bank Switching with XRAM

In this example the switching register is accessible with MOVX instructions for destination addresses in DPH/DPL >= 0x8000. This way, we are free to add XRAM in the lower 32k of XRAM space. The comments about the usage of the Flash given in the chapter Bank switching using Ports also applies.

The Implementation of Code Bank Switching in Flash509win

We may look at Flash509win like a program that spawns processes on the target hardware micro controller. To do so, it loads the executable code and a parameter set into the XRAM area 0xF400 - 0xFFFF and then starts the execution of the executable code in the XRAM. Flash509win does this for each task separately. If we activate code bank switching, Flash509win first spawns a process to select the appropriate code bank. The small piece of code, which executes, gets its parameters from the parameter block, which was loaded into XRAM by Flash509win before. Because the MCU is not reset, all MCU port lines remain in the selected state after the bank switching process terminates. The same is true for any data in a XRAM switching register outside the MCU.
Now, Flash509win can act on the selected bank equally like on any normal non-banked hardware. The bank keeps selected until a target reset is performed or Flash509win activates another bank.

The Parameters used for Bank Switching

By looking at the parameters used for code bank switching, we may understand in detail what happens during the bank selection process.

The Parameter "Code Bank Sw."

This parameter specifies the method used to select the banks. The two intended methods are introduced in Examples of a Hardware set-ups. The parameter may have the value "None", "Port" or "XDATA".

Parameter "First Bit"

This parameter applies to both methods "Port" and "XDATA". They define at which position in the port or XRAM register the lowest significant bit of the bit group used for bank switching is to be placed.

Parameter First Bit

When Flash509win selects a bank, it shifts left the "Current Bank" parameter by the value of the "First Bit" parameter.

Parameter "Bank Count"

This parameter needs to be set to the number of code banks the target hardware realizes. It applies to both methods "Port" and "XDATA". Flash509win uses this parameter for two purposes. First, to calculate the width of the bit group used for code bank switching and second to make sure only banks that exist can be selected by the parameter "Current Bank".

Parameter Bank Count

Parameter "Port"

This parameter applies to the method "Port" only. It defines on which IO-port of the C509 MCU the bank switching bit group is found.

Parameter "Switching Reg."

This parameter applies to the method "XDATA" only. It defines to which address in the XRAM the switching register is mapped.

Parameter "Current Bank"

With this parameter, the bank to perform actions on is selected. It applies to both methods "Port" and "XDATA". Before any action on the target hardware flash memory, Flash509win selects this code bank.

Per Code Bank Stored Parameters

In this section, we do not discuss the bank switching process itself. The focus is on the real actions (like writing, erasing and so on) we want to perform on the target Flash Memories.

Using code bank switching with Flash509win is quite easy. Once you have set up the parameters needed for the bank switching itself, you work with Flash509win just as you would work on a non-banked hardware. All actions you perform are automatically executed on the selected "Current Bank".

Therefore, you have to select several parameters for each bank:

Per code bank stored parameters are not visible in the GUI simultaeneously. You only can see the parameters active for the "Current Bank". As soon as you switch to another bank by manipulating the "Current Bank" control on the GUI, the parameters for the new "Current Bank" value are displayed. The previous values are kept in memory and reappear as soon as you reselect the previous "Current Bank".

Actions are not stored per code bank. They do not change when an other current bank is selected. In addition, the Flash Device does not change. It is assumed that only one Flash type is used for all banks on the same target hardware. If this is not the case, you may use the auto select feature.

The GUI Elements related to Code Bank Switching

GUI Elemets for Bank Switching Bank Count Bank Count HEX32 Source File for Current Bank HEX32 Source File for Current Bank First Bit First Bit Current Bank Current Bank Bank Switching Methode Bank Switching Methode Bank Switching Methode Switching Reg. Switching Reg. Batchmode for all Banks Batchmode for all banks Bank First Bank First Bank Last Bank Last Port Port


G Code Bank Sw. With this control the bank switching method is selected. For an introduction in bank switching principals start reading here.
W Apply to all Banks (Batch) When activated, the selected actions are applied to all banks. Flash509win starts with bank 0 and loops through all banks. This is useful because otherwise, each bank must be treated manually.
X Bank Count With this control, the number of banks the target hardware implements is specified. The exact description of this topic can be found in the chapter Parameter "Bank Count".
Y First Bit This parameter defines at which position in the port or XRAM register the lowest significant bit of the bit group used for bank switching is to be placed. Please refer to the chapter Parameter "First Bit" for a more accurate description.
Z Current Bank Select on which code bank you wish to perform actions. You will find more information on this topic in the chapter Parameter "Current Bank". When a bank is selected, all controls, which contain Per Code Bank Stored Parameters, are updated immediately.
alpha Switching Reg. This control defines to which address in the XRAM the switching register is mapped. See chapter Parameter "Switching Reg". This field takes a positive C-Style integer literal.
beta Bank First This is a Per Code Bank Stored Parameter. It has a similar effect like the FLASH first parameter in non-banked set-ups but is applied to the selected bank. The field takes a positive C-Style integer literal.
gamma Bank Last This is a Per Code Bank Stored Parameter. It has a similar effect like the FLASH last parameter in non-banked set-ups but is applied to the selected bank. The field takes a positive C-Style integer literal.
delta Port This parameter defines in which MCU port register the bank switching bit group is located. Details are described in chapter Parameter "Port".
epsilon Intel HEX32 Source File This is a Per Code Bank Stored Parameter. Ii defines the Source file to be used with the "Current Bank". Use the [Change...] button to select the file.

Putting it all Together

In this chapter we want to give you an idea of how to work with Flash509win for a bank switched application. The example considers a Keil compiler suite to be the preferred tool to create the HEX-Files.

Creating the HEX-Files

The first step is to go through the compile (C51, A51) and link (BL51) process. After this step, you end up with a banked absolute object file. Now you are ready to split (OC51) the banked absolute object file into simple absolute object files, one for each bank. These absolute object files can be converted (OH51) to Intel HEX-Files. We recommend to use file name extensions like "name.H00", "name.H01" etc. for the HEX-File, the number representing the bank.

Setting up Flash509win the Configuration

For this step, you need to know some details about your target hardware. Start Flash509win from the Windows Start-button.

  1. Select the bank switching method with the Flash509win GUI control G.
  2. Select the number of code banks with the Flash509win GUI control X.
  3. This step depends on the bank switching method you have chosen in step 1.
  4. Select the first bit with the Flash509win GUI control Y.
  5. Select "Auto Select Flash Device" in the Flash509win GUI with control E.

After step 5, the general set-up is complete. What still is missed are the Per Code Bank Stored Parameters. Now we have to loop manually through all code banks and enter the parameters. Please make sure you have your HEX-Files ready.

  1. For each code bank

Now the project is set up and it is time to save it to a file for later use.

  1. Press the [Save Config...] on the Flash509win GUI. (Control L). You may reload
    the configuration later with the [Load Config...]button on the Flash509win GUI. (Control K).

Selecting and Executing the Actions

The actions selected are not stored on a per bank basis. If the checkbox "Apply to all Banks (Batch)" (Flash509win GUI element control W) is not selected, just select the actions to be performed on the current bank. Be careful with the action "Erase Chip". If the FLASH stores more than one bank, this action erases the entire chip each time the actions are executed. It is saver to use "Erase Sectors". This action never touches memory areas outside the current code bank as long as each bank border meets a FLASH sector limit. This is one of the limitations described at the beginning of this document.

If you use the "Apply to all Banks (Batch)" suppresses some warning messages. These are:

The log-file contains all warnings.

Finaly press the "Execute Actions" button to have the selected actions performed. You probably save the setup again to store your action settings as well to the file.