Part Number Hot Search : 
XO753 00209 DVC125 1002G Q2NK60 SA604AD BCR16P AN421
Product Description
Full Text Search
 

To Download AN1201 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  1/33 march 2001 AN1201 application note software drivers for the m59dr008 and m59dr032 flash memories contents n introduction n the m59drxxx programming model n writing c code for the m59drxxx n c library functions provided n porting the drivers to the target system n limitations of the software n conclusion n c1201_16.h listing n c1201_16.c listing introduction this application note provides library source code in c for the m59dr008 and the m59dr032 flash memories. four parts in all are covered, the m59dr008e, the m59dr008f, the m59dr032a and the m59dr032b; they will all be referred to generically as the m59drxxx. listings of the source code can be found at the end of this doc- ument. the source code is also available in file form from the internet site http://www.st.com or from your stmicroelectronics distributor. the c1201_16.c and c1201_16.h files contain librar- ies for accessing the m59drxxx flash memory. also included in this application note is an overview of the pro- gramming model for the m59drxxx. this will familiarize the reader with the operation of the memory devices and provide a basis for understanding and modifying the accompanying source code. the source code is written to be as platform independent as possible and requires minimal changes by the user in order to compile and run. the application note explains how the user should modify the source code for their individual target hard- ware. all of the source code is backed up by comments explain- ing how it is used and why it has been written as it has. this application note does not replace the m59dr008 or m59dr032 datasheet. it refers to the datasheet throughout and it is necessary to have a copy in order to follow some of the explanations. the software and accompanying documentation has been fully tested on a target platform. it is small in size and can be applied to any target hardware. the m59drxxx programming model the m59dr008 is an 8 mbit (512kb x16) flash memory; the m59dr032 is 32 mbit (2mb x 16). they can be electrically erased and programmed through special coded command se- quences on most standard microprocessor buses. the device is broken down into different blocks of varying sizes. the main blocks are 32kb x16 in size, whereas the parameter blocks are 4kb x16 in size. each block can be erased individually. the blocks are grouped into two banks; each bank is independant from the other so that one can be programmed while the other is being read. all the blocks in one bank can be erased at the same time using one command.
AN1201 - application note 2/33 the m59drxxx is a smart voltage device. it differs from first generation devices which require a 12v sup- ply to program or erase. the m59drxxx is therefore easier to use since the hardware does not need to cater for special bus signal levels. the voltages needed to erase the device are generated by charge pumps inside the device. the v pp pin of the m59drxxx can be optionally supplied with 12v; this allows the double-word program command to be used and halves the programming time of the device. included in the device is a program/erase controller. with first generation flash memory devices the soft- ware had to manually program all of the words to 00h before erasing to ffh using special programming sequences. the program/erase controller in the m59drxxx allows a simpler programming model to be used, by taking care of all the necessary steps required to erase and program the memory. this has led to improved reliability so that in excess of 100,000 program/erase cycles are guaranteed per block on the device. bus operations and commands most of the functionality of the m59drxxx is available via the two standard bus operations: read and write. read operations retrieve data or status information from the device. write operations are interpreted by the device as commands, which modify the data stored or the behaviour of the device. only certain special sequences of write operations are recognized as commands by the m59drxxx. the various commands recognized by the m59drxxx are listed in the commands table of the datasheet and can be grouped as follows: 1. read/reset 2. auto select 3. erase 4. program 5. erase suspend the read/reset command returns the m59drxxx to its reset state where it behaves as a rom. in this state, a read operation outputs onto the data bus the data stored at the specified address of the device. the auto select command places the device in the auto select mode, which allows the user to read the electronic signature and block protection status of the device. the electronic signature (manufacturer and device codes) and the block protection status are accessed by reading different addresses whilst in the auto select mode. the erase commands are used to set all the bits to 1 in every memory location in the selected blocks (block erase command) or in a bank (bank erase command). all data previously stored in the erased blocks will be lost. the erase commands take longer to execute than the other commands, because entire blocks are erased at a time. the program command is used to modify the data stored at the specified address of the device. note that programming cannot change bits from 0 to 1. it may therefore be necessary to erase the block before programming to addresses within it. programming modifies a single word at a time. programming larger amounts of data must be done one word at a time, by issuing a program command, waiting for the com- mand to complete, then issuing the next program command, and so on. each program command requires 4 write operations to issue. the m59drxxx supports double word programming when v pp is at 12v. using this feature two words can be programmed at the same time, halving the programming time. for flash programmers with long bus cycles the unlock bypass command can improve programming times. after issuing the unlock bypass command, program commands only require 2 write operations and double- word program commands only require 3 write operations. using unlock bypass will thus save some time when a large number of addresses need to be programmed at a time.
3/33 AN1201 - application note issuing the erase suspend command during a block erase operation will temporarily place the m59drxxx in erase suspend mode. in this mode the blocks not being erased may be read or programmed as if in the reset state of the device. this allows the user to access information stored in the device immediately rather than waiting until the block erase operation completes, typically 1s for the m59drxxx. the block erase operation is resumed when the device receives the erase resume command. the status register while the m59drxxx is programming or erasing, a read from the device will output the status register of the program/erase controller. this provides valuable information about the current program or erase command. the status register bits are described in the status register bits table of the m59drxxx datasheet. their main use is to determine when programming or erasing is complete and whether it is suc- cessful or not. completion of the program or erase operation can be determined either from the polling bit (status reg- ister bit dq7) or from the toggle bit (status register bit dq6), by following the data polling flow chart figure or the data toggle flow chart figure in the datasheet. the library routines described in this appli- cation note use the data toggle flow chart. however, a function based on the data polling flow chart is also provided as an illustration. programming or erasing errors are indicated by the error bit (status register bit dq5) becoming 1 before the command has completed. if a failure occurs, the command will not complete and read operations will continue to output the status register bits until a read/reset command is issued to the device. a detailed example the commands table of the m59drxxx datasheet describes the sequences of write operations that will be recognized by the program/erase controller as valid commands. for example programming 65h to the address 03e2h requires the user to write the following sequence (in c): *(unsigned int*)(0x0555) = 0x00aa; *(unsigned int*)(0x02aa) = 0x0055; *(unsigned int*)(0x0555) = 0x00a0; *(unsigned int*)(0x03e2) = 0x9465; this example assumes that address 0000h of the m59drxxx is mapped to address 0000h in the micro- processor address space. in practice it is likely that the flash will have a base offset which needs to be added to the address. while the device is programming the specified address, read operations will access the status register bits. status register bit dq5 will be 1 if an error has occurred. bit dq6 will toggle while programming is on-going. bit dq7 will be the complement of the data being programmed. there are only two possible outcomes to this programming command: success or failure. success will be indicated by the toggle bit dq6 no longer toggling but being constant at its programmed value (of 1 in our example) and the polling bit dq7 also being at its programmed value (of 0 in our example). failure will be indicated by the error bit dq5 becoming 1 while the toggle bit dq6 still toggles and the polling bit dq7 remains the complement (1 in our example) of the data being programmed. note that failure of the device itself is extremely unlikely. if the command fails it will normally be because the user is attempting to change a 0 to a 1 by programming. it is only possible to change a 0 to a 1 by erasing. writing c code for the m59drxxx the low-level functions (drivers) described in this application note have been provided to simplify the pro- cess of developing application code in c for the stmicroelectronics flash memories (m59drxxx). this enables users to concentrate on writing the high level functions required for their particular applications. these high level functions can access the flash memories by calling the low level drivers, hence keeping details of special command sequences away from the users' high level code: this will result in source code both simpler and easier to maintain.
AN1201 - application note 4/33 code developed using the drivers provided can be decomposed into three layers: 1. the hardware specific bus operations 2. the low-level drivers 3. the high level functions written by the user the implementation in c of the hardware specific read and write bus operations is required by the low- level drivers in order to communicate with the m59drxxx. this implementation is hardware platform de- pendent as it is affected by which microprocessor the c code runs on and by where in the microproces- sor's address space the memory device is located. the user will have to write the c functions appropriate to his hardware platform (see flashread() and flashwrite() in the next section). the low-level drivers take care of issuing the correct sequences of write operations for each command and of interpreting the information received from the device during programming or erasing. these drivers encode all the specific details of how to issue commands and how to interpret the status register bits. the high level functions written by the user will access the memory device by calling the low-level func- tions. by keeping the specific details of how to access the m59drxxx away from the high level functions, the user is left with code which is simple and easier to maintain. it also makes the user's high level func- tions easier to apply to other stmicroelectronics flash memories. when developing an application, the user is advised to proceed as follows: C first write a simple program to test the low level drivers provided and verify that these operate as ex- pected on the user's target hardware and software environments. C then the user should write the high level code for his application, which will access the flash memories by calling the low level drivers provided. C finally test the complete application source code thoroughly. c library functions provided the software library provided with this application note provides the user with source code for the following functions: flashreadreset() is used to reset the device into the read mode. note that there should be no need to call this function under normal operation as all of the other software library functions leave the device in this mode. flashautoselect() is used to identify the manufacturer code, device code and the block protection status of the device. flashblockerase() is used to erase one or more blocks in the device. multiple blocks will be erased simultaneously to reduce the overall erase time. if multiple blocks are to be erased, the function checks which bank the first block to erase is a member of and that none of the blocks specified are outside of that bank. it also checks for protected blocks and hence does not erase any blocks if they are protected or across banks. flashbankerase() is used to erase one bank in the device at a time. before erasing the bank, this func- tion checks that none of the blocks within that bank are protected. flashblockunprotect() is used to unprotect one block in the device. it checks that the block to un- protect is valid and un-protects the block. the block is then tested to ensure that the command was suc- cessful. if this function is called and the block is already un-protected the user is warned that the function had no effect. flashblockprotect() is used to protect one block in the device. it checks that the block to protect is valid and protects the block. the block is then tested to ensure that the command was successful. if this function is called and the block is already protected the user is warned that the function had no effect. flashblocklock() is used to lock one block in the device. once a block is locked its protection status cannot be changed until a hardware reset or power-up.
5/33 AN1201 - application note flashprogram() is used to program data arrays into the flash. only previously erased words can be programmed reliably. the function will not program any data if any of the words in the array fall inside a protected block. the functions provided in the software library rely on the user implementing the hardware specific bus op- erations as well as a suitable timing function. this is to be done by writing three functions as follows: C flashread() must be written to read a value from the flash. C flashwrite() must be written to write a value to the flash. C flashpause() must be written to provide a timer with microsecond resolution. this is used to wait while the flash recovers from some conditions. an example of these functions is provided in the source code. in many instances these functions can be written as macros and therefore will not incur the function call time overhead. the two functions which perform the basic i/o to the device have been provided for users who have awkward systems. for example where the addressing system is peculiar or the data bus has d0..d7 of the device on d8..d15 of the microprocessor. they allow any user to quickly adapt the code to virtually any target system. throughout the functions assumptions have been made on the data types. these are: a char is 8 bits (1 byte). this is not the case in all microcontrollers. where it is not it will be necessary to mask the unused bits of the word. an int is 16 bits (2 bytes). again, like the char , if this is not the case it will be necessary to use a variable type which is 16 bits or longer and mask bits above 16 bits. a long is 32 bits (4 bytes). it is necessary to have arithmetic greater than 16 bits in order to address the entire device. two approaches to the addressing are available: the desired address in the flash can be specified by a 32 bit linear pointer or a 32 bit offset into the device could be provided by the user. the flashread( ) functions in each case would declared as: unsigned int flashread( unsigned int *addr); unsigned int flashread( unsigned long uloff); the pointer option has the advantage that it runs faster. the 32 bit offset needs to be changed to an ad- dress for each access and this involves 32 bit arithmetic. using a 32 bit offset is, however, more portable since the resulting software can easily be changed to run on microprocessors with segmented memory spaces (such as the 8086). for maximum portability all the functions in this application note use a 32 bit unsigned long offset, rather than a pointer. porting the drivers to the target system before using the software in the target system the user needs to do the following: 1. define use_m59dr008e , use_m59dr008f , use_m59dr032a or use_m59dr032b depending on which part is fitted is fitted. the top of the source file provided defines use_m59dr008e as an example. 2. write flashread() , flashwrite() and flashpause() functions appropriate to the target hard- ware. 3. search through the code for the /* dsi */ and /* eni */ comments and disable/enable interrupts at the appropriate points. the example flashread() and flashwrite() functions provided in the source code should give the user a good idea of what is required and can be used in many instances without much modification. to test the source code in the target system start by simply reading from the m59drxxx. if it is erased then only ffh data should be read. next read the manufacturer and device codes and check they are
AN1201 - application note 6/33 correct. if these functions work then it is likely that all of the functions will work but they should all be tested thoroughly. the programmer needs to take extra care when the device is accessed during an interrupt service routine. three situations exist which must be considered: 1. when the device is in read mode interrupts can freely read from the device. 2. interrupts which do not access the device may be used during the program, autoselect and chip erase functions. 3. during the time critical section of the block erase function interrupts are not permitted. an interrupt dur- ing this time may cause a time-out and result in some of the blocks not being erased correctly. the programmer should also take care when a reset is applied during program or erase operations. the flash will be left in an indeterminate state and data could be lost. c does not provide a standard library function for disabling interrupts. furthermore different applications have different tolerances on when interrupts may be disabled. therefore no protection from the misuse of interrupts could be incorporated into the library source code. it is strongly recommended that the user dis- ables interrupts where the /* dsi */ comments are placed in the source code. if this is not possible then the user should erase one block at a time. limitations of the software the software provided does not implement a full set of the m59drxxx's functionality. it is left to the user to implement the erase suspend and unlock bypass commands of the device. the standby mode is a hardware feature of the device and cannot be controlled through software. care should be taken in some of the while() loops. no time-outs have been implemented. software ex- ecution may stop in one of the loops due to a hardware error. a /* timeout! */ comment has been put at these places and the user can add a timer to them to prevent the software failing. the software only caters for one device in the system. to add software for more devices a mechanism for selecting the devices will be required. when an error occurs the software simply returns the error message. it is left to the user to decide what to do. either the command can be tried again or, if necessary the device may need to be replaced. conclusion the m59drxxx single voltage flash memories are ideal products for embedded and other computer sys- tems, able to be easily interfaced to microprocessors and driven with simple software drivers written in the c language. revision history date version revision details march 2001 -01 first issue
7/33 AN1201 - application note /**** c1201_16.h header file for c1201_16.c ************************************ filename: c1201_16.h description: header file for c1201_16.c. consult the c file for details copyright (c) 2000 stmicroelectronics. this program is provided as is without warranty of any kind,either expressed or implied, including but not limited to, the implied warranty of merchantability and fitness for a particular purpose. the entire risk as to the quality and performance of the program is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction. *******************************************************************************/ /******************************************************************************* commands for the various functions *******************************************************************************/ /* for use with flashbankerase(command) */ #define bank_a (0) #define bank_b (1) /* for use with flashautoselect(command) */ #define flash_read_manufacturer (-2) #define flash_read_device_code (-1) /******************************************************************************* error conditions and return values. see end of c file for explanations and help *******************************************************************************/ #define flash_block_protected (0x01) #define flash_block_unprotected (0x00) #define flash_block_locked (0x02) /* bit mask */ #define flash_block_not_erased (0xff) #define flash_block_erase_failure (0xfe) #define flash_block_erased (0xfd) #define flash_success (-1) #define flash_poll_fail (-2) #define flash_too_many_blocks (-3) #define flash_mpu_too_slow (-4) #define flash_block_invalid (-5) #define flash_program_fail (-6) #define flash_offset_out_of_range (-7) #define flash_wrong_type (-8) #define flash_block_failed_erase (-9) #define flash_unprotected (-10) #define flash_protected (-11) #define flash_erase_fail (-14) #define flash_toggle_fail (-15) #define flash_unprotect_fail (-16) #define flash_bank_invalid (-17) #define flash_protect_fail (-18) #define flash_lock_fail (-19) #define flash_locked (-20) /*******************************************************************************
AN1201 - application note 8/33 prototypes *******************************************************************************/ extern unsigned int flashread( unsigned long uloff ); extern void flashreadreset( void ); extern int flashautoselect( int ifunc ); extern int flashblockerase( unsigned char ucnumblocks, unsigned char ucblock[]); extern int flashbankerase( unsigned char bank ); extern int flashblockunprotect( unsigned char ucblock ); extern int flashblockprotect( unsigned char ucblock ); extern int flashblocklock( unsigned char ucblock ); extern int flashprogram( unsigned long uloff, size_t numwords, void *array ); extern char *flasherrorstr( int ierrnum );
9/33 AN1201 - application note /**** c1201_16.c ************************************************************** filename: c1201_16.c description: library routines for the m59dr008 & m59dr032 flash parts revision: 1.01 date: 05/02/2001 author: b. watts, oxford technical solutions (www.ots.ndirect.co.uk) copyright (c) 2000 stmicroelectronics. this program is provided as is without warranty of any kind, either expressed or implied, including but not limited to, the implied warranty of merchantability and fitness for a particular purpose. the entire risk as to the quality and performance of the program is with you. should the program prove defective, you assume the cost of all necessary servicing, repair or correction. ******************************************************************************** version history. ver. date comments 1.00 20/09/2000 fully tested using m59dr008e/f on target platform. 1.01 05/02/2001 added flash block lock and retested. ******************************************************************************** this source file provides library c code for using the m59dr008 and m59dr032 devices. the following devices are supported in the code: m59dr008e m59dr008f m59dr032a m59dr032b the following functions are available in this library: flashreadreset() to reset the flash for normal memory access flashautoselect() to get information about the device flashblockerase() to erase one or more blocks flashbankerase() to erase a whole bank flashblockunprotect() to unprotect one block flashblockprotect() to protect one block flashprogram() to program a word or an array flasherrorstr() to return the error string of an error for further information consult the data sheet and the application note. the application note gives information about how to modify this code for a specific application. the hardware specific functions which need to be modified by the user are: flashwrite() for writing a word to the flash flashread() for reading a word from the flash flashpause() for timing short pauses (in micro seconds) a list of the error conditions is given at the end of the code.
AN1201 - application note 10/33 there are no timeouts implemented in the loops in the code. at each point where an infinite loop is implemented a comment /# timeout! #/ has been placed. it is up to the user to implement these to avoid the code hanging instead of timing out. since c does not include a method for disabling interrupts to keep time- critical sections of code from being disabled. the user may wish to disable interrupt during parts of the code to avoid the flash_mpu_too_slow error from occuring if an interrupt occurs at the wrong time. where interrupt should be disabled and re-enabled there is a /# dsi! #/ or /# eni! #/ comment. the source code assumes that the compiler implements the numerical types as unsigned char 8 bits unsigned int 16 bits unsigned long 32 bits additional changes to the code will be necessary if these are not correct. *******************************************************************************/ #include #include c1201_16.h /* header file with global prototypes */ #define use_m59dr008e /******************************************************************************* constants *******************************************************************************/ #define counts_per_microsecond (200) #define manufacturer_st (0x0020) /* manufacturer code */ #define base_addr ((volatile unsigned int*)0x0000) /* base_addr is the base address of the flash, see the functions flashread() and flashwrite(). some applications which require a more complicated flashread() or flashwrite() may not use base_addr */ #define any_addr (0x0000l) /* any address offset within the flash memory will do */ #ifdef use_m59dr008e #define expected_device (0x00a2) /* device code for the m59dr008e */ #define flash_size (0x80000l) /* 512k */ #endif #ifdef use_m59dr008f #define expected_device (0x00a3) /* device code for the m59dr008f */ #define flash_size (0x80000l) /* 512k */ #endif #ifdef use_m59dr032a #define expected_device (0x00a0) /* device code for the m59dr032a */ #define flash_size (0x200000l) /* 2048k */ #endif #ifdef use_m59dr032b #define expected_device (0x00a1) /* device code for the m59dr032b */ #define flash_size (0x200000l) /* 2048k */ #endif #ifdef use_m59dr008e
11/33 AN1201 - application note /* block organisation for top boot block device */ static const unsigned long blockoffset[] = { 0x00000l, /* start offset of block 0 */ 0x08000l, /* start offset of block 1 */ 0x10000l, /* start offset of block 2 */ 0x18000l, /* start offset of block 3 */ 0x20000l, /* start offset of block 4 */ 0x28000l, /* start offset of block 5 */ 0x30000l, /* start offset of block 6 */ 0x38000l, /* start offset of block 7 */ 0x40000l, /* start offset of block 8 */ 0x48000l, /* start offset of block 9 */ 0x50000l, /* start offset of block 10 */ 0x58000l, /* start offset of block 11 */ 0x60000l, /* start offset of block 12 */ 0x68000l, /* start offset of block 13 */ 0x70000l, /* start offset of block 14 */ 0x78000l, /* start offset of block 15 */ 0x79000l, /* start offset of block 16 */ 0x7a000l, /* start offset of block 17 */ 0x7b000l, /* start offset of block 18 */ 0x7c000l, /* start offset of block 19 */ 0x7d000l, /* start offset of block 20 */ 0x7e000l, /* start offset of block 21 */ 0x7f000l /* start offset of block 22 */ }; /* bank organisation for top boot block device */ static const char bankfirstblock[] = { 8, /* first block of bank a */ 0 /* first block of bank b */ }; static const char banklastblock[] = { 22, /* last block of bank a */ 7 /* last block of bank b */ }; #endif /* use_m59dr008e */ #ifdef use_m59dr008f /* block organisation for bottom boot block device */ static const unsigned long blockoffset[] = { 0x00000l, /* start offset of block 0 */ 0x01000l, /* start offset of block 1 */ 0x02000l, /* start offset of block 2 */ 0x03000l, /* start offset of block 3 */ 0x04000l, /* start offset of block 4 */ 0x05000l, /* start offset of block 5 */ 0x06000l, /* start offset of block 6 */ 0x07000l, /* start offset of block 7 */ 0x08000l, /* start offset of block 8 */ 0x10000l, /* start offset of block 9 */ 0x18000l, /* start offset of block 10 */ 0x20000l, /* start offset of block 11 */
AN1201 - application note 12/33 0x28000l, /* start offset of block 12 */ 0x30000l, /* start offset of block 13 */ 0x38000l, /* start offset of block 14 */ 0x40000l, /* start offset of block 15 */ 0x48000l, /* start offset of block 16 */ 0x50000l, /* start offset of block 17 */ 0x58000l, /* start offset of block 18 */ 0x60000l, /* start offset of block 19 */ 0x68000l, /* start offset of block 20 */ 0x70000l, /* start offset of block 21 */ 0x78000l /* start offset of block 22 */ }; /* bank organisation for bottom boot block device */ static const char bankfirstblock[] = { 0, /* first block of bank a */ 15 /* first block of bank b */ }; static const char banklastblock[] = { 14, /* last block of bank a */ 22 /* last block of bank b */ }; #endif /* use_m59dr008f */ #ifdef use_m59dr032a /* block organisation for top boot block device */ static const unsigned long blockoffset[] = { 0x000000l, /* start offset of block 0 */ 0x008000l, /* start offset of block 1 */ 0x010000l, /* start offset of block 2 */ 0x018000l, /* start offset of block 3 */ 0x020000l, /* start offset of block 4 */ 0x028000l, /* start offset of block 5 */ 0x030000l, /* start offset of block 6 */ 0x038000l, /* start offset of block 7 */ 0x040000l, /* start offset of block 8 */ 0x048000l, /* start offset of block 9 */ 0x050000l, /* start offset of block 10 */ 0x058000l, /* start offset of block 11 */ 0x060000l, /* start offset of block 12 */ 0x068000l, /* start offset of block 13 */ 0x070000l, /* start offset of block 14 */ 0x078000l, /* start offset of block 15 */ 0x080000l, /* start offset of block 16 */ 0x088000l, /* start offset of block 17 */ 0x090000l, /* start offset of block 18 */ 0x098000l, /* start offset of block 19 */ 0x0a0000l, /* start offset of block 20 */ 0x0a8000l, /* start offset of block 21 */ 0x0b0000l, /* start offset of block 22 */ 0x0b8000l, /* start offset of block 23 */ 0x0c0000l, /* start offset of block 24 */ 0x0c8000l, /* start offset of block 25 */ 0x0d0000l, /* start offset of block 26 */
13/33 AN1201 - application note 0x0d8000l, /* start offset of block 27 */ 0x0e0000l, /* start offset of block 28 */ 0x0e8000l, /* start offset of block 29 */ 0x0f0000l, /* start offset of block 30 */ 0x0f8000l, /* start offset of block 31 */ 0x100000l, /* start offset of block 32 */ 0x108000l, /* start offset of block 33 */ 0x110000l, /* start offset of block 34 */ 0x118000l, /* start offset of block 35 */ 0x120000l, /* start offset of block 36 */ 0x128000l, /* start offset of block 37 */ 0x130000l, /* start offset of block 38 */ 0x138000l, /* start offset of block 39 */ 0x140000l, /* start offset of block 40 */ 0x148000l, /* start offset of block 41 */ 0x150000l, /* start offset of block 42 */ 0x158000l, /* start offset of block 43 */ 0x160000l, /* start offset of block 44 */ 0x168000l, /* start offset of block 45 */ 0x170000l, /* start offset of block 46 */ 0x178000l, /* start offset of block 47 */ 0x180000l, /* start offset of block 48 */ 0x188000l, /* start offset of block 49 */ 0x190000l, /* start offset of block 50 */ 0x198000l, /* start offset of block 51 */ 0x1a0000l, /* start offset of block 52 */ 0x1a8000l, /* start offset of block 53 */ 0x1b0000l, /* start offset of block 54 */ 0x1b8000l, /* start offset of block 55 */ 0x1c0000l, /* start offset of block 56 */ 0x1c8000l, /* start offset of block 57 */ 0x1d0000l, /* start offset of block 58 */ 0x1d8000l, /* start offset of block 59 */ 0x1e0000l, /* start offset of block 60 */ 0x1e8000l, /* start offset of block 61 */ 0x1f0000l, /* start offset of block 62 */ 0x1f8000l, /* start offset of block 63 */ 0x1f9000l, /* start offset of block 64 */ 0x1fa000l, /* start offset of block 65 */ 0x1fb000l, /* start offset of block 66 */ 0x1fc000l, /* start offset of block 67 */ 0x1fd000l, /* start offset of block 68 */ 0x1fe000l, /* start offset of block 69 */ 0x1ff000l /* start offset of block 70 */ }; /* bank organisation for top boot block device */ static const char bankfirstblock[] = { 56, /* first block of bank a */ 0 /* first block of bank b */ }; static const char banklastblock[] = { 70, /* last block of bank a */ 55 /* last block of bank b */ };
AN1201 - application note 14/33 #endif /* use_m59dr032a */ #ifdef use_m59dr032b /* block organisation for bottom boot block device */ static const unsigned long blockoffset[] = { 0x000000l, /* start offset of block 0 */ 0x001000l, /* start offset of block 1 */ 0x002000l, /* start offset of block 2 */ 0x003000l, /* start offset of block 3 */ 0x004000l, /* start offset of block 4 */ 0x005000l, /* start offset of block 5 */ 0x006000l, /* start offset of block 6 */ 0x007000l, /* start offset of block 7 */ 0x008000l, /* start offset of block 8 */ 0x010000l, /* start offset of block 9 */ 0x018000l, /* start offset of block 10 */ 0x020000l, /* start offset of block 11 */ 0x028000l, /* start offset of block 12 */ 0x030000l, /* start offset of block 13 */ 0x038000l, /* start offset of block 14 */ 0x040000l, /* start offset of block 15 */ 0x048000l, /* start offset of block 16 */ 0x050000l, /* start offset of block 17 */ 0x058000l, /* start offset of block 18 */ 0x060000l, /* start offset of block 19 */ 0x068000l, /* start offset of block 20 */ 0x070000l, /* start offset of block 21 */ 0x078000l, /* start offset of block 22 */ 0x080000l, /* start offset of block 23 */ 0x088000l, /* start offset of block 24 */ 0x090000l, /* start offset of block 25 */ 0x098000l, /* start offset of block 26 */ 0x0a0000l, /* start offset of block 27 */ 0x0a8000l, /* start offset of block 28 */ 0x0b0000l /* start offset of block 29 */ 0x0b8000l, /* start offset of block 30 */ 0x0c0000l, /* start offset of block 31 */ 0x0c8000l, /* start offset of block 32 */ 0x0d0000l, /* start offset of block 33 */ 0x0d8000l, /* start offset of block 34 */ 0x0e0000l, /* start offset of block 35 */ 0x0e8000l, /* start offset of block 36 */ 0x0f0000l, /* start offset of block 37 */ 0x0f8000l, /* start offset of block 38 */ 0x100000l, /* start offset of block 39 */ 0x108000l, /* start offset of block 40 */ 0x110000l, /* start offset of block 41 */ 0x118000l, /* start offset of block 42 */ 0x120000l, /* start offset of block 43 */ 0x128000l, /* start offset of block 44 */ 0x130000l, /* start offset of block 45 */ 0x138000l, /* start offset of block 46 */ 0x140000l, /* start offset of block 47 */ 0x148000l, /* start offset of block 48 */ 0x150000l, /* start offset of block 49 */ 0x158000l, /* start offset of block 50 */ 0x160000l, /* start offset of block 51 */
15/33 AN1201 - application note 0x168000l, /* start offset of block 52 */ 0x170000l, /* start offset of block 53 */ 0x178000l, /* start offset of block 54 */ 0x180000l, /* start offset of block 55 */ 0x188000l, /* start offset of block 56 */ 0x190000l, /* start offset of block 57 */ 0x198000l, /* start offset of block 58 */ 0x1a0000l, /* start offset of block 59 */ 0x1a8000l, /* start offset of block 60 */ 0x1b0000l, /* start offset of block 61 */ 0x1b8000l, /* start offset of block 62 */ 0x1c0000l, /* start offset of block 63 */ 0x1c8000l, /* start offset of block 64 */ 0x1d0000l, /* start offset of block 65 */ 0x1d8000l, /* start offset of block 66 */ 0x1e0000l, /* start offset of block 67 */ 0x1e8000l, /* start offset of block 68 */ 0x1f0000l, /* start offset of block 69 */ 0x1f8000l /* start offset of block 70 */ }; /* bank organisation for bottom boot block device */ static const char bankfirstblock[] = { 0, /* first block of bank a */ 15 /* first block of bank b */ }; static const char banklastblock[] = { 14, /* last block of bank a */ 70 /* last block of bank b */ }; #endif /* use_m59dr032b */ #define num_blocks (sizeof(blockoffset)/sizeof(blockoffset[0])) #define num_banks (sizeof(bankfirstblock)/sizeof(bankfirstblock[0])) /******************************************************************************* static prototypes the following functions are only needed in this module. *******************************************************************************/ static unsigned int flashwrite( unsigned long uloff, unsigned int uval ); static void flashpause( unsigned int umicroseconds ); static int flashdatatoggle( unsigned long uloff ); /****************************************************************************** the function flashdatapoll() declared below is not used by this library but is provided as an illustration of the data polling flow chart *******************************************************************************/ #define illustration_only #ifndef illustration_only static int flashdatapoll( unsigned long uloff, unsigned int uval ); #endif /* !illustration_only */ /*******************************************************************************
AN1201 - application note 16/33 function: unsigned int flashwrite( unsigned long uloff, unsigned int uval) arguments: uloff is the word offset in the flash to write to uval is the value to be written returns: uval description: this function is used to write a word to the flash. on many microprocessor systems a macro can be used instead, increasing the speed of the flash routines. for example: #define flashwrite( uloff, uval ) ( base_addr[uloff] = (unsigned int) uval ) a function is used here instead to allow the user to expand it if necessary. the function is made to return uval so that it is compatible with the macro. pseudo code: step 1: write uval to the word offset in the flash step 2: return uval *******************************************************************************/ static unsigned int flashwrite( unsigned long uloff, unsigned int uval ) { /* step1, 2: write uval to the word offset in the flash and return it */ return base_addr[uloff] = uval; } /******************************************************************************* function: unsigned int flashread( unsigned long uloff ) arguments: uloff is the word offset into the flash to read from returns: the unsigned int at the word offset description: this function is used to read a word from the flash. on many microprocessor systems a macro can be used instead, increasing the speed of the flash routines. for example: #define flashread( uloff ) ( base_addr[uloff] ) a function is used here instead to allow the user to expand it if necessary. pseudo code: step 1: return the value at word offset uloff *******************************************************************************/ unsigned int flashread( unsigned long uloff ) { /* step 1 return the value at word offset uloff */ return base_addr[uloff]; } /******************************************************************************* function: void flashpause( unsigned int umicroseconds ) arguments: umicroseconds is the length of the pause in microseconds returns: none description: this routine returns after umicroseconds have elapsed. it is used in several parts of the code to generate a pause required for correct operation of the flash part. the routine here works by counting. the user may already have a more suitable routine for timing which can be used. pseudo code: step 1: compute count size for pause. step 2: count to the required size. *******************************************************************************/
17/33 AN1201 - application note static void flashpause( unsigned int umicroseconds ) { volatile unsigned long ulcountsize; /* step 1: compute the count size */ ulcountsize = (unsigned long)umicroseconds * counts_per_microsecond; /* step 2: count to the required size */ while( ulcountsize > 0 ) /* test to see if finished */ ulcountsize--; /* and count down */ } /******************************************************************************* function: void flashreadreset( void ) arguments: none return value: none description: this function places the flash in the read mode described in the data sheet. in this mode the flash can be read as normal memory. all of the other functions leave the flash in the read mode so this is not strictly necessary. it is provided for completeness. note: a wait of 10us is required if the command is called during a program or erase instruction. this is included here to guarantee correct operation. the functions in this library call this function if they suspect an error during programming or erasing so that the 10us pause is included. otherwise they use the single instruction technique for increased speed. pseudo code: step 1: write command sequence (see commands table of the data sheet) step 2: wait 10us *******************************************************************************/ void flashreadreset( void ) { /* step 1: write command sequence */ flashwrite( 0x0555l, 0x00aa ); /* 1st cycle */ flashwrite( 0x02aal, 0x0055 ); /* 2nd cycle */ flashwrite( 0x0555l, 0x00f0 ); /* 3rd cycle */ /* step 2: wait 10us */ flashpause( 10 ); } /******************************************************************************* function: int flashautoselect( int ifunc ) arguments: ifunc should be set to either the read signature values or to the block number. the header file defines the values for reading the signature. note: the first block is block 0 return value: when ifunc is >= 0 the function returns flash_block_protected (01h) if the block is protected and flash_block_unprotected (00h) if it is unprotected. see the auto select command in the data sheet for further information. when ifunc is flash_read_manufacturer (-2) the function returns the manufacturers code. the manufacturer code for st is 0020h. when ifunc is flash_read_device_code (-1) the function returns the device code. the device codes for the parts are: m59dr008e 00a2h
AN1201 - application note 18/33 m59dr008f 00a3h m59dr032a 00a0h m59dr032b 00a1h when ifunc is invalid the function returns flash_block_invalid (-5) description: this function can be used to read the electronic signature of the device, the manufacturer code or the protection level of a block. pseudo code: step 1: send the auto select command to the device step 2: read the required function from the device. step 3: return the device to read mode. *******************************************************************************/ int flashautoselect( int ifunc ) { int iretval; /* holds the return value */ /* step 1: send the auto select command */ flashwrite( 0x0555l, 0x00aa ); /* 1st cycle */ flashwrite( 0x02aal, 0x0055 ); /* 2nd cycle */ flashwrite( 0x0555l, 0x0090 ); /* 3rd cycle */ /* step 2: read the required function */ if( ifunc == flash_read_manufacturer ) iretval = (int) flashread( 0x0000l ); /* a0 = a1 = 0 */ else if( ifunc == flash_read_device_code ) iretval = (int) flashread( 0x0001l ); /* a0 = 1, a1 = 0 */ else if( (ifunc >= 0) && (ifunc < num_blocks) ) iretval = flashread( blockoffset[ifunc] + 0x0002l ); /* a0 = 0, a1 = 1 */ else iretval = flash_block_invalid; /* step 3: return to read mode */ flashwrite(any_addr,0x00f0); return iretval; } /******************************************************************************* function: int flashblockerase( unsigned char ucnumblocks, unsigned char ucblock[] ) arguments: ucnumblocks holds the number of blocks in the array ucblock ucblock is an array containing the blocks to be erased. return value: the function returns the following conditions: flash_success (-1) flash_too_many_blocks (-3) flash_mpu_too_slow (-4) flash_wrong_type (-8) flash_erase_fail (-14) number of the first block found which is protected, invalid, or outside the particular bank. the users array, ucblock[] is used to report errors on the specified blocks. if a time-out occurs because the mpu is too slow then the blocks in ucblocks which are not erased are overwritten with flash_block_not_erased (ffh) and the function returns flash_mpu_too_slow.
19/33 AN1201 - application note if an error occurs during the erasing of the blocks the function returns flash_erase_fail. if both errors occur then the function will set the ucblock array to flash_block_not_erased for the unerased blocks. it will return flash_erase_fail even though the flash_mpu_too_slow has also occurred. description: this function erases up to ucnumblocks in the flash. the blocks can be listed in any order as long as they are within the same bank. the function does not return until the blocks are erased. if any blocks are protected,invalid or in different banks none of the blocks will be erased. during the erase cycle the data toggle flow chart of the data sheet is followed. the polling bit, dq7, is not used. pseudo code: step 1: check for correct flash type. step 2: check for protected or invalid blocks. step 3: discover which bank the first block to erase is in then ensure that other blocks listed in the array are also within that bank. step 4: write block erase command. step 5: check for time-out blocks. step 6: wait for the timer bit to be set. step 7: follow data toggle flow chart until program/erase controller has completed. step 8: return to read mode (if an error occurred). *******************************************************************************/ int flashblockerase( unsigned char ucnumblocks, unsigned char ucblock[] ) { unsigned char uccurblock; /* range variable to track current block */ unsigned char uccurbank;/* range variable to track current bank */ int iretval = flash_success; /* holds return value: optimistic initially! */ unsigned int ufirstread, usecondread; /* used to check toggle bit dq2 */ /* step 1: check for correct flash type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; /* step 2: check for protected or invalid blocks. */ if( ucnumblocks > num_blocks ) /* check specified blocks <= num_blocks */ return flash_too_many_blocks; for( uccurblock = 0; uccurblock < ucnumblocks; uccurblock++ ) { /* use flashautoselect to find protected or invalid blocks */ if( flashautoselect((int)ucblock[uccurblock]) != flash_block_unprotected ) return (int)ucblock[uccurblock]; /* return protected/invalid blocks */ } /* step 3: find which bank the first block in the array is member of */ for( uccurbank = 0; uccurbank < num_banks; uccurbank++ ) if ( (ucblock[0] >= bankfirstblock[uccurbank]) && (ucblock[0] <= banklastblock[uccurbank]) ) break; /* uccurbank is the current bank of block 0 */ /* check that all the other blocks are within the bank uccurbank */ for( uccurblock = 1; uccurblock < ucnumblocks; uccurblock++ ) if ( (ucblock[uccurblock] < bankfirstblock[uccurbank]) || (ucblock[uccurblock] > banklastblock[uccurbank]) )
AN1201 - application note 20/33 return ucblock[uccurblock]; /* return invalid blocks that are across banks */ /* step 4: write block erase command */ flashwrite( 0x0555l, 0x00aa ); flashwrite( 0x02aal, 0x0055 ); flashwrite( 0x0555l, 0x0080 ); flashwrite( 0x0555l, 0x00aa ); flashwrite( 0x02aal, 0x0055 ); /* dsi!: time critical section. additional blocks must be added every 100us*/ for( uccurblock = 0; uccurblock < ucnumblocks; uccurblock++ ) { flashwrite( blockoffset[ucblock[uccurblock]], 0x0030 ); /* check for erase timeout period (is bit dq3 set?) */ if( (flashread( blockoffset[ucblock[0]] ) & 0x0008) == 0x0008 ) break; /* cannot set any more blocks due to timeout */ } /* eni! */ /* step 5: check for time-out blocks */ /* if timeout occurred then check if current block is erasing or not */ /* use dq2 of status register, toggle implies block is erasing */ if( uccurblock < ucnumblocks ) { ufirstread = flashread( blockoffset[ucblock[uccurblock]] ) & 0x0004; usecondread = flashread( blockoffset[ucblock[uccurblock]] ) & 0x0004; if( ufirstread != usecondread ) { uccurblock++; /* point to the next block */ } if( uccurblock < ucnumblocks ) { /* indicate that some blocks have been timed out of the erase list */ iretval = flash_mpu_too_slow; } /* now specify all other blocks as not being erased */ while( uccurblock < ucnumblocks ) { ucblock[uccurblock++] = flash_block_not_erased; } } /* step 6: wait for the erase timer bit (dq3) to be set */ while( 1 ) /* timeout!: if, for some reason, the hardware fails then this loop may not exit. use a timer function to implement a timeout from the loop. */ { if( ( flashread( blockoffset[ucblock[0]] ) & 0x0008 ) == 0x0008 ) break; /* break when device starts the erase cycle */ } /* step 7: follow data toggle flow chart until program/erase controller completes */ if( flashdatatoggle( blockoffset[ucblock[0]] ) != flash_success ) { iretval = flash_erase_fail;
21/33 AN1201 - application note /* step 8: return to read mode (if an error occurred) */ flashreadreset(); } return iretval; } /******************************************************************************* function: int flashbankerase( unsigned char ucbank ) arguments: ucbank is 0 for bank a, 1 for bank b return value: the function returns the following conditions: flash_success (-1) flash_wrong_type (-8) flash_erase_fail (-14) flash_bank_invalid (-17) the first block in the bank found to be protected will cause the funtion to fail returning the block number that failed. description: the function can be used to erase a whole bank of the flash chip. each bank is erased in turn. the function only returns when all of the bank has been erased or if a error has been detected. pseudo code: step 1: check for correct flash type step 2: check that the bank value ucbank in not invalid step 3: check for protect blocks in the bank step 4: write bank erase sequence step 5: use data toggle to check for completetion step 6: return fail if data toggle fails step 7: read dq5 to see if p/ec failed step 8: return success *******************************************************************************/ int flashbankerase( unsigned char ucbank ) { unsigned char uccurblock; /* variable for checking for protected blocks */ /* step 1: check for correct flash type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; /* step 2: check that bank value is not greater than num_banks */ if( ucbank > num_banks ) return flash_bank_invalid; /* step 3: check for protected blocks in that bank */ for( uccurblock = bankfirstblock[ucbank]; uccurblock <= banklastblock[ucbank]; uccurblock++ ) { /* use flashautoselect to find protected or invalid blocks */ if( flashautoselect( uccurblock ) != flash_block_unprotected ) return (int)uccurblock; /* return protected/invalid blocks */ } /* step 4: write command sequence to erase bank */ flashwrite( 0x0555, 0x00aa ); flashwrite( 0x02aa, 0x0055 ); flashwrite( 0x0555, 0x0080 ); flashwrite( 0x0555, 0x00aa ); flashwrite( 0x02aa, 0x0055 );
AN1201 - application note 22/33 flashwrite( bankfirstblock[ucbank], 0x0010 ); /* step 5: follow data toggle flow chart until program/erase controller completes */ if( flashdatatoggle( bankfirstblock[ucbank] ) != flash_success ) { /* step 6: return to read mode (if an error occurred) */ flashreadreset(); return flash_erase_fail; } /* step 7: pass */ return flash_success; } /******************************************************************************* function: int flashblockunprotect(unsigned char ucblock) arguments: ucblock holds the block number to unprotect return value: the function returns the following conditons: flash_wrong_type (-8) flash_success (-1) flash_unprotect_fail (-16) flash_unprotected (-10) flash_block_invalid (-5) description: this function unprotects a block selected by ucblock but only if that particular block is protected and valid. the block unprotect command is then written and then checked to ensure that it was successful. pseudo code: step 1: check for correct flash type step 2: check to see if the block number ucblock is valid. step 3: check if the block really needs unprotecting as it may already be unprotected. step 4: unprotect the block. step 5: test to see if unprotection was successful. step 6: read reset if data toggle flow chart failed. *******************************************************************************/ int flashblockunprotect(unsigned char ucblock) { int iretval; /* store result */ /* step 1: check for correct flash type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; iretval = flashautoselect( ucblock ); /* step 2 */ if( iretval == flash_block_invalid ) return iretval; /* step 3 */ else if( iretval == flash_block_unprotected ) return flash_unprotected; else { /* step 4 unprotect the block */ flashwrite( 0x0555, 0x00aa );
23/33 AN1201 - application note flashwrite( 0x02aa, 0x0055 ); flashwrite( 0x0555, 0x0060 ); flashwrite( blockoffset[ucblock], 0x00d0 ); /* step 5 test to see that it worked */ if( flashautoselect( ucblock ) == flash_block_unprotected ) return flash_success; else { /* step 6 read reset if failure */ flashreadreset(); return flash_unprotect_fail; } } } /******************************************************************************* function: int flashblockprotect(unsigned char ucblock) arguments: ucblock holds the block number to protect return value: the function returns the following conditons: flash_success (-1) flash_protect_fail (-18) flash_protected (-11) flash_wrong_type (-8) flash_block_invalid (-5) description: this function protects a block selected by ucblock but only if that particular block is unprotected and valid. the block protect command is then written and checked to ensure that it was successful. pseudo code: step 1: check for correct flash type step 2: check to see if the block number ucblock is valid step 3: check if the block really needs protecting as it may already be protected. step 4: protect the block. step 5: test to see if protection was successful. step 6: read reset if data toggle flow chart failed. *******************************************************************************/ int flashblockprotect(unsigned char ucblock) { int iretval; /* store result */ /* step 1: check for correct flash type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; iretval = flashautoselect( ucblock ); /* step 2 */ if( iretval == flash_block_invalid ) return iretval; /* step 3 */ else if( iretval == flash_block_protected ) return flash_protected; else { /* step 4 protect the block */
AN1201 - application note 24/33 flashwrite( 0x0555, 0x00aa ); flashwrite( 0x02aa, 0x0055 ); flashwrite( 0x0555, 0x0060 ); flashwrite( blockoffset[ucblock], 0x0001 ); /* step 5 test to see that it worked */ if ( flashautoselect( ucblock ) == flash_block_protected ) return flash_success; else { /* step 6 read reset if failure */ flashreadreset(); return flash_unprotect_fail; } } } /******************************************************************************* function: int flashblocklock(unsigned char ucblock) arguments: ucblock holds the block number to lock return value: the function returns the following conditons: flash_success (-1) flash_lock_fail (-19) flash_locked (-20) flash_wrong_type (-8) flash_block_invalid (-5) description: this function locks a block selected by ucblock but only if that particular block is valid. the block lock command is then written and checked to ensure that it was successful. pseudo code: step 1: check for correct flash type step 2: check to see if the block number ucblock is valid step 3: check if the block really needs locking as it may already be locked. step 4: lock the block. step 5: test to see if lock was successful. step 6: read reset if data toggle flow chart failed. *******************************************************************************/ int flashblocklock(unsigned char ucblock) { int iretval; /* store result */ /* step 1: check for correct flash type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; iretval = flashautoselect( ucblock ); /* step 2 */ if( iretval == flash_block_invalid ) return iretval; /* step 3 */ if( ( iretval & flash_block_locked ) ) return flash_locked; else { /* step 4 lock the block */
25/33 AN1201 - application note flashwrite( 0x0555, 0x00aa ); flashwrite( 0x02aa, 0x0055 ); flashwrite( 0x0555, 0x0060 ); flashwrite( blockoffset[ucblock], 0x002f ); /* step 5 test to see that it worked */ if ( ( flashautoselect( ucblock ) & flash_block_locked ) ) return flash_success; else { /* step 6 read reset if failure */ flashreadreset(); return flash_lock_fail; } } } /******************************************************************************* function: int flashprogram( unsigned long uloff, size_t numwords, void *array ) arguments: uloff is the word offset into the flash to be programmed numwords holds the number of words in the array. array is a pointer to the array to be programmed. return value: the function returns the following conditions: flash_success (-1) flash_program_fail (-6) flash_offset_out_of_range (-7) flash_wrong_type (-8) number of the first protected or invalid block on success the function returns flash_success (-1). the function returns flash_program_fail (-6) if a programming failure occurs. if the address range to be programmed exceeds the address range of the flash device the function returns flash_offset_out_of_range (-7) and nothing is programmed. if the wrong type of flash is detected then flash_wrong_type (-8) is returned and nothing is programmed. if part of the address range to be programmed falls within a protected block, the function returns the number of the first protected block encountered and nothing is programmed. description: this function is used to program an array into the flash. it does not erase the flash first and may fail if the block(s) are not erased first. pseudo code: step 1: check for correct flash type. step 2: check the offset range is valid. step 3: check that the block(s) to be programmed are not protected. step 4: while there is more to be programmed. step 5: check for changes from 0 to 1. step 6: program the next word. step 7: follow data toggle flow chart until program/erase controller has completed. step 8: return to read mode (if an error occurred). step 9: update pointers. *******************************************************************************/ int flashprogram( unsigned long uloff, size_t numwords, void *array ) { unsigned int *uarraypointer; /* use an unsigned int to access the array */ unsigned long ullastoff; /* holds the last offset to be programmed */
AN1201 - application note 26/33 unsigned char uccurblock; /* range variable to track current block */ /* step 1: check for correct flash type */ if( !(flashautoselect( flash_read_manufacturer ) == manufacturer_st) || !(flashautoselect( flash_read_device_code ) == expected_device ) ) return flash_wrong_type; /* step 2: check the offset and range are valid */ ullastoff = uloff+numwords-1; if( ullastoff >= flash_size ) return flash_offset_out_of_range; /* step 3: check that the block(s) to be programmed are not protected */ for( uccurblock = 0; uccurblock < num_blocks; uccurblock++ ) { /* if the address range to be programmed ends before this block */ if( blockoffset[uccurblock] > ullastoff ) break; /* then we are done */ /* else if the address range starts beyond this block */ else if( (uccurblock < (num_blocks-1)) && (uloff >= blockoffset[uccurblock+1]) ) continue; /* then skip this block */ /* otherwise if this block is not unprotected */ else if( flashautoselect((int)uccurblock) != flash_block_unprotected ) return (int)uccurblock; /* return first protected block */ } /* step 4: while there is more to be programmed */ uarraypointer = (unsigned int *)array; while( uloff <= ullastoff ) { /* step 5: check for changes from 0 to 1 */ if( ~flashread( uloff ) & *uarraypointer ) /* indicate failure as it is not possible to change a 0 to a 1 using a program command. this must be done using an erase command */ return flash_program_fail; /* step 6: program the next word */ flashwrite( 0x0555l, 0x00aa ); /* 1st cycle */ flashwrite( 0x02aal, 0x0055 ); /* 2nd cycle */ flashwrite( 0x0555l, 0x00a0 ); /* program command */ flashwrite( uloff, *uarraypointer ); /* program value */ /* step 7: follow data toggle flow chart until program/erase controller has completed */ /* see data toggle flow chart of the data sheet */ if( flashdatatoggle( uloff ) == flash_toggle_fail ) { /* step 8: return to read mode (if an error occurred) */ flashreadreset(); return flash_program_fail; } /* step 9: update pointers */ uloff++; uarraypointer++; } return flash_success;
27/33 AN1201 - application note } /******************************************************************************* function: static int flashdatatoggle( void ) arguments: uloff should hold a valid offset for reading the status register. this address offset must be within the bank in which the current erase or program operation is taking place. return value: the function returns flash_success if the program/erase controller is successful or flash_toggle_fail if there is a problem. description: the function is used to monitor the program/erase controller during erase or program operations. it returns when the program/erase controller has completed. in the m59dr008 data sheet or the m59dr032 data sheet, the data toggle flow chart shows the operation of the function. pseudo code: step 1: read dq6 (into a word). step 2: read dq5 and dq6 (into another word). step 3: if dq6 did not toggle between the two reads then return flash_success step 4: else if dq5 is zero then operation is not yet complete, goto 1. step 5: else (dq5 != 0), read dq6 again. step 6: if dq6 did not toggle between the last two reads then return flash_success. step 7: else return flash_toggle_fail. *******************************************************************************/ static int flashdatatoggle( unsigned long uloff ) { unsigned int u1, u2; /* hold values read from address offset uloff */ while( 1 ) /* timeout!: if, for some reason, the hardware fails then this loop may not exit. use a timer function to implement a timeout from the loop. */ { /* step 1: read dq6 (into a word) */ u1 = flashread( uloff ); /* read dq6 from the flash (any address) */ /* step 2: read dq5 and dq6 (into another word) */ u2 = flashread( uloff ); /* read dq5 and dq6 from the flash (any address) */ /* step 3: if dq6 did not toggle between the two reads then return flash_success */ if( (u1&0x0040) == (u2&0x0040) ) /* dq6 == no toggle */ return flash_success; /* step 4: else if dq5 is zero then operation is not yet complete */ if( (u2&0x0020) == 0x0000 ) continue; /* step 5: else (dq5 == 1), read dq6 again */ u1 = flashread( uloff ); /* read dq6 from the flash (any address) */ /* step 6: if dq6 did not toggle between the last two reads then return flash_success */ if( (u2&0x0040) == (u1&0x0040) ) /* dq6 == no toggle */ return flash_success; /* step 7: else return flash_toggle_fail */ else /* dq6 == toggle here means fail */ return flash_toggle_fail;
AN1201 - application note 28/33 } /* end of while loop */ } #ifndef illustration_only /******************************************************************************* function: static int flashdatapoll( unsigned long uloff, unsigned int uval ) arguments: uloff should hold a valid offset to be polled. for programming this will be the offset of the word being programmed. for erasing this can be any offset in the block(s)/bank being erased. uval should hold the value being programmed. a value of ffh should be used when erasing. return value: the function returns flash_success if the program/erase controller is successful or flash_poll_fail if there is a problem. description: the function is used to monitor the program/erase controller during erase or program operations. it returns when the program/erase controller has completed. in the m59dr008 data sheet or the m59dr032 data sheet, the data polling flow chart shows the operation of the function. note: this library does not use the data polling flow chart to assess the correct operation of program/erase controller, but uses the data toggle flow chart instead. the flashdatapoll() function is only provided here as an illustration of the data polling flow chart in the data sheet. the code uses the function flashdatatoggle() instead. pseudo code: step 1: read dq5 and dq7 (into a word). step 2: if dq7 is the same as uval(bit 7) then return flash_success. step 3: else if dq5 is zero then operation is not yet complete, goto 1. step 4: else (dq5 != 0), read dq7. step 5: if dq7 is now the same as uval(bit 7) then return flash_success. step 6: else return flash_poll_fail. *******************************************************************************/ static int flashdatapoll( unsigned long uloff, unsigned int uval ) { unsigned int u; /* holds value read from valid address */ while( 1 ) /* timeout!: if, for some reason, the hardware fails then this loop may not exit. use a timer function to implement a timeout from the loop. */ { /* step 1: read dq5 and dq7 (into a word) */ u = flashread( uloff ); /* read dq5, dq7 at valid addr */ /* step 2: if dq7 is the same as value(bit 7) then return flash_success */ if( (u&0x0080) == (uval&0x0080) ) /* dq7 == data */ return flash_success; /* step 3: else if dq5 is zero then operation is not yet complete */ if( (u&0x0020) == 0x0000 ) continue; /* step 4: else (dq5 == 1) */ u = flashread( uloff ); /* read dq7 at valid addr */ /* step 5: if dq7 is now the same as uval(bit 7) then return flash_success */ if( (u&0x0080) == (uval&0x0080) ) /* dq7 == data */ return flash_success;
29/33 AN1201 - application note /* step 6: else return flash_poll_fail */ else /* dq7 != data here means fail */ return flash_poll_fail; } /* end of while loop */ } #endif /* !illustration_only */ /******************************************************************************* function: char *flasherrorstr( int ierrnum ); arguments: ierrnum is the error number returned from another flash routine return value: a pointer to a string with the error message description: this function is used to generate a text string describing the error from the flash. call with the return value from another flash routine. pseudo code: step 1: check the error message range. step 2: return the correct string. *******************************************************************************/ char *flasherrorstr( int ierrnum ) { static char *str[] = { flash success, flash poll failure, flash too many blocks, mpu is too slow to erase all the blocks, flash block selected is invalid, flash program failure, flash address offset out of range, flash is of wrong type, flash block failed erase, flash is unprotected, flash is protected, flash function not supported, flash vpp invalid, flash erase fail, flash toggle flow chart failure, flash unprotect failed, flash bank invalid, flash protect failed, flash lock failed, flash is locked }; /* step 1: check the error message range */ ierrnum = -ierrnum - 1; /* all errors are negative: make +ve & adjust */ if( ierrnum < 0 || ierrnum >= sizeof(str)/sizeof(str[0])) /* check range */ return unknown error\n; /* step 2: return the correct string */ else return str[ierrnum]; } /******************************************************************************* list of errors and return values, explanations and help. ******************************************************************************** return name: flash_success return value: -1
AN1201 - application note 30/33 description: this value indicates that the flash command has executed correctly. ******************************************************************************** error name: flash_poll_fail notes: the data polling flow chart, which applies to m59 and m29 series flash only, is not used in this library. the function flashdatapoll() is only provided as an illustration of the data polling flow chart. this error condition should not occur when using this library. return value: -2 description: the program/erase controller algorithm has not managed to complete the command operation successfully. this may be because the device is damaged solution: try the command again. if it fails a second time then it is likely that the device will need to be replaced. ******************************************************************************** error name: flash_too_many_blocks return value: -3 description: the user has chosen to erase more blocks than the device has. this may be because the array of blocks to erase contains the same block more than once. solutions: check that the program is trying to erase valid blocks. the device will only have num_blocks blocks (defined at the top of the file). also check that the same block has not been added twice or more to the array. ******************************************************************************** error name: flash_mpu_too_slow return value: -4 description: the mpu has not managed to write all of the selected blocks to the device before the timeout period expired. see block erase command section of the data sheet for details. solutions: if this occurs occasionally then it may be because an interrupt is occuring between writing the blocks to be erased. search for dsi! in the code and disable interrupts during the time critical sections. if this error condition always occurs then it may be time for a faster microprocessor, a better optimising c compiler or, worse still, learn assembly. the immediate solution is to only erase one block at a time. disable the test (by #defineing out the code) and always call the function with one block at a time. ******************************************************************************** error name: flash_block_invalid return value: -5 description: a request for an invalid block has been made. valid blocks number from 0 to num_blocks-1. solution: check that the block is in the valid range. ******************************************************************************** error name: flash_program_fail return value: -6 description: the programmed value has not been programmed correctly. solutions: make sure that the block containing the value was erased before programming. try erasing the block and re-programming the value. if it fails again then the device may need to be changed. ******************************************************************************** error name: flash_offset_out_of_range return value: -7 description: the address offset given is out of the range of the device.
31/33 AN1201 - application note solution: check that the address offset is in the valid range. ******************************************************************************** error name: flash_wrong_type return value: -8 description: the source code has been used to access the wrong type of flash. solutions: use a different flash chip with the target hardware or contact stmicroelectronics for a different source code library. ******************************************************************************** error name: flash_block_failed_erase return value: -9 description: the previous erase to this block has not managed to successfully erase the block. solution: sadly the flash needs replacing. ******************************************************************************** return name: flash_unprotected return value: -10 description: the user has requested to unprotect a flash that is already unprotected. this is just a warning to the user that their operation did not make any changes and was not necessary. ******************************************************************************** return name: flash_protected return value: -11 description: the user has requested to protect a flash that is already protected. this is just a warning to the user that their operation did not make any changes and was not necessary. ******************************************************************************** return name: flash_function_not_supported notes: this condition should not occur when using this library. return value: -12 description: the user has attempted to make use of functionality not available on this flash device (and thus not provided by the software drivers). this is simply a warning to the user. ******************************************************************************** error name: flash_vpp_invalid notes: this condition should not occur when using this library. return value: -13 description: a program or a block erase has been attempted with the vpp supply voltage outside the allowed ranges. this command had no effect since an invalid vpp has the effect of protecting the whole of the flash device. solution: the (hardware) configuration of vpp will need to be modified to make programming or erasing the device possible. ******************************************************************************** error name: flash_erase_fail return value: -14 description: this indicates that the previous erasure of one block, many blocks or a bank of the device has failed. solution: investigate this failure further by attempting to erase each block individually. if erasing a single block still causes failure, then the flash sadly needs replacing. ******************************************************************************** error name: flash_toggle_fail
AN1201 - application note 32/33 return value: -15 notes: this applies to m29 and m59 series flash only. description: the program/erase controller algorithm has not managed to complete the command operation successfully. this may be because the device is damaged. solution: try the command again. if it fails a second time then it is likely that the device will need to be replaced. ******************************************************************************** error name: flash_unprotect_fail return value: -16 notes: this applies to m59drxxx series flash only. description: this error return value indicates that a block unprotect command was unsuccessful. solution: try the command again. if it fails a second time then it is likely that the device is either locked or will need to be replaced. (part is unlocked but protected on power-up with /wp pin at v_high). ******************************************************************************** error name: flash_bank_invalid return value: -17 notes: this applies to m59drxxx series flash only. description: this error return value indicates that a bank does not exist. ******************************************************************************** error name: flash_protect_fail return value: -18 notes: this applies to m59drxxx series flash only. description: this error return value indicates that a block protect command was unsuccessful. solution: try the command again. if it fails a second time then it is likely that the device is either locked or will need to be replaced. (part is unlocked but protected on power-up with /wp pin at v_high). ******************************************************************************** error name: flash_lock_fail return value: -19 notes: this applies to m59drxxx series flash only. description: this error return value indicates that the lock bit for a particular block could not be set. ******************************************************************************** error name: flash_locked return value: -20 notes: this applies to m59drxxx series flash only. description: this error return value indicates that the block was already locked when the attempt to lock it was made. *******************************************************************************/
33/33 AN1201 - application note if you have any questions or suggestion concerning the matters raised in this document please send them to the following electronic mail address: ask.memory@st.com (for general enquiries) please remember to include your name, company, location, telephone number and fax number. information furnished is believed to be accurate and reliable. however, stmicroelectronics assumes no responsibility for the co nsequences of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. no license is granted by implication or otherwise under any patent or patent rights of stmicroelectronics. specifications mentioned in this publicati on are subject to change without notice. this publication supersedes and replaces all information previously supplied. stmicroelectronics prod ucts are not authorized for use as critical components in life support devices or systems without express written approval of stmicroelectro nics. the st logo is registered trademark of stmicroelectronics all other names are the property of their respective owners. ? 2001 stmicroelectronics - all rights reserved stmicroelectronics group of companies australia - brazil - china - finland - france - germany - hong kong - india - italy - japan - malaysia - malta - morocco - singapore - spain - sweden - switzerland - united kingdom - u.s.a. www.st.com


▲Up To Search▲   

 
Price & Availability of AN1201

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X