0
votes

I'm writing a bootloader for a c166 chip, to be exact, the 169FH. The bootloader can currently open a TCP/IP Connection so a PC can send an Intel hex file to the bootloader. This intel hex file is saved in the RAM. After receiving the hex file it is read line by line to set the bytes to the correct location in the flash. The flash location where the bootloader is stored is ofcourse different from where the main program can be saved.

This are the first two lines of the intel hex file:

:0200000400C03A
:20100000CA11EE111212341258127A129A12BC12DE12001322134413601388138813881349

The first line is to get the highest 16 bits of the 32bit flash address, which is in this case 0x00C0 in the second line are the lower 16 bits of the 32 bit flash address, which is in this case 0x1000. This creates to total address of 0x00C01000, the byte written to that address should be 0xCA.

I'm trying to write the byte to that address using the following code:

uint8_t u8Byte = (uint8_t )XHugeStringToDec((const __huge char *)&Ext_RamFlashSave.Byte[u32Idx], 9+(u8ByteIdx*2), 2);

uint8_t *u8Address =  (uint8_t*)((uint32_t)(u32ExtendedLinearAddress << 16) + (uint32_t)u16BaseAddress + (uint32_t)u8ByteIdx);

*u8Address = (u8Byte);

XHugeStringToDec() is a function to get the hexadecimal value from the intel hex string. I know this is going correct.

Ext_RamFlashSave.Byte is the array where the intel hex file is storedin.

The u32ExtendedLinearAddress variable is 0x0C00, and is set earlier. The u16BaseAddress is 0x1000 and is also set earlier in code.

The problem is in the last line:

*u8Address = (u8Byte);

I have verified that u8Address is indeed 0x0C01000 and u8Byte is indeed 0xCA. But when I monitor my flash address, I do not see the byte written. I can imagine that it is some kind of write protection, but I cannot find out how to work around this, or do I need a different way to write to the Flash address?

More info of how the intel-hex file is build is described here: https://en.wikipedia.org/wiki/Intel_HEX

1
I'm not familiar with that chip but, typically, writing flash is MUCH more involved than just poking some address with data. - Martin James
You cannot directly write to a flash memory address.. You have to "Unlock" the flash by sending specific sequence. Then only you can write data. Also, "Lock" the flash after write operation - Swanand
Yeah .. you should read the user manual for all the gory details. - Martin James
Typically you must erase a flash page before write it. Or, better, a page erase reset all bits to 1, and you can OTP program each bit to 0. A 0 to 1 transition must be implemented executing a page erase. - LPs
..and, with some chips, the page size varies through the flash memory-map. Typically, you will end up cacheing pages, loading them from the hex file, until an address outside the page turns up and you have to actually write the cached page to flash, (which takes ages, relatively). - Martin James

1 Answers

0
votes

I am not familier with the chip you said. But for writing to flash, Generally, following algorithm is used:

  1. Unlock the flash. This is usually sending specific data sequence. The flash I use right now has 0xA0A0-> delay of 1msec -> 0x0505. This will enable writing to flash
  2. Clear the error flags. Flash has some error flags like Write error or read error. Also, check for Busy flag to make sure Flash is ready
  3. Erase the Page. Yes... You have to erase the page before writing to it. Even if you want to change a single bit, You have to erase entire page.
  4. Write the data (Finally) But make sure that endien-ness is correct. Sometimes you Controller is Little Endien and Flash is Big Endien. So you have to match to Flash's.
  5. Lock the Flash. This is usually done by sending same sequence which used for unlocking. (Refer Datasheet)

You cannot write to a flash directly... Have to go through entire process. That's why Flash are slow.

Some flash support 8bit writing while some only 16bit or 32 bit etc. So, you have to send those many bits while writing.

When you have to modify a small portion of a page, Read the page in a buffer. Modify data in the buffer. Erase the page and write entire buffer. If you are modifying a