Flash Loader - Principles of Operation

Overview

The Flash Loader is a small program that is loaded into the Mini-Scanner during manufacture.  Its start address is written to the microcontroller (Z8Encore!) reset vector so that this will be the program that runs when the unit is powered on or reset.  So as to leave as much memory as possible for the user program, the Flash Loader is quite simple in function.  It is easy to think of additional things it could do, but this utility program should be considered complete if it does enough.  Note that the Flash Loader is not a "Boot Loader" in the usual sense.  It does not load the user program from separate storage each time the Mini-Scanner "boots".  It simply runs before the user program and gives you the chance to break in and change the user program if you wish.

Memory Address Map

The Flash Loader itself resides in Z8Encore! program memory from 0x0200 to 0x7FF, therefore taking up only 1.5 Kbytes.  (By contrast, the Flash Loader presented by ZiLOG in Application Note AN0118 takes up 16 Kbytes.)  In order to run on a reset, the start address of 0x0200 occupies the reset vector at address 0x0002/3.  The remainder of program memory is available to the user program as follows:

However, the user program must only use Flash pages 8 and above (0x1000 up) for program text that will change between versions and be updated by the Flash Loader.  Pages 4, 5, 6 and 7 are not erased by the Flash Loader.  Their intended use is for the storage of options and data that should persist between versions of the user program.  Alternatively, program code that will not be changed can be stored here.  These pages should be used in top-down order so that should a future version of the Flash Loader exceed the 1.5 Kbytes reserved for it another 0.5 Kbyte pages can be annexed to it from the bottom of these unerased pages.

The user program must start at address 0x0038.  Since the reset vector is used by the Flash Loader, the value that the user program would write here is intercepted.  In principle, the user program start address could be stored somewhere else by the Flash Loader to allow for additional flexibility.  This was not done largely because it is surprisingly difficult to build programs using the ZiLOG ZDS II tools that start anywhere other than as defined by the standard startup module.

Startup

On powerup or reset, the Flash Loader initializes the Z8Encore! UART and Flash Controller according to the input clock rate as defined at compile time (18.432 MHz for the Mini-Scanner).  The UART is configured for 9,600 bit/s, 8 data bits, no parity and one stop bit.  The Flash Loader then begins transmitting the period character (.) at the UART and at the same time checking for an escape character at the UART receiver.  The main reason for sending the periods is to establish a known timebase using the UART data rate.  This is preferable to counting instruction cycles.  However, the dots do give the user some feedback that the Flash Loader is running and the serial connection is working at least in one direction.

If an escape is detected at the receiver before a certain number of periods have been transmitted, then the Flash Loader begins reading all UART input.  The user has "got the attention of" the Flash Loader and can use it to update or verify firmware, etc.  If an escape is not detected in this time, the user program is run by a simple assembly language jump to address 0x0038.  As a first level of defence against not having a valid user program, the contents of address 0x0038 is checked and if it is in the erased state (0xFF) an error message is sent to the UART and an indefinite loop entered.

Reading Commands at the UART

After detecting an escape, the Flash Loader sends to the UART a banner and a prompt and then processes input as follows:

Other received characters, including all upper case, are ignored.  Input is not echoed, but all valid command characters result in some distinctive message so the user knows what has happened.

Reading Intel Hex16 Records

After detecting the colon (:) that begins a record in an Intel Hex16 file, the Flash Loader reads hexadecimal digits in pairs from the remainder of the line as follows:

Various error conditions are detected during the reading of a record:

Upon reading the end-of-file record, the Flash Loader is placed in the idle mode.

Programming

When data bytes are read from an Intel Hex16 record and the Flash Loader is in program mode, the data will be programmed into Flash at the appropriate address if certain checks are passed.  These checks are as follows:

Verifying

When data bytes are read from an Intel Hex16 record and the Flash Loader is in verify mode, the data is compared to the contents of Flash at the appropriate address.  Any mismatch causes a verify fail error to be returned for the complete record.  No attempt is made to count or locate the individual mismatches.  During this comparison, the actual contents of the reset vector at address 0x0002 and 0x0003 is ignored and data from the record, if present for these addresses, is instead compared to 0x00 and 0x38, respectively.  This checks that the user program start address is 0x0038 even though the reset vector is set to 0x0200.

Building User Programs for the Flash Loader

To generate an Intel Hex16 file from a ZiLOG ZDS II project, select "Project" -> "Settings..." on the menu to bring up the Project Settings dialog.  On the "Linker" tab, select "Output" in the "Category:" pick list.  Then, select "Intel Hex16 - Records" in the "Executable Format:" pick list.  You must repeat this for each configuration for which you want this output format.  I tend to set my projects up to generate IEEE 695 for the Debug configuration and Intel Hex16 for the Release configuration.


Last edited July 6, 2003.  All material Copyright 2003 Graham Davies.  No liability accepted.