The addresses 3?D000 do all access page 3F because the address D000 is in the unpaged area C000..FFFF which corresponds to page 3F. The page 3E can be accessed in the unpaged area at 4000..7fff; all other pages must be accessed through the page window at 8000..BFFF (the pages 3E and 3F can be accessed also trough this window). This means that you will get the access-error because the address you are writing to is in block 0 but the block selector bits in FCNFG points to some other block.
Try changing your long addresses to 3?9000 instead of 3?D000, and everything should work.
Message Edited by imajeff on 2006-07-06 07:52 AM
I haven't tested it, but it looks like your code should work. Note that the Flash_Erase_Sector function erases only one 512 byte sector (in one of the four 64 kbyte Flash banks). If you want to erase a complete 64 kb block you must call the function 128 times (or you could use the Mass Erase command (FCMD = 0x41)).
Which block (and sector) to erase is selected with the far address in the call to the function: Block 0 consists of pages 3C..3F, block 1 = 38..3B, block 2 = 34..37 and block 3 = pages 30..33. So to erase the first sector of block 3 you should call the function with address 0x308000. The next 512 byte sector starts at 0x308200, a.s.o. up to the last sector of page 0x30 that starts at 0x30BE00. Then continue with page 31, 32 and 33 and the block 3 should be erased.